分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-07-05 ddab693f389b648157c8b48c7fcbdefc92561dd2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using WIDESEA_Comm.LogInfo;
using WIDESEA_Core.EFDbContext;
using WIDESEA_WCS.IRepositories;
using WIDESEA_WCS.Repositories;
using WIDESEA_WCS.WCSClient;
using WIDESEA_WMS.IRepositories;
using WIDESEA_WMS.Repositories;
using static WIDESEA_Comm.Wheel_trace;
using static WIDESEA_WCS.GantryDB;
 
namespace WIDESEA_WCS
{
    public class info_interaction
    {
 
        /// <summary>
        /// 车轮参数交互
        /// </summary>
        /// <param name="client"></param>
        public static void wheeldata(PLCClient client)
        {
            VOLContext context = new VOLContext();
            Idt_geometry_dataRepository geometry_DataRepository = new dt_geometry_dataRepository(context);
            var groups = client.itemGroups.Where(x => x.opratortype == "wheeldata").OrderBy(x => x.Methods).ThenBy(x => x.name).GroupBy(x => x.Methods).ToList();
            foreach (var key in groups)
            {
                try
                {
                    DBItemGroup group = key.FirstOrDefault(x => x.name == "oi_on");
                    if (group != null)
                    {
                        var oi_on = DBExtension.Read(group, client);
                        if (Convert.ToByte(oi_on) == 1)
                        {
                            var o_wp_type = DBExtension.Read(key.FirstOrDefault(x => x.name == "o_wp_type"), client);
                            var geometry = geometry_DataRepository.Find(x => x.TypeId == Convert.ToInt32(o_wp_type)).FirstOrDefault();
                            if (geometry == null)
                            {
                                client.Write(key.FirstOrDefault(x => x.name == "i_status").dbAddress, (byte)2);//状态
                                client.Write(key.FirstOrDefault(x => x.name == "oi_on").dbAddress, (byte)0);//申请复位
                                throw new Exception($"{key.Key}未找到类型{o_wp_type}的车轮参数\n写入辊道信息:i_status:2,oi_on:0");
                            }
                            else
                            {
                                client.Write(key.FirstOrDefault(x => x.name == "i_parameter_a").dbAddress, (float)geometry.a);//车轮参数a
                                client.Write(key.FirstOrDefault(x => x.name == "i_parameter_b").dbAddress, (float)geometry.b);//车轮参数b
                                client.Write(key.FirstOrDefault(x => x.name == "i_parameter_c").dbAddress, (float)geometry.c);//车轮参数c
                                client.Write(key.FirstOrDefault(x => x.name == "i_parameter_d").dbAddress, (float)geometry.d);//车轮参数d
                                client.Write(key.FirstOrDefault(x => x.name == "i_parameter_e").dbAddress, (float)geometry.e);//车轮参数e
                                client.Write(key.FirstOrDefault(x => x.name == "i_parameter_f").dbAddress, (float)geometry.f);//车轮参数f
                                client.Write(key.FirstOrDefault(x => x.name == "i_parameter_g").dbAddress, (float)geometry.g);//车轮参数g
                                client.Write(key.FirstOrDefault(x => x.name == "i_parameter_h").dbAddress, (float)geometry.h);//车轮参数h
                                client.Write(key.FirstOrDefault(x => x.name == "i_status").dbAddress, (byte)1);//状态
                                client.Write(key.FirstOrDefault(x => x.name == "oi_on").dbAddress, (byte)0);//申请复位
                                #region 日志记录
                                WriteLog.Write_Log(key.Key + "辊道", "申请查询车轮信息", "", $"读取辊道信息:\n轮型ID:{o_wp_type}\n\n写入辊道信息:\ni_status:1" + $"\n" +
                                    $"a:{(float)geometry.a}\n" +
                                    $"b:{(float)geometry.b}\n" +
                                    $"c:{(float)geometry.c}\n" +
                                    $"d:{(float)geometry.d}\n" +
                                    $"e:{(float)geometry.e}\n" +
                                    $"f:{(float)geometry.f}\n" +
                                    $"g:{(float)geometry.g}\n" +
                                    $"h:{(float)geometry.h}\n" +
                                    $"oi_on:0");
                                #endregion
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    WriteLog.Write_Log(key.Key + "辊道", "申请查询车轮信息", "", ex.Message);
                }
            }
        }
 
        /// <summary>
        /// 扫码枪
        /// </summary>
        /// <param name="client"></param>
        public static void scan(PLCClient client)
        {
            VOLContext context = new VOLContext();
            Idt_geometry_dataRepository geometry_DataRepository = new dt_geometry_dataRepository(context);
            IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(context);
            var groups = client.itemGroups.Where(x => x.opratortype == "scan").OrderBy(x => x.Methods).ThenBy(x => x.name).GroupBy(x => x.Methods).ToList();
            foreach (var key in groups)
            {
                try
                {
                    DBItemGroup group = key.FirstOrDefault(x => x.name == "oi_on");
                    if (group != null)
                    {
                        var oi_on = DBExtension.Read(group, client);
                        if (Convert.ToByte(oi_on) == 1)
                        {
                            var o_wp_type = DBExtension.Read(key.FirstOrDefault(x => x.name == "o_wp_type"), client);
                            var o_wp_id = DBExtension.Read(key.FirstOrDefault(x => x.name == "o_wp_id"), client);
                            var geometry = geometry_DataRepository.Find(x => x.TypeId == Convert.ToInt32(o_wp_type)).FirstOrDefault();
                            if (geometry == null)
                            {
                                client.Write(key.FirstOrDefault(x => x.name == "i_status").dbAddress, (byte)2);//状态
                                client.Write(key.FirstOrDefault(x => x.name == "oi_on").dbAddress, (byte)0);//申请复位
                                throw new Exception($"{key.Key}未找到类型{o_wp_type}的车轮参数\n写入辊道信息:i_status:2,oi_on:0");
                            }
                            var Work = workinfoRepository.Find(x => x.SN == o_wp_id && (key.Key.Contains("4单元") ? x.processCode == "28" : x.processCode == "17")).OrderByDescending(x => x.CreateTime).FirstOrDefault();
                            if (Work == null)
                            {
                                client.Write(key.FirstOrDefault(x => x.name == "i_status").dbAddress, (byte)3);//状态
                                client.Write(key.FirstOrDefault(x => x.name == "oi_on").dbAddress, (byte)0);//申请复位
                                throw new Exception($"{key.Key}未找到SN:{o_wp_id}的工单信息\n写入辊道信息:i_status:3,oi_on:0");
                            }
                            client.Write(key.FirstOrDefault(x => x.name == "i_status").dbAddress, (byte)1);//状态
                            client.Write(key.FirstOrDefault(x => x.name == "i_direction").dbAddress, (byte)2);//去向默认写2
                            client.Write(key.FirstOrDefault(x => x.name == "oi_on").dbAddress, (byte)0);//申请复位
                            WriteLog.Write_Log(key.Key + "辊道", "扫码枪", "", "写入辊道信息:\ni_status:1,i_direction:2,oi_on:0");
                        }
                    }
                }
                catch (Exception ex)
                {
                    WriteLog.Write_Log(key.Key + "辊道", "扫码枪", "", ex.Message);
                }
            }
        }
 
        /// <summary>
        /// 贴标机交互
        /// </summary>
        /// <param name="client"></param>
        public static void labeller(PLCClient client)
        {
            VOLContext context = new VOLContext();
            Idt_geometry_dataRepository geometry_DataRepository = new dt_geometry_dataRepository(context);
            IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(context);
            var groups = client.itemGroups.Where(x => x.opratortype == "labeller").OrderBy(x => x.Methods).ThenBy(x => x.name).GroupBy(x => x.Methods).ToList();
            foreach (var key in groups)
            {
                try
                {
                    DBItemGroup group = key.FirstOrDefault(x => x.name == "oi_on");
                    if (group != null)
                    {
                        var oi_on = DBExtension.Read(group, client);
                        if (Convert.ToByte(oi_on) == 1)
                        {
                            var o_wp_id = DBExtension.Read(key.FirstOrDefault(x => x.name == "o_wp_id"), client);
                            if (!string.IsNullOrEmpty(o_wp_id.ToString()))
                            {
                                var Work = workinfoRepository.Find(x => x.SN == o_wp_id && (key.Key.Contains("4单元") ? x.processCode == "28" : x.processCode == "17")).OrderByDescending(x => x.CreateTime).FirstOrDefault();
                                if (Work == null)
                                {
                                    client.Write(key.FirstOrDefault(x => x.name == "i_status").dbAddress, (byte)2);//状态
                                    client.Write(key.FirstOrDefault(x => x.name == "oi_on").dbAddress, (byte)0);//申请复位
                                    throw new Exception($"{key.Key}未找到SN:{o_wp_id}的工单信息\n写入辊道信息:i_status:2,oi_on:0");
                                }
                                client.Write(key.FirstOrDefault(x => x.name == "i_job_id").dbAddress, Encoding.UTF8.GetBytes(Work.jobID));//工单编号
                                client.Write(key.FirstOrDefault(x => x.name == "i_heat_id").dbAddress, Encoding.UTF8.GetBytes(Work.heatID));//炉代号
                                client.Write(key.FirstOrDefault(x => x.name == "i_billet_id").dbAddress, Encoding.UTF8.GetBytes(Work.billetID.ToString()));//钢坯号
                                client.Write(key.FirstOrDefault(x => x.name == "i_batch_id").dbAddress, Encoding.UTF8.GetBytes(Work.heatBatchID));//热处理批次
                                client.Write(key.FirstOrDefault(x => x.name == "i_drawing_id").dbAddress, Encoding.UTF8.GetBytes(Work.drawingNo));//图号
                                client.Write(key.FirstOrDefault(x => x.name == "i_status").dbAddress, (byte)1);//状态
                                client.Write(key.FirstOrDefault(x => x.name == "oi_on").dbAddress, (byte)0);//申请复位
                                WriteLog.Write_Log(key.Key + "辊道", "贴标机", "SN号:" + o_wp_id.ToString(),
                                    $"写入辊道信息:\n" +
                                    $"i_job_id:{Work.jobID}\n" +
                                    $"i_heat_id:{Work.heatID}\n" +
                                    $"i_billet_id:{Work.billetID}\n" +
                                    $"i_batch_id:{Work.heatBatchID}\n" +
                                    $"i_drawing_id:{Work.drawingNo}\n" +
                                    $"i_status:{(byte)1}\n" +
                                    $"oi_on:{(byte)0}");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    WriteLog.Write_Log(key.Key + "辊道", "贴标机", "", ex.Message);
                }
            }
        }
    }
}