222
schangxiang@126.com
2025-09-29 f782248da68c035aae12f902f29d828e9867abb0
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
 
using HPSocketCS;
using iWareCc.Cache.Entity;
using iWareCc.Conveyor.Datagram;
using iWareCc.Conveyor.Service;
using iWareCc.Conveyor.Socket;
using iWareCcTest.Properties;
using iWareCc.Util;
using iWareCommon.Utils;
using iWareDataCore.BASE.Entity;
using iWareDataCore.ORM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace iWareCc.Conveyor.Entity
{
   public class GateEntity
    {
        /// <summary>
        /// 库位
        /// </summary>
        public PlaceViewEntity Place { get; private set; }
 
    
 
        #region Client
        /// <summary>
        /// 用于收发报文的Socket服务(客户端)
        /// </summary>
        public SocketClient Socket { get; set; }
 
        /// <summary>
        /// Client内存日志
        /// </summary>
        public List<string> ClientReceiveLogger { get; set; }
 
        /// <summary>
        /// Client内存发送日志
        /// </summary>
        public List<string> ClientSendLogger { get; set; } 
        #endregion
 
        /// <summary>
        /// 用于发送报文的Socket服务(服务端)
        /// </summary>
        public SocketServer SocketServer { get; set; }
 
        /// <summary>
        /// Server接收端内存接收日志
        /// </summary>
        public List<string> ServerReceiveLogger { get; set; }
 
        /// <summary>
        /// Server接收端内存发送日志
        /// </summary>
        public List<string> ServerSendLogger { get; set; }
 
        /// <summary>
        /// 缓存接收报文的字符串
        /// </summary>
        public string DatagramReceivePool { get; set; }
 
        /// <summary>
        /// 缓存接收报文的字符串
        /// </summary>
        public string DatagramReceiveAlermPool { get; set; }
 
        /// <summary>
        /// 缓存接收报文的字符串
        /// </summary>
        public string DatagramReceiveSCPool { get; set; }
 
        /// <summary>
        /// 缓存发送报文的字符串
        /// </summary>
        public string DatagramSendPool { get; set; }
 
        /// <summary>
        /// 发送标志
        /// </summary>
        public bool SendFlag { get; set; }
 
  
 
        /// <summary>
        /// 未捕获心跳次数
        /// </summary>
        public int DisconnectTimes = 5;
 
        /// <summary>
        /// 上传获得的心跳数
        /// </summary>
        public string LastHandShakeCount = "0";
 
        /// <summary>
        /// 报警
        /// </summary>
        public string AlarmCode = "0";
 
        /// <summary>
        /// 是否在线
        /// </summary>
        public bool IsOnline { get; set; }
 
 
        /// <summary>
        /// 任务号
        /// </summary>
        public string TaskId { get; set; }
 
       /// <summary>
       /// 取消
       /// </summary>
        public bool Canceled { get; set; }
 
 
        /// <summary>
        /// 到位
        /// </summary>
        public bool ROccupied { get; set; }
 
        /// <summary>
        /// 是否为空
        /// </summary>
        public bool RIsEmpty { get; set; }
 
        /// <summary>
        /// 扫描到的物料号
        /// </summary>
        public string RCheckMaterialCode { get; set; }
 
        /// <summary>
        /// 放货完成(开始任务)
        /// </summary>
        public bool WPutFinishSymbol { get; set; }
 
        /// <summary>
        /// 取货完成(开始任务)
        /// </summary>
        public bool WPickFinishSymbol { get; set; }
 
        /// <summary>
        /// 滚动开始
        /// </summary>
        public bool WStartRotation { get; set; }
 
        /// <summary>
        /// 是否入库
        /// </summary>
        public bool WIsInStorage { get; set; }
 
        /// <summary>
        /// 错误数量
        /// </summary>
        public int ErrorCount { get; set; }
 
        /// <summary>
        /// WCS需要写入的OPC地址
        /// </summary>
        public OpcReadItem OpcReadItems { get; set; }
 
        /// <summary>
        /// WCS需要读取的OPC地址
        /// </summary>
        public OpcWriteItem OpcWriteItems { get; set; }
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="conveyor">调用该货口的输送机</param>
        public GateEntity(PlaceViewEntity place)
        {
            this.Place = place;
            this.DatagramReceivePool = "";
            this.DatagramSendPool = "";
            this.DatagramReceiveAlermPool = "";
            this.DatagramReceiveSCPool = "";
            this.SendFlag = false;
            this.ErrorCount = 0;
            //Client
            //this.ClientReceiveLogger = new List<string>();
            //this.ClientSendLogger = new List<string>();
            //this.Socket = new GateEntitySocket(this, new TcpClient(), place.Ip, place.Point);
            //Server
            this.ServerReceiveLogger = new List<string>();
            this.ServerSendLogger = new List<string>();
            this.SocketServer = new GateSocketServer( this,new TcpServer(),place.Ip, place.Point);
            switch (place.PlaceTypeName)
            {
                //入库输送机(Conveyor1)
                //起始点
                case "gate11":
                    OpcWriteItems = CacheEntity.IsRealMode ? new OpcWriteItem
                    {
                        WStartRotation = "S7:[S7_Connection_1]DB600,X2366.0",//滚动开始
                        WIsInStorage= "S7:[S7_Connection_1]DB600,X2366.0"//是否入库
                    } : new OpcWriteItem
                    {                   
                        WStartRotation = "S7:[@LOCALSERVER]DB1,X682.2",//滚动开始
                        WIsInStorage= "S7:[S7_Connection_1]DB600,X2366.0"//是否入库
                    };
                    OpcReadItems = CacheEntity.IsRealMode ? new OpcReadItem
                    {
                        ROccupied = "S7:[S7_Connection_1]DB600,X262.3",//是否到位
                        RIsEmpty = "S7:[S7_Connection_1]DB600,X262.2"//是否为空端口
 
                    } : new OpcReadItem
                    {
                        ROccupied = "S7:[@LOCALSERVER]DB1,X688.0",//允许开始
                        RIsEmpty = "S7:[@LOCALSERVER]DB1,X684.1"//是否为空端口
 
                    };
                    break;
                //目标点
                case "gate12":
                    OpcWriteItems = CacheEntity.IsRealMode ? new OpcWriteItem
                    {
                        WStartRotation = "S7:[S7_Connection_1]DB601,X516.4"//滚动开始
                    } : new OpcWriteItem
                    {
 
                        WStartRotation = "S7:[@LOCALSERVER]DB1,X952.1"//滚动开始
                    };
                    OpcReadItems = CacheEntity.IsRealMode ? new OpcReadItem
                    {
                        ROccupied = "S7:[S7_Connection_1]DB601,X516.2",//是否到位
                        RIsEmpty = "S7:[@LOCALSERVER]DB1,X684.1"//是否为空端口
                    } : new OpcReadItem
                    {
 
                        ROccupied = "S7:[@LOCALSERVER]DB1,X1466.1",//是否到位
                        RIsEmpty = "S7:[@LOCALSERVER]DB1,X684.1"//是否为空端口
 
                    };
                    break;
                case "gate13":
                    OpcWriteItems = CacheEntity.IsRealMode ? new OpcWriteItem
                    {
                        WStartRotation = "S7:[S7_Connection_1]DB601,X516.4"//滚动开始
                    } : new OpcWriteItem
                    {
 
                        WStartRotation = "S7:[@LOCALSERVER]DB1,X952.1"//滚动开始
                    };
                    OpcReadItems = CacheEntity.IsRealMode ? new OpcReadItem
                    {
                        ROccupied = "S7:[S7_Connection_1]DB601,X516.2",//是否到位
                        RIsEmpty = "S7:[@LOCALSERVER]DB1,X684.1"//是否为空端口
                    } : new OpcReadItem
                    {
 
                        ROccupied = "S7:[@LOCALSERVER]DB1,X1466.1",//是否到位
                        RIsEmpty = "S7:[@LOCALSERVER]DB1,X684.1",//是否为空端口
 
                    };
                    break;
                case "gate14":
                    OpcWriteItems = CacheEntity.IsRealMode ? new OpcWriteItem
                    {
                        WStartRotation = "S7:[S7_Connection_1]DB601,X516.4"//滚动开始
                    } : new OpcWriteItem
                    {
 
                        WStartRotation = "S7:[@LOCALSERVER]DB1,X952.1"//滚动开始
                    };
                    OpcReadItems = CacheEntity.IsRealMode ? new OpcReadItem
                    {
                        ROccupied = "S7:[S7_Connection_1]DB601,X516.2",//是否到位
                        RIsEmpty = "S7:[@LOCALSERVER]DB1,X684.1"//是否为空端口
                    } : new OpcReadItem
                    {
 
                        ROccupied = "S7:[@LOCALSERVER]DB1,X1466.1",//是否到位
                        RIsEmpty = "S7:[@LOCALSERVER]DB1,X684.1"//是否为空端口
 
                    };
                    break;
                case "gate15":
                    OpcWriteItems = CacheEntity.IsRealMode ? new OpcWriteItem
                    {
                        WPickFinishSymbol = "S7:[S7_Connection_1]DB601,X516.4"//取货完成
                    } : new OpcWriteItem
                    {
 
                        WPickFinishSymbol = "S7:[@LOCALSERVER]DB1,X952.1"//取货完成
                    };
                    OpcReadItems = CacheEntity.IsRealMode ? new OpcReadItem
                    {
                        ROccupied = "S7:[S7_Connection_1]DB601,X516.2",//是否到位
                        RIsEmpty = "S7:[@LOCALSERVER]DB1,X684.1"//是否为空端口
                    } : new OpcReadItem
                    {
 
                        ROccupied = "S7:[@LOCALSERVER]DB1,X1466.1",//是否到位
                        RIsEmpty = "S7:[@LOCALSERVER]DB1,X684.1"//是否为空端口
                    };
                    break;
 
                //出库输送机conveyor2
                //起始点
                case "gate21":
                    OpcWriteItems = CacheEntity.IsRealMode ? new OpcWriteItem
                    {
                        WStartRotation = "S7:[S7_Connection_1]DB600,X2366.0"//滚动开始
                    } : new OpcWriteItem
                    {
                        WStartRotation = "S7:[S7_Connection_1]DB600,X2366.0"//滚动开始
                    };
                    OpcReadItems = CacheEntity.IsRealMode ? new OpcReadItem
                    {
                        ROccupied = "S7:[S7_Connection_1]DB600,X262.3",//是否到位
                        RIsEmpty = "S7:[S7_Connection_1]DB600,X262.2"//是否为空端口
                    } : new OpcReadItem
                    {
                        ROccupied = "S7:[S7_Connection_1]DB600,X262.3",//是否到位
                        RIsEmpty = "S7:[S7_Connection_1]DB600,X262.2"//是否为空端口
                    };
                    break;
                //目标点
                case "gate22":
                    OpcWriteItems = CacheEntity.IsRealMode ? new OpcWriteItem
                    {
                        WStartRotation = "S7:[S7_Connection_1]DB600,X2366.0"//滚动开始
                    } : new OpcWriteItem
                    {
 
                        WStartRotation = "S7:[S7_Connection_1]DB600,X2366.0"//滚动开始
                    };
                    OpcReadItems = CacheEntity.IsRealMode ? new OpcReadItem
                    {
                        ROccupied = "S7:[S7_Connection_1]DB601,X516.2",//是否到位
                        RIsEmpty = "S7:[S7_Connection_1]DB600,X262.2",//是否为空端口
                    } : new OpcReadItem
                    {
                        ROccupied = "S7:[@LOCALSERVER]DB1,X3018.0",//是否到位
                        RIsEmpty = "S7:[S7_Connection_1]DB600,X262.2",//是否为空端口
                    };
                    break;
 
           
            }
        }
 
 
        /// <summary>
        /// 发送任务
        /// </summary>
        /// <returns>是否发送成功</returns>
        public bool SendOk()
        {
            try
            {
                var flag = CacheEntity.OpcWcfServiceClient.WriteValue(this.OpcWriteItems.WStartRotation, true);
                LogTextHelper.WriteLine(Resources.LogDir + @"\输送机发送报文\" + Place.PlaceTypeName, "发送自动开始标志={0},结果={1}", true, flag);
                return flag;
 
            }
            catch (Exception)
            {
                return false;
            }
 
        }
 
        /// <summary>
        /// 发送任务
        /// </summary>
        /// <returns>是否发送成功</returns>
        public bool SendProcessCard(string processCardNumber, int roadwayNo, int taskType)
        {
            try
            {
                var flag = CacheEntity.OpcWcfServiceClient.WriteValues(new string[] {  }, new object[] { roadwayNo, taskType, processCardNumber });
                LogTextHelper.WriteLine(Resources.LogDir + @"\输送机发送报文\" + Place.PlaceTypeName, "发送流转卡--> processCardNumber={0},roadwayNo={1},taskType={2},结果={3}", processCardNumber, roadwayNo, taskType, flag);
                return flag;
 
            }
            catch (Exception)
            {
                return false;
            }
        }
 
        /// <summary>
        /// 发送任务号
        /// </summary>
        /// <param name="taskid">任务号</param>
        /// <returns>是否发送成功</returns>
        public bool SendTaskId(string taskid)
        {
            try
            {
                var flag = CacheEntity.OpcWcfServiceClient.WriteValues(new string[] { this.OpcWriteItems.WTaskId }, new object[] { taskid });
                LogTextHelper.WriteLine(Resources.LogDir + @"\输送机发送报文\" + Place.PlaceTypeName, "发送任务号--> taskId={0},结果={1}", taskid, flag);
                return flag;
 
            }
            catch (Exception)
            {
                return false;
 
            }
        }
 
 
        /// <summary>
        /// 发送取货完成
        /// </summary>
        /// <returns>是否发送成功</returns>
        public bool SendPickSymbol()
        {
            try
            {
                var flag = CacheEntity.OpcWcfServiceClient.WriteValues(new string[] { this.OpcWriteItems.WPutFinishSymbol }, new object[] { true });
                LogTextHelper.WriteLine(Resources.LogDir + @"\输送机发送报文\" + Place.PlaceTypeName, "发送取货完成={0},结果={1}", true, flag);
                return flag;
 
            }
            catch (Exception)
            {
                return false;
            }
 
        }
 
 
        /// <summary>
        /// 重置流转卡
        /// </summary>
        /// <param name="processCardNumber">流转卡号</param>
        /// <returns>是否重置成功</returns>
        public bool ResetProcessCard(string processCardNumber)
        {
            try
            {
                var flag = CacheEntity.OpcWcfServiceClient.WriteValues(new string[] {}, new object[] { processCardNumber });
                LogTextHelper.WriteLine(Resources.LogDir + @"\输送机发送报文\" + Place.PlaceTypeName, "重置流转卡--> processCardNumber={0},结果={1}", processCardNumber, flag);
                return flag;
            }
            catch (Exception)
            {
                return false;
 
            }
        }
 
        /// <summary>
        /// 发送手动开始
        /// </summary>
        /// <returns>是否发送成功</returns>
        public bool SendStartSymbolManual(out string msg)
        {
            using (var dbModel = new DbModelCore())
            {
                try
                {
                    msg = "";
                    if (!this.ROccupied)
                    {
                        msg = string.Format("端口{0}上没有检测到物料", this.Place.PlaceTypeName);
                        return false;
 
                    }
 
                    var flag = CacheEntity.OpcWcfServiceClient.WriteValues(new string[] { this.OpcWriteItems.WStartRotation }, new object[] { true });
                    LogTextHelper.WriteLine(Resources.LogDir + @"\输送机发送报文\" + Place.PlaceTypeName, "发送手动放货完成={0},结果={1}", true, flag);
 
                    if (flag)
                    {
                        var pvc = dbModel.BASEPlaceMaterialViews.FirstOrDefault(x => x.placetypename == this.Place.PlaceTypeName);
                        if (pvc != null)
                        {
                            dbModel.BASEPlaceMaterialViews.Remove(pvc);
                            dbModel.SaveChanges();
                        }
                    }
 
                    return flag;
 
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                    return false;
                }
 
 
            }
 
 
        }
 
        /// <summary>
        /// 发送开始
        /// </summary>
        /// <returns>是否发送成功</returns>
        public bool SendStartSymbol(out string msg)
        {
            using (var dbModel = new DbModelCore())
            {
                try
                {
                    msg = "";
                    if (!this.ROccupied)
                    {
                        msg = string.Format("端口{0}上没有检测到物料", this.Place.PlaceTypeName);
                        return false;
 
                    }
 
                    var flag = CacheEntity.OpcWcfServiceClient.WriteValues(new string[] { this.OpcWriteItems.WPutFinishSymbol }, new object[] { true });
                    LogTextHelper.WriteLine(Resources.LogDir + @"\输送机发送报文\" + Place.PlaceTypeName, "发送放货完成={0},结果={1}", true, flag);
 
                    if (flag)
                    {
                        var pvc = dbModel.BASEPlaceMaterialViews.FirstOrDefault(x => x.placetypename == this.Place.PlaceTypeName );
                        if (pvc != null)
                        {
                            dbModel.BASEPlaceMaterialViews.Remove(pvc);
                            dbModel.SaveChanges();
                        }
                    }
 
                    return flag;
 
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                    return false;
                }
            }
        }
 
        #region 使用Socket通讯发送信息
        /// <summary>
        /// 发送入库滚动的任务
        /// </summary>
        /// <param name="taskid">任务号</param>
        /// <returns>是否发送成功</returns>
        public bool SendGateTask()
        {
            try
            {
                if (!new GateService(this).Send(new GateDatagramParse(new GateDatagramProp
                {
                    Name = this.Place.PlaceTypeName,
                    Roll = "T",
                    InOut = "T"
                }).ParseByteDatagram()))
                {
                    LogTextHelper.WriteLine(Resources.LogDir, "在类{0}中执行方法{1}时出现异常:{2}", this.ToString(), "SendGateTask", "发送命令失败");
                    return false;
                }
                return true;
            }
            catch (Exception)
            {
                return false;
 
            }
        }
       /// <summary>
       /// 发送物料宽度
       /// </summary>
       /// <returns></returns>
        public bool SendMaterialWindTask(string materialwind,string  thick)
        {
            try
            {
                int wind = int.Parse(materialwind);
                string mwind = "";
                if (wind < 1000 && wind >= 100)
                {
                    mwind = wind.ToString();
                }
                else if (wind < 100 && wind >= 10)
                {
                    mwind = "0" + wind.ToString();
                }
                else if (wind < 10 && wind > 0)
                {
                    mwind = "00" + wind.ToString();
                }
                else
                {
                    mwind = "999";
                }
                int thic = int.Parse(thick);
                string wthik = "";
                if (thic < 10)
                {
                    wthik = "0" + thic.ToString();
                }
                else if (thic >= 10 && thic < 100)
                {
                    wthik = thic.ToString();
                }
                else 
                {
                    wthik = "99";
                }
 
 
                if (!new GateService(this).Send(new GateDatagramParse(new GateDatagramProp
                {
                    Name = this.Place.PlaceTypeName,
                    Wind= mwind,
                    Thick= wthik
                }).ParseByteDatagram()))
                {
                    LogTextHelper.WriteLine(Resources.LogDir, "在类{0}中执行方法{1}时出现异常:{2}", this.ToString(), "SendGateTask", "发送命令失败");
                    return false;
                }
                return true;
            }
            catch (Exception)
            {
                return false;
 
            }
        }
        /// <summary>
        /// 发送报警信息
        /// </summary>
        /// <param name="taskid">任务号</param>
        /// <returns>是否发送成功</returns>
        public bool SendGateAlarmTask(int  msg)
        {
            try
            {
                if (!new GateService(this).Send(new GateDatagramParse(new GateDatagramProp
                {
                    Name = this.Place.PlaceTypeName,
                    Alarmcode =("0"+(msg+1).ToString())
                }).ParseByteDatagram()))
                {
                    LogTextHelper.WriteLine(Resources.LogDir, "在类{0}中执行方法{1}时出现异常:{2}", this.ToString(), "SendGateTask", "发送命令失败");
                    return false;
                }
                return true;
            }
            catch (Exception)
            {
                return false;
 
            }
        }
       /// <summary>
       /// 发送不入库直接滚动的任务
       /// </summary>
       /// <returns></returns>
        public bool SendNotIngGateTask()
        {
            try
            {
                if (!new GateService(this).Send(new GateDatagramParse(new GateDatagramProp
                {
                    Name = this.Place.PlaceTypeName,
                    Roll = "F",
                    InOut = "F"
                }).ParseByteDatagram()))
                {
                    LogTextHelper.WriteLine(Resources.LogDir, "在类{0}中执行方法{1}时出现异常:{2}", this.ToString(), "SendGateTask", "发送命令失败");
                    return false;
                }
                return true;
            }
            catch (Exception)
            {
                return false;
 
            }
        }
        #endregion
 
    }
}