2
schangxiang@126.com
2024-11-22 76fc40d4f1e0dbd15383c0f3e29fd8bcb6dcff2a
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
 
 
using iWareSda.Common;
using System.Linq;
using iWareModel;
using System;
using System.Collections.Generic;
using System.Threading;
using iWareCommon.Utils;
using iWareCommon.Common.Globle;
using iWareCommon;
using iWareSda.Devices._3_Station.StationModel;
using Newtonsoft.Json;
using iWareSda.Model;
 
 
namespace iWareSda
{
    public class StationEntity : IDevice
    {
        public StationEntity()
        {
 
        }
 
        #region OPC相关
 
        /// <summary>
        /// 写的DB块
        /// </summary>
        public StationDBForWrite DBBlockForWrite { get; set; }
 
        /// <summary>
        /// 读的DB块
        /// </summary>
        public StationDBForRead DBBlockForRead { get; set; }
 
        /// <summary>
        /// 读取PLC实例
        /// </summary>
        public PLCService plcService { get; set; }
 
        /// <summary>
        /// 视图对象
        /// </summary>
        public StationView View { get; set; }
 
 
        #endregion
 
 
        /// <summary>
        /// 初始化数据
        /// </summary>
        public void InitData()
        {
 
 
        }
        public StationEntity(string srmName, int deviceId, StationDBForWrite _dbBlockForWrite, StationDBForRead _dbBlockForRead, PLCService _plcService)
        {
            this.View = new StationView();
            this.View.DeviceName = srmName;
            this.View.DeviceId = deviceId;
 
            //集合赋值初始化
            this.View.R_StationForReadCommList = new List<StationDB2ObjForReadComm>();
            this.View.W_StationForWriteCommList = new List<StationDB2ObjForWriteComm>();
            for (int i = 0; i < WareSdaStruct.StationStationList.Count; i++)
            {
                this.View.R_StationForReadCommList.Add(new StationDB2ObjForReadComm() { StationCode = WareSdaStruct.StationStationList[i] });
                this.View.W_StationForWriteCommList.Add(new StationDB2ObjForWriteComm() { StationCode = WareSdaStruct.StationStationList[i] });
            }
 
            plcService = _plcService;
            DBBlockForWrite = _dbBlockForWrite;
            DBBlockForRead = _dbBlockForRead;
        }
 
 
        #region 个性化方法
 
