hwh
2025-02-10 b16c4927b99063f463f83f855c53fb0af8440072
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
 
using Admin.NET.Core.Service;
using Elastic.Clients.Elasticsearch;
using Microsoft.CodeAnalysis.Text;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
using StackExchange.Profiling.Internal;
using System.Drawing.Drawing2D;
using WCS.Application.Entity;
using WCS.Application.Service.WcsDevice.Dto;
 
namespace WCS.Application;
 
/// <summary>
/// 设备信息服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class WcsDeviceService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<WcsPlc> _wcsPlcRep;
    private readonly SqlSugarRepository<WcsDevice> _wcsDeviceRep;
    private readonly SysCacheService _sysCacheService;
    public WcsDeviceService(SqlSugarRepository<WcsPlc> wcsPlcRep, SqlSugarRepository<WcsDevice> wcsDeviceRep, SysCacheService sysCacheService)
    {
        _wcsPlcRep = wcsPlcRep;
        _wcsDeviceRep = wcsDeviceRep;
        _sysCacheService = sysCacheService;
    }
 
    #region 工作台操作
 
 
    /// <summary>
    /// 手动操作PLC锁定写入工位信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "WriteLocationInfo")]
    [DisplayName("手动操作PLC锁定写入工位信息")]
    public async Task WriteLocationInfo(WcsDeviceUpInfo input)
    {
        WcsPlc modPlc;
        string DbNum = "";// DB块
 
        if (string.IsNullOrWhiteSpace(input.LocatNo))//如果工位为空
        {
            throw Oops.Bah("工位不能为空");
        }
        var plcList = await _wcsPlcRep.Context.Queryable<WcsPlc>().Where(s => s.Type == PLCTypeEnum.ConveyorLine && s.IsDelete == false).ToListAsync();
        switch (input.Layer)
        {
            case "1":
                modPlc = plcList.FirstOrDefault(m => m.Text == "1层托盘输送线");
                if (modPlc == null)
                    throw Oops.Bah($"未查询到{input.Layer}楼层PLC信息");
                DbNum = "";
                break;
            case "2":
                modPlc = plcList.FirstOrDefault(m => m.Text == "2层托盘输送线");
                if (modPlc == null)
                    throw Oops.Bah($"未查询到{input.Layer}楼层PLC信息");
                DbNum = "DB91";
                break;
            case "3":
                modPlc = plcList.FirstOrDefault(m => m.Text == "3层托盘输送线");
                if (modPlc == null)
                    throw Oops.Bah($"未查询到{input.Layer}楼层PLC信息");
                DbNum = "";
                break;
            default:
                throw Oops.Bah("楼层信息错误");
        }
 
        var device = await _wcsDeviceRep.Context.Queryable<WcsDevice>().FirstAsync(s => s.PlcId == modPlc.Id && s.StationNum == input.LocatNo && s.IsDelete == false && s.DeviceType == DeviceTypeEnum.Show);
        if (device == null)
        {
            throw Oops.Bah("当前楼层未查询到该工位信息");
        }
        PLCUtil modUtil = new PLCUtil(modPlc);
 
        switch (input.TypeName)
        {
            case "suoding":     // 锁定
                modUtil.SetPlcDBValue(PLCDataTypeEnum.Short, DbNum, "48.0", input.LocatNo);
                break;
 
            default:
                break;
        }
 
        modUtil.Close();
    }
 
 
    /// <summary>
    /// 获取工位上信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "GetLocationInfo")]
    [DisplayName("获取工位上信息")]
    public async Task<WcsDeviceUpInfo> GetLocationInfo(WcsDeviceUpInfo input)
    {
        WcsPlc modPlc;
        string DbNum = "";// DB块
 
        if (string.IsNullOrWhiteSpace(input.LocatNo))//如果工位为空
        {
            throw Oops.Bah("工位不能为空");
        }
        var plcList = await _wcsPlcRep.Context.Queryable<WcsPlc>().Where(s => s.Type == PLCTypeEnum.ConveyorLine && s.IsDelete == false).ToListAsync();
        switch (input.Layer)
        {
            case "1":
                modPlc = plcList.FirstOrDefault(m => m.Text == "1层托盘输送线");
                if (modPlc == null)
                    throw Oops.Bah($"未查询到{input.Layer}楼层PLC信息");
                DbNum = "";
                break;
            case "2":
                modPlc = plcList.FirstOrDefault(m => m.Text == "2层托盘输送线");
                if (modPlc == null)
                    throw Oops.Bah($"未查询到{input.Layer}楼层PLC信息");
                DbNum = "DB1100";
                break;
            case "3":
                modPlc = plcList.FirstOrDefault(m => m.Text == "3层托盘输送线");
                if (modPlc == null)
                    throw Oops.Bah($"未查询到{input.Layer}楼层PLC信息");
                DbNum = "";
                break;
            default:
                throw Oops.Bah("楼层信息错误");
        }
 
        var device = await _wcsDeviceRep.Context.Queryable<WcsDevice>().FirstAsync(s => s.PlcId == modPlc.Id && s.StationNum == input.LocatNo && s.IsDelete == false && s.DeviceType == DeviceTypeEnum.Show);
        if (device == null)
        {
            throw Oops.Bah("当前楼层未查询到该工位信息");
        }
        PLCUtil modUtil = new PLCUtil(modPlc);
        WcsDeviceUpInfo data = new WcsDeviceUpInfo();
        data.FuncName = "";
        data.Layer = "";
        data.LocatNo = input.LocatNo;
        data.TypeName = "";
        var (result2, value2) = modUtil.GetPlcDBValue(PLCDataTypeEnum.String, DbNum, "4");
        data.PalletNo = value2;
        var (result3, value3) = modUtil.GetPlcDBValue(PLCDataTypeEnum.String, DbNum, "14");
        data.TaskNo = value3;
        var (result4, value4) = modUtil.GetPlcDBValue(PLCDataTypeEnum.Short, DbNum, "34");
        data.StartLoction = value4.ToString();
        var (result5, value5) = modUtil.GetPlcDBValue(PLCDataTypeEnum.Short, DbNum, "36");
        data.EndLoction = value5.ToString();
 
        var (result6, value6) = modUtil.GetPlcDBValue(PLCDataTypeEnum.Short, DbNum, "38");
        data.StartPai = value6.ToString();
        var (result7, value7) = modUtil.GetPlcDBValue(PLCDataTypeEnum.Short, DbNum, "40");
        data.StartLie = value7.ToString();
        var (result8, value8) = modUtil.GetPlcDBValue(PLCDataTypeEnum.Short, DbNum, "42");
        data.StartCeng = value8.ToString();
 
 
        var (result9, value9) = modUtil.GetPlcDBValue(PLCDataTypeEnum.Short, DbNum, "44");
        data.EndPai = value9.ToString();
        var (result10, value10) = modUtil.GetPlcDBValue(PLCDataTypeEnum.Short, DbNum, "46");
        data.EndLie = value10.ToString();
        var (result11, value11) = modUtil.GetPlcDBValue(PLCDataTypeEnum.Short, DbNum, "48");
        data.EndCeng = value11.ToString();
 
        modUtil.Close();
        return data;
    }
    /// <summary>
    /// 手动操作PLC信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "WriteInfo")]
    [DisplayName("手动操作Plc")]
    public async Task WriteInfo(WcsDeviceUpInfo input)
    {
        WcsPlc modPlc;
        string DbNum = "";// DB块
 
        if (string.IsNullOrWhiteSpace(input.LocatNo))//如果工位为空
        {
            throw Oops.Bah("工位不能为空");
        }
        var plcList = await _wcsPlcRep.Context.Queryable<WcsPlc>().Where(s => s.Type == PLCTypeEnum.ConveyorLine && s.IsDelete == false).ToListAsync();
        switch (input.Layer)
        {
            case "1":
                modPlc = plcList.FirstOrDefault(m => m.Text == "1层托盘输送线");
                if (modPlc == null)
                    throw Oops.Bah($"未查询到{input.Layer}楼层PLC信息");
                DbNum = "";
                break;
            case "2":
                modPlc = plcList.FirstOrDefault(m => m.Text == "2层托盘输送线");
                if (modPlc == null)
                    throw Oops.Bah($"未查询到{input.Layer}楼层PLC信息");
                DbNum = "DB91";
                break;
            case "3":
                modPlc = plcList.FirstOrDefault(m => m.Text == "3层托盘输送线");
                if (modPlc == null)
                    throw Oops.Bah($"未查询到{input.Layer}楼层PLC信息");
                DbNum = "";
                break;
            default:
                throw Oops.Bah("楼层信息错误");
        }
 
        var device = await _wcsDeviceRep.Context.Queryable<WcsDevice>().FirstAsync(s => s.PlcId == modPlc.Id && s.StationNum == input.LocatNo && s.IsDelete == false && s.DeviceType == DeviceTypeEnum.Show);
        if (device == null)
        {
            throw Oops.Bah("当前楼层未查询到该工位信息");
        }
        PLCUtil modUtil = new PLCUtil(modPlc);
 
 
        // true : 手动模式    false:自动模式
        var (result, value) = modUtil.GetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "50.4");
        if (result.IsSucceed)
        {
            if (input.TypeName != "shoudong" && value.ToString().ToLower() == "false" && input.TypeName != "huifu" && input.TypeName != "UpTask")
            {
                throw Oops.Bah("当前为自动模式,请先切换为手动模式");
            }
        }
        else
        {
            throw Oops.Bah("读取PLC手自动值失败");
        }
 
        switch (input.TypeName)
        {
            case "zidong":     // 自动
                //modUtil.SetPlcDBValue(PLCDataTypeEnum.Short, DbNum, "48.0", input.LocatNo);
                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "50.4", "false");
                break;
            case "shoudong":    // 手动
                //modUtil.SetPlcDBValue(PLCDataTypeEnum.Short, DbNum, "48.0", input.LocatNo);
                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "50.4", "true");
                break;
            case "huifu":       // 报警恢复
                if (input.FuncName == "true")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "47.0", "true");
                }
                else if (input.FuncName == "false")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "47.0", "false");
                }
                else
                {
                    throw Oops.Bah("事件结果错误");
                }
 
                break;
            case "1diandong":   // 1点动
                if (input.FuncName == "true")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "50.7", "true");
                }
                else if (input.FuncName == "false")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "50.7", "false");
                }
                else
                {
                    throw Oops.Bah("事件结果错误");
                }
                break;
            case "1liandong":       // 1联动
                if (input.FuncName == "true")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "51.3", "true");
                }
                else if (input.FuncName == "false")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "51.3", "false");
                }
                else
                {
                    throw Oops.Bah("事件结果错误");
                }
                break;
            case "2diandong":
                if (input.FuncName == "true")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "51.0", "true");
                }
                else if (input.FuncName == "false")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "51.0", "false");
                }
                else
                {
                    throw Oops.Bah("事件结果错误");
                }
                break;
            case "2liandong":
                if (input.FuncName == "true")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "51.4", "true");
                }
                else if (input.FuncName == "false")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "51.4", "false");
                }
                else
                {
                    throw Oops.Bah("事件结果错误");
                }
                break;
            case "3diandong":
                if (input.FuncName == "true")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "50.5", "true");
                }
                else if (input.FuncName == "false")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "50.5", "false");
                }
                else
                {
                    throw Oops.Bah("事件结果错误");
                }
                break;
            case "3liandong":
                if (input.FuncName == "true")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "51.1", "true");
                }
                else if (input.FuncName == "false")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "51.1", "false");
                }
                else
                {
                    throw Oops.Bah("事件结果错误");
                }
                break;
            case "4diandong":
                if (input.FuncName == "true")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "50.6", "true");
                }
                else if (input.FuncName == "false")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "50.6", "false");
                }
                else
                {
                    throw Oops.Bah("事件结果错误");
                }
                break;
            case "4liandong":
                if (input.FuncName == "true")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "51.2", "true");
                }
                else if (input.FuncName == "false")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "51.2", "false");
                }
                else
                {
                    throw Oops.Bah("事件结果错误");
                }
                break;
            case "yizaisheng":
                if (input.FuncName == "true")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "51.5", "true");
                }
                else if (input.FuncName == "false")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "51.5", "false");
                }
                else
                {
                    throw Oops.Bah("事件结果错误");
                }
                break;
            case "yizaijiang":
                if (input.FuncName == "true")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "51.6", "true");
                }
                else if (input.FuncName == "false")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "51.6", "false");
                }
                else
                {
                    throw Oops.Bah("事件结果错误");
                }
                break;
            case "fanzhuansheng":
                if (input.FuncName == "true")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "51.7", "true");
                }
                else if (input.FuncName == "false")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "51.7", "false");
                }
                else
                {
                    throw Oops.Bah("事件结果错误");
                }
                break;
            case "fanzhuanjiang":
                if (input.FuncName == "true")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "52.0", "true");
                }
                else if (input.FuncName == "false")
                {
                    modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "52.0", "false");
                }
                else
                {
                    throw Oops.Bah("事件结果错误");
                }
                break;
            case "UpTask":
                var (result1, value2) = modUtil.GetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "50.0");
                if (result.IsSucceed)
                {
                    if (value2 == true)
                    {
                        modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "50.0", "false");
                    }
                    else
                    {
                        modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "50.0", "true");
                    }
                }
                else
                {
                    throw Oops.Bah("读取PLC值失败");
                }
 
 
                break;
            default:
                break;
        }
 
        modUtil.Close();
    }
 
 
    /// <summary>
    /// 手动写入PLC任务信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "WriteTaskInfo")]
    [DisplayName("写入Plc任务信息")]
    public async Task WriteTaskInfo(WcsDeviceUpInfo input)
    {
        WcsPlc modPlc;
        string DbNum = "";// DB块
        string TaskDbNum = "";// DB块
 
        if (string.IsNullOrWhiteSpace(input.LocatNo))//如果工位为空
        {
            throw Oops.Bah("工位不能为空");
        }
        var plcList = await _wcsPlcRep.Context.Queryable<WcsPlc>().Where(s => s.Type == PLCTypeEnum.ConveyorLine && s.IsDelete == false).ToListAsync();
        switch (input.Layer)
        {
            case "1":
                modPlc = plcList.FirstOrDefault(m => m.Text == "1层托盘输送线");
                if (modPlc == null)
                    throw Oops.Bah($"未查询到{input.Layer}楼层PLC信息");
                DbNum = "";
                break;
            case "2":
                modPlc = plcList.FirstOrDefault(m => m.Text == "2层托盘输送线");
                if (modPlc == null)
                    throw Oops.Bah($"未查询到{input.Layer}楼层PLC信息");
                DbNum = "DB91";
                TaskDbNum = "DB1100";
                break;
            case "3":
                modPlc = plcList.FirstOrDefault(m => m.Text == "3层托盘输送线");
                if (modPlc == null)
                    throw Oops.Bah($"未查询到{input.Layer}楼层PLC信息");
                DbNum = "";
                break;
            default:
                throw Oops.Bah("楼层信息错误");
        }
 
        var device = await _wcsDeviceRep.Context.Queryable<WcsDevice>().FirstAsync(s => s.PlcId == modPlc.Id && s.StationNum == input.LocatNo && s.IsDelete == false && s.DeviceType == DeviceTypeEnum.Show);
        if (device == null)
        {
            throw Oops.Bah("当前楼层未查询到该工位信息");
        }
 
        PLCUtil modUtil = new PLCUtil(modPlc);
        //var (result, value) = modUtil.GetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "50.0");
        //if (result.IsSucceed)
        //{
        //    if (value.ToString().ToLower() != "false")
        //    {
        //        throw Oops.Bah("请先切换为更改任务模式");
        //    }
        //}
        //else
        //{
        //    throw Oops.Bah("读取PLC手自动值失败");
        //}
        //modUtil.SetPlcDBValue(PLCDataTypeEnum.Short, DbNum, "48.0", input.LocatNo);
        switch (input.TypeName)
        {
            case "writeTask":
                modUtil.SetPlcDBValue(PLCDataTypeEnum.String, TaskDbNum, "4", input.PalletNo);
                modUtil.SetPlcDBValue(PLCDataTypeEnum.String, TaskDbNum, "14", input.TaskNo);
                //modUtil.SetPlcDBValue(PLCDataTypeEnum.Int, TaskDbNum, "34", input.StartLoction);
                modUtil.SetPlcDBValue(PLCDataTypeEnum.Short, TaskDbNum, "36.0", input.EndLoction);
 
 
                modUtil.SetPlcDBValue(PLCDataTypeEnum.Short, TaskDbNum, "38", input.StartPai);
                modUtil.SetPlcDBValue(PLCDataTypeEnum.Short, TaskDbNum, "40", input.StartLie);
                modUtil.SetPlcDBValue(PLCDataTypeEnum.Short, TaskDbNum, "42", input.StartCeng);
 
 
                modUtil.SetPlcDBValue(PLCDataTypeEnum.Short, TaskDbNum, "44", input.EndPai);
                modUtil.SetPlcDBValue(PLCDataTypeEnum.Short, TaskDbNum, "46", input.EndLie);
                modUtil.SetPlcDBValue(PLCDataTypeEnum.Short, TaskDbNum, "48", input.EndCeng);
 
                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "50.3", "true");
                Thread.Sleep(1000);
                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "50.3", "false");
                break;
            case "writeTaskDelete":
                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "50.2", "true");
                Thread.Sleep(1000);
                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "50.2", "false");
                break;
 
            default:
                break;
        }
 
        modUtil.Close();
    }
 
 
 
    /// <summary>
    /// 手动写入PLC启停信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "WriteStartStop")]
    [DisplayName("写入PLC启停信息")]
    public async Task WriteStartStop(WcsDeviceStartStop input)
    {
        switch (input.Type)
        {
            case "Shutter":
                {
                    string Pos = "";
                    if (input.Layer == "2")
                    {
                        Pos = "1152";
                    }
                    else
                    {
                        Pos = "1154";
                    }
                    var modPlc = await _wcsPlcRep.Context.Queryable<WcsPlc>().Where(s => s.Type == PLCTypeEnum.ConveyorLine).FirstAsync();
                    if (modPlc == null)
                        throw Oops.Bah($"未查询到输送线PLC信息");
                    PLCUtil modUtil = new PLCUtil(modPlc);
                    var (result, value) = modUtil.GetPlcDBValue(PLCDataTypeEnum.Short, "DB1000." + Pos);
                    if (result.IsSucceed)
                    {
                        if (input.FuncName == "Down")
                        {
                            if (value == 20)
                                modUtil.SetPlcDBValue(PLCDataTypeEnum.Short, "DB1000", Pos, "30");
                        }
                        else if (input.FuncName == "Up")
                        {
                            if (value == 0)
                                modUtil.SetPlcDBValue(PLCDataTypeEnum.Short, "DB1000", Pos, "10");
                        }
                        modUtil.Close();
                    }
                    else
                        throw Oops.Bah(result.Err);
                }
                break;
            case "ShuttleCar":
                {
                    string text = "";
                    if (input.No == "1")
                        text = "穿梭车1";
                    if (input.No == "2")
                        text = "穿梭车2";
                    var modPlc = await _wcsPlcRep.Context.Queryable<WcsPlc>().Where(s => s.Type == PLCTypeEnum.ShuttleCar && s.Text == text).FirstAsync();
                    if (modPlc == null)
                        throw Oops.Bah($"未查询到穿梭车PLC信息");
                    ModbusUtil modUtil = new ModbusUtil(modPlc);
                    var result = modUtil.SetDBValue(PLCDataTypeEnum.UShort, input.Layer, input.FuncName);
                    if (result.IsSucceed)
                    {
                        modUtil.Close();
                    }
                    else
                        throw Oops.Bah(result.Err);
                }
                break;
            default:
                break;
        }
        //WcsPlc modPlc;
        //string DbNum = "";// DB块 
        //var plcList = await _wcsPlcRep.Context.Queryable<WcsPlc>().Where(s => s.Type == PLCTypeEnum.ConveyorLine).ToListAsync();
        //if (input.Layer == "1")
        //{
        //    modPlc = plcList.FirstOrDefault(m => m.Text == "1层托盘输送线");
        //    if (modPlc == null)
        //        throw Oops.Bah($"未查询到1楼层PLC信息");
        //    DbNum = "";
        //}
        //else if (input.Layer == "2")
        //{
        //    modPlc = plcList.FirstOrDefault(m => m.Text == "2层托盘输送线");
        //    if (modPlc == null)
        //        throw Oops.Bah($"未查询到2楼层PLC信息");
        //    DbNum = "DB91";
        //    PLCUtil modUtil = new PLCUtil(modPlc);
        //    switch (input.Type)
        //    {
        //        case "TwoPalletStart":
        //            if (input.FuncName == "true")
        //            {
        //                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "46.0", "true");
        //            }
        //            else if (input.FuncName == "false")
        //            {
        //                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "46.0", "false");
        //            }
        //            else
        //            {
        //                throw Oops.Bah("事件结果错误");
        //            }
        //            break;
        //        case "TwoPalletStop":
        //            if (input.FuncName == "true")
        //            {
        //                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "46.1", "true");
        //            }
        //            else if (input.FuncName == "false")
        //            {
        //                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "46.1", "false");
        //            }
        //            else
        //            {
        //                throw Oops.Bah("事件结果错误");
        //            }
        //            break;
        //        case "TwoMaStart":
        //            if (input.FuncName == "true")
        //            {
        //                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "58.0", "true");
        //            }
        //            else if (input.FuncName == "false")
        //            {
        //                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "58.0", "false");
        //            }
        //            else
        //            {
        //                throw Oops.Bah("事件结果错误");
        //            }
        //            break;
        //        case "TwoMaStop":
        //            if (input.FuncName == "true")
        //            {
        //                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "58.1", "true");
        //            }
        //            else if (input.FuncName == "false")
        //            {
        //                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "58.1", "false");
        //            }
        //            else
        //            {
        //                throw Oops.Bah("事件结果错误");
        //            }
        //            break;
        //        case "TwoMaTwoStart":
        //            if (input.FuncName == "true")
        //            {
        //                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "58.2", "true");
        //            }
        //            else if (input.FuncName == "false")
        //            {
        //                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "58.2", "false");
        //            }
        //            else
        //            {
        //                throw Oops.Bah("事件结果错误");
        //            }
        //            break;
        //        case "TwoMaTwoStop":
        //            if (input.FuncName == "true")
        //            {
        //                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "58.3", "true");
        //            }
        //            else if (input.FuncName == "false")
        //            {
        //                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "58.3", "false");
        //            }
        //            else
        //            {
        //                throw Oops.Bah("事件结果错误");
        //            }
        //            break;
        //        case "TwoBoxStart":
        //            if (input.FuncName == "true")
        //            {
        //                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "46.2", "true");
        //            }
        //            else if (input.FuncName == "false")
        //            {
        //                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "46.2", "false");
        //            }
        //            else
        //            {
        //                throw Oops.Bah("事件结果错误");
        //            }
        //            break; 
        //        case "TwoBoxStop":
 
        //            if (input.FuncName == "true")
        //            {
        //                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "46.3", "true");
        //            }
        //            else if (input.FuncName == "false")
        //            {
        //                modUtil.SetPlcDBValue(PLCDataTypeEnum.Bit, DbNum, "46.3", "false");
        //            }
        //            else
        //            {
        //                throw Oops.Bah("事件结果错误");
        //            }
        //            break;
        //        default:
        //            break;
        //    }
        //    modUtil.Close();
        //}
        //else if (input.Layer == "3")
        //{
        //    modPlc = plcList.FirstOrDefault(m => m.Text == "3层托盘输送线");
        //    if (modPlc == null)
        //        throw Oops.Bah($"未查询到3楼层PLC信息");
        //    DbNum = "";
        //}
        //else
        //{
        //    throw Oops.Bah("操作类型信息错误");
        //}
 
    }
 
    #endregion
 
    /// <summary>
    /// 分页查询设备信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    [DisplayName("分页查询设备信息")]
    public async Task<SqlSugarPagedList<WcsDeviceOutput>> Page(PageWcsDeviceInput input)
    {
        if (input.Field.IsNullOrEmpty())
        {
            input.Field = "u.Id";
            input.Order = "desc";
        }
        input.SearchKey = input.SearchKey?.Trim();
        var query = _wcsDeviceRep.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(input.SearchKey), u =>
                u.Text.Contains(input.SearchKey)
                || u.StationNum.Contains(input.SearchKey)
            )
            .WhereIF(input.PlcId > 0, u => u.PlcId == input.PlcId)
            .WhereIF(!string.IsNullOrWhiteSpace(input.StationNum), u => u.StationNum.Contains(input.StationNum.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Text), u => u.Text.Contains(input.Text.Trim()))
            //处理外键和TreeSelector相关字段的连接
            .LeftJoin<WcsPlc>((u, plcid) => u.PlcId == plcid.Id)
            .Select((u, plcid) => new WcsDeviceOutput
            {
                Id = u.Id,
                PlcId = u.PlcId,
                DeviceType = (DeviceTypeEnum)u.DeviceType,
                PlcIdIP = plcid.IP,
                Level = (DeviceLevelEnum)u.Level,
                DbNumber = u.DbNumber,
                StationNum = u.StationNum,
                PlcPos = u.PlcPos,
                WcsPos = u.WcsPos,
                PosType = u.PosType,
                LedIP = u.LedIP,
                Text = u.Text,
                CreateTime = u.CreateTime,
                UpdateTime = u.UpdateTime,
                CreateUserId = u.CreateUserId,
                CreateUserName = u.CreateUserName,
                UpdateUserId = u.UpdateUserId,
                UpdateUserName = u.UpdateUserName,
                CreateOrgId = u.CreateOrgId,
                CreateOrgName = u.CreateOrgName,
                IsDelete = u.IsDelete,
            });
        return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }
 
    /// <summary>
    /// 增加设备信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    [DisplayName("增加设备信息")]
    public async Task<long> Add(AddWcsDeviceInput input)
    {
        var entity = input.Adapt<WcsDevice>();
        await _wcsDeviceRep.InsertAsync(entity);
        return entity.Id;
    }
 
    /// <summary>
    /// 删除设备信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    [DisplayName("删除设备信息")]
    public async Task Delete(DeleteWcsDeviceInput input)
    {
        var entity = await _wcsDeviceRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _wcsDeviceRep.FakeDeleteAsync(entity);   //假删除
        //await _wcsDeviceRep.DeleteAsync(entity);   //真删除
    }
 
    /// <summary>
    /// 更新设备信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    [DisplayName("更新设备信息")]
    public async Task Update(UpdateWcsDeviceInput input)
    {
        var entity = input.Adapt<WcsDevice>();
        await _wcsDeviceRep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }
 
    /// <summary>
    /// 获取设备信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    [DisplayName("获取设备信息")]
    public async Task<WcsDevice> Detail([FromQuery] QueryByIdWcsDeviceInput input)
    {
        return await _wcsDeviceRep.GetFirstAsync(u => u.Id == input.Id);
    }
 
    /// <summary>
    /// 获取PlcId列表
    /// </summary>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "WcsPlcPlcIdDropdown"), HttpGet]
    [DisplayName("获取PlcId列表")]
    public async Task<dynamic> WcsPlcPlcIdDropdown()
    {
        return await _wcsDeviceRep.Context.Queryable<WcsPlc>()
                .Select(u => new
                {
                    Label = u.Text,
                    Value = u.Id
                }
                ).ToListAsync();
    }
 
    /// <summary>
    /// 生成点位
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "GeneratePos")]
    [DisplayName("生成点位")]
    public async Task GeneratePos(GeneratePosInput input)
    {
        var modDevice = await _wcsDeviceRep.GetByIdAsync(input.Id);
 
        var listPosition = new List<WcsPosition>();
        listPosition.Add(new WcsPosition()
        {
            DeviceId = modDevice.Id,
            StationNum = modDevice.StationNum,
            PlcPos = input.Pos.ToString(),
            PosType = PLCDataTypeEnum.String,
            Text = "TaskNo"
        });
        listPosition.Add(new WcsPosition()
        {
            DeviceId = modDevice.Id,
            StationNum = modDevice.StationNum,
            PlcPos = (input.Pos + 4).ToString(),
            PosType = PLCDataTypeEnum.UShort,
            Text = "TaskType"
        });
        listPosition.Add(new WcsPosition()
        {
            DeviceId = modDevice.Id,
            StationNum = modDevice.StationNum,
            PlcPos = (input.Pos + 6).ToString(),
            PosType = PLCDataTypeEnum.UShort,
            Text = "StartLocatNo"
        });
        listPosition.Add(new WcsPosition()
        {
            DeviceId = modDevice.Id,
            StationNum = modDevice.StationNum,
            PlcPos = (input.Pos + 8).ToString(),
            PosType = PLCDataTypeEnum.UShort,
            Text = "EndLocatNo"
        });
 
        await _wcsDeviceRep.Context.Insertable(listPosition).ExecuteCommandAsync();
    }
 
    /// <summary>
    /// 获取设备信息列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    [DisplayName("获取设备信息列表")]
    public async Task<List<WcsDeviceOutput>> List([FromQuery] PageWcsDeviceInput input)
    {
        var list = await _wcsDeviceRep.AsQueryable()
                                    .LeftJoin<WcsPlc>((a, b) => a.PlcId == b.Id)
                                    .Where((a, b) => a.DeviceType == DeviceTypeEnum.Business)
                                    .Select<WcsDeviceOutput>((a, b) => new WcsDeviceOutput() { Type = b.Type }, true)
                                    .ToListAsync();
        //获取跺机的状态
        foreach (var modDevice in list)
        {
            if (_sysCacheService.ExistKey("PLCCONN:" + modDevice.PlcId))
            {
                var cachePlc = _sysCacheService.Get<WcsPlc>("PLCCONN:" + modDevice.PlcId);
                modDevice.Status = cachePlc.IsConn;
                if (modDevice.Status)
                {
                    //读取plc的值
                    var modConn = PLCTaskAction.listPlcConn.FirstOrDefault(s => s != null && s.PlcId == modDevice.PlcId);
                    if (modConn == null)
                    {
                        continue;
                    }
                    try
                    {
                        var listPosition = await _wcsDeviceRep.Context.Queryable<WcsPosition>().Where(s => s.DeviceId == modDevice.Id).ToListAsync();
                        (var result, var plc) = modConn.GetPlcDBValue(modDevice.PosType, modDevice.DbNumber, modDevice.PlcPos);
                        modDevice.Plc = Convert.ToString(plc);
                        (result, var wcs) = modConn.GetPlcDBValue(modDevice.PosType, modDevice.DbNumber, modDevice.WcsPos);
                        modDevice.Wcs = Convert.ToString(wcs);
                        //任务号
                        var modPositionTask = listPosition.FirstOrDefault(s => s.Text == "任务号");
                        if (modPositionTask != null)
                        {
                            (result, var taskNo) = modConn.GetPlcDBValue(modPositionTask.PosType, modDevice.DbNumber, modPositionTask.PlcPos, modPositionTask.StringLength);
                            modDevice.TaskNo = Convert.ToString(taskNo);
                        }
                        //任务类型
                        var modPositionTaskType = listPosition.FirstOrDefault(s => s.Text == "任务类型");
                        if (modPositionTaskType != null)
                        {
                            (result, var taskType) = modConn.GetPlcDBValue(modPositionTaskType.PosType, modDevice.DbNumber, modPositionTaskType.PlcPos);
                            modDevice.TaskType = (TaskTypeEnum)Convert.ToInt32(taskType);
                        }
                        //起始工位
                        var modPositionStartLocatNo = listPosition.FirstOrDefault(s => s.Text == "起始工位");
                        if (modPositionStartLocatNo != null)
                        {
                            (result, var startLocatNo) = modConn.GetPlcDBValue(modPositionStartLocatNo.PosType, modDevice.DbNumber, modPositionStartLocatNo.PlcPos);
                            modDevice.StartLocatNo = Convert.ToString(startLocatNo);
                        }
                        //目的工位
                        var modPositionEndLocatNo = listPosition.FirstOrDefault(s => s.Text == "目的工位");
                        if (modPositionEndLocatNo != null)
                        {
                            (result, var endLocatNo) = modConn.GetPlcDBValue(modPositionEndLocatNo.PosType, modDevice.DbNumber, modPositionEndLocatNo.PlcPos);
                            modDevice.EndLocatNo = Convert.ToString(endLocatNo);
                        }
                        //托盘码
                        var modPositionPalletNo = listPosition.FirstOrDefault(s => s.Text == "托盘码");
                        if (modPositionPalletNo != null)
                        {
                            (result, var palletNo) = modConn.GetPlcDBValue(modPositionPalletNo.PosType, modDevice.DbNumber, modPositionPalletNo.PlcPos, modPositionPalletNo.StringLength);
                            modDevice.PalletNo = Convert.ToString(palletNo);
                            Console.WriteLine(modDevice.DbNumber + "." + modPositionPalletNo.PlcPos + "----------");
                        }
                        if (cachePlc.Type == PLCTypeEnum.ConveyorLine || cachePlc.Type == PLCTypeEnum.StackingMachine)
                        {
                            //放货排
                            var modPositionReleaseRow = listPosition.FirstOrDefault(s => s.Text == "放货排");
                            if (modPositionReleaseRow != null)
                            {
                                (result, var releaseRow) = modConn.GetPlcDBValue(modPositionReleaseRow.PosType, modDevice.DbNumber, modPositionReleaseRow.PlcPos);
                                modDevice.ReleaseRow = Convert.ToInt32(releaseRow);
                            }
                            //放货列
                            var modPositionReleaseCol = listPosition.FirstOrDefault(s => s.Text == "放货列");
                            if (modPositionReleaseCol != null)
                            {
                                (result, var releaseCol) = modConn.GetPlcDBValue(modPositionReleaseCol.PosType, modDevice.DbNumber, modPositionReleaseCol.PlcPos);
                                modDevice.ReleaseCol = Convert.ToInt32(releaseCol);
                            }
                            //放货层
                            var modPositionReleaseStorey = listPosition.FirstOrDefault(s => s.Text == "放货层");
                            if (modPositionReleaseStorey != null)
                            {
                                (result, var releaseStorey) = modConn.GetPlcDBValue(modPositionReleaseStorey.PosType, modDevice.DbNumber, modPositionReleaseStorey.PlcPos);
                                modDevice.ReleaseStorey = Convert.ToInt32(releaseStorey);
                            }
                            //取货排
                            var modPositionPickRow = listPosition.FirstOrDefault(s => s.Text == "取货排");
                            if (modPositionPickRow != null)
                            {
                                (result, var pickRow) = modConn.GetPlcDBValue(modPositionPickRow.PosType, modDevice.DbNumber, modPositionPickRow.PlcPos);
                                modDevice.PickRow = Convert.ToInt32(pickRow);
                            }
                            //取货列
                            var modPositionPickCol = listPosition.FirstOrDefault(s => s.Text == "取货列");
                            if (modPositionPickCol != null)
                            {
                                (result, var pickCol) = modConn.GetPlcDBValue(modPositionPickCol.PosType, modDevice.DbNumber, modPositionPickCol.PlcPos);
                                modDevice.PickCol = Convert.ToInt32(pickCol);
                            }
                            //取货层
                            var modPositionPickStorey = listPosition.FirstOrDefault(s => s.Text == "取货层");
                            if (modPositionPickStorey != null)
                            {
                                (result, var pickStorey) = modConn.GetPlcDBValue(modPositionPickStorey.PosType, modDevice.DbNumber, modPositionPickStorey.PlcPos);
                                modDevice.PickStorey = Convert.ToInt32(pickStorey);
                            }
 
                            if (cachePlc.Type == PLCTypeEnum.StackingMachine)
                            {
                                //跺机的起始工位用取货排列层
                                modDevice.StartLocatNo = $"{modDevice.PickRow.ToString().PadLeft(2, '0')}{modDevice.PickCol.ToString().PadLeft(2, '0')}{modDevice.PickStorey.ToString().PadLeft(2, '0')}";
                                //跺机的目的工位用放货排列层
                                modDevice.EndLocatNo = $"{modDevice.ReleaseRow.ToString().PadLeft(2, '0')}{modDevice.ReleaseCol.ToString().PadLeft(2, '0')}{modDevice.ReleaseStorey.ToString().PadLeft(2, '0')}";
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            else
            {
                modDevice.Status = false;
            }
 
            //modDevice.TaskNo = "TK00001";
            //modDevice.TaskType = TaskTypeEnum.In;
            //modDevice.PalletNo = "2024209032";
            //modDevice.StartLocatNo = "010101";
            //modDevice.EndLocatNo = "020202";
            //modDevice.Wcs = new Random().Next(100).ToString();
            //modDevice.Plc = new Random().Next(100).ToString();
            //modDevice.Status = true;
        }
 
        return list;
    }
 
    [HttpPost]
    [ApiDescriptionSettings(Name = "WriteValue")]
    [DisplayName("写入值")]
    public async Task WriteValue(WriteWcsDeviceInput modDevice)
    {
        var modPlc = await _wcsDeviceRep.Context.Queryable<WcsPlc>().FirstAsync(s => s.Id == modDevice.PlcId);
        if (modPlc == null)
            throw Oops.Bah("找不到PLC信息");
        var modDbDevice = await _wcsDeviceRep.AsQueryable().FirstAsync(s => s.Id == modDevice.Id);
        PLCUtil modUtil = new PLCUtil(modPlc);
        var listPosition = await _wcsDeviceRep.Context.Queryable<WcsPosition>().Where(s => s.DeviceId == modDevice.Id).ToListAsync();
        var result = new IoTClient.Result();
        //任务号
        if (modDevice.TaskNo != null)
        {
            var modPositionTask = listPosition.FirstOrDefault(s => s.Text == "任务号");
            if (modPositionTask != null)
                result = modUtil.SetPlcDBValue(modPositionTask.PosType, modDbDevice.DbNumber, modPositionTask.PlcPos, modDevice.TaskNo);
        }
        //任务类型
        if (modDevice.TaskType != null)
        {
            var modPositionTaskType = listPosition.FirstOrDefault(s => s.Text == "任务类型");
            if (modPositionTaskType != null)
                result = modUtil.SetPlcDBValue(modPositionTaskType.PosType, modDbDevice.DbNumber, modPositionTaskType.PlcPos, ((int)modDevice.TaskType).ToString());
        }
        //起始工位
        if (modDevice.StartLocatNo != null)
        {
            if (modPlc.Type == PLCTypeEnum.StackingMachine)
            {
                if (modDevice.StartLocatNo.Length != 6)
                {
                    throw Oops.Bah("起始工位格式输入错误,请输入排列层,例:010203!");
                }
                modDevice.PickRow = Convert.ToInt32(modDevice.StartLocatNo.Substring(0, 2));
                modDevice.PickCol = Convert.ToInt32(modDevice.StartLocatNo.Substring(2, 2));
                modDevice.PickStorey = Convert.ToInt32(modDevice.StartLocatNo.Substring(4, 2));
 
                //取货排
                if (modDevice.PickRow != null)
                {
                    var modPositionPickRow = listPosition.FirstOrDefault(s => s.Text == "取货排");
                    if (modPositionPickRow != null)
                        modUtil.SetPlcDBValue(modPositionPickRow.PosType, modDbDevice.DbNumber, modPositionPickRow.PlcPos, modDevice.PickRow.ToString());
                }
                //取货列
                if (modDevice.PickCol != null)
                {
                    var modPositionPickCol = listPosition.FirstOrDefault(s => s.Text == "取货列");
                    if (modPositionPickCol != null)
                        modUtil.SetPlcDBValue(modPositionPickCol.PosType, modDbDevice.DbNumber, modPositionPickCol.PlcPos, modDevice.PickCol.ToString());
                }
                //取货层
                if (modDevice.PickStorey != null)
                {
                    var modPositionPickStorey = listPosition.FirstOrDefault(s => s.Text == "取货层");
                    if (modPositionPickStorey != null)
                        modUtil.SetPlcDBValue(modPositionPickStorey.PosType, modDbDevice.DbNumber, modPositionPickStorey.PlcPos, modDevice.PickStorey.ToString());
                }
            }
            else
            {
                var modPositionStartLocatNo = listPosition.FirstOrDefault(s => s.Text == "起始工位");
                if (modPositionStartLocatNo != null)
                    result = modUtil.SetPlcDBValue(modPositionStartLocatNo.PosType, modDbDevice.DbNumber, modPositionStartLocatNo.PlcPos, modDevice.StartLocatNo.ToString());
            }
        }
        //目的工位
        if (modDevice.EndLocatNo != null)
        {
            if (modPlc.Type == PLCTypeEnum.StackingMachine)
            {
                if (modDevice.EndLocatNo.Length != 6)
                {
                    throw Oops.Bah("目的工位格式输入错误,请输入排列层,例:010203!");
                }
                modDevice.ReleaseRow = Convert.ToInt32(modDevice.EndLocatNo.Substring(0, 2));
                modDevice.ReleaseCol = Convert.ToInt32(modDevice.EndLocatNo.Substring(2, 2));
                modDevice.ReleaseStorey = Convert.ToInt32(modDevice.EndLocatNo.Substring(4, 2));
 
                //放货排
                if (modDevice.ReleaseRow != null)
                {
                    var modPositionReleaseRow = listPosition.FirstOrDefault(s => s.Text == "放货排");
                    if (modPositionReleaseRow != null)
                        modUtil.SetPlcDBValue(modPositionReleaseRow.PosType, modDbDevice.DbNumber, modPositionReleaseRow.PlcPos, modDevice.ReleaseRow.ToString());
                }
                //放货列
                if (modDevice.ReleaseCol != null)
                {
                    var modPositionReleaseCol = listPosition.FirstOrDefault(s => s.Text == "放货列");
                    if (modPositionReleaseCol != null)
                        modUtil.SetPlcDBValue(modPositionReleaseCol.PosType, modDbDevice.DbNumber, modPositionReleaseCol.PlcPos, modDevice.ReleaseCol.ToString());
                }
                //放货层
                if (modDevice.ReleaseStorey != null)
                {
                    var modPositionReleaseStorey = listPosition.FirstOrDefault(s => s.Text == "放货层");
                    if (modPositionReleaseStorey != null)
                        modUtil.SetPlcDBValue(modPositionReleaseStorey.PosType, modDbDevice.DbNumber, modPositionReleaseStorey.PlcPos, modDevice.ReleaseStorey.ToString());
                }
            }
            else
            {
                var modPositionEndLocatNo = listPosition.FirstOrDefault(s => s.Text == "目的工位");
                if (modPositionEndLocatNo != null)
                    result = modUtil.SetPlcDBValue(modPositionEndLocatNo.PosType, modDbDevice.DbNumber, modPositionEndLocatNo.PlcPos, modDevice.EndLocatNo.ToString());
            }
        }
        //托盘码
        if (modDevice.PalletNo != null)
        {
            var modPositionPalletNo = listPosition.FirstOrDefault(s => s.Text == "托盘码");
            if (modPositionPalletNo != null)
                result = modUtil.SetPlcDBValue(modPositionPalletNo.PosType, modDbDevice.DbNumber, modPositionPalletNo.PlcPos, modDevice.PalletNo.ToString());
        }
        if (modDevice.Plc != null)
        {
            var modPositionPLC = listPosition.FirstOrDefault(s => s.Text == "PLC流程字");
            if (modPositionPLC != null)
                result = modUtil.SetPlcDBValue(modPositionPLC.PosType, modDbDevice.DbNumber, modPositionPLC.PlcPos, modDevice.Plc);
        }
        if (modDevice.Wcs != null)
        {
            var modPositionWCS = listPosition.FirstOrDefault(s => s.Text == "WCS流程字");
            if (modPositionWCS != null)
                result = modUtil.SetPlcDBValue(modPositionWCS.PosType, modDbDevice.DbNumber, modPositionWCS.PlcPos, modDevice.Wcs);
        }
        if (modPlc.Type == PLCTypeEnum.ConveyorLine)
        {
            //放货排
            if (modDevice.ReleaseRow != null)
            {
                var modPositionReleaseRow = listPosition.FirstOrDefault(s => s.Text == "放货排");
                if (modPositionReleaseRow != null)
                    modUtil.SetPlcDBValue(modPositionReleaseRow.PosType, modDbDevice.DbNumber, modPositionReleaseRow.PlcPos, modDevice.ReleaseRow.ToString());
            }
            //放货列
            if (modDevice.ReleaseCol != null)
            {
                var modPositionReleaseCol = listPosition.FirstOrDefault(s => s.Text == "放货列");
                if (modPositionReleaseCol != null)
                    modUtil.SetPlcDBValue(modPositionReleaseCol.PosType, modDbDevice.DbNumber, modPositionReleaseCol.PlcPos, modDevice.ReleaseCol.ToString());
            }
            //放货层
            if (modDevice.ReleaseStorey != null)
            {
                var modPositionReleaseStorey = listPosition.FirstOrDefault(s => s.Text == "放货层");
                if (modPositionReleaseStorey != null)
                    modUtil.SetPlcDBValue(modPositionReleaseStorey.PosType, modDbDevice.DbNumber, modPositionReleaseStorey.PlcPos, modDevice.ReleaseStorey.ToString());
            }
            //取货排
            if (modDevice.PickRow != null)
            {
                var modPositionPickRow = listPosition.FirstOrDefault(s => s.Text == "取货排");
                if (modPositionPickRow != null)
                    modUtil.SetPlcDBValue(modPositionPickRow.PosType, modDbDevice.DbNumber, modPositionPickRow.PlcPos, modDevice.PickRow.ToString());
            }
            //取货列
            if (modDevice.PickCol != null)
            {
                var modPositionPickCol = listPosition.FirstOrDefault(s => s.Text == "取货列");
                if (modPositionPickCol != null)
                    modUtil.SetPlcDBValue(modPositionPickCol.PosType, modDbDevice.DbNumber, modPositionPickCol.PlcPos, modDevice.PickCol.ToString());
            }
            //取货层
            if (modDevice.PickStorey != null)
            {
                var modPositionPickStorey = listPosition.FirstOrDefault(s => s.Text == "取货层");
                if (modPositionPickStorey != null)
                    modUtil.SetPlcDBValue(modPositionPickStorey.PosType, modDbDevice.DbNumber, modPositionPickStorey.PlcPos, modDevice.PickStorey.ToString());
            }
        }
        modUtil.Close();
    }
 
    #region 分拣码垛
    /// <summary>
    /// 获取码垛机器人和拆垛机器人列表
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "WcsPackPlcList")]
    [DisplayName("获取码垛机器人和拆垛机器人列表")]
    public async Task<dynamic> WcsPackPlcList()
    {
        return await _wcsDeviceRep.Context.Queryable<WcsPlc>()
                .Where(w => w.Type == PLCTypeEnum.RobotPalletizer || w.Type == PLCTypeEnum.StackingRobot)
                .OrderByDescending(o => o.Type)
                .Select(u => new
                {
                    id = u.Id,
                    name = u.Text
                }
                ).ToListAsync();
    }
    /// <summary>
    /// 获取设备对应工位列表
    /// </summary>
    /// <param name="entry"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "WcsPackStationPlcList")]
    [DisplayName("获取设备对应工位列表")]
    public async Task<List<WcsDeviceTaskOrderDto>> WcsPackStationPlcList([FromQuery] WcsDeviceBaseInput entry)
    {
        //var list = await _wcsDeviceRep.AsQueryable()
        //                            .LeftJoin<WcsPlc>((a, b) => a.PlcId == b.Id)
        //                            .Where((a, b) => a.DeviceType == DeviceTypeEnum.Business)
        //                            .Select<WcsDeviceOutput>((a, b) => new WcsDeviceOutput() { Type = b.Type }, true)
        //                            .ToListAsync();
 
 
        var list = await _wcsDeviceRep.Context.Queryable<WcsDevice>()
                .InnerJoin<WcsPlc>((device, plc) => device.PlcId == plc.Id)
                .LeftJoin<WcsCheckTask>((device, plc, task) => device.StationNum == task.Port)
                .Where((device, plc, task) => device.PlcId == entry.PlcId)
                .OrderBy((device, plc, task) => device.CreateTime)
                .Select((device, plc, task) => new WcsDeviceTaskOrderDto()
                {
                    Id = device.Id,
                    Text = device.Text,
                    TaskNo = task.TaskNo,
                    OrderNo = task.OrderNo,
                    LotNo = task.LotNo,
                    SkuNo = task.SkuNo,
                    SkuName = task.SkuName,
                    LineNo = task.LineNo,
                    Status = task.Status,
                    PZNo = task.PZNo,
                    Qty = task.Qty,
 
                    PlcId = device.PlcId,
                    Type = plc.Type
                })
                .ToListAsync();
 
        bool connStatus = false;
        //获取设备的状态
        var modPlc = await _wcsDeviceRep.Context.Queryable<WcsPlc>().FirstAsync(s => s.Id == list[0].PlcId);
        PLCUtil modUtil = new PLCUtil(modPlc);
        if (modUtil.Connected)
        {
            connStatus = true;
        }
        foreach (var item in list)
        {
            item.PlcStatus = connStatus;
        }
        return list;
    }
    #endregion
}