lss
12 小时以前 1319cb7072623dff12369a889af766db2d166def
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
using HH.WCS.JiaTong.api;
using HH.WCS.JiaTong.device;
using HH.WCS.JiaTong.dispatch;
using HH.WCS.JiaTong.util;
using HH.WCS.JiaTong.wms;
using Newtonsoft.Json;
using S7.Net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Services.Description;
using static HH.WCS.JiaTong.LISTA.process.HttpModel;
using static System.Collections.Specialized.BitVector32;
 
namespace HH.WCS.JiaTong.process
{
    /// <summary>
    /// 设备信号处理,主要是tcp信号,我们做server被动接收信号来处理,根据项目定制的
    /// </summary>
    internal class DeviceProcess
    {
        internal static void Analysis(string data, string ip)
        {
            if (data.Length >= 6)
            {
                //去掉消息头3F 00
                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)
                    {
                        if (data == "")
                        {
                            // Traystacker(plc);
                        }
                    }
                }
                else
                {
                    Console.WriteLine($"TCP信号处理:未查询到IP为{ip}的数据,请检查deviceInfo配置中心是否存在该IP的数据!");
                }
            }
 
        }
        /// <summary>
        /// 叠盘机空托返回
        /// </summary>    
        internal static void Traystacker(string loc, int qty, int type, string cntrcode = "")
        {
            try
            {
                //上传数据
                Traystacker result1 = new Traystacker() { station = loc, taskSource = 1, Qty = qty, dpType = type };
 
                HttpHelper httpHelper = new HttpHelper();
                Result result = new Result();
                string date = JsonConvert.SerializeObject(result1);
                var url = Settings.tableUrls.Find(a => a.id == 3);
                if (url != null)
                {
                    LogHelper.Info($"叠盘机空托返回:地址:{url.url},内容:{date}");
                    var r = httpHelper.WebPost(url.url, date);
                    result = JsonConvert.DeserializeObject<Result>(r);
                    if (result.code == "0")
                    {
 
                        LogHelper.Info($"叠盘机空托返回请求成功");
                    }
                    else
                    {
                        LogHelper.Info($"叠盘机空托返回请求失败=>msg:{result.msg}");
                    }
                }
                else
                {
                    LogHelper.Info($"叠盘机空托:根据id=3,未找到配置链接,请检查配置文件");
                }
 
            }
            catch (Exception ex)
            {
                LogHelper.Error($"叠盘机空托返回  Error=>{ex.ToString()}", ex);
            }
        }
 
        /// <summary>
        /// 原材料库agv叠盘回库
        /// </summary>
        internal static void Traffic()
        {
            Console.WriteLine("开始原材料叠盘位置判断");
            var db = new SqlHelper<object>().GetInstance();
            List<string> locs = new List<string>() { "LCDP-001", "BADPW-01" };
            var location = db.Queryable<Location>().Where(a => locs.Contains(a.S_CODE)).ToList();
            if (location.Count > 0)
            {
                string cntrcode = "";
                foreach (var item in location)
                {
 
                    if (item.N_CURRENT_NUM >= 6)
                    {
                        var cntr = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE.Trim() == item.S_CODE).ToList();
                        if (cntr.Count > 0)
                        {
                            cntrcode = cntr.OrderBy(a => a.T_CREATE).First().S_CNTR_CODE;
                            // cntrcode = string.Join(",", cntrcodes);
                        }
 
                        //创建过未完成的任务不发起请求
                        var mst = WCSHelper.GetTaskByStartloc(item.S_CODE);
                        if (mst == null)
                        {
                            AgvEmptyBack(item.S_CODE, cntrcode);
                        }
                    }
                }
            }
        }
 
        /// <summary>
        /// 井松agv叠盘回库
        /// </summary>
        internal static void KjTraffic()
        {
 
            Console.WriteLine("开始井松agv叠盘回库位置判断");
            var db = new SqlHelper<object>().GetInstance();
            List<string> locs = new List<string>() { "AGV12DP-001", "AGV01DP-001", "AGV05DP-001" };
            var location = db.Queryable<Location>().Where(a => locs.Contains(a.S_CODE)).ToList();
            if (location.Count > 0)
            {
                // string cntrcode = "";
                foreach (var item in location)
                {
 
                    if (item.N_CURRENT_NUM >= 6)
                    {
                        //var cntr = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE.Trim() == item.S_CODE).ToList();
                        //if (cntr.Count > 0)
                        //{
                        //    var cntrcodes = cntr.Select(a => a.S_CNTR_CODE).ToArray();
                        //    cntrcode = string.Join(",", cntrcodes);
                        //}
                        string cntrcode = "";
                        var cntr = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE.Trim() == item.S_CODE).ToList();
                        if (cntr.Count > 0)
                        {
                            cntrcode = cntr.OrderBy(a => a.T_CREATE).First().S_CNTR_CODE;
                            // cntrcode = string.Join(",", cntrcodes);
                        }
                        LogHelper.Info($"货位{item.S_CODE}叠满6个开始回调接口");
                        //创建过未完成的任务不发起请求
                        var mst = WCSHelper.GetTaskByStartloc(item.S_CODE);
                        if (mst == null)
                        {
                            Traystacker(item.S_CODE, item.N_CURRENT_NUM, 2, cntrcode);
                        }
                        else
                        {
                            LogHelper.Info($"有执行中的叠盘回库任务{mst.S_CODE},不触发叠盘任务");
                        }
                    }
                }
            }
        }
 
 
        //public static Dictionary<string, DpjErrorInfo> DpjInfoDirectory = new Dictionary<string, DpjErrorInfo>();
        public static Dictionary<string, DpjInfo> DpjInfoDirectory = new Dictionary<string, DpjInfo>();
        /// <summary>
        /// 井松叠盘机叠盘回库
        /// </summary>
        internal static void DpjTraffic()
        {
            var devices = Settings.deviceInfos.Where(a => a.deviceType == 1).ToList();
            //var db = new SqlHelper<object>().GetInstance();
            //var list = db.Queryable<Location>().Where(a => a.N_CURRENT_NUM > 0  && a.N_LOCK_STATE == 0).OrderByDescending(a => a.N_COL).Take(1).PartitionBy(a => a.N_ROW).ToList();
            try
            {
                if (devices.Count > 0)
                {
                    foreach (var device in devices)
                    {
 
                        try
                        {
                            // 为每个设备创建独立处理区
                            Task.Run(() =>
                            {
                                DpjTraffic1(device);
                            });
 
                        }
                        catch (Exception ex)
                        {
                            // 捕获单个设备异常,不影响其他设备
                            LogHelper.Error($"叠盘机{device.deviceName}({device.address})处理失败: {ex.Message}", ex);
                        }
                        Thread.Sleep(100);
                    }
                }
                //Thread.Sleep(5000);
            }
            catch (Exception ex)
            {
                LogHelper.Error($"ReadDpj DpjTraffic:{ex}", ex);
 
            }
 
        }
        /// <summary>
        /// 叠盘机线程读取
        /// </summary>
        internal static void DpjTrafficRead()
        {
            var devices = Settings.deviceInfos.Where(a => a.deviceType == 1).ToList();
            //var db = new SqlHelper<object>().GetInstance();
            //var list = db.Queryable<Location>().Where(a => a.N_CURRENT_NUM > 0  && a.N_LOCK_STATE == 0).OrderByDescending(a => a.N_COL).Take(1).PartitionBy(a => a.N_ROW).ToList();
            try
            {
                if (devices.Count > 0)
                {
                    foreach (var device in devices)
                    {
 
                        try
                        {
                            // 为每个设备创建独立处理区
                            Task.Run(() =>
                            {
                                DpjTrafficRead(device);
                            });
 
                        }
                        catch (Exception ex)
                        {
                            // 捕获单个设备异常,不影响其他设备
                            LogHelper.Error($"叠盘机{device.deviceName}({device.address})处理失败: {ex.Message}", ex);
                        }
                        Thread.Sleep(100);
                    }
                }
                //Thread.Sleep(5000);
            }
            catch (Exception ex)
            {
                LogHelper.Error($"ReadDpj DpjTraffic:{ex}", ex);
 
            }
 
        }
        internal static void DpjCreateTask()
        {
            var devices = DpjInfoDirectory;
            //var db = new SqlHelper<object>().GetInstance();
            //var list = db.Queryable<Location>().Where(a => a.N_CURRENT_NUM > 0  && a.N_LOCK_STATE == 0).OrderByDescending(a => a.N_COL).Take(1).PartitionBy(a => a.N_ROW).ToList();
            try
            {
                if (devices.Count > 0)
                {
                    foreach (var device in devices.Values)
                    {
 
                        try
                        {
                            // 为每个设备创建独立处理区
                            Task.Run(() =>
                            {
                                DpjTraffic2(device);
                            });
 
                        }
                        catch (Exception ex)
                        {
                            // 捕获单个设备异常,不影响其他设备
                            LogHelper.Error($"叠盘机{device.Address}处理失败: {ex.Message}", ex);
                        }
                        Thread.Sleep(100);
                    }
                }
                //Thread.Sleep(5000);
            }
            catch (Exception ex)
            {
                LogHelper.Error($"ReadDpj DpjTraffic:{ex}", ex);
 
            }
 
        }
        internal static void DpjTrafficRead(Settings.deviceInfo device)
        {
            while (true)
            {
                try
                {
                    var IsDp = S7Helper.ReadDpj(device.address, 2001, S7.Net.VarType.Byte);
                    var value = S7Helper.ReadDpj(device.address, 2014, S7.Net.VarType.Int);
                    int value2004 = S7Helper.ReadDpj(device.address, 2004, S7.Net.VarType.Byte);
                    LogHelper.Info($"读取叠盘机{device.address},叠盘信号{IsDp},数量{value},安全交互信号{value2004}");
                    if (DpjInfoDirectory.ContainsKey(device.address))
                    {
                        DpjInfoDirectory[device.address].Address = device.address;
                        DpjInfoDirectory[device.address].Loc = device.TN_Location;
                        DpjInfoDirectory[device.address].PickUp = IsDp;
                        DpjInfoDirectory[device.address].qty = value;
                        DpjInfoDirectory[device.address].value2004 = value2004;
 
                    }
                    else
                    {
 
                        DpjInfoDirectory.Add(device.address, new DpjInfo() { Address = device.address, Loc = device.TN_Location, PickUp = IsDp, qty = value, value2004 = value2004 });
                    }
            
                }
                catch (Exception ex)
                {
 
                    LogHelper.Error($"ReadDpj DpjTraffic:{ex}", ex);
                }
            }
          
 
        }
        internal static void DpjTraffic1(Settings.deviceInfo device)
        {
 
            try
            {
                var IsDp = S7Helper.ReadDpj(device.address, 2001, S7.Net.VarType.Byte);
                var value = S7Helper.ReadDpj(device.address, 2014, S7.Net.VarType.Int);
               // int value2004 = S7Helper.ReadDpj(device.address, 2004, S7.Net.VarType.Byte);
                //var ErrorCode = S7Helper.ReadDpj(device.address, 2016, S7.Net.VarType.Int);
                ////记录叠盘机报错信息
                //if (DpjInfoDirectory.ContainsKey(device.address))
                //{
                //    DpjInfoDirectory[device.address].Address = device.address;
                //    DpjInfoDirectory[device.address].DeviceName = device.deviceName;
                //    var ErrorInfo = Settings.dpjErrorInfos.Where(a => a.ErrorCode == ErrorCode).First();
                //    if (ErrorInfo != null)
                //    {
                //        DpjInfoDirectory[device.address].ErrorInfo = ErrorInfo.ErrorInfo;
                //    }
                //}
                //else
                //{
                //    string errorinfo = "";
                //    var ErrorInfo = Settings.dpjErrorInfos.Where(a => a.ErrorCode == ErrorCode).First();
                //    if (ErrorInfo != null)
                //    {
                //        errorinfo = ErrorInfo.ErrorInfo;
                //    }
                //    DpjInfoDirectory.Add(device.address, new DpjErrorInfo() { Address = device.address, DeviceName = device.deviceName, ErrorInfo = errorinfo });
                //}
             
                if (value >= 1 && IsDp == 1)
                {
                    //var cntr = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE.Trim() == item.S_CODE).ToList();
                    //if (cntr.Count > 0)
                    //{
                    //    var cntrcodes = cntr.Select(a => a.S_CNTR_CODE).ToArray();
                    //    cntrcode = string.Join(",", cntrcodes);
                    //}
 
                    //创建过未完成的任务不发起请求
                    var mst = WCSHelper.GetTaskByStartloc(device.TN_Location);
                    if (mst == null)
                    {
                        Traystacker(device.TN_Location, value, 2);
                    }
                    else
                    {
                        LogHelper.Info($"有执行中的叠盘回库任务{mst.S_CODE},不触发叠盘任务");
                    }
                    //var loc = LocationHelper.GetLocByLoc(device.TN_Location);
                    //if (loc != null)
                    //{
                    //    if (loc.N_LOCK_STATE == 0) {
                    //        Traystacker(device.TN_Location, value, 2);
                    //    }
                    //    else
                    //    {
                    //        LogHelper.Info($"设备起点:{device.TN_Location}有锁,不生成任务");
                    //    }
                    //}
                    //else
                    //{
                    //    LogHelper.Info($"根据设备:{device.deviceName},起点:{device.TN_Location},未找到货位");
                    //}
                }
            }
            catch (Exception ex)
            {
 
                LogHelper.Error($"ReadDpj DpjTraffic:{ex}", ex);
            }
 
 
 
 
        }
 
        internal static void DpjTraffic2(DpjInfo dpj)
        {
 
            try
            {
                LogHelper.Info($"叠盘机{dpj.Address},叠盘信号{dpj.PickUp},数量{dpj.qty}");
                if (dpj.qty >= 1 && dpj.PickUp == 1)
                {
                    //var cntr = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE.Trim() == item.S_CODE).ToList();
                    //if (cntr.Count > 0)
                    //{
                    //    var cntrcodes = cntr.Select(a => a.S_CNTR_CODE).ToArray();
                    //    cntrcode = string.Join(",", cntrcodes);
                    //}
 
                    //创建过未完成的任务不发起请求
                    var mst = WCSHelper.GetTaskByStartloc(dpj.Loc);
                    if (mst == null)
                    {
                        Traystacker(dpj.Loc, dpj.qty, 2);
                    }
                    else
                    {
                        LogHelper.Info($"有执行中的叠盘回库任务{mst.S_CODE},不触发叠盘任务");
                    }
                    //var loc = LocationHelper.GetLocByLoc(device.TN_Location);
                    //if (loc != null)
                    //{
                    //    if (loc.N_LOCK_STATE == 0) {
                    //        Traystacker(device.TN_Location, value, 2);
                    //    }
                    //    else
                    //    {
                    //        LogHelper.Info($"设备起点:{device.TN_Location}有锁,不生成任务");
                    //    }
                    //}
                    //else
                    //{
                    //    LogHelper.Info($"根据设备:{device.deviceName},起点:{device.TN_Location},未找到货位");
                    //}
                }
            }
            catch (Exception ex)
            {
 
                LogHelper.Error($"ReadDpj DpjTraffic:{ex}", ex);
            }
 
 
 
 
        }
        /// <summary>
        /// 原材料库接口访问
        /// </summary>    
        internal static Resultmobox AgvEmptyBack(string loc, string cntrcode)
        {
            Resultmobox result = new Resultmobox();
            try
            {
                string partNumber = "";
                //上传数据
                Resultmobox<Traystackermobox> result1 = new Resultmobox<Traystackermobox>() { Name = "EmptyCntrReturn", Source = "HostToAgv" };
                Traystackermobox traystacker = new Traystackermobox() { LocationCode = loc, CntrCode = cntrcode, PartNumber = partNumber, Qty = 6 };
                result1.data = traystacker;
                HttpHelper httpHelper = new HttpHelper();
                //调用接口
 
                string date = JsonConvert.SerializeObject(result1);
                string url = Settings.tableUrls.Find(a => a.id == 11).url;
                LogHelper.Info($"空托返回:地址:{url},内容:{date}");
                var r = httpHelper.WebPostycl(url, date);
                result = JsonConvert.DeserializeObject<Resultmobox>(r);
                if (result.err_code == "0")
                {
 
                    LogHelper.Info($"空托返回请求成功");
                }
                else
                {
                    LogHelper.Info($"空托返回请求失败=>msg:{result.err_msg}");
                }
                return result;
            }
            catch (Exception ex)
            {
                LogHelper.Info($"空托返回  Error=>{ex.ToString()}");
                return result;
            }
        }
 
        public class DpjErrorInfo
        {
 
            public string Address { get; set; }
            public string DeviceName { get; set; }
            public string ErrorInfo { get; set; }
        }
 
        public class DpjInfo
        {
 
            public string Address { get; set; }
            public string Loc { get; set; }
            public int PickUp { get; set; }
            public int qty { get; set; }
            public int value2004 { get; set; }
        }
    }
}