111
cjs
2025-06-20 ad3c446720a996d3bad0e53d1c2bc448753c5e1c
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
using Hanhe.iWCS.Business;
using Hanhe.iWCS.Common;
using Hanhe.iWCS.Event;
using Hanhe.iWCS.Interface;
using Hanhe.iWCS.MData;
using Hanhe.iWCS.Model;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Linq;
using System.Threading;
using static Hanhe.iWCS.JingmenGEMTwoProtocol.PLCControl;
using static Hanhe.iWCS.JingmenGEMTwoProtocol.ProcessHelper;
using static Hanhe.iWCS.JingmenGEMTwoProtocol.ProtocolAnalysis;
 
namespace Hanhe.iWCS.JingmenGEMTwoProtocol
{
    /// <summary>
    /// 事件监听处理和触发事件回调
    /// </summary>
    public class EnentListen : IEnentListen
    {
        private EquipmentCommandEQBLL CommandEQBLL = new EquipmentCommandEQBLL();
        private ICallTaskProcessing iCallTask = new ICallTaskProcessing();
        static object locker = new object();
        static object lock10015 = new object();
        static EnentListen() {
        }
        /// <summary>
        /// 事件订阅
        /// </summary>
        /// <param name="currActionModel">当前动作模型</param>
        /// <returns></returns>
        public bool EnentSubscribe(TN_I_TASK_DTL_ACTION currActionModel) {
            try {
                EventDelegate eventDelegate = new EventDelegate();
                eventDelegate.CurrActionModel = currActionModel;
                //eventDelegate.iSleep = 3000;
                eventDelegate.StartListening();
 
                return true;
            }
            catch (Exception ex) {
                throw new Exception(ex.Message);
            }
        }
 
        private SortByDocument sortBy = new SortByDocument { { "createTime", 1 } };
        /// <summary>
        /// 事件监听处理
        /// </summary>
        /// <param name="currActionModel">当前动作模型</param>
        /// <returns>true:条件成立 false:条件不成立</returns>
        public bool MonitorProcess(TN_I_TASK_DTL_ACTION currActionModel) {
            bool bResult = false;
            string msg = string.Empty;
            #region   处理任务状态
            if (currActionModel.CN_N_ACTION_CODE == 1027)
            {
                var request = MongoDBSingleton.Instance.FindAll<Mongo_AGV_TASK_STATE>();
                if (request.Count > 0)
                {
                    request.ForEach(a =>
                    {
                        try
                        {
                            CMMLog.Info($"进入workflowToDeviceDriver,CN_S_TASK_NO:{a.TaskNo},CN_N_ACTION_CODE:{a.State},CN_S_DEVICE_CODE:{a.ForkliftNo},Ext2:{a.LockNo},Ext3:{a.ExtData}");
                            new ProtocolAnalysis().workflowToDeviceDriver(new TN_I_TASK_DTL_ACTION { CN_S_TASK_NO = a.TaskNo, CN_N_ACTION_CODE = a.State, CN_S_DEVICE_CODE = a.ForkliftNo, Ext2 = a.LockNo, Ext3 = a.ExtData });
                            MongoDBSingleton.Instance.Remove<Mongo_AGV_TASK_STATE>(Query.EQ("_id", a._id), RemoveFlags.Single);
                        }
                        catch (Exception ex)
                        {
                            CMMLog.Error(ex.Message, ex);
                        }
                    });
                }
                Thread.Sleep(3000);
            }
            #endregion
            #region   3楼设备任务
            #region 10001-读取包装线信号-3楼包装取料--已完成(待测试) 一期
            if (currActionModel.CN_N_ACTION_CODE == 10001)
            {
                var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "1").ToList();
                if (list.Count > 0)
                {
                    list.ForEach(a =>
                    {
                        // 包装机启动时读取【翻页通道】,根据页号获取五条数据,然后写入包装机
                        //ERPService.WriteItemInfo(a);
                        //ERPService.WriteEmpInfo(a);
 
                        CMMLog.Debug($"一期当前包装机号:{a.location},当前时间:{DateTime.Now},当前ip:{a.ip},当前端口:{a.port}");
                        PLCControl.CheckPackingMachine(a);
                    });
                }
                Thread.Sleep(3000);
                
            }
            #endregion
            
