11
jinxin
2025-06-23 3a2b71a09ec682d96023a6b7d5ca30e90d90a681
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
using HH.WCS.Mobox3.KeKou.device;
using HH.WCS.Mobox3.KeKou.dispatch;
using HH.WCS.Mobox3.KeKou.util;
using HH.WCS.Mobox3.KeKou.wms;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Threading;
 
namespace HH.WCS.Mobox3.KeKou.process
{
    /// <summary>
    /// 设备信号处理,主要是tcp信号,我们做server被动接收信号来处理,根据项目定制的
    /// </summary>
    internal class DeviceProcess
    {
        internal static void Analysis(string data, string ip)
        {
            LogHelper.Info($"TCP TP--{ip} data--{data}");
            if (data.Length >= 6)
            {
                //去掉消息头3F 00
                if (data.Substring(0, 4) == "3f00")
                {
                    data = data.Substring(4);
                }
                //Console.WriteLine($"{ip}-{data}");
                var plc = Settings.deviceInfos.Where(a => a.address == ip && a.enable == 1).FirstOrDefault();
                if (plc != null)
                {
                    if (plc.deviceType == 1)
                    {
                        AnalysisBottleCapmolding(data, plc);
                    }
                    else if (plc.deviceType == 2)
                    {
                        AnalysisButton(data, plc);
                    }
                }
                else
                {
                    Console.WriteLine($"TCP信号处理:未查询到IP为{ip}的数据,请检查deviceInfo配置中心是否存在该IP的数据!");
                }
            }
 
        }
 
        private static void AnalysisButton(string data, Settings.deviceInfo plc)
        {
            LogHelper.Info($"{plc.deviceName}--IP={plc.address}--msg={data}");
            var db = new SqlHelper<object>().GetInstance();
            if (data == "3a00020001057300020101b9")
            {
                if (plc.location.Length > 0)
                {
                    var location = plc.location[0];
                    var locinfo = db.Queryable<Location>().Where(a => a.S_CODE == location).First();
                    if (locinfo != null && locinfo.N_LOCK_STATE != 1 && locinfo.N_LOCK_STATE != 2)
                    {
                        LocationHelper.UnLockLoc(plc.location[0]);
                    }
                    PlcHelper.SendHex(plc.address, "2A00010002057300020101A9");
                }
            }
        }
 