        /// <summary>
        /// Station任务发送
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="taskId"></param>
        /// <param name="sourcePlce"></param>
        /// <param name="toPlace"></param>
        /// <param name="taskType"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool SendTask(int deviceId, int taskId, string sourcePlce, string toPlace, out string msg)
        {
            msg = "";
            return false;
            //try
            //{
            //    msg = "";
            //    if (!CanSendTask(deviceId, out msg))
            //    {
            //        return false;
            //    }
 
            //    //这里特殊转换一下
            //    if (toPlace == ((int)EDevice.出库口1012).ToString())
            //    {
            //        toPlace = ((int)EDevice.提升机1030).ToString();
            //    }
 
 
            //    StationDBForWriteComm w_db_source = this.DBBlockForWrite.W_StationForWriteCommList.FirstOrDefault(x => x.StationCode == sourcePlce);
            //    StationDB2ObjForWriteComm w_dbObj_source = this.View.W_StationForWriteCommList.FirstOrDefault(x => x.StationCode == sourcePlce);
 
            //    //StationDBForWriteComm w_db_to = this.DBBlockForWrite.W_StationForWriteCommList.FirstOrDefault(x => x.StationCode == toPlace);
            //    //StationDB2ObjForWriteComm w_dbObj_to = this.View.W_StationForWriteCommList.FirstOrDefault(x => x.StationCode == toPlace);
            //    //如果ACK为true,表示不能发任务
            //    var readObj = this.View.R_StationForReadCommList.FirstOrDefault(x => x.StationCode == sourcePlce);
            //    if (readObj.R_ACK == true)
            //    {
            //        msg = "ACK为TRUE,不允许发任务";
            //        return false;
            //    }
 
            //    MessageModel b2 = null;
            //    if (!b2.result)
            //    {
            //        msg = string.Format("向Station{0},起点{1}发送任务{2},写入目标站点{3}失败,返回结果:" + b2.resMsg, this.View.DeviceName, sourcePlce, taskId, toPlace);
            //        return false;
            //    }
            //    b2 = this.plcService.WriteValuePoint(w_db_source.W_TaskID, Convert.ToInt32(taskId), w_dbObj_source.W_TaskID);
            //    if (!b2.result)
            //    {
            //        msg = string.Format("向Station{0},起点{1},目标点{2},写入任务号{3}失败,返回结果:" + b2.resMsg, this.View.DeviceName, sourcePlce, toPlace, taskId);
            //        return false;
            //    }
 
 
            //    return true;
            //}
            //catch (Exception ex)
            //{
            //    msg = ex.Message;
            //    Log4NetHelper.WriteErrorLog(Station_CacheEntity.curLogType, "发送任务出现异常:" + ex.Message, ex);
            //    return false;
            //}
        }
 
 
        /// <summary>
        /// 任务下发确认(收到PLC反馈后WCS清除)
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool WriteStp(int deviceId, string stationCode, bool value, out string msg)
        {
            try
            {
                msg = "";
                StationDBForWriteComm w_db = this.DBBlockForWrite.W_StationForWriteCommList.FirstOrDefault(x => x.StationCode == stationCode);
                StationDB2ObjForWriteComm w_dbObj = this.View.W_StationForWriteCommList.FirstOrDefault(x => x.StationCode == stationCode);
                MessageModel b2 = null;
                if (!b2.result)
                {
                    msg = string.Format("向Station{0},站点号{1}写 任务下发确认(收到PLC反馈后WCS清除) " + value + "失败,返回结果:" + b2.resMsg,
                        this.View.DeviceName, stationCode);
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                var logcontext = string.Format("向Station{0},站点号{1}写 任务下发确认(收到PLC反馈后WCS清除) " + true + "出现异常:" + JsonConvert.SerializeObject(ex),
                        deviceId, stationCode);
                Log4NetHelper.WriteErrorLog(Station_CacheEntity.curLogType, logcontext, ex);
                return false;
            }
        }
 
 
        /// <summary>
        /// 写入齐套结果
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="value">true:表示 齐套。false:不齐套</param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool WriteQiTaoInfo(string stationCode, bool value, out string msg)
        {
            try
            {
                msg = "";
                var stationObj = this.DBBlockForWrite.W_StationForWriteCommList.Where(x => x.StationCode == stationCode).FirstOrDefault();
 
                var view_stationObj = this.View.W_StationForWriteCommList.Where(x => x.StationCode == stationCode).FirstOrDefault();
 
                int i_result = 0;
                if (value)
                {
                    i_result = 998;
                }
                else
                {
                    i_result = 999;
                }
                MessageModel b2 = this.plcService.WriteValuePoint(stationObj.W_KittingInfo, i_result, view_stationObj.W_KittingInfo);
                if (!b2.result)
                {
                    msg = string.Format("向Station地址{0} 写  " + value + "失败,返回结果:" + b2.resMsg,
                      stationObj.W_DataFinish);
                    return false;
                }
 
                b2 = this.plcService.WriteValuePoint(stationObj.W_DataFinish, true, view_stationObj.W_DataFinish);
                if (!b2.result)
                {
                    msg = string.Format("向Station地址{0} 写  " + true + "失败,返回结果:" + b2.resMsg,
                       stationObj.W_DataFinish);
                    return false;
                }
 
                return true;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                var logcontext = string.Format("向Station地址{0}  " + value + "出现异常:" + JsonConvert.SerializeObject(ex),
                      "");
                Log4NetHelper.WriteErrorLog(Station_CacheEntity.curLogType, logcontext, ex);
                return false;
            }
        }
 
 
        public bool InboundFinishConfirm(string stationCode, bool value, out string msg)
        {
            try
            {
                msg = "";
                var stationObj = this.DBBlockForWrite.W_StationForWriteCommList.Where(x => x.StationCode == stationCode).FirstOrDefault();
 
                var view_stationObj = this.View.W_StationForWriteCommList.Where(x => x.StationCode == stationCode).FirstOrDefault();
                MessageModel b2 = this.plcService.WriteValuePoint(stationObj.W_InboundFinishConfirm, value, view_stationObj.W_DataFinish);
                if (!b2.result)
                {
                    msg = string.Format("向Station地址{0}  入库完成确认 写  " + value + "失败,返回结果:" + b2.resMsg,
                        stationObj.W_InboundFinishConfirm);
                    return false;
                }
 
                return true;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                var logcontext = string.Format("向Statio 入库完成确认 出现异常:" + JsonConvert.SerializeObject(ex),
                        "");
                Log4NetHelper.WriteErrorLog(Station_CacheEntity.curLogType, logcontext, ex);
                return false;
            }
        }
 
        public bool OutboundFinishConfirm(string stationCode, bool value, out string msg)
        {
            try
            {
                msg = "";
                var stationObj = this.DBBlockForWrite.W_StationForWriteCommList.Where(x => x.StationCode == stationCode).FirstOrDefault();
 
                var view_stationObj = this.View.W_StationForWriteCommList.Where(x => x.StationCode == stationCode).FirstOrDefault();
                MessageModel b2 = this.plcService.WriteValuePoint(stationObj.W_OutboundFinishConfirm, value, view_stationObj.W_OutboundFinishConfirm);
                if (!b2.result)
                {
                    msg = string.Format("向Station 出库任务完成确认 地址{0} 写  " + value + "失败,返回结果:" + b2.resMsg,
                        stationObj.W_OutboundFinishConfirm);
                    return false;
                }
 
                return true;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                var logcontext = string.Format("向Statio 出库任务完成确认 出现异常:" + JsonConvert.SerializeObject(ex),
                        "");
                Log4NetHelper.WriteErrorLog(Station_CacheEntity.curLogType, logcontext, ex);
                return false;
            }
        }
 
        public bool WriteInStoreTaskInfo(string stationCode, string toPlaceNo, short length, short width, short height, out string msg, int inbound_TaskID)
        {
            try
            {
                msg = "";
                var stationObj = this.DBBlockForWrite.W_StationForWriteCommList.Where(x => x.StationCode == stationCode).FirstOrDefault();
 
                var view_stationObj = this.View.W_StationForWriteCommList.Where(x => x.StationCode == stationCode).FirstOrDefault();
 
                MessageModel b2 = this.plcService.WriteValuePoint(stationObj.W_Inbound_TaskID, inbound_TaskID, view_stationObj.W_Inbound_TaskID);
                if (!b2.result)
                {
                    msg = string.Format("向Station地址{0} 写 入库任务号 " + inbound_TaskID + "失败,返回结果:" + b2.resMsg,
                      stationObj.W_Inbound_TaskID);
                    return false;
                }
 
                b2 = this.plcService.WriteValuePoint(stationObj.W_Destination_In, Convert.ToInt32(toPlaceNo), view_stationObj.W_Destination_In);
                if (!b2.result)
                {
                    msg = string.Format("向Station地址{0} 写 库位号 " + toPlaceNo + "失败,返回结果:" + b2.resMsg,
                      stationObj.W_Destination_In);
                    return false;
                }
 
                b2 = this.plcService.WriteValuePoint(stationObj.W_Length, length, view_stationObj.W_Length);
                if (!b2.result)
                {
                    msg = string.Format("向Station地址{0} 写 长度 " + length + "失败,返回结果:" + b2.resMsg,
                      stationObj.W_Length);
                    return false;
                }
                b2 = this.plcService.WriteValuePoint(stationObj.W_Width, width, view_stationObj.W_Width);
                if (!b2.result)
                {
                    msg = string.Format("向Station地址{0} 写 宽度 " + width + "失败,返回结果:" + b2.resMsg,
                      stationObj.W_Width);
                    return false;
                }
                b2 = this.plcService.WriteValuePoint(stationObj.W_Height, height, view_stationObj.W_Height);
                if (!b2.result)
                {
                    msg = string.Format("向Station地址{0} 写 高度 " + height + "失败,返回结果:" + b2.resMsg,
                     stationObj.W_Height);
                    return false;
                }
 
 
                b2 = this.plcService.WriteValuePoint(stationObj.W_DataFinish, true, view_stationObj.W_DataFinish);
                if (!b2.result)
                {
                    msg = string.Format("向Station地址{0} 写 读码解析完成 " + true + "失败,返回结果:" + b2.resMsg,
                       stationObj.W_DataFinish);
                    return false;
                }
 
 
                return true;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                var logcontext = string.Format("WriteInStoreTaskInfo出现异常:" + JsonConvert.SerializeObject(ex),
                        "");
                Log4NetHelper.WriteErrorLog(Station_CacheEntity.curLogType, logcontext, ex);
                return false;
            }
        }
 
 
        public bool WriteOutStoreTaskInfo(string stationCode, string sourcePlaceNo, out string msg, int outbound_TaskID)
        {
            try
            {
                msg = "";
                var stationObj = this.DBBlockForWrite.W_StationForWriteCommList.Where(x => x.StationCode == stationCode).FirstOrDefault();
 
                var view_stationObj = this.View.W_StationForWriteCommList.Where(x => x.StationCode == stationCode).FirstOrDefault();
                MessageModel b2 = this.plcService.WriteValuePoint(stationObj.W_Outbound_TaskID, true, view_stationObj.W_Outbound_TaskID);
                if (!b2.result)
                {
                    msg = string.Format("向Station地址{0} 写 出库任务号 " + true + "失败,返回结果:" + b2.resMsg,
                       stationObj.W_Outbound_TaskID);
                    return false;
                }
 
 
                b2 = this.plcService.WriteValuePoint(stationObj.W_ReqOut, true, view_stationObj.W_ReqOut);
                if (!b2.result)
                {
                    msg = string.Format("向Station地址{0} 写 请求出库 " + true + "失败,返回结果:" + b2.resMsg,
                      stationObj.W_ReqOut);
                    return false;
                }
 
                b2 = this.plcService.WriteValuePoint(stationObj.W_Destination_Out, Convert.ToInt32(sourcePlaceNo), view_stationObj.W_Destination_Out);
                if (!b2.result)
                {
                    msg = string.Format("向Station地址{0} 写 库位号 " + sourcePlaceNo + "失败,返回结果:" + b2.resMsg,
                      stationObj.W_Destination_Out);
                    return false;
                }
 
 
                return true;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                var logcontext = string.Format("WriteOutStoreTaskInfo 出现异常:" + JsonConvert.SerializeObject(ex),
                        "");
                Log4NetHelper.WriteErrorLog(Station_CacheEntity.curLogType, logcontext, ex);
                return false;
            }
        }
 
 
        /// <summary>
        ///  Station的任务完成ACK赋值
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="value"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool WriteStation_FinishACK(int deviceId, bool value, int plcTaskId, out string msg)
        {
            msg = "";
            return false;
        }
 
 
        /// <summary>
        ///  四个入库口扫描验证ACK赋值
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="value"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool WriteScanValidateACK(bool isReset, int deviceId, string stationCode, bool value, int plcTaskId, out string msg)
        {
            msg = "";
            return false;
        }
 
 
        /// <summary>
        /// 取消报警
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool ClearAlarm(int deviceId, out string msg)
        {
            msg = "";
            return true;
            //try
            //{
            //    msg = "";
            //    var dbNumber = Station_CacheEntity.W_DBHeader;
            //    MessageModel b2 = this.plcService.WriteValuePoint( this.DBBlockForWrite.W_Reset, true, this.View.W_Reset);
            //    if (!b2.result)
            //    {
            //        msg = string.Format("向Station{0}写入设备复位" + true + "失败,返回结果:" + b2.resMsg, this.View.DeviceName);
            //        return false;
            //    }
            //    return true;
            //}
            //catch (Exception ex)
            //{
            //    msg = ex.Message;
            //    Log4NetHelper.WriteErrorLog(Station_CacheEntity.curLogType, "取消报警出现异常:" + ex.Message, ex);
            //    return false;
            //}
        }
 
        /// <summary>
        /// 任务删除
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="taskId"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool DeleteTask(int deviceId, int taskId, out string msg)
        {
            msg = "";
            return true;
            //try
            //{
            //    msg = "";
            //    if (this.View.R_TaskNo != taskId)
            //    {
            //        msg = string.Format("Station当前非执行taskId={0}的任务", taskId);
            //        return false;
            //    }
            //    var dbNumber = Station_CacheEntity.W_DBHeader;
            //    MessageModel b2 = this.plcService.WriteValuePoint( this.DBBlockForWrite.W_TaskNo, taskId, this.View.W_TaskNo);
            //    if (!b2.result)
            //    {
            //        msg = string.Format("向Station{0}写入任务号" + taskId + "失败,返回结果:" + b2.resMsg, this.View.DeviceName);
            //        return false;
            //    }
            //    b2 = this.plcService.WriteValuePoint( this.DBBlockForWrite.W_Reset, 1, this.View.W_Reset);
            //    if (!b2.result)
            //    {
            //        msg = string.Format("向Station{0}写入设备复位" + 1 + "失败,返回结果:" + b2.resMsg, this.View.DeviceName);
            //        return false;
            //    }
            //    return true;
            //}
            //catch (Exception ex)
            //{
            //    msg = ex.Message;
            //    Log4NetHelper.WriteErrorLog(Station_CacheEntity.curLogType, "任务删除出现异常:" + ex.Message, ex);
            //    return false;
            //}
        }
 
        /// <summary>
        /// 急停
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool EmergencyStop(int deviceId, string stationCode, out string msg)
        {
            msg = "";
            return true;
            //try
            //{
            //    msg = "";
            //    var dbNumber = Station_CacheEntity.W_DBHeader;
            //    MessageModel b2 = this.plcService.WriteValuePoint( this.DBBlockForWrite.W_StationForWriteCommList.Find(x => x.StationCode == stationCode)., 1, this.View.W_EStop);
            //    if (!b2.result)
            //    {
            //        msg = string.Format("向Station{0}写入急停" + 1 + "失败,返回结果:" + b2.resMsg, this.View.DeviceName);
            //        return false;
            //    }
            //    return true;
            //}
            //catch (Exception ex)
            //{
            //    msg = ex.Message;
            //    Log4NetHelper.WriteErrorLog(Station_CacheEntity.curLogType, "急停出现异常:" + ex.Message, ex);
            //    return false;
            //}
        }
 
        /// <summary>
        /// 是否可以发送任务
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="taskType"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool CanSendTask(int deviceId, out string msg)
        {
            msg = "";
            //模拟
            //if (!this.View.IsConnected)
            //{
            //    msg = "已离线";
            //    return false;
            //}
            //if (this.View.R_Mode != (int)EStationMode.连机自动模式)
            //{
            //    msg = "当前非自动模式";
            //    return false;
            //}
            //if (this.View.R_State != (int)EStationState.空闲)
            //{
            //    msg = "设备状态非空闲,无法执行其他任务";
            //    return false;
            //}
            return true;
        }
 
        /// <summary>
        /// Station是否是自动待机模式
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool StationIsAutoAwaitMode(string stationCode, out string msg)
        {
            msg = "";
            var readData = this.View.R_StationForReadCommList.Where(x => x.StationCode == stationCode).FirstOrDefault();
            if (readData.R_Model != (int)EStationMode.自动待机模式)
            {
                EStationMode mode = (EStationMode)Enum.Parse(typeof(EStationMode), readData.R_Model.ToString());
                msg = "站点" + stationCode + " 当前模式是[" + mode.ToString() + "],非自动待机模式";
                return false;
            }
            return true;
        }
 
        /// <summary>
        /// Station是否是自动模式
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool StationIsAuto(string stationCode, out string msg)
        {
            msg = "";
            var readData = this.View.R_StationForReadCommList.Where(x => x.StationCode == stationCode).FirstOrDefault();
            if (readData.R_Model != (int)EStationMode.自动待机模式 && readData.R_Model != (int)EStationMode.自动运行模式)
            {
                EStationMode mode = (EStationMode)Enum.Parse(typeof(EStationMode), readData.R_Model.ToString());
                msg = "站点" + stationCode + " 当前模式是[" + mode.ToString() + "],非自动待机模式或自动运行模式";
                return false;
            }
            return true;
        }
 
        /// <summary>
        /// Station是否是空闲状态
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool StationIsStateFree(string stationCode, out string msg)
        {
            msg = "";
            var readData = this.View.R_StationForReadCommList.Where(x => x.StationCode == stationCode).FirstOrDefault();
            if (readData.R_State != (int)EStationState.空闲)
            {
                EStationMode mode = (EStationMode)Enum.Parse(typeof(EStationMode), readData.R_Model.ToString());
                msg = "站点" + stationCode + " 当前状态是[" + mode.ToString() + "],非空闲";
                return false;
            }
            return true;
        }
 
        /// <summary>
        ///  发动机出库,给西港PLC发送发动机数据 【Editby shaocx,2023-07-23】
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="value"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool WriteStation_XigangGetDataForOut(int deviceId, string value__Out_MaterialModel,
            string value_Out_OrderNo,
            string value_Out_SerialNumber,
            int plcTaskId, out string msg)
        {
            try
            {
                msg = "";
                return true;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                var logcontext = $"任务号{plcTaskId},机型{value__Out_MaterialModel},订货号{value_Out_OrderNo},编号{value_Out_SerialNumber},写入 发动机出库,给西港PLC发送发动机数据出现异常:" + JsonConvert.SerializeObject(ex);
                Log4NetHelper.WriteErrorLog(Station_CacheEntity.curLogType, logcontext, ex);
                return false;
            }
        }
 
        #endregion
 
        public void SetPropertyValueForRead()
        {
            var r_dbBlock = this.DBBlockForRead;
            SdaHelper.SetPropertyValueForDB<StationDBForRead, StationView>(r_dbBlock, this.View, this.plcService, "");
            //特殊处理List集合
            foreach (var item in r_dbBlock.R_StationForReadCommList)
            {
                StationDB2ObjForReadComm StationDB2ObjForReadComm = this.View.R_StationForReadCommList.Where(x => x.StationCode == item.StationCode).First();
                SdaHelper.SetPropertyValueForDB<StationDBForReadComm, StationDB2ObjForReadComm>(item, StationDB2ObjForReadComm, this.plcService, "");
            }
            //这里特殊处理 读取的发动机码 
            this.View.R_1019_CargoNoScan = GetNewCategoryNo(this.View.R_1019_CargoNoScan);
        }
 
 
        public void SetPropertyValueForWrite()
        {
            var w_dbBlock = this.DBBlockForWrite;
            SdaHelper.SetPropertyValueForDB<StationDBForWrite, StationView>(w_dbBlock, this.View, this.plcService, "");
            //特殊处理List集合
            foreach (var item in w_dbBlock.W_StationForWriteCommList)
            {
                StationDB2ObjForWriteComm StationDB2ObjForWriteComm = this.View.W_StationForWriteCommList.Where(x => x.StationCode == item.StationCode).First();
                SdaHelper.SetPropertyValueForDB<StationDBForWriteComm, StationDB2ObjForWriteComm>(item, StationDB2ObjForWriteComm, this.plcService, "");
            }
        }
 
        /// <summary>
        /// 是否有心跳
        /// </summary>
        /// <returns></returns>
        public bool IsHaveHeatBeat()
        {
            try
            {
                var r_dbBlock = this.DBBlockForRead;
                short value = Convert.ToInt16(this.plcService.ReadValuePoint(r_dbBlock.R_HandShake, typeof(short)));
                //约定 等待2秒
                Thread.Sleep(2000);
                short value_next = Convert.ToInt16(this.plcService.ReadValuePoint(r_dbBlock.R_HandShake, typeof(short)));
                if (value == value_next)
                {//没有心跳
                    return false;
                }
                else
                {//有心跳
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        #region 获取当前报警信息
 
 
        /// <summary>
        /// 获取Station当前报警信息
        /// </summary>
        /// <returns></returns>
        public List<DeviceWarningInfo> GetStationAlertDatas()
        {
            List<DeviceWarningInfo> db_warningList = new List<DeviceWarningInfo>();
            try
            {
                string _dbValue;
                var isHasAlert = false;
                IDictionary<string, string> StationAlertDict = new Dictionary<string, string>();
                StationAlertDict = WareSdaStruct.StationAlertDict;
                foreach (var item in StationAlertDict)
                {
                    _dbValue = item.Key;
                    isHasAlert = Convert.ToBoolean(this.plcService.ReadValuePoint(Station_CacheEntity.R_DBHeader_Station_Alert, item.Key, typeof(bool)));
                    if (isHasAlert)
                    {
                        db_warningList.Add(new DeviceWarningInfo()
                        {
                            Address = item.Key,
                            Code = item.Key,
                            Context = item.Value.Trim()
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return db_warningList;
        }
 
        /// <summary>
        /// 获取输送线当前报警信息
        /// </summary>
        /// <returns></returns>
        public List<DeviceWarningInfo> GetLineAlertDatas()
        {
            List<DeviceWarningInfo> db_warningList = new List<DeviceWarningInfo>();
            try
            {
                IDictionary<string, string> srmAlertDict = new Dictionary<string, string>();
                foreach (var item in this.View.R_StationForReadCommList)
                {
                    if (item.R_AlarmCode > 0)
                    {
                        item.R_AlarmCodeMsg = WareSdaStruct.StationAlertDict[item.R_AlarmCode.ToString()];
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return db_warningList;
        }
 
 
        #endregion
 
        /// <summary>
        /// 特殊处理下 读取的发动机码
        /// </summary>
        /// <param name="orgCategoryNo"></param>
        /// <returns></returns>
        private string GetNewCategoryNo(string orgCategoryNo)
        {
            Log4NetHelper.WriteInfoLog(Station_CacheEntity.curLogType, "读取的发动机码,原始读取的值:" + orgCategoryNo);
            //如果有 $R内容,就替换为空
            var repStr = "$R";
            if (!string.IsNullOrEmpty(orgCategoryNo) && orgCategoryNo.IndexOf(repStr) > -1)
            {
                orgCategoryNo = orgCategoryNo.Replace(repStr, "");
            }
            Log4NetHelper.WriteInfoLog(Station_CacheEntity.curLogType, "读取的发动机码,替换后的值:" + orgCategoryNo);
            return orgCategoryNo;
        }
    }
}