            #region 10002-读取包装线信号-3楼包装取料--已完成(待测试) 二期
            if (currActionModel.CN_N_ACTION_CODE == 10002)
            {
                //var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && (a.deviceType == "22" || a.deviceType == "")).ToList(); //变更注释
                var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "22").ToList();
                if (list.Count > 0)
                {
                    list.ForEach(a =>
                    {
                        // 包装机启动时读取【翻页通道】,根据页号获取五条数据,然后写入包装机
                        //ERPService.WriteItemInfo(a);
                        //ERPService.WriteEmpInfo(a);
                        CMMLog.Debug($"二期当前包装机号:{a.location},当前时间:{DateTime.Now},当前ip:{a.ip},当前端口:{a.port}");
                        PLCControl.CheckPackingMachineTwo(a);
                        CMMLog.Debug($"二期包装机取料任务结束,当前时间:{DateTime.Now},当前ip:{a.ip},当前端口:{a.port}");
                    });
                }
                Thread.Sleep(3000);
            }
            #endregion
 
            #region 10002-读取包装线信号-3楼复称入缓存架--弃用
            //if (currActionModel.CN_N_ACTION_CODE == 10002)
            //{
            //    var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "2").ToList();
            //    if (list.Count > 0)
            //    {
            //        list.ForEach(a =>
            //        {
            //            PLCControl.SecondWeightInCache(a);
            //        });
            //    }
            //    //Thread.Sleep(3000);
            //}
            #endregion
 