        internal static void Traffic(string forkliftNo, string lockNo, int State)
        {
            if (State == 1023)
            {
                SendHexOnce("172.31.94.107", 4001, "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 01 00 00 00 00 00 00 00 00 00 00");
                SendHexOnce("172.31.94.108", 4001, "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 01 00 00 00 00 00 00 00 00 00 00");
                //PlcHelper.SendHex("172.31.94.107", "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 01 00 00 00 00 00 00 00 00 00 00");
                //PlcHelper.SendHex("172.31.94.108", "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 01 00 00 00 00 00 00 00 00 00 00");
 
            }
 
            if (State == 1025)
            {
                SendHexOnce("172.31.94.107", 4001, "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 00 00 01 00 00 00 00 00 00 00 00");
                SendHexOnce("172.31.94.108", 4001, "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 00 00 01 00 00 00 00 00 00 00 00");
                //PlcHelper.SendHex("172.31.94.107", "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 00 00 01 00 00 00 00 00 00 00 00");
                //PlcHelper.SendHex("172.31.94.108", "00 00 00 00 00 13 01 10 00 00 00 06 0C 00 00 00 01 00 00 00 00 00 00 00 00");
            }
        }
 
 
        private static string SendHexOnce(string ip, int port, string hex)
        {
            var res = string.Empty;
            try
            {
                Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //client.Connect(ip, port);
                IAsyncResult connectResult = client.BeginConnect(ip, port, null, null);
                if (!connectResult.AsyncWaitHandle.WaitOne(2000))
                {
                    client.Close();
                    return "";
                }
                client.ReceiveTimeout = 2000;
                if (client.Connected)
                {
                    client.Send(Hex2Bytes(hex));
                    byte[] buffer = new byte[1024];
 
                    var length = client.Receive(buffer, SocketFlags.None);
                    byte[] data = new byte[length];
                    Array.Copy(buffer, data, length);
                    res = BitConverter.ToString(data).Replace("-", "");
 
 
                    client.Disconnect(true);
                    client.Dispose();
                }
                client = null;
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.Message, ex);
            }
            return res;
        }
 
 
        internal static byte[] Hex2Bytes(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
 
 
        /// <summary>
        /// 接收PLC注塑机信号
        /// </summary>
        /// <param name="data"></param>
        /// <param name="plc"></param>
        private static void AnalysisBottleCapmolding(string data, Settings.deviceInfo plc)
        {
            LogHelper.Info($"{plc.deviceName}--IP={plc.address}--msg={data}", "注塑机");
            //下满框信号生成下满框任务 待取货完成解绑起点货位 生成送空任务
            if (data.Substring(0, 2) == "11")
            {
                if (moldingFull(plc, plc.location[0]))
                {
                    LogHelper.Info($"{plc.deviceName}--IP={plc.address}--msg=注塑机A口生成取满任务", "注塑机");
                }
            }
            if (data.Substring(2, 2) == "21")
            {
                if (moldingFull(plc, plc.location[1]))
                {
                    LogHelper.Info($"{plc.deviceName}--IP={plc.address}--msg=注塑机B口生成取满任务", "注塑机");
                }
            }
 
        }
 
        private static bool moldingFull(Settings.deviceInfo plc, string location)
        {
            var db = new SqlHelper<object>().GetInstance();
            //通过站点编号查询工单
            //通过工单获取物料编码、批号(前期富勒没上线批号自行编写)
            var result = false;
 
 
            //上空任务卸货完成之后,复位线体信号,并记录卸货完成时间,当卸货完成再五分钟之内的时候,不允许下满   避免造成下线信号异常导致的异常下满(舒哥杰作,酌情优化)
            var canOff = true;
            var cgInfo = db.Queryable<CGTTable>().Where(a => a.Bit == location).First();
            if (cgInfo != null)
            {
                if (cgInfo.time.AddMinutes(5) > DateTime.Now)
                {
                    canOff = false;
                }
 
            }
            if (canOff)
            {
                if (location != "")
                {
                    if (LocationHelper.CheckLocFree(location))
                    {
                        LogHelper.Info($"瓶盖机:{plc.deviceName} 下线信号:{location} 查询工单", "瓶盖机");
                        var workOrder = db.Queryable<WorkOrder>().Where(a => a.S_BP_CODE == plc.deviceName).First();
                        if (workOrder != null)
                        {
                            LogHelper.Info($"查到了工单{workOrder.S_WOB_CODE} 工单状态为: {workOrder.S_WOB_STATE} 即产即用={workOrder.S_USING_NOW} 连接区域={workOrder.S_LINKLINENO}", "瓶盖机");
                            if (workOrder.S_WOB_STATE.Trim() == "执行中")
                            {
                                LogHelper.Info($"查询当前货位 托盘数量 货位编码={location}");
                                var trayInfo = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE == location).ToList();
                                if (trayInfo.Count() > 0)
                                {
                                    LogHelper.Info($"当前货位托盘数量大于0");
                                    var trayNo = trayInfo[0].S_CNTR_CODE;
                                    ProduceTask(plc, location, db, workOrder, trayNo);
                                }
                                else
                                {
                                    LogHelper.Info($"瓶盖机:{plc.deviceName} 下线信号:{location} 该站点没有托盘无法取满", "瓶盖机");
                                }
                            }
                        }
                        else
                        {
                            LogHelper.Info($"瓶盖机:{plc.deviceName} 下线信号:{location} 未找到工单", "瓶盖机");
                        }
                    }
                    else
                    {
                        LogHelper.Info($"瓶盖机:{plc.deviceName} 当前位置{location}有任务,不可触发满托下线", "瓶盖机");
                    }
                }
            }
 
 
            return result;
        }
 
