分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-03-05 db6156a92cc59467bde608a00c76952ebc75e488
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Comm;
using WIDESEA_Core.EFDbContext;
using WIDESEA_WCS.IRepositories;
using WIDESEA_WCS.Repositories;
using WIDESEA_WCS.WCSClient;
using static System.Collections.Specialized.BitVector32;
 
namespace WIDESEA_WCS
{
    public class Gantry
    {
        static VOLContext Context = new VOLContext();
        Idt_plcinfoheadRepository repository = new dt_plcinfoheadRepository(Context);
        Idt_plcinfodetailRepository plcRepository = new dt_plcinfodetailRepository(Context);
        Idt_geometry_dataRepository dataRepository = new dt_geometry_dataRepository(Context);
        Idt_geometry_data_detectionlineRepository detectionlineRepository = new dt_geometry_data_detectionlineRepository(Context);
        Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(Context);
        Idt_mes_detailRepository mes_DetailRepository = new dt_mes_detailRepository(Context);
        Idt_mes_headRepository mes_HeadRepository = new dt_mes_headRepository(Context);
        Idt_patternRepository patternRepository = new dt_patternRepository(Context);
        #region 查询车轮数据
        public void QueryWheeldata(PLCClient client)
        {
            try
            {
                var plc = repository.FindFirst(x => x.plcinfo_name == client.PLCName);
                List<string> names = new List<string>() { "1单元辊道下料查询车轮", "2单元辊道下料查询车轮", "3单元辊道下料查询车轮", "辊道上料查询车轮" };
                foreach (string name in names)
                {
                    var details = plcRepository.Find(x => x.plcdetail_iotype == plc.plcinfo_iotyep && x.plcdetail_number == name).ToList();
                    var on = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_oi_on").First(), client);
                    if ((byte)on == 1)
                    {
                        var wp_type = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_o_wp_type").First(), client);
                        if (name != "辊道上料查询车轮")
                        {
                            var geometry = dataRepository.FindFirst(x => x.TypeId == (Int32)wp_type);
                            if (geometry == null)
                            {
                                //DBExtension.Write(details.Where(x => x.plcdetail_name == "W_i_status").First(), client, (Int16)2);
                                //1-找到数据,2-未找到工件类型数据
                                client.WriteByOrder("W_i_status", (byte)2, name);
                                client.WriteByOrder("R_oi_on", (byte)0, name);
                            }
                            else
                            {
                                //1-找到数据,2-未找到工件类型数据
                                client.WriteByOrder("W_i_status", (byte)1, name);
                                client.WriteByOrder("W_i_parameter_a", (float)geometry.a, name);
                                client.WriteByOrder("W_i_parameter_b", (float)geometry.b, name);
                                client.WriteByOrder("W_i_parameter_c", (float)geometry.c, name);
                                client.WriteByOrder("W_i_parameter_d", (float)geometry.d, name);
                                client.WriteByOrder("W_i_parameter_e", (float)geometry.e, name);
                                client.WriteByOrder("W_i_parameter_f", (float)geometry.f, name);
                                client.WriteByOrder("W_i_parameter_g", (float)geometry.g, name);
                                client.WriteByOrder("W_i_parameter_h", (float)geometry.h, name);
                                client.WriteByOrder("R_oi_on", (byte)0, name);
                            }
                        }
                        else
                        {
                            var geometry = detectionlineRepository.FindFirst(x => x.TypeId == (Int32)wp_type);
                            if (geometry == null)
                            {
                                client.WriteByOrder("W_i_status", (byte)2, name);
                                client.WriteByOrder("R_oi_on", (byte)0, name);
                            }
                            else
                            {
                                client.WriteByOrder("W_i_status", (byte)1, name);
                                client.WriteByOrder("W_i_parameter_a", (float)geometry.a, name);
                                client.WriteByOrder("W_i_parameter_b", (float)geometry.b, name);
                                client.WriteByOrder("W_i_parameter_c", (float)geometry.c, name);
                                client.WriteByOrder("W_i_parameter_d", (float)geometry.d, name);
                                client.WriteByOrder("W_i_parameter_e", (float)geometry.e, name);
                                client.WriteByOrder("W_i_parameter_f", (float)geometry.f, name);
                                client.WriteByOrder("W_i_parameter_g", (float)geometry.g, name);
                                client.WriteByOrder("W_i_parameter_h", (float)geometry.h, name);
                                client.WriteByOrder("R_oi_on", (byte)0, name);
                            }
                        }
 
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        #endregion
 
        #region 查询订单,是否需要加工
        public void QueryOrder(PLCClient client)
        {
            try
            {
                var plc = repository.FindFirst(x => x.plcinfo_name == client.PLCName);
                List<string> names = new List<string>() { "1单元辊道下料查询订单", "2单元辊道下料查询订单", "3单元辊道下料查询订单", "辊道上料查询订单" };
                foreach (string name in names)
                {
                    var details = plcRepository.Find(x => x.plcdetail_iotype == plc.plcinfo_iotyep && x.plcdetail_number == name).ToList();
                    var on = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_oi_on").First(), client);
                    if ((byte)on == 1)
                    {
                        //var wp_id = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_o_wp_id").First(), client).ToString();
                        var wp_id = client.ReadByOrder<string>("R_o_wp_id", name);
                        var mes_Detail = mes_DetailRepository.FindFirst(x => x.SN == wp_id);
                        if (mes_Detail == null)
                        {
                            client.WriteByOrder("W_i_status", (byte)2, name);
                            client.WriteByOrder("R_oi_on", (byte)0, name);
                            continue;
                        }
                        var mes_Head = mes_HeadRepository.FindFirst(x => x.jobID == mes_Detail.jobID);
                        if (mes_Head == null)
                        {
                            client.WriteByOrder("W_i_status", (byte)2, name);
                            client.WriteByOrder("R_oi_on", (byte)0, name);
                            continue;
                        }
 
                        if (name != "辊道上料查询订单")
                        {
                            //需添加查询车轮SN号订单逻辑   需添加入库模式判断,手动入库模式需人工添加入库库区,否则报警
                            var pattern = patternRepository.FindFirst(x => x.pattern_name == "入库模式").pattern_state;
                            if (pattern != 1 && string.IsNullOrEmpty(mes_Head.area))
                            {
                                client.WriteByOrder("W_i_status", (byte)4, name);
                                client.WriteByOrder("R_oi_on", (byte)0, name);
                                continue;
                            }
                            var wp_type = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_o_wp_type").First(), client);
                            //var geometry = dataRepository.FindFirst(x => x.TypeId == (Int32)wp_type);
                            var geometry = dataRepository.FindFirst(x => x.Description == mes_Head.drawingNo);
                            if (geometry == null)
                            {
                                //1-好,允许加工,2-不在列表中,3-不加工车轮类型,4-手动入库模式人工未选择库区
                                client.WriteByOrder("W_i_status", (byte)2, name);
                                client.WriteByOrder("R_oi_on", (byte)0, name);
                                continue;
                            }
                            else
                            {
                                //1-好,允许加工,2-不在列表中,3-不加工车轮类型,4-手动入库模式人工未选择库区
                                client.WriteByOrder("W_i_status", (byte)1, name);
                                client.WriteByOrder("W_i_job_id", mes_Detail.jobID, name);
                                client.WriteByOrder("W_i_drawing_id", mes_Head.drawingNo, name);
                                client.WriteByOrder("W_i_heat_id", mes_Detail.heatID, name);
                                //client.WriteByOrder("W_i_sourceheat", "", name);
                                client.WriteByOrder("W_i_batch_id", mes_Detail.heatBatchID, name);
 
                                //屏蔽工艺(屏蔽视觉检测/屏蔽涂油)
                                //if (name != "辊道上料查询车轮")
                                //{
                                client.WriteByOrder("W_i_skip_op_SJ1", mes_Head.skip_op_1, name);//屏蔽视觉检测设备1
                                client.WriteByOrder("W_i_skip_op_SJ2", mes_Head.skip_op_2, name);//屏蔽视觉检测设备2
                                client.WriteByOrder("W_i_skip_op_LT", mes_Head.skip_op_3, name);//屏蔽链条机
                                //}
                                client.WriteByOrder("R_oi_on", (byte)0, name);
 
                            }
                        }
                        else
                        {
                            var geometry = dataRepository.FindFirst(x => x.Description == mes_Head.drawingNo);
                            if (geometry == null)
                            {
                                //1-好,允许加工,2-不在列表中,3-不加工车轮类型,4-手动入库模式人工未选择库区
                                client.WriteByOrder("W_i_status", (byte)2, name);
                                client.WriteByOrder("R_oi_on", (byte)0, name);
                                continue;
                            }
                            else
                            {
                                //1-好,允许加工,2-不在列表中,3-不加工车轮类型,4-手动入库模式人工未选择库区
                                client.WriteByOrder("W_i_status", (byte)1, name);
                                client.WriteByOrder("W_i_job_id", mes_Detail.jobID, name);
                                client.WriteByOrder("W_i_drawing_id", mes_Head.drawingNo, name);
                                client.WriteByOrder("W_i_heat_id", mes_Detail.heatID, name);
                                //client.WriteByOrder("W_i_sourceheat", "", name);
                                client.WriteByOrder("W_i_batch_id", mes_Detail.heatBatchID, name);
                                client.WriteByOrder("R_oi_on", (byte)0, name);
                            }
                        }
 
 
 
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        #endregion
 
        #region 下料位交互   !!!下料逻辑需修改
        public void Layofflevel(PLCClient client)
        {
            try
            {
                var plc = repository.FindFirst(x => x.plcinfo_name == client.PLCName);
                var Pipeline_client = PLCClient.Clients.FirstOrDefault(t => t.PLCName == "链条机");
                if (Pipeline_client == null) throw new Exception("链条机调度服务未开启!");
                if (!Pipeline_client.IsConnected) throw new Exception("与链条机连接超时!");
                var Pipelineplc = repository.FindFirst(x => x.plcinfo_name == Pipeline_client.PLCName);
                List<string> names = new List<string>() { "1单元下料区", "2单元下料区", "3单元下料区" };
                foreach (string name in names)
                {
                    var details = plcRepository.Find(x => x.plcdetail_iotype == plc.plcinfo_iotyep && x.plcdetail_number == name).ToList();
 
                    var Request = (bool)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_Request_Load").First(), client);
                    if (!Request) continue;
 
                    var Stations = stationinfoRepository.Find(x => x.area == PipelineJob.area_code(name));
                    foreach (var station in Stations)
                    {
                        if (!station.enable)
                            client.WriteByOrder("W_Enabl_Load", false, name);//信号为false桁架停止进入
                    }
                    var NGStation = "X01001003";
                    if (name == "2单元下料区") NGStation = "X02001003";
                    if (name == "3单元下料区") NGStation = "X03001002";
                    var Wheel_Type = (Int32)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_Wheel_Type").First(), client);//车轮类型
                    var Wheel_id = DBExtension.Read(details.Where(x => x.plcdetail_name == "R_Wheel_id").First(), client).ToString();//车轮SN号
 
                    var mes_Detail = mes_DetailRepository.FindFirst(x => x.SN == Wheel_id);
                    var mes_Head = mes_HeadRepository.FindFirst(x => x.jobID == mes_Detail.jobID);
 
                    var PartStatus = (Int32)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_PartStatus").First(), client);//1:ok;2:nok
                    if (PartStatus == 1)
                    {
                        var Stationinfo = Stations
                        .Where(x => x.enable
                        && x.stationCode != NGStation
                        && x.location_state == LocationStateEnum.Stroge.ToString()
                        && x.quantity < 5
                        && x.stationType == PipelineJob.QueryMateriel(Wheel_Type)
                        && x.Number == mes_Detail.jobID
                        && x.heatNumber == mes_Detail.heatID).FirstOrDefault();
                        if (Stationinfo == null)
                            Stationinfo = Stations.Where(x => x.enable && x.stationCode != NGStation && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity == 0).FirstOrDefault();
                        if (Stationinfo != null)
                        {
                            var Pipelinedetails = plcRepository.Find(x => x.plcdetail_iotype == Pipelineplc.plcinfo_iotyep && x.plcdetail_number == Stationinfo.stationCode).ToList();
                            var PalletSignal = (Int16)DBExtension.Read(Pipelinedetails.Where(x => x.plcdetail_name == "R_PalletSignal").First(), client);//读取托盘信号:1:有,2无
                            if (PalletSignal == 1)
                            {
                                var SNS = Stationinfo.bindSN.Split(",");
                                List<string> list = new List<string>();
                                foreach (var SN in SNS)
                                {
                                    if (!string.IsNullOrEmpty(SN))
                                        list.Add(SN);
                                }
                                //缓存架上车轮数量与SN号数量不一致
                                if (list.Count != Stationinfo.quantity)
                                {
                                    Stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
                                    stationinfoRepository.Update(Stationinfo, true);
                                    //写入桁架报警信号
                                    throw new Exception("下料位车轮数量与SN号数量不一致,下料位编号:" + Stationinfo.stationCode);
                                }
                                var area = Convert.ToInt16(Stationinfo.stationCode.Substring(Stationinfo.stationCode.Length - 1, 1));
                                client.WriteByOrder("W_AreaNr", (Int16)area, name);//区域货位号
                                client.WriteByOrder("W_IndexNr", (Int16)Stationinfo.quantity + 1, name);//托盘上的第几个车轮
                                client.WriteByOrder("W_Storage_Type", (Int16)1, name);//托盘类型1-横放;2-竖放(暂时只有横放托盘)
                                client.WriteByOrder("W_Enabl_Load", true, name);//是否允许
                                return;
                            }
                        }
                    }
                    else if (PartStatus == 2)
                    {
                        var Stationinfo = Stations
                        .Where(x => x.enable
                        && x.stationCode == NGStation
                        && x.location_state == LocationStateEnum.Stroge.ToString()
                        && x.quantity < 5
                        && x.stationType == PipelineJob.QueryMateriel(Wheel_Type)
                        && x.Number == mes_Detail.jobID
                        && x.heatNumber == mes_Detail.heatID).FirstOrDefault();
                        if (Stationinfo != null)
                        {
                            var Pipelinedetails = plcRepository.Find(x => x.plcdetail_iotype == Pipelineplc.plcinfo_iotyep && x.plcdetail_number == Stationinfo.stationCode).ToList();
                            var PalletSignal = (Int16)DBExtension.Read(Pipelinedetails.Where(x => x.plcdetail_name == "R_PalletSignal").First(), client);//读取托盘信号:1:有,2无
                            if (PalletSignal == 1)
                            {
                                var SNS = Stationinfo.bindSN.Split(",");
                                List<string> list = new List<string>();
                                foreach (var SN in SNS)
                                {
                                    if (!string.IsNullOrEmpty(SN))
                                        list.Add(SN);
                                }
                                //缓存架上车轮数量与SN号数量不一致
                                if (list.Count != Stationinfo.quantity)
                                {
                                    Stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
                                    stationinfoRepository.Update(Stationinfo, true);
                                    //写入桁架报警信号
                                    throw new Exception("下料位车轮数量与SN号数量不一致,下料位编号:" + Stationinfo.stationCode);
                                }
                                var area = Convert.ToInt16(Stationinfo.stationCode.Substring(Stationinfo.stationCode.Length - 1, 1));
                                client.WriteByOrder("W_AreaNr", (Int16)area, name);//区域货位号
                                client.WriteByOrder("W_IndexNr", (Int16)Stationinfo.quantity + 1, name);//托盘上的第几个车轮
                                client.WriteByOrder("W_Storage_Type", (Int16)1, name);//托盘类型1-横放;2-竖放(暂时只有横放托盘)
                                client.WriteByOrder("W_Enabl_Load", true, name);//是否允许
                                return;
                            }
                        }
                    }
                    #region
                    //var Stationinfo = Stations
                    //    .Where(x => x.enable
                    //    && x.location_state == LocationStateEnum.Stroge.ToString()
                    //    && x.quantity < 5
                    //    && x.stationType == PipelineJob.QueryMateriel(Wheel_Type)
                    //    && x.Number == mes_Detail.jobID
                    //    && x.heatNumber == mes_Detail.heatID).FirstOrDefault();
                    //if (Stationinfo == null)
                    //    Stationinfo = Stations.Where(x => x.enable && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity == 0).FirstOrDefault();
                    //if (Stationinfo != null)
                    //{
 
                    //}
 
                    //List<string> StationCodes = new List<string>() { "X01001001", "X01001002", "X01001003" };
                    //if (name == "2单元下料区")
                    //    StationCodes = new List<string>() { "X02001001", "X02001002", "X02001003" };
                    //if (name == "3单元下料区")
                    //    StationCodes = new List<string>() { "X03001001", "X03001002" };
 
                    #endregion
                    var finished = (bool)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_load_finished").First(), client);//放料完成
                    var updatefinished = (bool)DBExtension.Read(details.Where(x => x.plcdetail_name == "W_Storage_update").First(), client);//货位状态更新
                    if (!finished && updatefinished)
                        client.WriteByOrder("W_Storage_update", false, name);//货位状态更新
                    if (finished && !updatefinished)
                    {
                        var AreaNr = (Int16)DBExtension.Read(details.Where(x => x.plcdetail_name == "W_AreaNr").First(), client);
                        var station = stationinfoRepository.FindFirst(x => x.stationCode.Substring(x.stationCode.Length - 1, 1) == AreaNr.ToString());
                        station.quantity = station.quantity++;
                        station.bindSN = station.bindSN == string.Empty ? Wheel_id : station.bindSN + "," + Wheel_id;
                        if (station.quantity == 0)
                        {
                            station.stationType = Wheel_id;
                            station.Number = Wheel_id;
                            station.heatNumber = Wheel_id;
                        }
                        var count = stationinfoRepository.Update(station);
                        if (count < 1)
                            throw new Exception($"下料位信息更新失败!下料位编号:{station.stationCode}");
                        client.WriteByOrder("W_Storage_update", true, name);//货位状态更新
                        client.WriteByOrder("W_Enabl_Load", false, name);//是否允许
                    }
                    #region
                    //for (int i = 0; i < StationCodes.Count - 1; i++)
                    //{
                    //    var PartStatus = (Int32)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_PartStatus").First(), client);//1:ok;2:nok
                    //    string NG = StationCodes[StationCodes.Count - 1];//NG货位
                    //    var station = stationinfoRepository.FindFirst(x => x.stationCode == StationCodes[i] && x.enable && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity < 5);
                    //    if (station == null) continue;
                    //    var Pipelinedetails = plcRepository.Find(x => x.plcdetail_iotype == Pipelineplc.plcinfo_iotyep && x.plcdetail_number == StationCodes[i]).ToList();
                    //    var PalletSignal = (Int16)DBExtension.Read(Pipelinedetails.Where(x => x.plcdetail_name == "R_PalletSignal").First(), client);//读取托盘信号:1:有,2无
                    //    if (PalletSignal == 1)
                    //    {
                    //        if (PartStatus == 1 && NG != station.stationCode)
                    //        {
                    //            client.WriteByOrder("W_AreaNr", (Int16)i + 1, name);//区域货位号
                    //            client.WriteByOrder("W_IndexNr", (Int16)station.quantity + 1, name);//托盘上的第几个车轮
                    //            client.WriteByOrder("W_Storage_Type", (Int16)1, name);//托盘类型1-横放;2-竖放(暂时只有横放托盘)
                    //            client.WriteByOrder("W_Enabl_Load", true, name);//是否允许
                    //            return;
                    //        }
                    //        else if (PartStatus == 2 && NG == station.stationCode)
                    //        {
                    //            client.WriteByOrder("W_AreaNr", (Int16)i + 1, name);//区域货位号
                    //            client.WriteByOrder("W_IndexNr", (Int16)station.quantity + 1, name);//托盘上的第几个车轮
                    //            client.WriteByOrder("W_Storage_Type", (Int16)1, name);//托盘类型1-横放;2-竖放(暂时只有横放托盘)
                    //            client.WriteByOrder("W_Enabl_Load", true, name);//是否允许
                    //            return;
                    //        }
                    //    }
                    //}
                    #endregion
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        #endregion
    }
}