            #region   10003-读取叠包机信号(三楼双层缓存架入叠盘机流程)-3楼缓存架入叠托--已完成(待测试)
            if (currActionModel.CN_N_ACTION_CODE == 10003)
            {
                var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "3").ToList();
                if (list.Count > 0)
                {
                    list.ForEach(a =>
                    {
                        PLCControl.CacheStackingMouth(a);
                        Thread.Sleep(1000);
                    });
                }
                Thread.Sleep(3000);
            }
            #endregion
            
            #region   10004-读包装机信号(包装机补空托流程)-3楼包装补空--已完成(待测试)
            if (currActionModel.CN_N_ACTION_CODE == 10004)
            {
                var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && (a.deviceType == "1" || a.deviceType == "22")).ToList();
                if (list.Count > 0)
                {
                    list.ForEach(a =>
                    {
                        PLCControl.PickUpBlank(a);
                        Thread.Sleep(1000);
                    });
                }
                Thread.Sleep(3000);
            }
            #endregion
            
            #region 10005-打包线空托补料请求(3楼拆盘机补空托)-3楼拆盘补空--已完成(待测试)
            if (currActionModel.CN_N_ACTION_CODE == 10005)
            {
                var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "6").FirstOrDefault();
                if (plc != null)
                {
                    if (PickUpEndFree(plc.location))
                    {
                        PLCControl.CheckPackingLineEmpty(plc);
                    }
                }
                Thread.Sleep(3000);
            }
            #endregion
            
            #region 10006-叠盘机满托下线(3楼叠盘下线)-3楼叠盘下线--已完成(待测试)
            if (currActionModel.CN_N_ACTION_CODE == 10006)
            {
                var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "15").FirstOrDefault();
                if (plc != null)
                {
                    if (ProcessHelper.PickUpStartFree(plc.location))
                    {
                        PLCControl.StackingLineEmpty(plc);
                    }
                }
                Thread.Sleep(3000);
            }
            #endregion
            
            #region 10007-打包线下线-3楼打包下线--已完成(待测试)
            //if (currActionModel.CN_N_ACTION_CODE == 10007)
            //{
            //    var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "4").FirstOrDefault();
            //    if (plc != null)
            //    {
            //        if (CheckStartFree(plc.location))
            //        {
            //            PLCControl.CheckPackingLineFull(plc);
            //        }
            //    }
            //    //Thread.Sleep(500);
            //}
            
            //测试用,直接生成任务
            if (currActionModel.CN_N_ACTION_CODE == 10007)
            {
                var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "4").FirstOrDefault();
                if (plc != null)
                {
                    if (CheckStartFree(plc.location))
                    {
                        PLCControl.CheckPackingLineFull1(plc);
                    }
                }
                //Thread.Sleep(500);
            }
            #endregion
            
            #region 10008-接驳位入立库 辅材流程
            if (currActionModel.CN_N_ACTION_CODE == 10008)
            {
                var conInfo = Settings.GetConnectingbitsList().Where(a => a.enable == 1).ToList();
                if(conInfo != null)
                {
                    foreach(var a in conInfo)
                    {
                        CMMLog.Info(JsonConvert.SerializeObject(a));
                        PLCControl.InWarehouse(a.locCode);
                    }
                }
            }
            #endregion
            
            #region 10009-缓存位入入料平台
            if (currActionModel.CN_N_ACTION_CODE == 10009)
            {
                var plc = Settings.GetPlcInfo().Where(a => a.deviceType == "18" && a.enable == 1).ToList();
                if(plc.Count > 0)
                {
                    foreach(var a in plc)
                    {
                        PLCControl.InInlet(a);
                    }
                }
                Thread.Sleep(3000);
            }
            #endregion
 
            #endregion
 
            #region 1000-电梯任务处理--待确认
            //if (currActionModel.CN_N_ACTION_CODE == 1000)
            //{
                //ProcessHelper.CheckElevatorTask();
                //Thread.Sleep(500);
            //}
            #endregion
 
            #region   2000-充电任务--无需改动
            if (currActionModel.CN_N_ACTION_CODE == 2000)
            {
                ProcessHelper.AGVBatteryTime();
                //Thread.Sleep(500);
            }
            #endregion
 
            #region   2001-包装机写电量--已完成(待测试)
            if (currActionModel.CN_N_ACTION_CODE == 2001)
            {
                PLCControl.WriteBattery();
                Thread.Sleep(3000);
            }
            #endregion
 
            #region   10010-任务动作线程
 
            if (currActionModel.CN_N_ACTION_CODE == 10010)
            {
                lock (locker)
                {
                    var request = MongoDBSingleton.Instance.FindAll<ActionControlModel>();
            
                    if (request.Count > 0)
                    {
                        request.ForEach(a =>
                        {
                            try
                            {
                                CMMLog.Info("复称入缓存架任务,当前数据:"+ JsonConvert.SerializeObject(a));
                                if (a.actionNo == "1") SecondWeightActionOne(a);
                            }
                            catch (Exception ex)
                            {
                                CMMLog.Error($"10010" + ex.Message, ex);
                            }
                        });
                    }
                }
                
                Thread.Sleep(3000);
            }
 
            #endregion
 
            #region 10011-设备合格率计算
            if (currActionModel.CN_N_ACTION_CODE == 10011)
            {
                PLCControl.machine();
            }
            #endregion
 
            #region 10012-删除变更中间表时间大于两周的任务
            if (currActionModel.CN_N_ACTION_CODE == 10012)
            {
                PLCControl.deleteTableTask();
                Thread.Sleep(30000);
            }
            #endregion
 
            #region 10013-ERP变更功能 插入简单生产入库表
            if (currActionModel.CN_N_ACTION_CODE == 10013)
            {
                //ERPService.SendERPTaskCompleteFunc();
                ERPService.SendERPTaskCompleteFuncTwo(); //2025-1-7 变更 增加分录ID 更改erp入库表
            }
            #endregion
 
            #region 10014-车间看板内容写入通道
            if (currActionModel.CN_N_ACTION_CODE == 10014)
            {
                var cjWare = Settings.GetwareInfoList().Where(a => a.wareName == "车间" && a.enable == 1).FirstOrDefault();
                if (cjWare != null)
                {
                    PLCControl.writeCJQtt(cjWare);
                }
                else CMMLog.Info("车间看板信息未配置");
 
                Thread.Sleep(3000);
            }
            #endregion
 
            #region 10015-ERP变更功能 将物料表数据插入中间表
            if (currActionModel.CN_N_ACTION_CODE == 10015)
            {
                lock (lock10015)
                {
                  ERPService.insertMidTable();
                }
            }
            #endregion
 
            #region 10016-MES入库反馈
            if (currActionModel.CN_N_ACTION_CODE == 10016)
            {
                ERPService.SendMESTaskCompleteFunc();
            }
            #endregion
 
            #region 10021-erp简单生产入库表反馈失败,重新反馈
            if (currActionModel.CN_N_ACTION_CODE == 10021)
            {
                ERPService.SendERPTaskCompleteFuncThree();
            }
            #endregion
 
            #region 电钴
 
            #region 10017-A口
            if (currActionModel.CN_N_ACTION_CODE == 10017)
            {
                var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "41").FirstOrDefault();
                if (plc != null)
                {
                    PLCControl.AMachine(plc);
                }
            }
            #endregion
 
            #region 10018-C口
            if (currActionModel.CN_N_ACTION_CODE == 10018)
            {
                var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "42").ToList();
                if (list.Count > 0)
                {
                    list.ForEach(a =>
                    {
                        PLCControl.CMachine(a);
                    });
                }
                Thread.Sleep(3000);
 
            }
            #endregion
 
            #region 10019-D口
            if (currActionModel.CN_N_ACTION_CODE == 10019)
            {
                var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "43").ToList();
                if (list.Count > 0)
                {
                    list.ForEach(a =>
                    {
                        PLCControl.DMachine(a);
                    });
                }
                Thread.Sleep(3000);
 
            }
            #endregion
 
            #region 10020-电钴接驳位入立库
            if (currActionModel.CN_N_ACTION_CODE == 10020)
            {
                var conInfo = Settings.GetDGConnectingbitsList().Where(a => a.enable == 1).ToList();
                if (conInfo != null)
                {
                    foreach (var a in conInfo)
                    {
                        CMMLog.Info(JsonConvert.SerializeObject(a));
                        PLCControl.DGInWarehouse(a.locCode);
                    }
                }
            }
            #endregion
 
            #region 10022-反馈erp 插入简单生产入库表
            if (currActionModel.CN_N_ACTION_CODE == 10022)
            {
                ERPService.SendERPTaskCompleteFuncDG();
            }
            #endregion
 
            #endregion
            return bResult;
        }
 
        /// <summary>
        /// 触发回调处理
        /// </summary>
        /// <param name="currActionModel">当前动作模型</param>
        /// <returns></returns>
        public bool EventCallbacks(TN_I_TASK_DTL_ACTION currActionModel) {
            bool bResult = true;
            //测试码检测
            if (currActionModel.CN_N_ACTION_CODE == 99999) {
            }
            return bResult;
        }
 
        private DateTime GetTime(int hour, int min = 0,int second = 0) {
            var now = DateTime.Today;
            var dt = new DateTime(now.Year, now.Month, now.Day, hour, min, second);
            //Console.WriteLine(dt.ToString());
            return dt;
        }
        public class Mongo_AGV_TASK_STATE
        {
            public ObjectId _id { get; set; }
            public int State { get; set; }
            public string TaskNo { get; set; }
            public string ForkliftNo { get; set; }
            public string LockNo { get; set; }
            /// <summary>
            /// 扩展参数 可接收任何变量目前对应ext3
            /// </summary>
            public string ExtData { get; set; }
        }
 
 
        
    }
}