        public static void FeiLiaoTask(string location, SqlSugar.SqlSugarClient db, string trayNo)
        {
            Location endLoc = null;
            Location startLoc = db.Queryable<Location>().Where(a => a.S_CODE == location).First();
            var endArea = "Flq";
 
            LogHelper.Info($"库区:{endArea} ");
            endLoc = db.Queryable<Location>()
                .Where(a =>
                a.S_AREA_CODE == endArea &&
                a.N_CURRENT_NUM == 0 &&
                a.N_LOCK_STATE == 0)
                .First();
            if (endLoc != null && startLoc != null)
            {
                //创建任务
                var res = WMSHelper.CreatOpenation(startLoc, endLoc, trayNo, "注塑机下线", 1);
            }
            else
            {
                LogHelper.Info($"库区:{endArea}没有找到可用库位");
            }
        }
 
        internal static void TaskState(WCSTask mst, int state)
        {
            //if (mst.S_OP_NAME == "注塑机下线" && state == 6)
            //{
            //    LogHelper.Info($"车辆回报完后调用空框上线");
            //    KKoutTask(mst);
            //}
            if (state == 6)
            {
                var deviceInfo = Settings.deviceInfos.Where(a => a.location.Contains(mst.S_END_LOC) && a.deviceType == 1).FirstOrDefault();
                if (deviceInfo != null)
                {
                    //发送复位信号
                    if (deviceInfo.location[0] == mst.S_END_LOC)
                    {
                        //A口
                        PlcHelper.SendHex(deviceInfo.address, "3F00120D0A");
                    }
                    else
                    {
                        //B口
                        PlcHelper.SendHex(deviceInfo.address, "3F00220D0A");
                    }
                }
            }
        }
 
        private static void KKoutTask(WCSTask mst)
        {
            var db = new SqlHelper<object>().GetInstance();
            try
            {
                //卸货完成生成上空任务
                var deviceInfo = Settings.deviceInfos.Where(a => a.location.Contains(mst.S_START_LOC)).FirstOrDefault();
                if (deviceInfo != null)
                {
                    //查找对应产线的工单是否执行
                    var workInfo = db.Queryable<WorkOrder>().Where(a => a.S_BP_CODE == deviceInfo.deviceName && a.S_WOB_STATE == "执行中").First();
                    if (workInfo != null)
                    {
                        //查找产线对应的空框库区配置
                        var prodectArea = Settings.connectAreas.Where(a => a.deviceName == deviceInfo.deviceName && a.locAttribute == "空").FirstOrDefault();
                        if (prodectArea != null)
                        {
                            //查询库区是否有空框使用
                            Location startLoc = null;
                            string trayNo = "";
                            for (int i = 0; i < prodectArea.connectAreaList.Length; i++)
                            {
                                LogHelper.Info($"伪长度{i}");
                                var startArea = prodectArea.connectAreaList[i];
                                startLoc = db.Queryable<Location>().Where(a => a.S_AREA_CODE == startArea && a.N_CURRENT_NUM > 0 && a.N_LOCK_STATE == 0).Includes(a => a.LocCntrRel).First();
                                if (startLoc != null)
                                {
                                    LogHelper.Info($"S_CODE{startLoc.S_CODE}");
                                    if (startLoc.LocCntrRel != null)
                                    {
                                        trayNo = startLoc.LocCntrRel.S_CNTR_CODE;
                                        break;
                                    }
                                }
                                LogHelper.Info($"trayNo{trayNo}");
 
                            }
                            LogHelper.Info($"进来了");
                            if (startLoc != null && !string.IsNullOrEmpty(trayNo))
                            {
                                var endLoc = db.Queryable<Location>().Where(a => a.S_CODE == mst.S_START_LOC).First();
                                WMSHelper.CreatOpenation(startLoc, endLoc, trayNo, "注塑机上线", 2);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error($"注塑上线异常 任务号{mst.S_CODE} 异常信息{ex.Message}", ex);
            }
        }
 
        private static void ProduceTask(Settings.deviceInfo plc, string location, SqlSugar.SqlSugarClient db, WorkOrder workOrder, string trayNo)
        {
            var startLoc = db.Queryable<Location>().Where(a => a.S_CODE == location).First();
            Location endLoc = null;
            try
            {
                var endLocList = db.Queryable<KeKouSettings>().Where(a => a.deviceName == plc.deviceName).ToList();
                if (endLocList.Count > 0)
                {
                    foreach (var item in endLocList)
                    {
                        endLoc = db.Queryable<Location>()
                            .Where(a => a.S_CODE == item.location &&
                            a.N_CURRENT_NUM == 0 &&
                            a.N_LOCK_STATE == 0)
                            .First();
                        if (endLoc != null)
                        {
                            break;
                        }
                    }
 
                    if (startLoc != null && endLoc != null)
                    {
                        //创建任务
                        var res = WMSHelper.CreatOpenation(startLoc, endLoc, trayNo, "注塑机下线", 1);
                        if (res)
                        {
                            //将托盘绑定物料
                            var itemInfo = db.Queryable<CntrItemRel>()
                                .Where(a =>
                                a.S_ITEM_CODE == workOrder.S_ITEM_CODE &&
                                a.S_BP_CODE == workOrder.S_BP_CODE &&
                                a.S_BATCH_NO == workOrder.S_BATCH_NO &&
                                a.S_CNTR_CODE == trayNo)
                                .First();
                            if (itemInfo == null)
                            {
                                //新增
                                itemInfo = new CntrItemRel { S_ITEM_CODE = workOrder.S_ITEM_CODE, S_BP_CODE = workOrder.S_BP_CODE, S_CNTR_CODE = trayNo, S_BATCH_NO = workOrder.S_BATCH_NO };
                                db.Insertable(itemInfo).ExecuteCommand();
                            }
                        }
                    }
                    else
                    {
                        LogHelper.Info($"生成任务失败,没有找到可用库位");
                    }
                }
                //var connectArea = Settings.connectAreas.Where(a => a.deviceName == plc.deviceName && a.locAttribute == "满").FirstOrDefault();
                //if (connectArea != null)
                //{
                //    for (int i = 0; i < connectArea.connectAreaList.Length; i++)
                //    {
                //        var endArea = connectArea.connectAreaList[i];
                //
                //        LogHelper.Info($"库区:{endArea} ");
                //        endLoc = db.Queryable<Location>()
                //            .Where(a =>
                //            a.S_AREA_CODE == endArea &&
                //            a.N_CURRENT_NUM == 0 &&
                //            a.N_LOCK_STATE == 0)
                //            .First();
                //        if (endLoc != null)
                //        {
                //            break;
                //        }
                //        else
                //        {
                //            LogHelper.Info($"库区:{endArea}没有找到可用库位");
                //        }
                //    }
                //
                //    if (startLoc != null && endLoc != null)
                //    {
                //        //创建任务
                //        var res = WMSHelper.CreatOpenation(startLoc, endLoc, trayNo, "注塑机下线", 1);
                //        if (res)
                //        {
                //            //将托盘绑定物料
                //            var itemInfo = db.Queryable<CntrItemRel>()
                //                .Where(a =>
                //                a.S_ITEM_CODE == workOrder.S_ITEM_CODE &&
                //                a.S_BP_CODE == workOrder.S_BP_CODE &&
                //                a.S_BATCH_NO==workOrder.S_BATCH_NO &&
                //                a.S_CNTR_CODE == trayNo)
                //                .First();
                //            if (itemInfo == null)
                //            {
                //                //新增
                //                itemInfo = new CntrItemRel { S_ITEM_CODE = workOrder.S_ITEM_CODE, S_BP_CODE = workOrder.S_BP_CODE, S_CNTR_CODE = trayNo ,S_BATCH_NO=workOrder.S_BATCH_NO};
                //                db.Insertable(itemInfo).ExecuteCommand();
                //            }
                //        }
                //    }else
                //        LogHelper.Info($"生成任务失败,没有找到可用库位");
                //}
            }
            catch (Exception ex)
            {
                LogHelper.Error($"注塑机下线算取满框缓存位异常 异常信息{ex.Message}", ex);
            }
        }
    }
}