123
cjs
2025-06-12 b7cf29b366aa2afb5fada4b340bac268c1bb1534
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
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
using Hanhe.iWCS.Business;
using Hanhe.iWCS.Common;
using Hanhe.iWCS.Interface;
using Hanhe.iWCS.TaizhouGEMTwoTCP;
using Hanhe.iWCS.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using MongoDB.Bson;
using Hanhe.iWCS.MData;
using Newtonsoft.Json;
using MongoDB.Driver.Builders;
using static Hanhe.iWCS.TaizhouGEMTwoProtocol.MESHelper;
using MongoDB.Driver;
using static Hanhe.iWCS.TaizhouGEMTwoProtocol.ProcessHelper;
using static Hanhe.iWCS.TaizhouGEMTwoProtocol.ApiHelper;
using System.Threading;
using Hanhe.iWCS.Model.AMS;
using static Hanhe.iWCS.TaizhouGEMTwoProtocol.ERPService;
using static Hanhe.iWCS.TaizhouGEMTwoProtocol.AMSHelper;
using static Hanhe.iWCS.TaizhouGEMTwoProtocol.ProtocolAnalysis;
using log4net.Appender;
using System.Net.Configuration;
 
namespace Hanhe.iWCS.TaizhouGEMTwoProtocol
{
    public class PLCControl : IPLCControl
    {
        private EquipmentCommandEQBLL commandBLL = new EquipmentCommandEQBLL();
 
        /// <summary>
        /// PLC 数据发送
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public void SendMessage(EquipmentCommandEQ command)
        {
            bool sended = SessionInstance.Instance.PLCSend(command.EquipmentIP, int.Parse(command.EquipmentPort), command.CommandText);
            if (sended)
            {
                //CMMLog.Info("设备指令发送成功;EquipmentCode=" + command.EquipmentCode + "-指令=" + command.CommandText);
                commandBLL.UpdateCommandEQStatus(command._id, Constants.COMMANDEQ_STATUS_SENDED, command.SendCount);
            }
            else
            {
                //CMMLog.Info("设备指令发送失败;EquipmentCode=" + command.EquipmentCode + "-指令=" + command.CommandText);
                commandBLL.UpdateCommandEQStatus(command._id, Constants.COMMANDEQ_STATUS_SEND_ERRORS, command.SendCount);
            }
        }
 
        /// <summary>
        /// 处理光电信息
        /// </summary>
        /// <param name="data"></param>
        public static void Analysis(string data) {
            //3f 00 01 00 00 00 24 24
        }
 
        #region   纯3楼设备任务
 
        #region   3楼包装机取料至复称平台流程——无需回报WMS任务状态——3楼包装取料——已完成(待测试,时间戳处理)
 
        #region   收到包装机下料信号1,开始判断复称平台是否满足条件,选择性下发任务
 
        /// <summary>
        /// 3楼包装机取料至复称平台流程——任务开始前,判断包装机有没有下线信号,并判断当前复称平台是否NG以及有没有任务在进行中
        /// </summary>
        /// <param name="a"></param>
        internal static void CheckPackingMachine(Settings.PlcInfo pmInfo)
        {
            //var count = MongoDBSingleton.Instance.FindOne<>//occupy
            var count = MongoDBSingleton.Instance.FindOne<MachineInfo>(Query.EQ("occupy", "1"), "MachineInfo");
 
            //检查包装机通道0是否有下料信号,如果有生成下料任务。线程循环读取,无需设置循环。——通道0参数为1
            if(count == null)
            {
                // var secondInfo = MongoDBSingleton.Instance.FindOne<secondResult>(Query.EQ("Bit", pmInfo.location), "secondResult");
                // if(secondInfo == null || secondInfo.Status == "OK")
                // {
                if (PickUpStartFree(pmInfo.location) && PickUpEndFree(pmInfo.location))
                {
                    try
                    {
                        var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                        {
                            dataNum = 1,
                            addr = pmInfo.readAddr,
                            host = pmInfo.ip,
                            port = pmInfo.port
                        });
                        if (result != null && result.errCode == 0)
                        {
                            CMMLog.Debug($"包装下线流程-{pmInfo.location}:读取包装机通道号为:{pmInfo.readAddr}里面的值为{result.result[0]}");
                            //Console.WriteLine($"包装下线流程:读取包装机通道号为:{pmInfo.readAddr}里面的值为{result.result[0]}");
                            if (result != null && result.errCode == 0 && result.result.Count() > 0)
                            {
                                //1:取料
                                if (result.result[0] == 1)
                                {
                                    string PlcBit02 = Settings.GetPlcInfo().Where(a => a.deviceType == "2").FirstOrDefault().location;
                                    //包装下线任务,终点为复称位置(这里判断包装机起点,复称起点终点均无任务,则推送任务)
                                    if (ProcessHelper.PickUpStartFree(pmInfo.location) && ProcessHelper.CheckEndFree(PlcBit02) && ProcessHelper.PickUpStartFree(PlcBit02))
                                    {
                                        //判断复称位置,没有货并且不为NG(ng参数0,1,2——0,不确定(包装机刚开始工作时)1,ok 2,ng)
                                        var state = GetSecondWeighState(pmInfo.location);
                                        CMMLog.Debug($"包装下线流程-{pmInfo.location}:判断复称位置空满状态为:{state.full},ng状态为:{state.ng}");
                                        if (state.full == 0)
                                        {
                                            if (Settings.mesOpen == "0")
                                            {
                                                //可以生成任务,调mes接口获取生产信息       2022.7.28   变更  MES可能需要1~2年才能上线,先把MES功能注销
                                                var info = MESHelper.GetPackingMachineInfo(pmInfo.location);
                                                if (info != null)
                                                {
                                                    CMMLog.Debug($"包装下线流程:获取MES之后进入info判断!");
                                                    Console.WriteLine($"包装下线流程:获取MES之后进入info判断!");
                                                    var trayCode = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                                                    {
                                                        addr = pmInfo.readAddr + 10,
                                                        dataNum = 90,//ERP变更:90  原先:80
                                                        host = pmInfo.ip,
                                                        port = pmInfo.port
                                                    });
                                                    if (trayCode.errCode == 0 && trayCode.result.Length == 90)
                                                    {
                                                        //获取托盘码等信息  读取通道 10、11、12的数据作为托盘码   读取其它通道 重量 叠包等信息 所有数据存入MachineInfo表
                                                        string location = "";
                                                        GetMachineData(trayCode.result,ref location);
                                                        var tray = MongoDBSingleton.Instance.FindOne<MachineInfo>(Query.EQ("machineNo", pmInfo.location), "MachineInfo");
                                                        if (tray != null)
                                                        {
                                                            if (tray.addState == 0)
                                                            {
                                                                MongoDBSingleton.Instance.Remove<MachineInfo>(Query.EQ("machineNo", pmInfo.location), "MachineInfo", RemoveFlags.None);
                                                                if (ERPService.ERPSwitch01 == "0") MongoDBSingleton.Instance.Remove<MachineInfoTwo>(Query.EQ("machineNo", pmInfo.location), "MachineInfoTwo", RemoveFlags.Single);
                                                                CMMLog.Debug($"包装下线流程:叠托层数为0,不允许生成任务");
                                                            }
                                                            else if (tray.trayCode != "0" && tray.trayCode != "" && tray.trayCode != null)
                                                            {
                                                                //AMSHelper.CreateTask(DateTime.Now.Ticks.ToString(), pmInfo.location, GetSecondWeighBit(), "3楼包装取料", 1, JsonConvert.SerializeObject(info));
                                                                int i = 1;
                                                                while (i == 1)
                                                                {
                                                                    var amsResult = AMSHelper.CreateTask(DateTime.Now.Ticks.ToString(), pmInfo.location, PlcBit02, "3楼包装取料", 0, tray.trayCode);
                                                                    if (amsResult.success)
                                                                    {
                                                                        i = 2;
                                                                        string weight = (int.Parse(tray.oneTrayWeight) / 100).ToString();
                                                                        ERPService.packageInfo(tray.machineNo, tray.trayCode, "", weight);
                                                                        CMMLog.Debug($"包装下线流程:AMS调用API成功!");
                                                                    }
                                                                    else CMMLog.Debug($"包装下线流程:AMS调用API失败,开始重新调用!");
                                                                }
                                                                CMMLog.Debug($"包装下线流程:接受并读取包装机通道里面的物料信息,并将其写入MachineInfo中间表中!接收到的信息:{JsonConvert.SerializeObject(trayCode.result)}");
                                                            }
                                                        }
                                                        else
                                                        {
                                                            CMMLog.Debug($"包装下线流程:tray==null!");
                                                            Console.WriteLine($"包装下线流程:tray==null!");
                                                        }
                                                    }
                                                    else
                                                    {
                                                        CMMLog.Debug($"包装下线流程:3楼包装至复称获取MODBUS值错误!trayCode.errCode:{trayCode.errCode},trayCode.result.Length:{trayCode.result.Length}");
                                                        Console.WriteLine($"包装下线流程:3楼包装至复称获取MODBUS值错误!trayCode.errCode:{trayCode.errCode},trayCode.result.Length:{trayCode.result.Length}");
                                                    }
                                                }
                                                else CMMLog.Debug($"包装下线流程:MES接口数据返回空值!MES接口数据:{info}");
                                            }
                                            else if (Settings.mesOpen == "1")
                                            {
                                                //可以生成任务,调mes接口获取生产信息       2022.7.28   变更  MES可能需要1~2年才能上线,先把MES功能注销
                                                CMMLog.Debug($"包装下线流程-{pmInfo.location}:获取MES之后进入info判断!");
                                                var trayCode = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                                                {
                                                    addr = pmInfo.readAddr + 10,
                                                    dataNum = 90,////ERP变更:90  原先:80
                                                    host = pmInfo.ip,
                                                    port = pmInfo.port
                                                });
                                                if (trayCode.errCode == 0 && trayCode.result.Length == 90)
                                                {
                                                    //获取托盘码等信息  读取通道 11、12、13的数据作为托盘码   读取其它通道 重量 叠包等信息 所有数据存入MachineInfo表
                                                    string location = "";
                                                    string flLoc = "";
                                                    GetMachineData(trayCode.result, ref location, false);
                                                    if(pmInfo.location.Contains("FLZT"))
                                                    {
                                                        flLoc = pmInfo.location;
                                                        pmInfo.location = location;
                                                    }
                                                    var tray = MongoDBSingleton.Instance.FindOne<MachineInfo>(Query.EQ("machineNo", pmInfo.location), "MachineInfo");
                                                    if (tray != null)
                                                    {
                                                        bool IsContLaterCode = true;
                                                        if (ERPService.ERPSwitch01 == "1")
                                                        {
                                                            string employeeId = "G" + tray.trayCodeWeight.PadLeft(7, '0');
                                                            // 判断当前【员工编号】通道信息读出来的员工编号是否已经存在于我们的员工信息表-ERPEmployeeTable(查询字段-employee_id)
                                                            var erpEmployeeInfo = MongoDBSingleton.Instance.FindOne<ERPEmployeeTable>(Query.EQ("employee_id", employeeId), "ERPEmployeeTable");
                                                            if (erpEmployeeInfo == null) IsContLaterCode = false;
                                                        }
 
                                                        if (IsContLaterCode)
                                                        {
                                                            if (tray.addState == 0)
                                                            {
                                                                MongoDBSingleton.Instance.Remove<MachineInfo>(Query.EQ("machineNo", pmInfo.location), "MachineInfo", RemoveFlags.None);
                                                                if (ERPService.ERPSwitch01 == "0") MongoDBSingleton.Instance.Remove<MachineInfoTwo>(Query.EQ("machineNo", pmInfo.location), "MachineInfoTwo", RemoveFlags.None);
                                                                CMMLog.Debug($"包装下线流程-{pmInfo.location}:叠托层数为0,不允许生成任务");
                                                            }
                                                            else if (tray.trayCode != "0" && !string.IsNullOrEmpty(tray.trayCode))
                                                            {
                                                                string timeStamp = ProcessHelper.GetTimeStamp(31, 1, 1);
                                                                pmInfo.location = !string.IsNullOrEmpty(flLoc) ? flLoc : pmInfo.location;
                                                                HHAmsExecuteResult req = AMSHelper.CreateTask(DateTime.Now.Ticks.ToString(), pmInfo.location, PlcBit02, "3楼包装取料", 0, tray.trayCode, timeStamp);
                                                                if (req.success)
                                                                {
                                                                    string weight = (double.Parse(tray.oneTrayWeight) / 100).ToString();
                                                                    //int weight = int.Parse(tray.oneTrayWeight) / 100;
                                                                    ERPService.packageInfo(tray.machineNo, tray.trayCode, tray.lotNo, weight);
                                                                }
                                                            }
                                                        }
                                                        else
                                                        {
                                                            CMMLog.Info($"包装下线流程-{pmInfo.location}:员工编码不存在,员工编码:G{tray.trayCodeWeight.PadLeft(7, '0')}");
                                                            //检索员工信息失败写入对方通道值1 读取地址+3 
                                                            var wirteall01 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                                                            {
                                                                addr = pmInfo.readAddr + 3,
                                                                host = pmInfo.ip,
                                                                port = pmInfo.port,
                                                                data = 1
                                                            });
                                                        }
                                                    }
                                                    else CMMLog.Debug($"包装下线流程-{pmInfo.location}:tray==null!");
                                                }
                                                else CMMLog.Debug($"包装下线流程-{pmInfo.location}:3楼包装至复称获取MODBUS值错误!trayCode.errCode:{trayCode.errCode},trayCode.result.Length:{trayCode.result.Length}");
                                            }
                                        }
                                        else
                                        {
                                            var task = MongoDBSingleton.Instance.FindOne<TN_I_TASK_MST>(Query.Or(Query.EQ("CN_S_START_BIT", PlcBit02), Query.EQ("CN_S_END_BIT", PlcBit02)), "TN_I_TASK_MST");
                                            if (task != null) CMMLog.Debug($"包装下线流程-{pmInfo.location}:检查复称中间表是否为空,不为空则检查其数据full以及ng是否为0和1!任务号为:{task.CN_S_TASK_NO}");
                                            else CMMLog.Debug("包装下线流程-{pmInfo.location}:检查复称中间表是否为空,不为空则检查其数据full以及ng是否为0和1!");
                                        }
                                    }
                                    else CMMLog.Debug($"包装下线流程-{pmInfo.location}:包装机起点或者复称起点终点有任务在执行中!包装机点位:{pmInfo.location},复称点位:{PlcBit02}");
                                }
                                else CMMLog.Debug($"包装下线流程-{pmInfo.location}:包装机通道0里面的数据不为1!result为:{JsonConvert.SerializeObject(result)}");
                            }
                            else CMMLog.Debug($"包装下线流程-{pmInfo.location}:未读取到包装机通道0里面的数据!!!result:{JsonConvert.SerializeObject(result)}");
                        }
                        else CMMLog.Debug($"包装下线流程-{pmInfo.location}:未读取到包装机通道0里面的数据!!!result:{JsonConvert.SerializeObject(result)}");
                    }
                    catch (Exception ex)
                    {
                        CMMLog.Debug($"包装下线流程-{pmInfo.location}:" +ex.Message);
                    }
                }
                else
                    {
                        //var taskNo = MongoDBSingleton.Instance.FindOne<TN_I_TASK_MST>(Query.Or(Query.EQ("CN_S_START_BIT", pmInfo.location), Query.EQ("CN_S_END_BIT", pmInfo.location)), "TN_I_TASK_MST");
                        //string task = "", startbit = "", endbit = "";
                        //if (taskNo != null)
                        //{
                        //    task = taskNo.CN_S_TASK_NO;
                        //    startbit = taskNo.CN_S_START_BIT;
                        //    endbit = taskNo.CN_S_END_BIT;
                        //}
                        CMMLog.Debug($"当前包装机起点终点有任务正在执行,请确认当前包装机的任务是否取货卸货完成!包装机货位编码:{pmInfo.location}");
                    }
                // }
                // else CMMLog.Info($"当前包装机出现故障,无法生成任务。包装机号:{pmInfo.location}");
            }
            else CMMLog.Debug($"当前machineInfo中间表已存在数据:{JsonConvert.SerializeObject(count)}");
        }
 
        #endregion
 
        #region   包装机MODBUS交互(开始取料,取料完成)
 
        /// 小车请求进料    1    AMS写,PLC读    1-请求进入;2-到位后,请求接驳滚动;3-取货完成;4-离开安全门外,清零通道
        /// 设备允许出料    1    PLC写,AMS读    1-XXXXXXXX;2-允许进入;3-接驳滚动,送出物料  ;4-结束;5-安全门关闭
 
        /// <summary>
        /// 小车到达包装机安全门口请求进入
        /// </summary>
        /// <param name="ips"></param>
        internal static void PickUpFullDoorUnload(string ip, string taskNo)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.ip == ip).FirstOrDefault();
            if(plc.device != "24")
            {
                //ASM写入通道0小车动作,1-取料
                int[] num = new int[2] { 1, 1 };
                var writeRes0 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti
                {
                    host = ip,
                    addr = plc.writeAddr,
                    data = num,//原先是1,单个写入
                    port = plc.port
                });
                DateTime dateTime = DateTime.Now;
                if (writeRes0.errCode == 0)
                {
                    //小车请求进入安全门交互
                    //小车请求进料,并且查询设备是否允许AGV进入
                    //读地址1设备是否  2-可以进入
                    var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                    {
                        dataNum = 1,
                        host = ip,
                        addr = plc.readAddr + 1,
                        port = plc.port
                    });
                    CMMLog.Debug($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}.");
                    Console.WriteLine($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}.");
                    if (readRes != null && readRes.errCode == 0)
                    {
                        if (readRes.result[0] == 2)
                        {
                            //设备允许进入,改参数通知AGV
                            WorkFlowAction.TrackLog(taskNo, 10, 1012, "success");
                            TSHelper.GoToAGV(taskNo, 10, 1);
                        }
                    }
                    else CMMLog.Debug($"包装下线:1012,readRes:{readRes},readRes.errCode:{readRes.errCode}");
                }
            }
            else
            {
                var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                {
                    host = plc.ip,
                    port = plc.port,
                    addr = plc.writeAddr + 1,
                    data = 1
                });
 
                if (writeRes.errCode == 0)
                {
                    //小车请求进入安全门交互
                    //小车请求进料,并且查询设备是否允许AGV进入
                    //读地址1设备是否  2-可以进入
                    var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                    {
                        dataNum = 1,
                        host = ip,
                        addr = plc.readAddr + 1,
                        port = plc.port
                    });
                    CMMLog.Debug($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}.");
                    Console.WriteLine($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}.");
                    if (readRes != null && readRes.errCode == 0)
                    {
                        if (readRes.result[0] == 2)
                        {
                            //设备允许进入,改参数通知AGV
                            WorkFlowAction.TrackLog(taskNo, 10, 1012, "success");
                            TSHelper.GoToAGV(taskNo, 10, 1);
                        }
                    }
                    else CMMLog.Debug($"包装下线:1012,readRes:{readRes},readRes.errCode:{readRes.errCode}");
                }
            }
        }
        /// <summary>
        /// 小车请求取货
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="taskNo"></param>
        internal static void PickUpFullUnload(string ip , string taskNo)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.ip == ip).FirstOrDefault();
            if (ip != "")
            {
                //小车请求出料,并且查询设备是否允许AGV进入
                //读地址2设备是否  2-允许进料  保险起见,可以不读
                var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                {
                    dataNum = 1,
                    host = ip,
                    addr = plc.readAddr + 1,
                    port = plc.port
                });
                CMMLog.Debug($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}.");
                Console.WriteLine($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}.");
                if (readRes != null && readRes.errCode == 0)
                {
                    if (readRes.result[0] == 2)
                    {
                        //TSHelper.GoToAGV(taskNo, 10, 2);//原先先改AGV参数
                        var writeRes1 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                        {
                            host = ip,
                            addr = plc.writeAddr + 1,
                            data = 2,
                            port = plc.port
                        });
                        if(writeRes1.errCode == 0) TSHelper.GoToAGV(taskNo, 10, 2);
                        CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为2.");
                        Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为2.");
                    }
                }
                else CMMLog.Debug($"包装下线,1112,readRes={readRes}");
            }
            else CMMLog.Debug($"包装下线,1112,ip=null!");
        }
        /// <summary>
        /// 小车取货完成--二期协议无此指令动作
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="taskNo"></param>
        internal static void PickUpFullComplete(string ip, string taskNo)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.ip == ip).FirstOrDefault();
            if (ip != "")
            {
                //小车请求出料,并且查询设备是否允许AGV进入
                //读地址2设备是否  2-允许进料
                var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                {
                    dataNum = 1,
                    host = ip,
                    addr = plc.readAddr+1,
                    port = plc.port
                });
                CMMLog.Debug($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}.");
                Console.WriteLine($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}.");
                DateTime dateTime = DateTime.Now;
                if (readRes != null && readRes.errCode == 0)
                {
                    if (readRes.result[0] == 3)
                    {
                        var writeRes1 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                        {
                            host = ip,
                            addr = plc.writeAddr+1,
                            data = 3,
                            port = plc.port
                        });
                        CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为3.");
                        Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为3.");
                    }
                    else if(readRes.result[0] == 4)
                    {
                        WorkFlowAction.TrackLog(taskNo, 10, 1212, "success");
                        TSHelper.GoToAGV(taskNo, 10, 5);
                    }
                }
                else CMMLog.Debug($"包装下线,1212,readRes={readRes}!");
            }
            else CMMLog.Debug($"包装下线,1212,ip=null!");
        }
        /// <summary>
        /// 小车卸货完成离开安全门后通知包装机
        /// </summary>
        /// <param name="ips"></param>
        /// <param name="taskNo"></param>
        internal static void PickUpFullDoorComplete(string ip, string taskNo)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.ip == ip).FirstOrDefault();
            if (plc != null && !plc.location.Contains("FLZT"))
            {
                //写入包装机--安全门关门指令
                var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                {
                    host = ip,
                    addr = plc.writeAddr + 1,
                    data = 3,
                    port = plc.port
                });
                CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为4.");
                Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为4.");
            }
            else CMMLog.Debug($"包装下线,4,ip=null!");
        }
        #endregion
 
        #region   复称平台交互
 
        ///小车请求进料       1       AMS写,PLC读       1-请求进入;2-小车进料,输送托盘;3-XXXXXXX
        ///设备允许出料       1       PLC写,AMS读       1-XXXXXXXX;2-允许进料,开始滚动;3-上料完成
        ///复称平台小车动作:    1:上料复称;2:上砝码教称;3:取复称NG托盘,4:取砝码到包装机
 
        /// <summary>
        ///复称平台安全请求进入——因为现场需求,所以额外增加一个类似安全门的交互,直接改参数就行
        /// </summary>
        internal static void SecondWeightSafe(string ip,string taskNo)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.ip == ip).FirstOrDefault();
            //ASM写入小车动作,1:上料复称;2:上砝码教称;3:取复称NG托盘,4:取砝码到包装机
            int[] num = new int[2] { 1, 1 };
            var writeRes0 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti
            {
                host = ip,
                addr = plc.writeAddr,
                data = num,//原先是1,单个写入
                port = plc.port
            });
            CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr}里面数据为{JsonConvert.SerializeObject(num)}.");
            Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr}里面数据为{JsonConvert.SerializeObject(num)}.");
            ///小车和复称位对接
            //小车请求进料,并且查询设备是否允许AGV进入
            var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
            {
                dataNum = 1,
                host = plc.ip,
                addr = plc.readAddr + 1,
                port = plc.port
            });
            CMMLog.Debug($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}.");
            Console.WriteLine($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}.");
            if (readRes != null && readRes.errCode == 0)
            {
                if(readRes.result[0] == 2)
                {
                    WorkFlowAction.TrackLog(taskNo, 10, 1013, "success");
                    TSHelper.GoToAGV(taskNo, 10, 3);
                }
            }
        }
 
        /// <summary>
        /// 复称卸货
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="cN_S_TASK_NO"></param> 
        internal static void CheckUpReqUnload(string ip, string taskNo)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.location == ip).FirstOrDefault();
            ///小车和复称位对接
            if (plc != null)
            {
                //设备允许进入,改参数通知AGV
                WorkFlowAction.TrackLog(taskNo, 10, 1113, "success");
                TSHelper.GoToAGV(taskNo, 10, 4);
                CMMLog.Debug($"改AGV参数:10,4");
                //判断任务起点,找到起点包装机设备号,在中间表MachineInfo中查找上一台设备(包装机)的数据,通过modbus通道传入(数据需要转换,ASCII转16short,可能还需要数据拆分)
                var taskInfo = MongoDBSingleton.Instance.FindOne<TN_I_TASK_MST>(Query.EQ("CN_S_TASK_NO", taskNo), "TN_I_TASK_MST");
                if (plc.deviceType == "22")
                {
                    //四钴车间
                    var machine = MongoDBSingleton.Instance.FindOne<MachineInfoTetracobalt>(Query.EQ("trayCode", taskInfo.CN_S_BATCH_NO), "MachineInfoTetracobalt");
                    if (machine != null)
                    {
                        CMMLog.Debug("进入machine判断");
                        //向复称卸货复称,并将从包装机获取到的数据写入通道中
                        #region   多个数据写入MODBUS
 
                        var arr = JsonConvert.DeserializeObject<List<int>>(machine.jsonData);
                        int[] num = new int[22];
                        for (int i = 0; i <= 21; i++) num[i] = arr[i];
 
                        // 14,15 替换为时间戳,后续处理
                        //num[14] = int.Parse(machine.overlappingLayers);
                        //CMMLog.Debug($"machine.machinedown[14]:{num[14]}");
                        //num[15] = int.Parse(machine.bagNo);
                        //CMMLog.Debug($"machine.machinedown[15]:{num[15]}");
                        //时间戳处理
                        //for (int b = 16; b <= 17; b++)
                        //{
                        //    int k = b % 2 == 0 ? 0 : 16;
                        //    num[b] = Convert.ToInt32(Convert.ToString(int.Parse(taskInfo.Ext2), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
                        //}
 
                        CMMLog.Debug($"{JsonConvert.SerializeObject(num)}");
                        var wirteall = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti
                        {
                            addr = plc.writeAddr + 5,
                            host = plc.ip,
                            data = num,
                            port = plc.port
                        });
                        var writeRes1 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                        {
                            host = plc.ip,
                            addr = plc.writeAddr + 1,
                            data = 2,
                            port = plc.port
                        });
                        CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为2.");
                        Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为2.");
                        #endregion
                    }
                    else CMMLog.Debug($"machine判断错误,为null!");
                }
                else
                {
                    var machine = MongoDBSingleton.Instance.FindOne<MachineInfo>(Query.EQ("trayCode", taskInfo.CN_S_BATCH_NO), "MachineInfo");
                    if (machine != null)
                    {
                        CMMLog.Debug("进入machine判断");
                        //向复称卸货复称,并将从包装机获取到的数据写入通道中
                        #region   多个数据写入MODBUS
 
                        var arr = JsonConvert.DeserializeObject<List<int>>(machine.jsonData);
                        int[] num = new int[18];
                        for (int i = 0; i <= 15; i++) num[i] = arr[i];
 
                        // 14,15 替换为时间戳,后续处理
                        num[14] = int.Parse(machine.overlappingLayers);
                        CMMLog.Debug($"machine.machinedown[14]:{num[14]}");
                        num[15] = int.Parse(machine.bagNo);
                        CMMLog.Debug($"machine.machinedown[15]:{num[15]}");
                        //时间戳处理
                        for (int b = 16; b <= 17; b++)
                        {
                            int k = b % 2 == 0 ? 0 : 16;
                            num[b] = Convert.ToInt32(Convert.ToString(int.Parse(taskInfo.Ext2), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
                        }
 
                        CMMLog.Debug($"{num}");
                        Console.WriteLine(num);
                        var wirteall = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti
                        {
                            addr = plc.writeAddr + 10,
                            host = plc.ip,
                            data = num,
                            port = plc.port
                        });
                        var writeRes1 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                        {
                            host = plc.ip,
                            addr = plc.writeAddr + 1,
                            data = 2,
                            port = plc.port
                        });
                        CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为2.");
                        Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为2.");
                        #endregion
                    }
                    else CMMLog.Debug($"machine判断错误,为null!");
                }
                
                
            }
            else CMMLog.Debug($"包装下线,1113,ip=null!");
        }
        /// <summary>
        /// 复称卸货完成
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="cN_S_TASK_NO"></param>
        /// <returns></returns>
        internal static void CheckUpUnloadComplete(string ip, string cN_S_TASK_NO, int port)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.ip == ip && a.port == port).FirstOrDefault();
            int[] num = new int[1] { 2 };
            var writeRes0 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti
            {
                host = ip,
                addr = plc.writeAddr + 1,
                data = num,//原先是1,单个写入
                port = plc.port
            });
 
            var tray = MongoDBSingleton.Instance.FindOne<TN_I_TASK_MST>(Query.EQ("CN_S_TASK_NO", cN_S_TASK_NO), "TN_I_TASK_MST");
 
            var query = Query.And(Query.EQ("actionNo", "1"), Query.EQ("ext1", ip), Query.EQ("ext2", tray.CN_S_BATCH_NO));
            var ActionControlInfo = MongoDBSingleton.Instance.FindOne<ActionControlModel>(query, "ActionControlModel");
            if (ActionControlInfo == null)
            {
                MongoDBSingleton.Instance.Insert<ActionControlModel>(new ActionControlModel
                {
                    actionNo = "1",
                    ext1 = ip,
                    ext2 = tray.CN_S_BATCH_NO,
                    ext3 = tray.CN_S_START_BIT,
                    ext4 = port,
                    machince = tray.CN_S_START_BIT
                }, "ActionControlModel");
                string PlcBit02 = Settings.GetPlcInfo().Where(a => a.deviceType == "2").FirstOrDefault().location;
                MongoDBSingleton.Instance.Update<SecondWeighState>(Query.EQ("location", PlcBit02), Update.Set("full",1), "SecondWeighState", UpdateFlags.None);
            }
        }
        /// <summary>
        /// 复称平台安全退出(已退出)--二期无需执行此指令动作
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="cN_S_TASK_NO"></param>
        /// <returns></returns>
        internal static bool SecondWeightSafeComplete(string ip, string cN_S_TASK_NO)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.ip == ip).FirstOrDefault();
            bool result = true;
            if(ip != "")
            {
                //var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                //{
                //    addr = plc.writeAddr,
                //    data = 0,
                //    host = ip,
                //    port = plc.port
                //});
                //CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr}里面数据为0.");
                //Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr}里面数据为0.");
                //var wirte1 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                //{
                //    addr = plc.writeAddr + 1,
                //    data = 0,
                //    host = ip,
                //    port = plc.port
                //});
                //CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为0.");
                //Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为0.");
                result = true;
            }
            else
            {
                CMMLog.Debug($"包装下线,4,ip=null!");
            }
            return result;
        }
 
        internal static void SecondWeightActionOne(ActionControlModel model)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.ip == model.ext1 && a.port == model.ext4).FirstOrDefault();
 
            if (plc != null)
            {
                bool req = false;
                var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                {
                    dataNum = 28,
                    addr = plc.readAddr + 1,
                    host = plc.ip,
                    port = plc.port
                });
                if (result.result.Length > 0)
                {
                    if(plc.deviceType == "22")
                    {
                        var newWeight = result.result[19] + "," + result.result[20];
                        MongoDBSingleton.Instance.Update<MachineInfoTetracobalt>(Query.EQ("trayCode", model.ext2), Update.Set("secondNg", result.result[2]), UpdateFlags.None);
                        MongoDBSingleton.Instance.Update<MachineInfoTwoTetracobalt>(Query.EQ("trayCode", model.ext2), Update.Set("secondNg", result.result[2]).Set("oneTrayWeightFC",newWeight), UpdateFlags.None);
                    }
                    else
                    {
                        MongoDBSingleton.Instance.Update<MachineInfo>(Query.EQ("trayCode", model.ext2), Update.Set("secondNg", result.result[2]), UpdateFlags.None);
                    }
                    
                    //获取从复称平台读出的 时间戳,并转换处理为 string 字符串
                    //string timeStamp = Convert.ToInt32(Completion(result.result[26]) + Completion(result.result[27]), 2).ToString();
                    string timeStamp = Convert.ToInt32(Completion(result.result[25]) + Completion(result.result[26]), 2).ToString();
                    string wmstaskno = "";
                    string traycode = "";
 
                    CMMLog.Info($"SecondWeightActionOne:readData:{JsonConvert.SerializeObject(result.result)},timeStamp:{timeStamp}");
 
                    if (result.result[0] == 3)
                    {
                        int[] num = new int[1] { 0 };
                        var writeRes0 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti
                        {
                            host = plc.ip,
                            addr = plc.writeAddr + 1,
                            data = num,//原先是1,单个写入
                            port = plc.port
                        });
                    }
 
                    if (result.result[0] == 3 && result.result[2] == 1)
                    //if (result.result[2] == 1)
                    {
                        CMMLog.Info($"SecondWeightActionOne:准备生成复称入缓存架任务,开始确定托盘号:{model.ext2}");
                        if(plc.deviceType == "22")
                        {
                            req = WMSHelper.WMSInTwo(plc.location, model.ext2, ref wmstaskno, ref traycode, timeStamp);
                        }
                        else
                        {
                            req = WMSHelper.WMSIn(plc.location, model.ext2, ref wmstaskno, ref traycode, timeStamp);
                        }
                        
                        ERPService.updatePackageInfo(model.machince, model.ext2, result.result);
                        //if (req) TSHelper.GoToAGV(cN_S_TASK_NO, 10, 6);
                        //else CMMLog.Debug($"WMS返回{req}");
                        //var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                        //{
                        //    addr = plc.writeAddr + 3,
                        //    host = plc.ip,
                        //    port = plc.port,
                        //    data = 1
                        //});
                    }
                    if (result.result[2] == 2)
                    {
                        //复称平台复称货物NG——进入NG复称流程  调用WMS接口分配货架库位
                        //TSHelper.GoToAGV(cN_S_TASK_NO, 10, 6);
                        if(plc.deviceType == "22")
                        {
                            req = WMSHelper.WMSInTwo(plc.location, model.ext2, ref wmstaskno, ref traycode, timeStamp);
                        }
                        else
                        {
                            req = WMSHelper.WMSIn(plc.location, model.ext2, ref wmstaskno, ref traycode, timeStamp);
                        }
                        
                        if (req) CMMLog.Info($"SecondWeightActionOne:复称入缓存架NG流程:WMS生成任务成功!");
                        else CMMLog.Info($"SecondWeightActionOne:复称入缓存架NG流程:WMS生成任务失败!");
                        var plcInfo = Settings.GetPlcInfo().Where(a => a.location == model.ext3).FirstOrDefault();
                        if (plcInfo != null)
                        {
                            CMMLog.Info($"SecondWeightActionOne:复称入缓存架NG流程:写入包装机NG通道数据1!");
                            var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                            {
                                addr = plcInfo.writeAddr + 29,
                                host = plcInfo.ip,
                                port = plcInfo.port,
                                data = 1
                            });
                        }
                    }
                    string PlcBit02 = Settings.GetPlcInfo().Where(a => a.deviceType == "2").FirstOrDefault().location;
                    UpdateBuilder update = Update.Set("ng", result.result[2]);
                    MongoDBSingleton.Instance.Update<SecondWeighState>(Query.EQ("location", PlcBit02), update, "SecondWeighState", UpdateFlags.None);
                    if (req) MongoDBSingleton.Instance.Remove<ActionControlModel>(Query.EQ("_id", model._id), "ActionControlModel", RemoveFlags.Single);
                }
            }
            else CMMLog.Debug($"包装下线,1213,ip=null!");
        }
 
        #endregion
 
        #endregion
 
        #region   3楼复称平台入3楼双层缓存架流程——需回报WMS任务状态——3楼复称入缓存架——已完成(待测试)
 
        /// <summary>
        /// 判断复称通道3里面的值是否为1,并且复称平台上有货,调WMS生成入缓存架任务
        /// </summary>
        /// <param name="pmInfo"></param>
        internal static void SecondWeightInCache(Settings.PlcInfo pmInfo)
        {
            //检查复称平台通道3是否值为1,并且复称平台上有货
            if(ProcessHelper.CheckStartFree(GetSecondWeighBit()) && ProcessHelper.CheckEndFree(GetSecondWeighBit()))
            {
                try
                {
                    var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                    {
                        dataNum = 1,
                        addr = pmInfo.readAddr + 3,
                        host = pmInfo.ip,
                        port = pmInfo.port
                    });
                    if (result != null && result.errCode == 0)
                    {
                        //复称平台 重新复核称重后,发送结果给AMS 1:OK,2:NG,当检测到通道2或者4的值为
                        if (result.result[0] == 1)
                        {
                            //判断一下当前复称点有没有任务占用中
                            if (ProcessHelper.CheckStartFree(GetSecondWeighBit()) && ProcessHelper.CheckEndFree(GetSecondWeighBit()))
                            {
                                //判断复称位置,产品NG合格值为1并且复称上有货
                                //var state = GetSecondWeighState(pmInfo.location);
                                //if (state.full == 1 && state.ng == 1)
                                //{
                                    //#region    获取托盘码
                                    //CMMLog.Info("复称入缓存:获取托盘,fc01");
                                    //string traycode = GetMachineTrayCode();
                                    //#endregion
 
                                    //string wmstaskno = "";
                                    //bool req = WMSHelper.WMSIn(pmInfo.location, traycode, ref wmstaskno, ref traycode);
                                    //if (req)
                                    //{
                                    //    CMMLog.Info($"复称入缓存架:WMS生成任务成功!");
                                    //}
                                    //else
                                    //{
                                    //    CMMLog.Info($"复称入缓存架:WMS生成任务失败!");
                                    //}
                                //}
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    CMMLog.Error(ex.Message, ex);
                }
            }
        }
 
        #region   复称交互
 
        public static int SecondRelax = 0;
        /// <summary>
        /// 请求进入复称取货
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="taskNo"></param>
        internal static void SecondWeightInCache1012(string ip, string taskNo) 
        {
            var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault();
            if(ip != null)
            {
                ///小车和复称位对接
                if (ip != "")
                {
                    //小车请求进料,并且查询设备是否允许AGV进入
                    WorkFlowAction.TrackLog(taskNo, 10, 1012, "success");
                    TSHelper.GoToAGV(taskNo, 10, 1);
                    SecondRelax = 0;
                }
                else CMMLog.Info($"复称入缓存架,1012,ip=null!");
            }
        }
        /// <summary>
        /// 取货完成,离开复称,复位通道,并读取设备通道数据
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="cN_S_TASK_NO"></param>
        internal static void SecondWeightInCache4(string ip, string taskNo)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault();
 
            var trayCodeTask = MongoDBSingleton.Instance.FindOne<TN_I_TASK_MST>(Query.EQ("CN_S_TASK_NO", taskNo), "TN_I_TASK_MST");
            if(trayCodeTask != null)
            {
                string trayCodeNo = trayCodeTask.CN_S_BATCH_NO;
                if(trayCodeNo != "")
                {
                    var status = MongoDBSingleton.Instance.FindOne<SecondWeighState>(Query.EQ("location", GetSecondWeighBit()), "SecondWeighState");
                    //UpdateBuilder update = Update.Set("full", 0);
                    //if (status.ng != 2)
                    //{
                    //    update = Update.Set("ng", 0).Set("full", 0);
                    //}
 
                    UpdateBuilder update = Update.Set("ng", 0).Set("full", 0);
                    CMMLog.Debug($"开始更改复称状态中间表状态:full:0");
                    CMMLog.Debug($"三楼双层缓存架卸货交互:trayCode:{trayCodeTask.CN_S_BATCH_NO}");
                    if (plc.deviceType =="22")
                    {
                        MongoDBSingleton.Instance.Remove<MachineInfoTetracobalt>(Query.EQ("trayCode", trayCodeTask.CN_S_BATCH_NO), "MachineInfoTetracobalt", RemoveFlags.Single);
                        if (ERPService.ERPSwitch01 == "0") MongoDBSingleton.Instance.Remove<MachineInfoTetracobalt>(Query.EQ("trayCode", trayCodeTask.CN_S_BATCH_NO), "MachineInfoTetracobalt", RemoveFlags.Single);
                    }
                    else
                    {
                        MongoDBSingleton.Instance.Remove<MachineInfo>(Query.EQ("trayCode", trayCodeTask.CN_S_BATCH_NO), "MachineInfo", RemoveFlags.Single);
                        if (ERPService.ERPSwitch01 == "0") MongoDBSingleton.Instance.Remove<MachineInfoTwo>(Query.EQ("trayCode", trayCodeTask.CN_S_BATCH_NO), "MachineInfoTwo", RemoveFlags.Single);
                    }
                    
                    //MongoDBSingleton.Instance.Update<TN_I_TASK_MST>(Query.EQ("CN_S_TASK_NO", taskNo), Update.Set("CN_S_BATCH_NO", trayCodeNo), UpdateFlags.None);
                    MongoDBSingleton.Instance.Update<SecondWeighState>(Query.EQ("location", GetSecondWeighBit()), update, "SecondWeighState", UpdateFlags.None);
                    SecondRelax = 1;
                }
            }
            if (plc != null)
            {
                var writeRes1 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                {
                    host = plc.ip,
                    addr = plc.writeAddr,
                    data = 0,
                    port = plc.port
                });
 
                writeRes1 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                {
                    host = plc.ip,
                    addr = plc.writeAddr + 3,
                    data = 1,
                    port = plc.port
                });
            }
        }
 
        #endregion
 
        #region  三楼双层缓存架交互
 
        /// <summary>
        /// 请求进入三楼双层缓存架
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="taskNo"></param>
        internal static void SecondWeightInCache1013(string taskNo)
        {
            TSHelper.GoToAGV(taskNo,10,3);
        }
        /// <summary>
        /// 离开三楼双层缓存架回报信息
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="taskNo"></param>
        internal static bool SecondWeightInCache6(string ip, string cN_S_TASK_NO)
        {
            //CMMLog.Info($"三楼双层缓存架卸货交互:trayCode:{trayCode}");
 
            //var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault();
 
            //if (plc != null)
            //{
            //    var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
            //    {
            //        dataNum = 28,
            //        addr = plc.readAddr + 1,
            //        host = plc.ip,
            //        port = plc.port
            //    });
            //    var tray = MongoDBSingleton.Instance.FindOne<TN_I_TASK_MST>(Query.EQ("CN_S_TASK_NO", cN_S_TASK_NO), "TN_I_TASK_MST");
            //    MongoDBSingleton.Instance.Update<MachineInfo>(Query.EQ("trayCode", tray.CN_S_BATCH_NO), Update.Set("secondNg", result.result[2]), UpdateFlags.None);
            //    //获取从复称平台读出的 时间戳,并转换处理为 string 字符串
            //    string timeStamp = Convert.ToInt32(Completion(result.result[26]) + Completion(result.result[27]), 2).ToString();
            //    string wmstaskno = "";
            //    string traycode = "";
            //    if (result.result[0] == 3 && result.result[2] == 1)
            //    {
            //        CMMLog.Debug($"准备生成复称入缓存架任务,开始确定托盘号:{tray.CN_S_BATCH_NO}");
            //        bool req = WMSHelper.WMSIn(plc.location, tray.CN_S_BATCH_NO, ref wmstaskno, ref traycode, timeStamp);
            //        if (req) TSHelper.GoToAGV(cN_S_TASK_NO, 10, 6);
            //        else CMMLog.Debug($"WMS返回{req}");
            //    }
            //    if (result.result[2] == 2)
            //    {
            //        //复称平台复称货物NG——进入NG复称流程  调用WMS接口分配货架库位
            //        TSHelper.GoToAGV(cN_S_TASK_NO, 10, 6);
            //        bool req = WMSHelper.WMSIn(plc.location, tray.CN_S_BATCH_NO, ref wmstaskno, ref traycode, timeStamp);
            //        if (req) CMMLog.Debug($"复称入缓存架NG流程:WMS生成任务成功!");
            //        else CMMLog.Debug($"复称入缓存架NG流程:WMS生成任务失败!");
            //    }
            //    string PlcBit02 = Settings.GetPlcInfo().Where(a => a.deviceType == "2").FirstOrDefault().location;
            //    UpdateBuilder update = Update.Set("ng", result.result[2]).Set("full", 1);
            //    MongoDBSingleton.Instance.Update<SecondWeighState>(Query.EQ("location", PlcBit02), update, UpdateFlags.None);
            //}
            //else CMMLog.Debug($"包装下线,1213,ip=null!");
 
 
            return true;
        }
 
        #endregion
 
        #endregion
 
        internal static void writeStackingMouth6(string loca, string taskNo)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.location == loca && a.enable == 1).FirstOrDefault();
            CMMLog.Info($"3楼缓存架入叠托:收到信号4,查询设备信息:{JsonConvert.SerializeObject(plc)}。");
            if (plc != null)
            {
                var task = MongoDBSingleton.Instance.FindOne<TN_I_TASK_MST>(Query.EQ("CN_S_TASK_NO", taskNo), "TN_I_TASK_MST");
                CMMLog.Info($"3楼缓存架入叠托:收到信号4,查询任务信息:{JsonConvert.SerializeObject(task)}。");
                var machine = MongoDBSingleton.Instance.FindOne<WMSInfo>(Query.EQ("trayCode", task.CN_S_BATCH_NO), "WMSInfo");
                CMMLog.Info($"3楼缓存架入叠托:收到信号4,查询WMSInfo表信息:{JsonConvert.SerializeObject(machine)}。");
                if (machine != null)
                {
                    //int[] num = DiePan(machine);
                    //CMMLog.Info($"send num:{JsonConvert.SerializeObject(num)}");
                    //var wirteall01 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti
                    //{
                    //    addr = plc.writeAddr + 10,
                    //    host = plc.ip,
                    //    port = plc.port,
                    //    data = num
                    //});
 
                    WriteCacheStackingData(plc, task, machine);
 
 
                    var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                    {
                        addr = plc.writeAddr + 1,
                        host = plc.ip,
                        port = plc.port,
                        data = 2
                    });
                }
            }
        }
 
        #region   3楼双层缓存货架到三楼叠托口——需回报WMS任务状态——3楼缓存架入叠托——半完成(待测试)
 
        /// <summary>
        /// 判断三楼叠托口是否有允许上料请求(通道1参数1),有就调WMS生成缓存架到叠托口任务
        /// </summary>
        /// <param name="pmInfo"></param>
        internal static void CacheStackingMouth(Settings.PlcInfo pmInfo)
        {
            //需要设置时间延迟
            try
            {
                //读取通道1里面参数是否为1,判断叠盘机是否需要上料
                var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                {
                    dataNum = 1,
                    addr = pmInfo.readAddr + 1,
                    host = pmInfo.ip,
                    port = pmInfo.port
                });
                if (result != null && result.errCode == 0)
                {
                    //参数1表示叠托点申请入料
                    if (result.result[0] == 1)
                    {
                        var tasks = MongoDBSingleton.Instance.Find<TN_I_TASK_MST>(Query.EQ("CN_S_END_BIT", pmInfo.location), "TN_I_TASK_MST");
                        //判断一下当前叠托点是否有任务占用
                        if (ProcessHelper.CheckEndFree(pmInfo.location) && tasks.Count == 0)
                        {
                            //可以生成任务,调WMS接口获取任务信息
                            bool req = WMSHelper.WMSOut(pmInfo.location, "");
                            if (req) CMMLog.Debug($"调用WMS获取碟盘出库生成任务成功!");//现在任务由WMS自己下发,AMS做拦截处理(查询ext1里面对应的任务类型,并更改任务类型)
                            else CMMLog.Debug($"调用WMS获取碟盘出库生成任务失败!");
                        }
                        else CMMLog.Debug($"检查当前叠托点是否有任务占用,或者MST主表中有缓存架入叠盘机的任务!");
                    }
                }
                else CMMLog.Debug($"缓存入叠盘机,创建任务,result={result.errMsg}");
            }
            catch (Exception ex)
            {
                CMMLog.Error(ex.Message, ex);
            }
        }
 
        #region   三楼叠托机交互
 
        /// <summary>
        /// 请求进入三楼叠托机
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="taskNo"></param>
        internal static void CacheStackingMouth1013(string ip,string taskNo)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault();
            if(plc != null)
            {
                var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                {
                    addr = plc.writeAddr + 1,
                    host = plc.ip,
                    port = plc.port,
                    data = 2
                });
 
                var readres = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                {
                    dataNum = 1,
                    addr = plc.readAddr + 1,
                    host = plc.ip,
                    port = plc.port
                });
                if (readres.result[0] == 0 || readres.result[0] == 1)
                {
                    wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                    {
                        addr = plc.writeAddr + 1,
                        host = plc.ip,
                        port = plc.port,
                        data = 1
                    });
                }
                else if (readres.result[0] == 2)
                {
                    TSHelper.GoToAGV(taskNo, 10, 3);
                }
            }
            else CMMLog.Debug($"缓存入叠盘机,1013,plc=null!");
        }
 
        public static void WriteCacheStackingData(Settings.PlcInfo plc, TN_I_TASK_MST task, WMSInfo machine)
        {
            if (plc.deviceType == "23")
            {
                //四钴车间
                var machineTwo = MongoDBSingleton.Instance.FindOne<MachineInfoTwoTetracobalt>(Query.EQ("trayCode", task.CN_S_BATCH_NO), "MachineInfoTwoTetracobalt");
                CMMLog.Info($"3楼缓存架入叠托:收到信号4,查询MachineInfoTwoTetracobalt表信息:{JsonConvert.SerializeObject(machineTwo)}。");
                if (machineTwo != null)
                {
                    CMMLog.Info($"3楼缓存架入叠托:获取数据:{JsonConvert.SerializeObject(machineTwo)},IPort:{plc.ip},{plc.port}");
                    int[] num = DiePanTwoTetracobalt(machine, machineTwo);
                    CMMLog.Info($"3楼缓存架入叠托:写入数据:{JsonConvert.SerializeObject(num)},IPort:{plc.ip},{plc.port}");
                    var wirteall01 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti
                    {
                        addr = plc.writeAddr + 5,
                        host = plc.ip,
                        port = plc.port,
                        data = num
                    });
                    CMMLog.Info($"3楼缓存架入叠托:返回数据:{JsonConvert.SerializeObject(wirteall01)},IPort:{plc.ip},{plc.port}");
                    if (wirteall01.errCode == 0)
                    {
                        MongoDBSingleton.Instance.Remove<MachineInfoTwoTetracobalt>(Query.EQ("trayCode", task.CN_S_BATCH_NO), "MachineInfoTwoTetracobalt", RemoveFlags.Single);
                    }
                }
            }
            else
            {
                var machineTwo = MongoDBSingleton.Instance.FindOne<MachineInfoTwo>(Query.EQ("trayCode", task.CN_S_BATCH_NO), "MachineInfoTwo");
                CMMLog.Info($"3楼缓存架入叠托:收到信号4,查询MachineInfoTwo表信息:{JsonConvert.SerializeObject(machineTwo)}。");
                if (machineTwo != null)
                {
                    CMMLog.Info($"3楼缓存架入叠托:获取数据:{JsonConvert.SerializeObject(machineTwo)},IPort:{plc.ip},{plc.port}");
                    int[] num = DiePanTwo(machine, machineTwo);
                    CMMLog.Info($"3楼缓存架入叠托:写入数据:{JsonConvert.SerializeObject(num)},IPort:{plc.ip},{plc.port}");
                    var wirteall01 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti
                    {
                        addr = plc.writeAddr + 10,
                        host = plc.ip,
                        port = plc.port,
                        data = num
                    });
                    CMMLog.Info($"3楼缓存架入叠托:返回数据:{JsonConvert.SerializeObject(wirteall01)},IPort:{plc.ip},{plc.port}");
                    if (wirteall01.errCode == 0)
                    {
                        MongoDBSingleton.Instance.Remove<MachineInfoTwo>(Query.EQ("trayCode", task.CN_S_BATCH_NO), "MachineInfoTwo", RemoveFlags.Single);
                    }
                }
            }
        }
 
        private static int[] DiePanTwoTetracobalt(WMSInfo machine, MachineInfoTwoTetracobalt machineInfoTwo)
        {
 
            var ErpItemInfo = MongoDBSingleton.Instance.FindOne<ERPItemTable>(Query.EQ("item_code", machineInfoTwo.materialCode), "ERPItemTable");
            CMMLog.Info($"3楼缓存架入叠托:收到信号4,查询ERPItemTable表信息:{JsonConvert.SerializeObject(ErpItemInfo)}。");
 
            //偏移量 +11 通道范围: 40311 ~ 40390
            CMMLog.Info($"叠盘机数据处理.");
            //10~19通道
            int[] num = new int[105];//总长度:105
 
            #region   0~23 => 6~29
            int a = 0;//员工编号处理
            for (int b = 0; b <= 4; b++)
            {
                num[b] = int.Parse(AsciiToTen(machineInfoTwo.empCode.Substring(a, 2)));
            }
 
            a = 0;//托盘码处理
            for (int b = 5; b <= 7; b++)
            {
                //num[b] = int.Parse(AsciiToTen(machine.trayCode.Substring(a, 2)));
                //a = a + 2;
 
                num[b] = 0;
            }
            num[8] = int.Parse(AsciiToTen(machine.location));//包装机号
            for (int b = 9; b <= 10; b++)
            {
                int k = b % 2 == 0 ? 0 : 16;
                num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.productWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
            }
            for (int b = 11; b <= 12; b++)
            {
                int k = b % 2 == 0 ? 0 : 16;
                num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machineInfoTwo.trayCodeWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
            }
            for (int b = 13; b <= 14; b++)
            {
                int k = b % 2 == 0 ? 0 : 16;
                num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.oneTrayWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
            }
            for (int b = 15; b <= 16; b++)
            {
                var arrTwo = machineInfoTwo.oneTrayWeightFC.Split(',').ToList();
                num[b] = int.Parse(arrTwo[b - 15]);//复称重量
            }
            //for (int b = 15; b <= 16; b++) num[b] = 0;// 叠包后实际重量 && 复称结果 默认:0
            num[17] = int.Parse(machineInfoTwo.trayType);
 
            num[18] = machine.addState;//是否需要叠包
            num[19] = int.Parse(machine.packageCode);//袋号
            for (int b = 20; b <= 21; b++)
            {
                int k = b % 2 == 0 ? 0 : 16;
                num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machineInfoTwo.productTime), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
            }
 
            //时间戳处理
            num[22] = 0;
            num[23] = 0;
            #endregion
 
            string pcHead = "";
            string cpHead = "";
            string mtHead = "";// 设备的真实物料编码
 
            #region  35~54 => 41~60 产品批次号
 
            string data = machineInfoTwo.lotNo;
            CMMLog.Info($"41~60data1_1:{data}");
            if (data.Length % 2 != 0)
            {
                data = "0" + machineInfoTwo.lotNo;
                pcHead = "" + machineInfoTwo.lotNo.Substring(0, 1);
            }
            CMMLog.Info($"41~60data1_2:{data}");
            //字符串长度 / 2 = 写入通道数量(两位一转) 列: 3L0050 写入三个通道
            int maxLength = 34 + (data.Length / 2);// data.Length / 2 至少为 1
            int aa = 0;
            for (int i = 35; i <= maxLength; i++)
            {
                num[i] = int.Parse(AsciiToTen(data.Substring(aa, 2)));
                aa = aa + 2;
            }
            for (int x = maxLength + 1; x <= 54; x++) num[x] = 0;//将产品批次号无数据的通道全部置为 0
            #endregion
 
            #region   55~64 => 61~70 产品型号
 
            ERPService.HandleItemInfoChina(machine.itemCode, 55, 64, num);// 中文处理 55~64 61~70
 
            //string data2 = machine.itemCode;
            //CMMLog.Info($"51~50data2_1:{data2}");
            //if (data2.Length % 2 != 0)
            //{
            //    data2 = "0" + machine.itemCode;
            //    cpHead = "" + machine.itemCode.Substring(0, 1);
            //}
            //CMMLog.Info($"51~60data2_2:{data2}");
            //maxLength = 39 + (data2.Length / 2);
            //int aaa = 0;
            //for (int i = 40; i <= maxLength; i++)
            //{
            //    num[i] = int.Parse(AsciiToTen(data2.Substring(aaa, 2)));
            //    aaa = aaa + 2;
            //}
            //for (int x = maxLength + 1; x <= 49; x++) num[x] = 0;//将产品型号无数据的通道全部置为0
            #endregion
 
            #region   65~79 => 71~85 物料编码
 
            string data3 = machineInfoTwo.materialCode;
            CMMLog.Info($"71~85 data3_1:{data3}");
            if (data3.Length % 2 != 0)
            {
                //data3 = "0" + machineInfoTwo.materialCode;
                mtHead = "" + machineInfoTwo.materialCode.Substring(0, 1);
            }
            CMMLog.Info($"71~85 data3_2:{data3}");
            maxLength = 64 + (data3.Length / 2);
            int aaaa = 0;
            for (int i = 65; i <= maxLength; i++)
            {
                num[i] = int.Parse(AsciiToTen(data3.Substring(aaaa, 2)));
                aaaa = aaaa + 2;
            }
            for (int x = maxLength + 1; x <= 79; x++) num[x] = 0;//将产品型号无数据的通道全部置为0
            #endregion
 
            ERPService.HandleItemInfoChina(ErpItemInfo.item_name, 80, 99, num);// 中文处理 80~99 86~105
            ERPService.HandleItemInfo(ErpItemInfo.item_uom, 100, 104, num);// 100~104 106~110
 
            // 如果 产品批次号 或者 产品型号 位数 为 奇数,则将提前取出的 首字符 重新转码 写入 其所对应通道区域的 首通道
            if (pcHead != "") num[35] = int.Parse(AsciiToTen(pcHead));
            if (cpHead != "") num[55] = int.Parse(AsciiToTen(cpHead));
            if (mtHead != "") num[65] = int.Parse(AsciiToTen(mtHead));
            if (pcHead != "" || cpHead != "") CMMLog.Info($"产品型号或批次号数量为奇数,特将首位取出单独处理,以下为批次号和产品型号的十进制数值{num[35]},{num[55]}");
 
            CMMLog.Info($"叠盘机数据处理完毕:{JsonConvert.SerializeObject(num)}");
 
            return num;
        }
 
        /// <summary>
        /// 三楼叠托机扫码
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="taskNo"></param>
        internal static void CacheStackingMouth1313(string ip,string taskNo)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault();
            if(plc != null)
            {
                if(plc.deviceType == "23")
                {
                    var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                    {
                        addr = plc.writeAddr + 1,
                        host = plc.ip,
                        data = 3,
                        port = plc.port
                    });
                    if (wirte.errCode == 0)
                    {
                        var taskInfo = MongoDBSingleton.Instance.FindOne<TN_I_TASK_MST>(Query.EQ("CN_S_TASK_NO", taskNo), "TN_I_TASK_MST");
                        MongoDBSingleton.Instance.Remove<WMSInfo>(Query.EQ("trayCode", taskInfo.CN_S_BATCH_NO), RemoveFlags.Single);
 
                        wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                        {
                            addr = plc.writeAddr + 1,
                            host = plc.ip,
                            data = 0,
                            port = plc.port
                        });
                        TSHelper.GoToAGV(taskNo, 10, 7);
                    }
                }
                else
                {
                    OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                    {
                        addr = plc.writeAddr + 1,
                        host = plc.ip,
                        data = 2,
                        port = plc.port
                    });
                    var readres = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                    {
                        dataNum = 2,
                        addr = plc.readAddr + 1,
                        host = plc.ip,
                        port = plc.port
                    });
                    if (readres != null)
                    {
                        var taskInfo = MongoDBSingleton.Instance.FindOne<TN_I_TASK_MST>(Query.EQ("CN_S_TASK_NO", taskNo), "TN_I_TASK_MST");
                        if (readres.result[0] == 3 && readres.result[1] == 1)
                        {
                            CMMLog.Debug($"3楼缓存架入叠托OK:TaskNo:{taskInfo.CN_S_TASK_NO},叠包OK写入值3。");
                            //读码信息比对结果:OK           读到通道2参数为1时,小车将托盘放置在叠盘机上(改参数10为7)
                            var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                            {
                                addr = plc.writeAddr + 1,
                                host = plc.ip,
                                data = 3,
                                port = plc.port
                            });
                            if (wirte.errCode == 0)
                            {
                                //并删除WMSInfo中间表中对应托盘号的数据(也可在小车离开叠盘机之后删除,暂定通道2参数1)
                                CMMLog.Debug($"读码信息比对结果:OK ,并删除WMSInfo中间表中对应托盘号的数据");
                                MongoDBSingleton.Instance.Remove<WMSInfo>(Query.EQ("trayCode", taskInfo.CN_S_BATCH_NO), RemoveFlags.Single);
                                CMMLog.Debug($"3楼缓存架入叠托OK:TaskNo:{taskInfo.CN_S_TASK_NO},叠包OK写入值4。");
                                if (CacheStackingMouth8(plc))
                                {
                                    CMMLog.Debug($"3楼缓存架入叠托OK:TaskNo:{taskInfo.CN_S_TASK_NO},叠包OK写入值成功,更改AGV参数10为7,让小车离开。");
                                    TSHelper.GoToAGV(taskNo, 10, 7);
                                }
                            }
                        }
                        else if (readres.result[0] == 3 && readres.result[1] == 2)
                        {
                            CMMLog.Debug($"读码信息比对结果:条码与传送的数据不一致");
                            //二期--调用 WMS 改道接口 获取 目标点位,并 更改 AGV 站点
                            string ChangeBit = WMSHelper.WmsUpdateWay(taskInfo.CN_S_SOURCE_NO, plc.Extend);
                            if (!string.IsNullOrEmpty(ChangeBit))
                            {
                                int[] parms = { StockInstance.Instance.GetAGVCodeForBitCode(taskInfo.CN_S_END_BIT), StockInstance.Instance.GetAGVCodeForBitCode(ChangeBit) };
                                TSHelper.ChangeParam(taskNo, 1, parms);
                                TSHelper.GoToAGV(taskNo, 3, 1);// 泰州格林美更改起点层数为1-荆门也需要更改
 
                                //TSHelper.ChangeParam(taskNo, 1, StockInstance.Instance.GetAGVCodeForBitCode(taskInfo.CN_S_END_BIT));
                                //TSHelper.ChangeParam(taskNo, 2, StockInstance.Instance.GetAGVCodeForBitCode(ChangeBit));
                                TSHelper.GoToAGV(taskNo, 10, 8);
                                MongoDBSingleton.Instance.Update<TN_I_TASK_MST>(Query.EQ("CN_S_TASK_NO", taskNo), Update.Set("CN_S_END_BIT", ChangeBit).Set("CN_S_START_BIT", taskInfo.CN_S_END_BIT), "TN_I_TASK_MST", UpdateFlags.None);
                            }
                            else CMMLog.Debug($"调用WMS改道接口,未获取到可用点位。");
                            Console.WriteLine($"读码信息比对结果:条码与传送的数据不一致");
                        }
                        //PLCControl.CacheStackingMouth6(plc);
                    }
                }
                
            }
        }
 
        /// <summary>
        /// 叠托OK写入4
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="taskNo"></param>
        internal static void CacheStackingMouth6(Settings.PlcInfo plc)
        {
            var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
            {
                addr = plc.writeAddr + 1,
                data = 3,
                host = plc.ip,
                port = plc.port
            });
            Thread.Sleep(2000);
            wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
            {
                addr = plc.writeAddr + 1,
                data = 0,
                host = plc.ip,
                port = plc.port
            });
        }
 
        /// <summary>
        /// 卸货离开三楼叠托机--二期:需要和TS对接,让小车 卸货完成等到 小车离开之后再发送
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="taskNo"></param>
        internal static bool CacheStackingMouth8(Settings.PlcInfo plc)
        {
            bool result = false;
            var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
            {
                addr = plc.writeAddr + 1,
                data = 4,
                host = plc.ip,
                port = plc.port
            });
            if (wirte.errCode == 0) result = true;
            return result;
        }
        #endregion
 
        #endregion
 
        #region   3楼包装机空托补给3楼包装机流程——无需回报WMS任务状态——3楼包装补空——已完成(待测试)
 
        /// <summary>
        /// 3楼包装机补空托流程——任务开始前,判断包装机有没有下线信号,并判断当前包装机对应的地堆位是否有任务占用
        /// </summary>
        /// <param name="a"></param>
        internal static void PickUpBlank(Settings.PlcInfo pmInfo)
        {
            /* 包装补空流程:
             * 读取 包装机 是否需要补空托,如果有请求,则继续判断当前包装机无任务占用,以及当前包装机所对应的 地堆位 无任务占用
             * 任务创建方式:调用WMS,传输 终点(当前包装机所对应的 地堆位)
             * **/
            try
            {
                //读取输入寄存器还是输出寄存器待定,读取通道0的数据
                var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                {
                    dataNum = 1,
                    addr = pmInfo.readAddr,
                    host = pmInfo.ip,
                    port = pmInfo.port
                });
                if (result != null && result.errCode == 0)
                {
                    //1:取料;3:人工叫空托盘
                    if (result.result[0] == 3)
                    {
                        CMMLog.Info("包装机补空托流程:判断包装机通道0值为3");
                        //空托库区 -> 包装机对应地堆空托缓存位
                        if (ProcessHelper.PickUpEndFree(pmInfo.Extend))
                        {
                            bool req = WMSHelper.WMSOut(pmInfo.Extend, "");
                            if (req)
                            {
                                //任务完成之后,需要复位此通道数据为 0 
                                var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                                {
                                    host = pmInfo.ip,
                                    addr = pmInfo.writeAddr + 2,
                                    data = 1,
                                    port = pmInfo.port
                                });
                            }
                        }
                    }
                }
                
            }
            catch (Exception ex)
            {
                CMMLog.Error(ex.Message, ex);
            }
        }
 
        /// <summary>
        /// 包装补空任务完成--复位 收到上空信号 为 0
        /// </summary>
        /// <param name="PlcBitCache01"></param>
        internal static void PickUpBlankComplete(string PlcBitCache01)
        {
            string PlcBit01 = PlcBitCache01;
            var plcInfo = Settings.GetPlcInfo().Where(a => a.location == PlcBit01).FirstOrDefault();
            var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
            {
                host = plcInfo.ip,
                addr = plcInfo.writeAddr + 2,
                data = 0,
                port = plcInfo.port
            });
        }
 
        #endregion
 
        #region   3楼补给成品空托区到3楼拆盘机流程——需回报WMS任务状态——3楼拆盘补空——已完成(待测试)
 
        /// <summary>
        /// 线程循环查询拆盘机是否需要补空托
        /// </summary>
        /// <param name="plc"></param>
        internal static void CheckPackingLineEmpty(Settings.PlcInfo plc)
        {
            //读plc信号,看有没有叫料请求,判断是否已经有任务
            //没有任务,调WMS生成空托补充任务
            try
            {
                //RegisterReadInPut 读取输入寄存器还是输出寄存器待定,读取通道1的数据
                var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                {
                    dataNum = 1,
                    addr = plc.readAddr+1,
                    host = plc.ip,
                    port = plc.port
                });
                if (result != null && result.errCode == 0)
                {
                    //1.补空托盘
                    if (result.result[0] == 1)
                    {
                        //判断3楼拆盘机(补空托盘点位)终点是否有任务占用,没有则生成任务
                        if (ProcessHelper.CheckEndFree(plc.location))
                        {
                            CMMLog.Debug("拆盘补空流程调用。。。");
                            string wmstaskno = "";
                            string traycode = "";
                            string trayType = "";
                            if (result.result[1] == 1) trayType = "田字托";
                            else trayType = "川字托";
                            bool req = WMSHelper.WMSEmptyOut(plc.location, "", ref wmstaskno, ref traycode, trayType);
                            if (req) CMMLog.Debug($"调用WMS获取三楼拆盘机生成任务成功!");
                            else CMMLog.Debug($"调用WMS获取三楼拆盘机生成任务失败!");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CMMLog.Error(ex.Message, ex);
            }
        }
 
        #region   拆盘机交互
 
        /// 小车请求出料    1    AMS写,PLC读    1-请求出料;2-出料进行中;3-动作完成
        /// 设备允许进料    1    PLC写,AMS读    1-允许进料;2-进料进行中;3-动作完成
        /// <summary>
        /// 向拆盘机请求卸货
        /// </summary>
        /// <param name="ips"></param>
        internal static void DiscRemoverReqUnload(string ip, string taskNo)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault();
            if (plc != null)
            {
                var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                {
                    dataNum = 1,
                    host = plc.ip,
                    port = plc.port,
                    addr = plc.readAddr + 1
                });
                if (readRes != null && readRes.errCode == 0)
                {
                    if (readRes.result[0] == 0 || readRes.result[0] == 1)
                    {
                        //不允许进料,ams写入请求出料
                        var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                        {
                            host = plc.ip,
                            port = plc.port,
                            addr = plc.writeAddr + 1,
                            data = 1
                        });
                    }
                    else if (readRes.result[0] == 2)
                    {
                        //设备允许进料
                        //改参数通知agv
                        if (TSHelper.GoToAGV(taskNo, 10, 3))
                        {
                            WorkFlowAction.TrackLog(taskNo, 10, 1013, "success");
                            //改成出料进行中
                            CMMLog.Debug("三楼拆盘机:小车正在卸空盘中!");
                            var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                            {
                                host = plc.ip,
                                port = plc.port,
                                addr = plc.writeAddr + 1,
                                data = 2
                            });
                        }
                        else CMMLog.Debug($"三楼拆盘补空,1013,TS,10,3,false!");
                    }
                }
            }
        }
 
        /// <summary>
        /// 小车卸货完成离开通知拆盘机
        /// </summary>
        /// <param name="ips"></param>
        /// <param name="taskNo"></param>
        internal static bool DiscRemoverUnloadComplete(string ip, string taskNo)
        {
            var result = false;
            var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault();
            if (plc != null)
            {
                //1.0 读地址1设备是否动作完成
                var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                {
                    dataNum = 1,
                    host = plc.ip,
                    port = plc.port,
                    addr = plc.readAddr + 1
                });
                if (readRes != null && readRes.errCode == 0)
                {
                    if (readRes.result[0] == 2)
                    {
                        var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                        {
                            host = plc.ip,
                            port = plc.port,
                            addr = plc.writeAddr + 1,
                            data = 3
                        });
                        result = true;
                        CMMLog.Debug("读取设备【参数不为0】,AMS设置小车【动作完成】,等待设备【参数为0】时再改参数通知agv");
                    }
                }
            }
            return result;
        }
 
        #endregion
 
        #endregion
 
        #region   3楼叠盘机满托下线入库流程——需回报WMS任务状态——3楼叠盘下线--已完成(待测试)
 
        /// <summary>
        /// 线程循环查询叠盘机是否需要下满托
        /// </summary>
        /// <param name="plc"></param>
        internal static void StackingLineEmpty(Settings.PlcInfo plc)
        {
            //读plc信号,看有没有叫料请求,判断是否已经有任务
            //没有任务,调WMS生成空托补充任务
            try
            {
                //RegisterReadInPut 读取输入寄存器还是输出寄存器待定,读取通道1的数据
                var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                {
                    dataNum = 2,
                    addr = plc.readAddr + 1,
                    host = plc.ip,
                    port = plc.port
                });
                if (result != null && result.errCode == 0)
                {
                    //1.满托下线
                    if (result.result[0] == 1)
                    {
                        //判断3楼叠盘起点是否有任务占用,没有则生成任务
                        if (ProcessHelper.CheckStartFree(plc.location))
                        {
                            string wmstaskno = "";
                            string traycode = "";
                            string trayType = "";
                            if(plc.deviceType == "25") trayType = result.result[1] == 1 ? "川字托" : "田字托";
                            bool req = WMSHelper.WMSIn(plc.location, "", ref wmstaskno, ref traycode,"","",false, trayType);
                            if (req)
                            {
                                CMMLog.Debug($"调用WMS获取三楼叠盘机生成任务成功!");
                                if (plc.deviceType == "25")
                                {
                                    //记录托盘类型
                                    var trayTypeInfo = MongoDBSingleton.Instance.FindOne<trayTypeTable>(Query.EQ("locCode", plc.location), "trayTypeTable");
                                    if (trayTypeInfo == null)
                                    {
                                        MongoDBSingleton.Instance.Insert<trayTypeTable>(new trayTypeTable
                                        {
                                            locCode = plc.location,
                                            trayType = result.result[1].ToString()
                                        });
                                    }
                                }
                            }
                            else CMMLog.Debug($"调用WMS获取三楼叠盘机生成任务失败!");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CMMLog.Error(ex.Message, ex);
            }
        }
 
        #region   叠盘机交互
 
        /// 小车请求出料    1    AMS写,PLC读    1-请求出料;2-出料进行中;3-动作完成
        /// 设备允许进料    1    PLC写,AMS读    1-允许进料;2-进料进行中;3-动作完成
        /// <summary>
        /// 向叠盘机请求取货
        /// </summary>
        /// <param name="ips"></param>
        internal static void StackingReqUnload(string ip, string taskNo)
        {
 
            var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault();
            if (plc != null)
            {
                var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                {
                    addr = plc.writeAddr + 1,
                    host = plc.ip,
                    port = plc.port,
                    data = 1
                });
 
                var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                {
                    dataNum = 1,
                    host = plc.ip,
                    port = plc.port,
                    addr = plc.readAddr + 1
                });
                if (readRes != null && readRes.errCode == 0)
                {
                    if (readRes.result[0] == 0 || readRes.result[0] == 1)
                    {
                        //不允许进料,ams写入请求出料
                        var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                        {
                            host = plc.ip,
                            port = plc.port,
                            addr = plc.writeAddr + 1,
                            data = 1
                        });
                    }
                    else if (readRes.result[0] == 2)
                    {
                        //设备允许进料
                        //改参数通知agv
                        if (TSHelper.GoToAGV(taskNo, 10, 1))
                        {
                            WorkFlowAction.TrackLog(taskNo, 10, 1013, "success");
                            //改成出料进行中
                            CMMLog.Debug("三楼叠盘机:小车正在取托盘中!");
                            var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                            {
                                host = plc.ip,
                                port = plc.port,
                                addr = plc.writeAddr + 1,
                                data = 2
                            });
                        }
                        else CMMLog.Debug($"三楼叠盘机,1013,TS,10,3,false!");
                    }
                }
            }
 
            #region
            //var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault();
            //if (plc != null)
            //{
            //    var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
            //    {
            //        dataNum = 1,
            //        host = plc.ip,
            //        port = plc.port,
            //        addr = plc.readAddr + 1
            //    });
            //    if (readRes != null && readRes.errCode == 0)
            //    {
            //        if (readRes.result[0] == 0 || readRes.result[0] == 1)
            //        {
            //            //不允许进料,ams写入请求出料
            //            var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
            //            {
            //                host = plc.ip,
            //                port = plc.port,
            //                addr = plc.writeAddr + 1,
            //                data = 1
            //            });
            //        }
            //        else if (readRes.result[0] == 2)
            //        {
            //            //设备允许进料
            //            //改参数通知agv
            //            if (TSHelper.GoToAGV(taskNo, 10, 3))
            //            {
            //                WorkFlowAction.TrackLog(taskNo, 10, 1013, "success");
            //                //改成出料进行中
            //                CMMLog.Debug("三楼叠盘机:小车正在取托盘中!");
            //                var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
            //                {
            //                    host = plc.ip,
            //                    port = plc.port,
            //                    addr = plc.writeAddr + 1,
            //                    data = 2
            //                });
            //            }
            //            else CMMLog.Debug($"三楼叠盘机,1013,TS,10,3,false!");
            //        }
            //    }
            //}
            #endregion
        }
 
        /// <summary>
        /// 小车取货完成离开通知叠盘机
        /// </summary>
        /// <param name="ips"></param>
        /// <param name="taskNo"></param>
        internal static bool StackingComplete(string ip, string taskNo)
        {
            var result = false;
            var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault();
            if (plc != null)
            {
                //1.0 读地址1设备是否动作完成
                var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                {
                    host = plc.ip,
                    port = plc.port,
                    addr = plc.writeAddr + 1,
                    data = 3
                });
                result = true;
                CMMLog.Debug("读取设备【参数不为0】,AMS设置小车【动作完成】,等待设备【参数为0】时再改参数通知agv");
            }
            return result;
        }
 
        #endregion
 
        #endregion
 
        #endregion
 
        #region   跨楼层任务
 
        #region   货物入库流程——满托转运
 
        #region   收到打包机下线信号,判断条件是否允许,选择性下发任务
        /// <summary>
        /// 3楼打包机下线,判断打包机是否由生成打包下线的任务,如果有,自己生成两条任务
        /// </summary>
        /// <param name="plc"></param>
        internal static void CheckPackingLineFull(Settings.PlcInfo plc)
        {
            //读plc信号,看有没有下线请求,判断是否已经有任务
            //没有任务,继续plc,包装下线的物料信号
            //检查打包机通道1是否有出料任务信号,如果有生成取料任务。
            if (CheckStartFree(plc.location))
            {
                try
                {
                    var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                    {
                        dataNum = 1,
                        addr = plc.readAddr + 1,
                        host = plc.ip,
                        port = plc.port
                    });
                    if (result != null && result.errCode == 0)
                    {
                        if (result.result[0] == 1)
                        {
                            //获取MODBUS通道里面存放的时间戳
                            var resultTime = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                            {
                                addr = plc.readAddr + 10,
                                host = plc.ip,
                                port = plc.port,
                                dataNum = 2
                            });
                            if (resultTime.errCode == 0)
                            {
                                string wmstaskno = "";
                                string traycode = "";
                                if (Settings.packChange == "0")
                                {
                                    string timeCuoData = (resultTime.result[0] * 65536 + resultTime.result[1]).ToString();
                                    CMMLog.Info($"获取到的MODBUS时间戳:{timeCuoData}");
                                    Console.WriteLine($"获取到的MODBUS时间戳:{timeCuoData}");
                                    //这里需要调用照相机接口,获取数据存入中间表,等到入库流程完成,将数据传送给WMS
                                    string deviceNo = "";
                                    var timecuo = TimeCuo(timeCuoData, ref deviceNo);
                                    if (timecuo != null)
                                    {
                                        string timeStamp = timecuo.data.First().timeStamp.ToString();
                                        string employeeId = timecuo.data.First().employeeID1.ToString();
                                        // ERP变更-调用ERP接口传输相关数据
                                        if (ERPService.ERPSwitch01 == "1")
                                        {
                                            // string employeeId = ERPService.GetEmployeeId(plc.readAddr + 18, plc.ip, plc.port);
                                            // MongoDBSingleton.Instance.Update<TimeCuoInfoCom>(Query.EQ("timeStamp", int.Parse(timeStamp)), Update.Set("employeeId", employeeId), "TimeCuoInfoCom", UpdateFlags.None);
                                            
                                            // if (ERPService.ERPApiFunc(timeStamp))
                                            //     WMSHelper.WMSIn(plc.location, "time", ref wmstaskno, ref timeStamp, timeStamp, employeeId);
 
                                            WMSHelper.WMSIn(plc.location, "time", ref wmstaskno, ref timeStamp, timeStamp, employeeId);
                                        }
                                        else WMSHelper.WMSIn(plc.location, "time", ref wmstaskno, ref timeStamp, timeStamp);
                                    }
                                    else CMMLog.Info($"时间戳接口返回值为空!");
                                }
                                else if(Settings.packChange == "1")
                                {
                                    bool req = WMSHelper.WMSIn(plc.location, "", ref wmstaskno, ref traycode, "");
                                }
                            }
                            else CMMLog.Info($"MODBUS时间戳数据返回值错误:返回值解析:{JsonConvert.SerializeObject(result)}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    CMMLog.Error(ex.Message);
                }
            }
        }
 
        /// <summary>
        /// 3楼打包机下线,判断打包机是否由生成打包下线的任务(四钴车间)
        /// </summary>
        /// <param name="plc"></param>
        internal static void CheckPackingLineFullTwo(Settings.PlcInfo plc)
        {
            //读plc信号,看有没有下线请求,判断是否已经有任务
            //没有任务,继续plc,包装下线的物料信号
            //检查打包机通道1是否有出料任务信号,如果有生成取料任务。
            if (CheckStartFree(plc.location))
            {
                try
                {
                    var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                    {
                        dataNum = 1,
                        addr = plc.readAddr + 1,
                        host = plc.ip,
                        port = plc.port
                    });
                    if (result != null && result.errCode == 0)
                    {
                        if (result.result[0] == 1)
                        {
                            //获取MODBUS通道里面存放的时间戳
                            var resultTime = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                            {
                                addr = plc.readAddr + 10,
                                host = plc.ip,
                                port = plc.port,
                                dataNum = 170
                            });
                            if (resultTime.errCode == 0)
                            {
                                string wmstaskno = "";
                                TimeCuoInfoComTwo timecuo = TimeCuoTwo(resultTime.result);
                                if (timecuo != null)
                                {
                                    string timeStamp = timecuo.TimeCuo;
                                    string employeeId = timecuo.employeeId1;
                                    // ERP变更-调用ERP接口传输相关数据
                                    WMSHelper.WMSIn(plc.location, "time", ref wmstaskno, ref timeStamp, timeStamp, employeeId,true);
                                }
                                else CMMLog.Info($"时间戳接口返回值为空!");
                            }
                            else CMMLog.Info($"MODBUS时间戳数据返回值错误:返回值解析:{JsonConvert.SerializeObject(result)}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    CMMLog.Error(ex.Message);
                }
            }
        }
        #endregion
 
        #region   打包机出口MODBUS交互(开始取料,取料完成)
 
        /// 小车请求进料    1    AMS写,PLC读    1-AGV到位信号;2-到位后,请求接驳滚动;3-取货完成;
        /// 设备允许出料    1    PLC写,AMS读    1-设备请求出料;2-允许进入;3-确认货物取走,送出物料;
 
        ///打包线下线——小车开始取货
        internal static void PackingLineUnload1012(string ip, string taskNo)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.deviceType == "4").FirstOrDefault();
            if (plc != null)
            {
                try
                {
                    var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                    {
                        dataNum = 1,
                        addr = plc.readAddr + 1,
                        host = plc.ip,
                        port = plc.port
                    });
                    if (readRes != null && readRes.errCode == 0)
                    {
                        if (readRes.result[0] == 1)
                        {
                            var write = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                            {
                                addr = plc.writeAddr + 1,
                                data = 1,
                                host = plc.ip,
                                port = plc.port
                            });
                        }
                        else if (readRes.result[0] == 2)
                        {
                            TSHelper.GoToAGV(taskNo, 10, 1);
                            var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                            {
                                addr = plc.writeAddr + 1,
                                data = 2,
                                host = plc.ip,
                                port = plc.port
                            });
                        }
                    }
                }
                catch (Exception ex)
                {
                    CMMLog.Info(ex.Message);
                }
            }
        }
 
        /// <summary>
        /// 打包线下线——小车取货完成
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="taskNo"></param>
        internal static void PackingLineComplete4(string ip, string taskNo)
        {
            //取货完成后需要读取设备时间戳里面的数据,并将其暂存至中间表等到入库任务完成传输给WMS
            
            var plc = Settings.GetPlcInfo().Where(a => a.deviceType == "4").FirstOrDefault();
            if (plc != null)
            {
                try
                {
                    var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                    {
                        dataNum = 1,
                        addr = plc.readAddr + 1,
                        host = plc.ip,
                        port = plc.port
                    });
                    if (readRes != null && readRes.errCode == 0)
                    {
                        if (readRes.result[0] == 3)
                        {
                            var write = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                            {
                                addr = plc.writeAddr + 1,
                                data = 3,
                                host = plc.ip,
                                port = plc.port
                            });
                        }
                    }
                }
                catch (Exception ex)
                {
                    CMMLog.Info(ex.Message);
                }
            }
        }
        #endregion
 
        #region   电梯安全门对接
        /// <summary>
        /// 入库一段任务请求进入
        /// </summary>
        /// <param name="mst"></param>
        /// <param name="startfloor"></param>
        internal static void RuKuOne1013(TN_I_TASK_MST mst, int startfloor)
        {
            //对电梯的modbus通道进行安全开门对接,
            //var pmInfo = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "8").FirstOrDefault();
            var pmInfo = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.location == mst.Ext3).FirstOrDefault();
            if (pmInfo != null)
            {
                try
                {
                    //进入Agv状态
                    var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                    {
                        addr = pmInfo.writeAddr + 2,
                        data = 1,
                        host = pmInfo.ip,
                        port = pmInfo.port
                    });
                    CMMLog.Debug($"写入设备{pmInfo.location}通道{pmInfo.writeAddr + 2}里面数据为1.");
                    var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                    {
                        dataNum = 1,
                        host = pmInfo.ip,
                        port = pmInfo.port,
                        addr = pmInfo.readAddr + 1
                    });
                    CMMLog.Debug($"读取设备{pmInfo.location}通道{pmInfo.readAddr + 1}里面数据为{JsonConvert.SerializeObject(readRes)}.");
                    if (readRes != null && readRes.errCode == 0)
                    {
                        //判断电梯状态是否是停梯状态,停梯状态则发对应的信号
                        if (readRes.result[0] == 0)
                        {
                            int num = startfloor == 1 ? 1 : startfloor == 2 ? 4 : startfloor == 3 ? 6 : 0;
                            //写入电梯楼层
                            wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                            {
                                addr = pmInfo.writeAddr + 1,
                                data = num,
                                host = pmInfo.ip,
                                port = pmInfo.port
                            });
                            CMMLog.Debug($"写入设备{pmInfo.location}通道{pmInfo.writeAddr + 1}里面数据为{num}.");
                            //读取输入寄存器还是输出寄存器待定,读取通道0的数据
                            var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                            {
                                dataNum = 2,
                                addr = pmInfo.readAddr + 2,
                                host = pmInfo.ip,
                                port = pmInfo.port
                            });
                            CMMLog.Debug($"读取设备{pmInfo.location}通道{pmInfo.readAddr + 2}里面数据为{JsonConvert.SerializeObject(result)}.");
                            if (result != null && result.errCode == 0)
                            {
                                if (result.result[0] == 2 && result.result[1] == num)
                                {
                                    var eleBit = MongoDBSingleton.Instance.FindOne<ElevatorTask>(Query.EQ("taskNo", mst.CN_S_SOURCE_NO), "ElevatorTask");
                                    //改终点为开启的电梯
                                    TSHelper.GoToAGV(mst.CN_S_TASK_NO, 2, StockInstance.Instance.GetAGVCodeForBitCode(eleBit.endBit));
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    CMMLog.Error(ex.Message, ex);
                }
            }
            else CMMLog.Info($"请检查电梯配置中心有没有开启,当前电梯点位:{mst.Ext3}");
        }
 
        /// <summary>
        /// 入库一段任务请求退出
        /// </summary>
        /// <param name="mst"></param>
        /// <param name="endfloor"></param>
        internal static bool RuKuOne6(TN_I_TASK_MST mst, int endfloor)
        {
            bool req = false;
            //对电梯的modbus通道进行安全开门对接,
            var pmInfo = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.location == mst.Ext3).FirstOrDefault();
            if (pmInfo != null)
            {
                try
                {
                    //写入楼层信号和关门信号
                    int floorNum = endfloor == 1 ? 1 : endfloor == 2 ? 4 : endfloor == 3 ? 6 : 0;
                    int[] num = new int[2] { 4, floorNum };
                    var writeRes0 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti
                    {
                        host = pmInfo.ip,
                        addr = pmInfo.writeAddr,
                        data = num,//原先是1,单个写入
                        port = pmInfo.port
                    });
                    CMMLog.Debug($"写入设备{pmInfo.location}通道{pmInfo.writeAddr}里面数据为{JsonConvert.SerializeObject(num)}.");
                    req = true;
                }
                catch (Exception ex)
                {
                    CMMLog.Error(ex.Message, ex);
                }
            }
            else CMMLog.Info($"请检查电梯配置中心有没有开启,当前电梯点位:{mst.Ext3}");
            return req;
        }
 
        /// <summary>
        /// 入库二段任务请求进入
        /// </summary>
        /// <param name="mst"></param>
        /// <param name="startfloor"></param>
        internal static void RuKuOne1012(TN_I_TASK_MST mst, int endfloor)
        {
            //对电梯的modbus通道进行安全开门对接,
            var eleBit = MongoDBSingleton.Instance.FindOne<ElevatorTask>(Query.EQ("taskNo", mst.CN_S_SOURCE_NO), "ElevatorTask");
            if(eleBit == null) CMMLog.Info($"当前任务来源号:{mst.CN_S_SOURCE_NO},请在ElevatorTask表中查询字段taskNo为当前任务来源号");
            var pmInfo = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.location == eleBit.endBit02).FirstOrDefault();
            CMMLog.Info($"二段任务 1312  1  bit:{eleBit.endBit02}  deivicetype:{pmInfo.deviceType}");
            if (pmInfo != null)
            {
                try
                {
                    int num = endfloor == 1 ? 1 : endfloor == 2 ? 4 : endfloor == 3 ? 6 : 0;
 
                    var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                    {
                        dataNum = 3,
                        addr = pmInfo.readAddr + 1,
                        host = pmInfo.ip,
                        port = pmInfo.port
                    });
                    CMMLog.Debug($"读取设备{pmInfo.location}通道{pmInfo.readAddr + 1}里面数据为{JsonConvert.SerializeObject(result)}.");
                    if (result != null && result.errCode == 0)
                    {
                        if (result.result[0] == 0 && result.result[1] == 2 && result.result[2] == num)
                        {
                            TSHelper.GoToAGV(mst.CN_S_TASK_NO, 1, StockInstance.Instance.GetAGVCodeForBitCode(eleBit.endBit02));
                        }
                    }
 
                    #region
                    //var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                    //{
                    //    dataNum = 1,
                    //    addr = pmInfo.readAddr + 5,
                    //    host = pmInfo.ip,
                    //    port = pmInfo.port
                    //});
                    //var result2 = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                    //{
                    //    dataNum = 1,
                    //    addr = pmInfo.readAddr + 6,
                    //    host = pmInfo.ip,
                    //    port = pmInfo.port
                    //});
                    //if (result != null && result2 != null)
                    //{
                    //    if (result.result[0] == 1 && result2.result[0] == endfloor)
                    //    {
                    //        //TSHelper.GoToAGV(mst.CN_S_TASK_NO, 10, 1);
                    //        //改终点为开启的电梯
                    //        TSHelper.GoToAGV(mst.CN_S_TASK_NO, 1, StockInstance.Instance.GetAGVCodeForBitCode(eleBit.endBit02));
                    //    }
                    //}
                    #endregion
 
                }
                catch (Exception ex)
                {
                    CMMLog.Error(ex.Message, ex);
                }
            }
            else CMMLog.Info($"请检查电梯配置中心有没有开启,当前电梯点位:{eleBit.endBit02}");
        }
 
        /// <summary>
        /// 入库二段任务请求退出
        /// </summary>
        /// <param name="mst"></param>
        /// <param name="startfloor"></param>
        internal static bool RuKuOne4(TN_I_TASK_MST mst, int endfloor)
        {
            bool req = false;
            var eleBit = MongoDBSingleton.Instance.FindOne<ElevatorTask>(Query.EQ("taskNo", mst.CN_S_SOURCE_NO), "ElevatorTask");
            if (eleBit == null) CMMLog.Info($"当前任务来源号:{mst.CN_S_SOURCE_NO},请在ElevatorTask表中查询字段taskNo为当前任务来源号");
            var pmInfo = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.location == eleBit.endBit02).FirstOrDefault();
            CMMLog.Info($"二段任务 1312  2  bit:{eleBit.endBit02}  deivicetype:{pmInfo.deviceType}");
            if (pmInfo != null)
            {
                try
                {
                    int[] num = new int[4] { 4, 0,0,0 };
                    var writeRes0 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti
                    {
                        host = pmInfo.ip,
                        addr = pmInfo.writeAddr,
                        data = num,//原先是1,单个写入
                        port = pmInfo.port
                    });
                    CMMLog.Debug($"写入设备{pmInfo.location}通道{pmInfo.writeAddr}里面数据为{JsonConvert.SerializeObject(num)}.");
                    //var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                    //{
                    //    addr = pmInfo.writeAddr + 1,
                    //    data = 3,
                    //    host = pmInfo.ip,
                    //    port = pmInfo.port
                    //});
                    req = true;
                }
                catch (Exception ex)
                {
                    CMMLog.Error(ex.Message, ex);
                }
            }
            else CMMLog.Info($"请检查电梯配置中心有没有开启,当前电梯点位:{eleBit.endBit02}");
            return req;
        }
        #endregion
 
        #endregion
 
        #region   电梯选择以及电梯点位赋值
 
        /// <summary>
        /// 读取MODBUS返回允许使用的电梯点位,如果没有电梯可以使用,返回0
        /// </summary>
        /// <returns></returns>
        internal static string ElevatorTaskBit(int bit)
        {
            Console.WriteLine("进入电梯选择判断!");
            CMMLog.Info("进入电梯选择判断!");
            string location = "";
            CMMLog.Info("满托转运流程,判断三楼两部电梯设备");
            //满托转运流程,判断三楼两部电梯设备(获取到可用电梯之后,需要判断这部电梯的一楼以及三楼是否有任务占用,如果有,则不可用)
            var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "8").FirstOrDefault();
            var plc2 = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "11").FirstOrDefault();
 
            if(bit == 2)
            {
                plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "12").FirstOrDefault();
                plc2 = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "13").FirstOrDefault();
            }
 
            if (plc != null || plc2 != null)
            {
                if (plc != null)
                {
                    CMMLog.Info("进入3楼电梯1判断是否开启");
 
                    var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                    {
                        addr = plc.readAddr,
                        dataNum = 1,//数据个数要增加       addr以及dataNum的具体数据为16个
                        host = plc.ip,
                        port = plc.port
                    });
                    if (result != null && result.result.Length > 0)
                    {
                        if (result.result[0] == 3)
                        {
                            // 判断当前电梯的 一楼以及三楼 均没有任务占用,则可以使用
                            // (三楼电梯不能有任务执行中,一楼电梯取货完成可以使用)
                            var oneFloor = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "12").FirstOrDefault();
                            if (bit == 2)
                            {
                                oneFloor = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "8").FirstOrDefault();
                            }
                            var startTask = MongoDBSingleton.Instance.FindOne<TN_I_TASK_MST>(Query.EQ("Ext3",location), "TN_I_TASK_MST");
                            bool endTaskState = false;
                            var endTask = MongoDBSingleton.Instance.FindOne<TN_I_TASK_MST>(Query.EQ("CN_S_START_BIT", oneFloor.location), "TN_I_TASK_MST");
                            if (endTask != null)
                            {
                                if (WorkFlowAction.ExistsTrackLogs(endTask.CN_S_TASK_NO, 1, 4)) endTaskState = true;
                            }
                            else endTaskState = true;
                            if (startTask == null && endTaskState) location = plc.location;
                        }
                    }
                }
                if (string.IsNullOrEmpty(location) && plc2 != null)
                {
                    CMMLog.Info("进入三楼电梯2判断是否开启");
 
                    var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                    {
                        addr = plc2.readAddr,
                        dataNum = 1,//数据个数要增加       addr以及dataNum的具体数据为16个
                        host = plc2.ip,
                        port = plc2.port
                    });
                    CMMLog.Info($"获取电梯状态:{JsonConvert.SerializeObject(result)}");
                    if (result != null && result.result.Length > 0)
                    {
                        if (result.result[0] == 3)
                        {
                            // 判断当前电梯的 一楼以及三楼 均没有任务占用,则可以使用
                            var oneFloor = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "13").FirstOrDefault();
                            if (bit == 2)
                            {
                                oneFloor = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "11").FirstOrDefault();
                            }
                            var startTask = MongoDBSingleton.Instance.FindOne<TN_I_TASK_MST>(Query.EQ("Ext3", location), "TN_I_TASK_MST");
                            bool endTaskState = false;
                            var endTask = MongoDBSingleton.Instance.FindOne<TN_I_TASK_MST>(Query.EQ("CN_S_START_BIT", oneFloor.location), "TN_I_TASK_MST");
                            if (endTask != null)
                            {
                                if (WorkFlowAction.ExistsTrackLogs(endTask.CN_S_TASK_NO, 1, 4)) endTaskState = true;
                                
                            }
                            else endTaskState = true;
                            if (startTask == null && endTaskState) location = plc2.location;
                        }
                    }
                }
                if (location == "")
                {
                    Console.WriteLine("无电梯开启!");
                    CMMLog.Info("无电梯开启!");
                    location = "0";
                }
            }
            else
            {
                CMMLog.Info("检查配置文件里面两部电梯是否有开启!");
                return "0";
            }
            Console.WriteLine("电梯选择判断结束!");
            CMMLog.Info("电梯选择判断结束!");
            return location;
        }
        /// <summary>
        /// 获取电梯点位赋值
        /// </summary>
        /// <param name="eleBit"></param>
        /// <returns></returns>
        internal static string ElevatorTaskBit02(string eleBit)
        {
            CMMLog.Info("赋值阶段2任务电梯起点");
            string bit = "";
            if (eleBit != "")
            {
                CMMLog.Info("两部电梯,四个配置均需要开启");
                //1号电梯三楼和一楼电梯点位
                var plc = Settings.GetPlcInfo().Where(a => a.deviceType == "8").FirstOrDefault();
                var plc2 = Settings.GetPlcInfo().Where(a => a.deviceType == "12").FirstOrDefault();
                //2号电梯三楼和一楼电梯点位
                var plc3 = Settings.GetPlcInfo().Where(a => a.deviceType == "11").FirstOrDefault();
                var plc4 = Settings.GetPlcInfo().Where(a => a.deviceType == "13").FirstOrDefault();
                if (eleBit == plc.location) bit = plc2.location;
                else if(eleBit == plc2.location) bit = plc.location;
                else if(eleBit == plc3.location) bit = plc4.location;
                else if(eleBit == plc4.location) bit = plc3.location;
            }
            else CMMLog.Info("EleBit is  null!");
            CMMLog.Info("获取电梯二段任务电梯点位:"+bit);
            return bit;
        }
        internal static void ElevatorTaskFloor(int startFloor,string bit)
        {
            CMMLog.Info($"终点安全对接:电梯卸货:呼叫楼层:{startFloor},当前电梯点位:{bit}");
 
            var pmInfo = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.location == bit).FirstOrDefault();
            if (pmInfo != null)
            {
                int[] num = new int[4] { startFloor, 0, 1, 1 };
                var writeRes0 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti
                {
                    host = pmInfo.ip,
                    addr = pmInfo.writeAddr,
                    data = num,//原先是1,单个写入
                    port = pmInfo.port
                });
            }
            else CMMLog.Info($"终点安全对接:电梯卸货:未当前电梯点位的配置数据:{bit}");
 
 
 
            //var write = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
            //{
            //    addr = pmInfo.writeAddr+2,
            //    data = 1,
            //    host = pmInfo.ip,
            //    port = pmInfo.port
            //});
            //var write0 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
            //{
            //    addr = pmInfo.writeAddr,
            //    data = startFloor,
            //    host = pmInfo.ip,
            //    port = pmInfo.port
            //});
        }
        #endregion
 
        #endregion
 
        public static DateTime TenMin = DateTime.MinValue;
        /// <summary>
        /// 每隔十分钟往所有开启的打包下线口MODBUS通道里面写小车电量
        /// 其实地址26,连续往里面写入四个值
        /// </summary>
        internal static void WriteBattery()
        {
            CMMLog.Info("写入小车电量:进入写入电量信息流程!");
            //Console.WriteLine("写入小车电量:进入写入电量信息流程!");
            if(TenMin == DateTime.MinValue) TenMin = DateTime.Now;
            else if (DateTime.Now.Subtract(TenMin).TotalMinutes >= 10)
            {
                Console.WriteLine("写入小车电量:写入所有开启的打包下线口MODBUS通道电量值,并重置时间为当前时间");
                CMMLog.Info("写入小车电量:写入所有开启的打包下线口MODBUS通道电量值,并重置时间为当前时间");
                //写入所有开启的打包下线口MODBUS通道电量值,并重置时间为当前时间
                //获取所有电量值
                int[] num = ProcessHelper.GetBatteryHang();
                CMMLog.Info($"写入小车电量:写入数据:{JsonConvert.SerializeObject(num)}");
                Console.WriteLine($"写入小车电量:写入数据:{JsonConvert.SerializeObject(num)}");
                var PlcInfo04 = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "4").FirstOrDefault();//plcInfo
                if (PlcInfo04 != null)
                {
                    var wirteall = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti
                    {
                        addr = PlcInfo04.writeAddr + 5,
                        host = PlcInfo04.ip,
                        data = num,
                        port = PlcInfo04.port
                    });
                }
                TenMin = DateTime.Now;
            }
        }
        #region   数据处理业务
 
        /// <summary>
        /// 获取设备的包装物料信息(并写入中间表)
        /// </summary>
        /// <param name="info"></param>
        /// <param name="result"></param>
        /// <param name="noPack">true : MES交互方式  false : 无MES交互方式</param>
        private static void GetMachineData(int[] result, ref string loc, bool noPack = true)
        {
            CMMLog.Info($"数据处理流程:进入数据处理流程!");
            
            string trayCode = GetTrayCode(result.Take(3).ToArray());//托盘码
            string location = GetTrayCode(result.Skip(3).Take(1).ToArray());//设备货位编码
            loc = location;
            if(trayCode != null && location != null)
            {
                CMMLog.Info($"数据处理流程:trayCode:{trayCode},location:{location}");
                var json = "";
                if (!noPack)
                {
                    CMMLog.Info($"数据处理流程:包装机无MES情况下,自动生成以当前包装机号{location}为首的数据");
                    var task = MongoDBSingleton.Instance.FindOne<MachineInfo>(Query.EQ("machineNo", location), "MachineInfo");
                    if(task == null)
                    {
                        MongoDBSingleton.Instance.Insert<MachineInfo>(new MachineInfo
                        {
                            machineNo = location
                        });
                        MongoDBSingleton.Instance.Insert<MachineInfoTwo>(new MachineInfoTwo
                        {
                            machineNo = location,
                            trayCode = trayCode
                        });
                    }
                    else CMMLog.Info($"数据处理流程:包装机无MES情况下,模拟MES数据,中间表<MachineInfo>中已有当前包装机号{location}为首的数据");
                }
                var info = MongoDBSingleton.Instance.FindOne<MachineInfo>(Query.EQ("machineNo", location), "MachineInfo");
 
                if (info != null)
                {
                    CMMLog.Info($"数据处理流程:info:{info}");
                    #region   获取设备通道信息
 
                    #region   转换后
                    info.location = location;
                    info.trayCode = trayCode;
                    //获取重量 叠包等其它信息
                    info.productWeight = Convert.ToInt32(Completion(result[4]) + Completion(result[5]), 2).ToString();
                    // ERP变更:根据最新信号协议  trayCodeWeight 变更为 【员工编号】通道
                    info.trayCodeWeight = Convert.ToInt32(Completion(result[6]) + Completion(result[7]), 2).ToString();
                    info.oneTrayWeight = Convert.ToInt32(Completion(result[8]) + Completion(result[9]), 2).ToString();
                    info.addWeight = Convert.ToInt32(Completion(result[10]) + Completion(result[11]), 2).ToString();
                    info.packNg = 1;//不需要此参数 result[12]
                    info.secondNg = Convert.ToInt32(Completion(result[13]), 2);
                    info.addState = Convert.ToInt32(Completion(result[14]), 2);
                    if (info.addState == 0) CMMLog.Error($"数据处理流程:获取MODBUS信息异常:是否叠包值为0.");
                    info.packageCode = Convert.ToInt32(Completion(result[15]), 2).ToString();
 
                    CMMLog.Info($"数据处理流程:获取MODBUS转换后的数据信息:location:{info.location},trayCode:{info.trayCode},productWeight:{info.productWeight}" +
                        $",trayCodeWeight:{info.trayCodeWeight},oneTrayWeight:{info.oneTrayWeight},addWeight:{info.addWeight}" +
                        $",packNg:{info.packNg},secondNg:{info.secondNg},addState:{info.addState},packageCode:{info.packageCode}");
 
                    #endregion
 
                    #region   转换前
                    for (int i = 0; i <= 15; i++)
                    {
                        info.machinedown[i] = result[i];
                    }
                    //将数组数据接受转换为JSON数据并在后续方法判断中存入中间表
                    json = JsonConvert.SerializeObject(info.machinedown);
                    CMMLog.Info($"数据处理流程:获取MODBUS转换前的数据信息:{json}");
                    #endregion
 
                    #endregion
                }
                else CMMLog.Info($"数据处理流程:请查询<MachineInfo>表中machineNo字段是否为当前包装机号,并且trayCode是否为0");
 
                var infoPack = MongoDBSingleton.Instance.FindOne<MachineInfo>(Query.And(Query.EQ("machineNo", location), Query.EQ("trayCode", "0")), "MachineInfo");
 
                if (infoPack != null)
                {
                    //包装机下线至复称,读取包装机平台数据
                    CMMLog.Info("数据处理流程:进入info判断01");
                    //AMS包装机下线首先调用MES,之后获取新的设备数据存入中间表
                    //根据托盘号获取指定的通道的JSON数据,将其转换为数组类型——写入通道数据时会使用
                    //var arr = JsonConvert.DeserializeObject<List<int>>(json);
                    if(!noPack)
                    {
                        CMMLog.Info($"数据处理流程:包装机无MES情况下,获取以往MES传输的数据,并原样写入原先MES字段中。");
 
                        //info.lotNo = RemoveNull(GetTrayCode(result.Skip(20).Take(30).ToArray()).Trim().ToString());
                        //info.productType = RemoveNull(GetTrayCode(result.Skip(50).Take(10).ToArray()).Trim().ToString());
                        //info.materialCode = RemoveNull(GetTrayCode(result.Skip(60).Take(20).ToArray()).Trim().ToString());
 
                        info.lotNo = RemoveNull(GetTrayCode(result.Skip(20).Take(20).ToArray()).Trim().ToString());
                        // info.productType = RemoveNull(GetTrayCode(result.Skip(40).Take(10).ToArray()).Trim().ToString());
                        info.productType = "";
                        info.materialCode = RemoveNull(GetTrayCode(result.Skip(50).Take(20).ToArray()).Trim().ToString());
 
                        CMMLog.Info($"数据处理流程:包装机无MES情况下,获取以往MES传输的数据,新获得数据,lotNo:{info.lotNo},productType:{info.productType},materialCode:{info.materialCode}");
                        
 
                        //注意:这里赋值中间表参数请对照信息交互表具体通道值对应
                        UpdateBuilder update = Update.Set("palletLayers", info.secondNg.ToString()).Set("overlappingLayers", info.addState.ToString()).Set("bagNo", info.packageCode).
                            Set("lotNo", info.lotNo).Set("productType", info.productType).Set("materialCode", info.materialCode);
                        MongoDBSingleton.Instance.Update<MachineInfo>(Query.EQ("machineNo", location), update, "MachineInfo", UpdateFlags.None);
                        MongoDBSingleton.Instance.Update<MachineInfoTwo>(Query.EQ("trayCode", trayCode), update, "MachineInfoTwo", UpdateFlags.None);
                    }
                    //在中间表中找到刚刚插入的MES数据(目前还没有读取并写入设备数据)
                    var query1 = Query.And(Query.EQ("machineNo", location), Query.EQ("trayCode", "0"));
                    UpdateBuilder updateBuider = Update.Set("location", info.location).
                        Set("trayCode", info.trayCode).Set("productWeight", info.productWeight).
                        Set("trayCodeWeight", info.trayCodeWeight).Set("oneTrayWeight", info.oneTrayWeight).
                        Set("addWeight", info.addWeight).Set("packNg", info.packNg).Set("secondNg", info.secondNg).Set("addState", info.addState).
                        Set("packageCode", info.packageCode).Set("jsonData", json).Set("modify", DateTime.Now);
                    MongoDBSingleton.Instance.Update<MachineInfo>(query1, updateBuider, "MachineInfo", UpdateFlags.None);
                    MongoDBSingleton.Instance.Update<MachineInfoTwo>(Query.EQ("trayCode", trayCode), updateBuider, "MachineInfoTwo", UpdateFlags.None);
                    CMMLog.Info($"数据处理流程:更新MachineInfo中间表刚刚插入的MES数据!设备号为:{location},托盘号为:{trayCode}");
                }
                else
                {
                    //复称入缓存架,读取复称平台数据
                    CMMLog.Info("数据处理流程:进入info判断02");
                    //读取除包装机外其他设备的信息,更新包装机下线时的数据
                    var query2 = Query.And(Query.EQ("machineNo", location), Query.EQ("trayCode", trayCode));
                    var info2 = MongoDBSingleton.Instance.FindOne<MachineInfo>(query2, "MachineInfo");
                    if (info2 != null)
                    {
                        UpdateBuilder updateBuider = Update.Set("location", info.location).
                        Set("trayCode", info.trayCode).Set("productWeight", info.productWeight).
                        Set("trayCodeWeight", info.trayCodeWeight).Set("oneTrayWeight", info.oneTrayWeight).
                        Set("addWeight", info.addWeight).Set("packNg", info.packNg).Set("secondNg", info.secondNg).Set("addState", info.addState).
                        Set("packageCode", info.packageCode).Set("jsonData", json).Set("modify", DateTime.Now);
                        MongoDBSingleton.Instance.Update<MachineInfo>(query2, updateBuider, "MachineInfo", UpdateFlags.None);
                        MongoDBSingleton.Instance.Update<MachineInfoTwo>(Query.EQ("trayCode", trayCode), updateBuider, "MachineInfoTwo", UpdateFlags.None);
                        CMMLog.Info($"数据处理流程:更新MachineInfo中间表其他设备的数据!设备号为:{location},托盘号为:{trayCode}");
                    }
                    else CMMLog.Info($"数据处理流程:无法在MachineInfo中间表中找到当前设备编号的数据!当前设备编号为:{location},托盘码为:{trayCode}");
                }
            }
            else CMMLog.Error("数据处理流程:数据流程处理托盘号或包装机号为空值!");
 
            CMMLog.Info($"数据处理流程:数据处理流程结束!");
        }
 
        /// <summary>
        /// 获取设备的包装物料信息(并写入中间表)
        /// </summary>
        /// <param name="info"></param>
        /// <param name="result"></param>
        /// <param name="noPack">true : MES交互方式  false : 无MES交互方式</param>
        private static void GetMachineDataTetracobalt(int[] result, ref string timeStamp, bool noPack = true)
        {
            CMMLog.Info($"数据处理流程:进入数据处理流程!");
            
            string empCode = RemoveNull(GetTrayCode(result.Take(5).ToArray()));//员工编码
            string trayCode = GetTrayCode(result.Skip(5).Take(3).ToArray());//托盘码
            trayCode = "0";
            string location = GetTrayCode(result.Skip(8).Take(1).ToArray());//设备货位编码
            if(trayCode != null && location != null)
            {
                CMMLog.Info($"数据处理流程:trayCode:{trayCode},location:{location},empCode:{empCode}");
                var json = "";
                if (!noPack)
                {
                    CMMLog.Info($"数据处理流程:包装机无MES情况下,自动生成以当前包装机号{location}为首的数据");
                    var task = MongoDBSingleton.Instance.FindOne<MachineInfoTetracobalt>(Query.EQ("machineNo", location), "MachineInfo");
                    if(task == null)
                    {
                        MongoDBSingleton.Instance.Insert<MachineInfoTetracobalt>(new MachineInfoTetracobalt
                        {
                            machineNo = location
                        });
                        MongoDBSingleton.Instance.Insert<MachineInfoTwoTetracobalt>(new MachineInfoTwoTetracobalt
                        {
                            machineNo = location,
                            trayCode = trayCode
                        });
                    }
                    else CMMLog.Info($"数据处理流程:包装机无MES情况下,模拟MES数据,中间表<MachineInfoTetracobalt>中已有当前包装机号{location}为首的数据");
                }
                var info = MongoDBSingleton.Instance.FindOne<MachineInfoTetracobalt>(Query.EQ("machineNo", location), "MachineInfoTetracobalt");
 
                if (info != null)
                {
                    CMMLog.Info($"数据处理流程:info:{info}");
                    #region   获取设备通道信息
 
                    #region   转换后
                    info.empCode = empCode;
                    info.location = location;
                    info.trayCode = trayCode;
                    //获取重量 叠包等其它信息
                    info.productWeight = Convert.ToInt32(Completion(result[9]) + Completion(result[10]), 2).ToString();
                    // ERP变更:根据最新信号协议  trayCodeWeight 变更为 【员工编号】通道
                    info.trayCodeWeight = Convert.ToInt32(Completion(result[11]) + Completion(result[12]), 2).ToString();
                    info.oneTrayWeight = Convert.ToInt32(Completion(result[13]) + Completion(result[14]), 2).ToString();
                    info.trayType = Convert.ToInt32(Completion(result[17]), 2).ToString();
                    info.addState = Convert.ToInt32(Completion(result[18]), 2);
                    if (info.addState == 0) CMMLog.Error($"数据处理流程:获取MODBUS信息异常:是否叠包值为0.");
                    info.packageCode = Convert.ToInt32(Completion(result[19]), 2).ToString();
                    info.productTime = Convert.ToInt32(Completion(result[20]) + Completion(result[21]), 2).ToString();
 
                    //info.addWeight = Convert.ToInt32(Completion(result[10]) + Completion(result[11]), 2).ToString();
                    //info.packNg = 1;//不需要此参数 result[12]
                    //info.secondNg = Convert.ToInt32(Completion(result[13]), 2);
 
                    CMMLog.Info($"数据处理流程:获取MODBUS转换后的数据信息:empCoe:{info.empCode},location:{info.location},trayCode:{info.trayCode},productWeight:{info.productWeight}" +
                        $",trayCodeWeight:{info.trayCodeWeight},oneTrayWeight:{info.oneTrayWeight},trayType:{info.trayType}" +
                        $",productTime:{info.productTime},addState:{info.addState},packageCode:{info.packageCode}");
 
                    #endregion
 
                    #region   转换前
                    for (int i = 0; i <= 21; i++)
                    {
                        info.machinedown[i] = result[i];
                    }
                    //将数组数据接受转换为JSON数据并在后续方法判断中存入中间表
                    json = JsonConvert.SerializeObject(info.machinedown);
                    CMMLog.Info($"数据处理流程:获取MODBUS转换前的数据信息:{json}");
                    #endregion
 
                    #endregion
                }
                else CMMLog.Info($"数据处理流程:请查询<MachineInfoTetracobalt>表中machineNo字段是否为当前包装机号,并且trayCode是否为0");
 
                var infoPack = MongoDBSingleton.Instance.FindOne<MachineInfoTetracobalt>(Query.And(Query.EQ("machineNo", location), Query.EQ("trayCode", "0")), "MachineInfoTetracobalt");
 
                if (infoPack != null)
                {
                    //包装机下线至复称,读取包装机平台数据
                    CMMLog.Info("数据处理流程:进入info判断01");
                    //AMS包装机下线首先调用MES,之后获取新的设备数据存入中间表
                    //根据托盘号获取指定的通道的JSON数据,将其转换为数组类型——写入通道数据时会使用
                    //var arr = JsonConvert.DeserializeObject<List<int>>(json);
                    if(!noPack)
                    {
                        CMMLog.Info($"数据处理流程:包装机无MES情况下,获取以往MES传输的数据,并原样写入原先MES字段中。");
 
                        //info.lotNo = RemoveNull(GetTrayCode(result.Skip(20).Take(30).ToArray()).Trim().ToString());
                        //info.productType = RemoveNull(GetTrayCode(result.Skip(50).Take(10).ToArray()).Trim().ToString());
                        //info.materialCode = RemoveNull(GetTrayCode(result.Skip(60).Take(20).ToArray()).Trim().ToString());
 
                        info.lotNo = RemoveNull(GetTrayCode(result.Skip(35).Take(20).ToArray()).Trim().ToString());
                        info.productType = RemoveNull(GetTrayCode(result.Skip(55).Take(10).ToArray()).Trim().ToString());
                        info.materialCode = RemoveNull(GetTrayCode(result.Skip(65).Take(15).ToArray()).Trim().ToString());
                        info.materialName = RemoveNull(GetTrayCode(result.Skip(80).Take(20).ToArray()).Trim().ToString());
                        info.measurementUnit = RemoveNull(GetTrayCode(result.Skip(100).Take(5).ToArray()).Trim().ToString());
 
                        CMMLog.Info($"数据处理流程:包装机无MES情况下,获取以往MES传输的数据,新获得数据,lotNo:{info.lotNo},productType:{info.productType},materialCode:{info.materialCode},materialName:{info.materialName},measurementUnit:{info.measurementUnit}");
                        
 
                        //注意:这里赋值中间表参数请对照信息交互表具体通道值对应
                        UpdateBuilder update = Update.Set("palletLayers", info.secondNg.ToString()).Set("overlappingLayers", info.addState.ToString()).Set("bagNo", info.packageCode).
                            Set("lotNo", info.lotNo).Set("productType", info.productType).Set("materialCode", info.materialCode).Set("materialName", info.materialName).Set("measurementUnit", info.measurementUnit);
                        MongoDBSingleton.Instance.Update<MachineInfoTetracobalt>(Query.EQ("machineNo", location), update, "MachineInfoTetracobalt", UpdateFlags.None);
                        MongoDBSingleton.Instance.Update<MachineInfoTwoTetracobalt>(Query.EQ("trayCode", trayCode), update, "MachineInfoTwoTetracobalt", UpdateFlags.None);
                    }
                    //在中间表中找到刚刚插入的MES数据(目前还没有读取并写入设备数据)
 
                    timeStamp = ProcessHelper.GetTimeStamp(31, 1, 1);
                    trayCode = "VW" + timeStamp;
 
                    var query1 = Query.And(Query.EQ("machineNo", location), Query.EQ("trayCode", "0"));
                    UpdateBuilder updateBuider = Update.Set("empCode", info.empCode).Set("location", info.location).
                        Set("trayCode", trayCode).Set("productWeight", info.productWeight).
                        Set("trayCodeWeight", info.trayCodeWeight).Set("oneTrayWeight", info.oneTrayWeight).Set("trayType", info.trayType).Set("addState", info.addState).
                        Set("addWeight", info.addWeight).Set("packNg", info.packNg).Set("secondNg", info.secondNg).Set("productTime", info.productTime).Set("packageCode", info.packageCode).
                        Set("jsonData", json).Set("modify", DateTime.Now);
                    MongoDBSingleton.Instance.Update<MachineInfoTetracobalt>(query1, updateBuider, "MachineInfoTetracobalt", UpdateFlags.None);
                    MongoDBSingleton.Instance.Update<MachineInfoTwoTetracobalt>(query1, updateBuider, "MachineInfoTwoTetracobalt", UpdateFlags.None);
                    CMMLog.Info($"数据处理流程:更新MachineInfoTetracobalt中间表刚刚插入的MES数据!设备号为:{location},托盘号为:{trayCode}");
                }
                else
                {
                    //复称入缓存架,读取复称平台数据
                    CMMLog.Info("数据处理流程:进入info判断02");
                    //读取除包装机外其他设备的信息,更新包装机下线时的数据
                    var query2 = Query.And(Query.EQ("machineNo", location), Query.EQ("trayCode", trayCode));
                    var info2 = MongoDBSingleton.Instance.FindOne<MachineInfoTetracobalt>(query2, "MachineInfo");
                    if (info2 != null)
                    {
                        UpdateBuilder updateBuider = Update.Set("empCode", info.empCode).Set("location", info.location).
                        Set("trayCode", info.trayCode).Set("productWeight", info.productWeight).
                        Set("trayCodeWeight", info.trayCodeWeight).Set("oneTrayWeight", info.oneTrayWeight).
                        Set("addWeight", info.addWeight).Set("packNg", info.packNg).Set("secondNg", info.secondNg).Set("productTime", info.productTime).
                        Set("jsonData", json).Set("modify", DateTime.Now);
                        MongoDBSingleton.Instance.Update<MachineInfoTetracobalt>(query2, updateBuider, "MachineInfoTetracobalt", UpdateFlags.None);
                        MongoDBSingleton.Instance.Update<MachineInfoTwoTetracobalt>(Query.EQ("trayCode", trayCode), updateBuider, "MachineInfoTwoTetracobalt", UpdateFlags.None);
                        CMMLog.Info($"数据处理流程:更新MachineInfoTetracobalt中间表其他设备的数据!设备号为:{location},托盘号为:{trayCode}");
                    }
                    else CMMLog.Info($"数据处理流程:无法在MachineInfoTetracobalt中间表中找到当前设备编号的数据!当前设备编号为:{location},托盘码为:{trayCode}");
                }
            }
            else CMMLog.Error("数据处理流程:数据流程处理托盘号或包装机号为空值!");
 
            CMMLog.Info($"数据处理流程:数据处理流程结束!");
        }
 
        /// <summary>
        /// 获取复称点状态信息
        /// </summary>
        /// <returns></returns>
        public static SecondWeighState GetSecondWeighState(string location)
        {
            //传入当前设备编号,并将传入的设备编号放入复称中间表的来源变量中
            string PlcBit02 = Settings.GetPlcInfo().Where(a => a.deviceType == "2").FirstOrDefault().location;
            var query = Query.EQ("location", PlcBit02);
            var model = MongoDBSingleton.Instance.FindOne<SecondWeighState>(query, "SecondWeighState");
            if (model == null)
            {
                MongoDBSingleton.Instance.Insert<SecondWeighState>(new SecondWeighState { location = PlcBit02, from = location,full = 0,ng = 1 });
                CMMLog.Debug($"判断复称状态信息服务,复称中间表数据为空,开始插入默认值!");
            }
            else MongoDBSingleton.Instance.Update<SecondWeighState>(query, Update.Set("from", location), "SecondWeighState", UpdateFlags.None);
            return model;
        }        
        
        /// <summary>
        /// 获取复称点状态信息(四钴)
        /// </summary>
        /// <returns></returns>
        public static SecondWeighState GetSecondWeighStateTetracobalt(string location)
        {
            //传入当前设备编号,并将传入的设备编号放入复称中间表的来源变量中
            string PlcBit02 = Settings.GetPlcInfo().Where(a => a.deviceType == "22").FirstOrDefault().location;
            var query = Query.EQ("location", PlcBit02);
            var model = MongoDBSingleton.Instance.FindOne<SecondWeighState>(query, "SecondWeighState");
            if (model == null)
            {
                MongoDBSingleton.Instance.Insert<SecondWeighState>(new SecondWeighState { location = PlcBit02, from = location,full = 0,ng = 1 });
                CMMLog.Debug($"判断复称状态信息服务,复称中间表数据为空,开始插入默认值!");
            }
            else MongoDBSingleton.Instance.Update<SecondWeighState>(query, Update.Set("from", location), "SecondWeighState", UpdateFlags.None);
            return model;
        }
 
        /// <summary>
        /// 复称平台数据
        /// </summary>
        public class SecondWeighState
        {
            public ObjectId _id { get; set; }
            public string location { get; set; }
            /// <summary>
            /// 0未知 1合格 2不合格
            /// </summary>
            public int ng { get; set; }
            /// <summary>
            /// 复称平台货从哪里来的
            /// </summary>
            public string from { get; set; }
            /// <summary>
            /// 批次号
            /// </summary>
            public string batchNo { get; set; }
            /// <summary>
            /// 包装机下线重量
            /// </summary>
            public int weight { get; set; }
            /// <summary>
            /// 1 满 0 空
            /// </summary>
            public int full { get; set; }
            public string mesInfo { get; set; }
        }
 
        /// <summary>
        /// 获取复称点位位置
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public static string GetSecondWeighBit()
        {
            //改成读settings
            var plc = Settings.GetPlcInfo().Where(a => a.deviceType == "2").FirstOrDefault();
            return plc.location;
        }
 
        /// <summary>
        /// 将设备通道里面读取的16位short转成ascii字符串
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string GetTrayCode(int[] data)
        {
            StringBuilder sb = new StringBuilder();
            data.ToList().ForEach(a => {
                var bytes = BitConverter.GetBytes((short)a).Reverse().ToArray();
                sb.Append(Encoding.ASCII.GetString(bytes));
            });
            return sb.ToString();
        }
 
        /// <summary>
        /// 十进制转换为2进制(自动补全16位)
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Completion(int data)
        {
            var result = Convert.ToString(data, 2).PadLeft(16, '0');
            return result;
        }
 
        /// <summary>
        /// ascii转10进制(通过16进制中转)
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string AsciiToTen(string data)
        {
            byte[] oneSixbad = System.Text.ASCIIEncoding.Default.GetBytes(data);
            StringBuilder oneSixsb = new StringBuilder();
            foreach (byte b in oneSixbad)
            {
                oneSixsb.Append(b.ToString("x"));
            }
            int oneSixA2 = Convert.ToInt32(oneSixsb.ToString(), 16);
            return oneSixA2.ToString();
        }
 
        #region   叠盘机批量写入数据
 
        #region   产品型号分离处理方法
        //public static int[] DiePan(WMSInfo machine)
        //{
        //    //偏移量 +11 通道范围: 40311 ~ 40370
        //    CMMLog.Info($"进入DiePan数据处理方法");
        //    //10~19通道
        //    int[] num = new int[60];
        //    int[] num3 = new int[40];
        //    int a = 0;//托盘码处理
        //    for (int b = 0; b <= 2; b++)
        //    {
        //        num[b] = int.Parse(AsciiToTen(machine.trayCode.Substring(a, 2)));
        //        a = a + 2;
        //    }
        //    num[3] = int.Parse(AsciiToTen(machine.location));//包装机号
        //    for (int b = 4; b <= 5; b++)
        //    {
        //        int k = b % 2 == 0 ? 0 : 16;
        //        num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.productWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
        //    }
        //    for (int b = 6; b <= 7; b++)
        //    {
        //        int k = b % 2 == 0 ? 0 : 16;
        //        num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.trayCodeWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
        //    }
        //    for (int b = 8; b <= 9; b++)
        //    {
        //        int k = b % 2 == 0 ? 0 : 16;
        //        num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.oneTrayWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
        //    }
        //    for (int b = 10; b <= 12; b++) num[b] = 0;// 叠包后实际重量 && 复称结果 默认:0
        //    num[13] = string.IsNullOrEmpty(machine.palletLayers) ? 0 : int.Parse(machine.palletLayers);//是否需要叠托盘
        //    num[14] = machine.addState;//是否需要叠包
        //    num[15] = int.Parse(machine.packageCode);//袋号
        //    //时间戳处理
        //    string timeStamp = ProcessHelper.GetTimeStamp(32, 1, 1);
        //    for (int b = 16; b <= 17; b++)
        //    {
        //        int k = b % 2 == 0 ? 0 : 16;
        //        num[b] = Convert.ToInt32(Convert.ToString(int.Parse(timeStamp), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
        //    }
        //    for (int b = 18; b <= 19; b++) num[b] = 0;//预留的两个通道 默认:0
 
        //    //19 = 30  31 ~ 60 
        //    //30~59通道  60~70通道
        //    string pcHead = "";
        //    string cpHead = "";
        //    #region   31~60
 
        //    string data = machine.itemPCode;
        //    CMMLog.Info($"31~60data1_1:{data}");
        //    if (data.Length % 2 != 0)
        //    {
        //        data = "0" + machine.itemPCode;
        //        pcHead = "" + machine.itemPCode.Substring(0, 1);
        //    }
        //    CMMLog.Info($"31~60data1_2:{data}");
        //    int x = data.Length / 2;
        //    int aa = 0;
        //    for (int i = 0; i <= x - 1; i++)
        //    {
        //        num3[i] = int.Parse(AsciiToTen(data.Substring(aa, 2)));
        //        aa = aa + 2;
        //    }
        //    for (int m = x; m <= 29; m++) num3[m] = 0;//将产品批次号无数据的通道全部置为 0
        //    CMMLog.Info($"itemPCode:" + JsonConvert.SerializeObject(num3));
        //    #endregion
 
        //    #region   61~70
 
        //    string data2 = machine.itemCode;
        //    CMMLog.Info($"61~70data2_1:{data2}");
        //    if (data2.Length % 2 != 0)
        //    {
        //        data2 = "0" + machine.itemCode;
        //        cpHead = "" + machine.itemCode.Substring(0, 1);
        //    }
        //    CMMLog.Info($"61~70data2_2:{data2}");
        //    int y = data2.Length / 2;
        //    CMMLog.Info($"{y}");
        //    int aaa = 0;
        //    //4 2 32 9 41 
        //    for (int i = 30; i <= y + 29; i++)
        //    {
        //        num3[i] = int.Parse(AsciiToTen(data2.Substring(aaa, 2)));
        //        CMMLog.Info(JsonConvert.SerializeObject(num3[i]));
        //        aaa = aaa + 2;
        //    }
        //    int mm = y + 30;
        //    CMMLog.Info($"mm:{mm}");
        //    for (int mmm = mm; mmm <= 39; mmm++)
        //    {
        //        num3[mmm] = 0;
        //        CMMLog.Info(JsonConvert.SerializeObject(num3[mmm]));
        //    }
        //    CMMLog.Info($"itemPCode+itemCode:{JsonConvert.SerializeObject(num3)}");
        //    #endregion
 
        //    for (int i = 20; i <= 60; i++) num[i] = num3[i - 20];
 
        //    // 如果 产品批次号 或者 产品型号 位数 为 奇数,则将提前取出的 首字符 重新转码 写入 其所对应通道区域的 首通道
        //    if (pcHead != "" || cpHead != "")
        //    {
        //        if (pcHead != "") num[20] = int.Parse(AsciiToTen(pcHead));
        //        if (cpHead != "") num[50] = int.Parse(AsciiToTen(cpHead));
        //        CMMLog.Info($"产品型号或批次号数量为奇数,特将首位取出单独处理,以下为批次号和产品型号的十进制数值{num[20]},{num[50]}");
        //    }
        //    CMMLog.Info($"DiePan数据处理方法完毕");
 
        //    return num;
        //}
        #endregion
 
        public static int[] DiePan(WMSInfo machine)
        {
            //偏移量 +11 通道范围: 40311 ~ 40370
            CMMLog.Info($"叠盘机数据处理.");
            //10~19通道
            int[] num = new int[50];//ERP变更:变更后通道 50  变更前:60
 
            #region   0~19 => 11~30
 
            int a = 0;//托盘码处理
            for (int b = 0; b <= 2; b++)
            {
                num[b] = int.Parse(AsciiToTen(machine.trayCode.Substring(a, 2)));
                a = a + 2;
            }
            num[3] = int.Parse(AsciiToTen(machine.location));//包装机号
            for (int b = 4; b <= 5; b++)
            {
                int k = b % 2 == 0 ? 0 : 16;
                num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.productWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
            }
            for (int b = 6; b <= 7; b++)
            {
                int k = b % 2 == 0 ? 0 : 16;
                num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.trayCodeWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
            }
            for (int b = 8; b <= 9; b++)
            {
                int k = b % 2 == 0 ? 0 : 16;
                num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.oneTrayWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
            }
            for (int b = 10; b <= 12; b++) num[b] = 0;// 叠包后实际重量 && 复称结果 默认:0
            num[13] = string.IsNullOrEmpty(machine.palletLayers) ? 0 : int.Parse(machine.palletLayers);//是否需要叠托盘
            num[14] = machine.addState;//是否需要叠包
            num[15] = int.Parse(machine.packageCode);//袋号
            //时间戳处理
            string timeStamp = ProcessHelper.GetTimeStamp(32, 1, 1);
            for (int b = 16; b <= 17; b++)
            {
                int k = b % 2 == 0 ? 0 : 16;
                num[b] = Convert.ToInt32(Convert.ToString(int.Parse(timeStamp), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
            }
            for (int b = 18; b <= 19; b++) num[b] = 0;//预留的两个通道 默认:0
 
            #endregion
 
            string pcHead = "";
            string cpHead = "";
 
            #region  20~39 => 31~50 产品批次号
 
            string data = machine.itemPCode;
            CMMLog.Info($"31~50data1_1:{data}");
            if (data.Length % 2 != 0)
            {
                data = "0" + machine.itemPCode;
                pcHead = "" + machine.itemPCode.Substring(0, 1);
            }
            CMMLog.Info($"31~50data1_2:{data}");
            //字符串长度 / 2 = 写入通道数量(两位一转) 列: 3L0050 写入三个通道
            int maxLength = 19 + (data.Length / 2);// data.Length / 2 至少为 1
            int aa = 0;
            for (int i = 20; i <= maxLength; i++)
            {
                num[i] = int.Parse(AsciiToTen(data.Substring(aa, 2)));
                aa = aa + 2;
            }
            for (int x = maxLength + 1; x <= 39; x++) num[x] = 0;//将产品批次号无数据的通道全部置为 0
            #endregion
 
            #region   40~49 => 51~60 产品型号
 
            string data2 = machine.itemCode;
            CMMLog.Info($"51~60data2_1:{data2}");
            if (data2.Length % 2 != 0)
            {
                data2 = "0" + machine.itemCode;
                cpHead = "" + machine.itemCode.Substring(0, 1);
            }
            CMMLog.Info($"51~60data2_2:{data2}");
            maxLength = 39 + (data2.Length / 2);
            int aaa = 0;
            for (int i = 40; i <= maxLength; i++)
            {
                num[i] = int.Parse(AsciiToTen(data2.Substring(aaa, 2)));
                aaa = aaa + 2;
            }
            for (int x = maxLength + 1; x <= 49; x++) num[x] = 0;//将产品型号无数据的通道全部置为0
            #endregion
 
            // 如果 产品批次号 或者 产品型号 位数 为 奇数,则将提前取出的 首字符 重新转码 写入 其所对应通道区域的 首通道
            if (pcHead != "") num[20] = int.Parse(AsciiToTen(pcHead));
            if (cpHead != "") num[40] = int.Parse(AsciiToTen(cpHead));
            if (pcHead != "" || cpHead != "") CMMLog.Info($"产品型号或批次号数量为奇数,特将首位取出单独处理,以下为批次号和产品型号的十进制数值{num[20]},{num[50]}");
            
            CMMLog.Info($"叠盘机数据处理完毕:{JsonConvert.SerializeObject(num)}");
 
            return num;
        }
 
        public static int[] DiePanTwo(WMSInfo machine, MachineInfoTwo machineInfoTwo)
        {
            var ErpItemInfo = MongoDBSingleton.Instance.FindOne<ERPItemTable>(Query.EQ("item_code", machineInfoTwo.materialCode), "ERPItemTable");
            CMMLog.Info($"3楼缓存架入叠托:收到信号5,查询MachineInfoTwo表信息:{JsonConvert.SerializeObject(ErpItemInfo)}。");
 
            //偏移量 +11 通道范围: 40311 ~ 40390
            CMMLog.Info($"叠盘机数据处理.");
            //10~19通道
            int[] num = new int[90];//总长度:80
 
            #region   0~19 => 11~30
 
            int a = 0;//托盘码处理
            for (int b = 0; b <= 2; b++)
            {
                num[b] = int.Parse(AsciiToTen(machine.trayCode.Substring(a, 2)));
                a = a + 2;
            }
            num[3] = int.Parse(AsciiToTen(machine.location));//包装机号
            for (int b = 4; b <= 5; b++)
            {
                int k = b % 2 == 0 ? 0 : 16;
                num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.productWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
            }
            for (int b = 6; b <= 7; b++)
            {
                int k = b % 2 == 0 ? 0 : 16;
                num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.trayCodeWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
            }
            for (int b = 8; b <= 9; b++)
            {
                int k = b % 2 == 0 ? 0 : 16;
                num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.oneTrayWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
            }
            for (int b = 10; b <= 11; b++) num[b] = 0;// 叠包后实际重量 && 复称结果 默认:0
            num[12] = 1;
            num[13] = 2;//是否需要叠托盘 // string.IsNullOrEmpty(machine.palletLayers) ? 0 : int.Parse(machine.palletLayers)
            num[14] = machine.addState;//是否需要叠包
            num[15] = int.Parse(machine.packageCode);//袋号
            //时间戳处理
            string timeStamp = ProcessHelper.GetTimeStamp(32, 1, 1);
            for (int b = 16; b <= 17; b++)
            {
                int k = b % 2 == 0 ? 0 : 16;
                num[b] = Convert.ToInt32(Convert.ToString(int.Parse(timeStamp), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
            }
            for (int b = 18; b <= 19; b++) num[b] = 0;//预留的两个通道 默认:0
 
            #endregion
 
            string pcHead = "";
            string cpHead = "";
            string mtHead = "";// 设备的真实物料编码
 
            #region  20~39 => 31~50 产品批次号
 
            string data = machineInfoTwo.lotNo;
            CMMLog.Info($"31~50data1_1:{data}");
            if (data.Length % 2 != 0)
            {
                data = "0" + machineInfoTwo.lotNo;
                pcHead = "" + machineInfoTwo.lotNo.Substring(0, 1);
            }
            CMMLog.Info($"31~50data1_2:{data}");
            //字符串长度 / 2 = 写入通道数量(两位一转) 列: 3L0050 写入三个通道
            int maxLength = 19 + (data.Length / 2);// data.Length / 2 至少为 1
            int aa = 0;
            for (int i = 20; i <= maxLength; i++)
            {
                num[i] = int.Parse(AsciiToTen(data.Substring(aa, 2)));
                aa = aa + 2;
            }
            for (int x = maxLength + 1; x <= 39; x++) num[x] = 0;//将产品批次号无数据的通道全部置为 0
            #endregion
 
            #region   40~49 => 51~60 产品型号
 
            ERPService.HandleItemInfoChina(machine.itemCode, 40, 49, num);// 中文处理 70~84 81~95
 
            //string data2 = machine.itemCode;
            //CMMLog.Info($"51~50data2_1:{data2}");
            //if (data2.Length % 2 != 0)
            //{
            //    data2 = "0" + machine.itemCode;
            //    cpHead = "" + machine.itemCode.Substring(0, 1);
            //}
            //CMMLog.Info($"51~60data2_2:{data2}");
            //maxLength = 39 + (data2.Length / 2);
            //int aaa = 0;
            //for (int i = 40; i <= maxLength; i++)
            //{
            //    num[i] = int.Parse(AsciiToTen(data2.Substring(aaa, 2)));
            //    aaa = aaa + 2;
            //}
            //for (int x = maxLength + 1; x <= 49; x++) num[x] = 0;//将产品型号无数据的通道全部置为0
            #endregion
 
            #region   50~69 => 61~80 物料编码
 
            string data3 = machineInfoTwo.materialCode;
            CMMLog.Info($"61~80data3_1:{data3}");
            if (data3.Length % 2 != 0)
            {
                //data3 = "0" + machineInfoTwo.materialCode;
                mtHead = "" + machineInfoTwo.materialCode.Substring(0, 1);
            }
            CMMLog.Info($"61~80data3_2:{data3}");
            maxLength = 49 + (data3.Length / 2);
            int aaaa = 0;
            for (int i = 50; i <= maxLength; i++)
            {
                num[i] = int.Parse(AsciiToTen(data3.Substring(aaaa, 2)));
                aaaa = aaaa + 2;
            }
            for (int x = maxLength + 1; x <= 69; x++) num[x] = 0;//将产品型号无数据的通道全部置为0
            #endregion
 
            ERPService.HandleItemInfoChina(ErpItemInfo.item_name, 70, 84, num);// 中文处理 70~84 81~95
            ERPService.HandleItemInfo(ErpItemInfo.item_uom, 85, 89, num);// 85~89 96~100
 
            // 如果 产品批次号 或者 产品型号 位数 为 奇数,则将提前取出的 首字符 重新转码 写入 其所对应通道区域的 首通道
            if (pcHead != "") num[20] = int.Parse(AsciiToTen(pcHead));
            if (cpHead != "") num[40] = int.Parse(AsciiToTen(cpHead));
            if (mtHead != "") num[50] = int.Parse(AsciiToTen(mtHead));
            if (pcHead != "" || cpHead != "") CMMLog.Info($"产品型号或批次号数量为奇数,特将首位取出单独处理,以下为批次号和产品型号的十进制数值{num[20]},{num[50]}");
 
            CMMLog.Info($"叠盘机数据处理完毕:{JsonConvert.SerializeObject(num)}");
 
            return num;
        }
 
 
        public static int[] DiePanThree(WMSInfo machine, MachineInfoTwo machineInfoTwo)
        {
            //偏移量 +11 通道范围: 40311 ~ 40390
            CMMLog.Info($"叠盘机数据处理.");
            //10~19通道
            int[] num = new int[80];//总长度:80
 
            #region   0~19 => 11~30
 
            int a = 0;//托盘码处理
            for (int b = 0; b <= 2; b++)
            {
                num[b] = int.Parse(AsciiToTen(machine.trayCode.Substring(a, 2)));
                a = a + 2;
            }
            num[3] = int.Parse(AsciiToTen(machine.location));//包装机号
            for (int b = 4; b <= 5; b++)
            {
                int k = b % 2 == 0 ? 0 : 16;
                num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.productWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
            }
            for (int b = 6; b <= 7; b++)
            {
                int k = b % 2 == 0 ? 0 : 16;
                num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.trayCodeWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
            }
            for (int b = 8; b <= 9; b++)
            {
                int k = b % 2 == 0 ? 0 : 16;
                num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.oneTrayWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
            }
            for (int b = 10; b <= 12; b++) num[b] = 0;// 叠包后实际重量 && 复称结果 默认:0
            num[13] = string.IsNullOrEmpty(machine.palletLayers) ? 0 : int.Parse(machine.palletLayers);//是否需要叠托盘
            num[14] = machine.addState;//是否需要叠包
            num[15] = int.Parse(machine.packageCode);//袋号
            //时间戳处理
            string timeStamp = ProcessHelper.GetTimeStamp(32, 1, 1);
            for (int b = 16; b <= 17; b++)
            {
                int k = b % 2 == 0 ? 0 : 16;
                num[b] = Convert.ToInt32(Convert.ToString(int.Parse(timeStamp), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2);
            }
            for (int b = 18; b <= 19; b++) num[b] = 0;//预留的两个通道 默认:0
 
            #endregion
 
            string pcHead = "";
            string cpHead = "";
            string mtHead = "";// 设备的真实物料编码
 
            #region  20~49 => 31~60 产品批次号
 
            string data = machine.itemPCode;
            CMMLog.Info($"31~60data1_1:{data}");
            if (data.Length % 2 != 0)
            {
                data = "0" + machine.itemPCode;
                pcHead = "" + machine.itemPCode.Substring(0, 1);
            }
            CMMLog.Info($"31~60data1_2:{data}");
            //字符串长度 / 2 = 写入通道数量(两位一转) 列: 3L0050 写入三个通道
            int maxLength = 19 + (data.Length / 2);// data.Length / 2 至少为 1
            int aa = 0;
            for (int i = 20; i <= maxLength; i++)
            {
                num[i] = int.Parse(AsciiToTen(data.Substring(aa, 2)));
                aa = aa + 2;
            }
            for (int x = maxLength + 1; x <= 49; x++) num[x] = 0;//将产品批次号无数据的通道全部置为 0
            #endregion
 
            #region   50~59 => 61~70 产品型号
 
            string data2 = machine.itemCode;
            CMMLog.Info($"61~70data2_1:{data2}");
            if (data2.Length % 2 != 0)
            {
                data2 = "0" + machine.itemCode;
                cpHead = "" + machine.itemCode.Substring(0, 1);
            }
            CMMLog.Info($"61~70data2_2:{data2}");
            maxLength = 49 + (data2.Length / 2);
            int aaa = 0;
            for (int i = 50; i <= maxLength; i++)
            {
                num[i] = int.Parse(AsciiToTen(data2.Substring(aaa, 2)));
                aaa = aaa + 2;
            }
            for (int x = maxLength + 1; x <= 59; x++) num[x] = 0;//将产品型号无数据的通道全部置为0
            #endregion
 
            #region   60~79 => 71~90 产品型号
 
            string data3 = machineInfoTwo.materialCode;
            CMMLog.Info($"71~90data3_1:{data3}");
            if (data3.Length % 2 != 0)
            {
                data3 = "0" + machineInfoTwo.materialCode;
                mtHead = "" + machineInfoTwo.materialCode.Substring(0, 1);
            }
            CMMLog.Info($"71~90data3_2:{data3}");
            maxLength = 59 + (data3.Length / 2);
            int aaaa = 0;
            for (int i = 60; i <= maxLength; i++)
            {
                num[i] = int.Parse(AsciiToTen(data3.Substring(aaaa, 2)));
                aaaa = aaaa + 2;
            }
            for (int x = maxLength + 1; x <= 79; x++) num[x] = 0;//将产品型号无数据的通道全部置为0
            #endregion
 
            // 如果 产品批次号 或者 产品型号 位数 为 奇数,则将提前取出的 首字符 重新转码 写入 其所对应通道区域的 首通道
            if (pcHead != "") num[20] = int.Parse(AsciiToTen(pcHead));
            if (cpHead != "") num[50] = int.Parse(AsciiToTen(cpHead));
            if (mtHead != "") num[60] = int.Parse(AsciiToTen(mtHead));
            if (pcHead != "" || cpHead != "") CMMLog.Info($"产品型号或批次号数量为奇数,特将首位取出单独处理,以下为批次号和产品型号的十进制数值{num[20]},{num[50]}");
 
            CMMLog.Info($"叠盘机数据处理完毕:{JsonConvert.SerializeObject(num)}");
 
            return num;
        }
        #endregion
 
        #endregion
 
        public class Z_ChaiPanEmptyOrFull
        {
            public ObjectId _id { get; set; }
            /// <summary>
            /// 拆盘点位
            /// </summary>
            public string ChaiPanBit { get; set; }
            /// <summary>
            /// 拆盘点位空满状态--空|满
            /// </summary>
            public string ChaiPanState { get; set; }
        }
 
 
        public class ActionControlModel
        {
            public ObjectId _id { get; set; }
            /// <summary>
            /// 功能码-- 1-生成复称平台入缓存架任务
            /// </summary>
            public string actionNo { get; set; } = "0";
            /// <summary>
            /// 1-ip
            /// </summary>
            public string ext1 { get; set; } = "";
            /// <summary>
            /// 1-CN_S_BATCH_NO
            /// </summary>
            public string ext2 { get; set; } = "";
            public string ext3 { get; set; } = "";
            public int ext4 { get; set; } = 0;
            public string machince { get; set; } = "";
        }
 
        internal static void changeSignal()
        {
            CMMLog.Info("车辆状态处理:执行开始!");
            try
            {
                //查询小车状态表,写入小车坐标和方向 并写入中间表
                var agvState = MongoDBSingleton.Instance.FindAll<AGV_STATE>();
                if (agvState.Count > 0)
                {
                    agvState.ForEach(a =>
                    {
                        var stateInfo = Settings.GetAGVStateList().Where(it => it.AgvNo == a.forkliftNo).FirstOrDefault();
                        if (stateInfo != null)
                        {
                            int[] num = new int[6];//总长度:6
                            int divisor = 65536; // 除数
                            int xPos = int.Parse(a.xPos) * 10;
                            int yPos = int.Parse(a.yPos) * 10;
 
                            int xquotient = xPos / divisor; // 计算商数
                            int xremainder = xPos % divisor; // 计算余数
                            num[0] = xquotient;
                            num[1] = xremainder;
 
                            int yquotient = yPos / divisor; // 计算商数
                            int yremainder = yPos % divisor; // 计算余数
                            num[2] = yquotient;
                            num[3] = yremainder;
 
                            #region
                            //int m = 0;//托盘码处理
                            //for (int b = 0; b <= 1; b++)
                            //{
                            //    num[b] = int.Parse(AsciiToTen(a.xPos.Substring(m, 2)));
                            //    m = m + 2;
                            //}
                            //m = 0;
                            //for (int b = 2; b <= 3; b++)
                            //{
                            //    num[b] = int.Parse(AsciiToTen(a.yPos.Substring(m, 2)));
                            //    m = m + 2;
                            //}
                            #endregion
 
                            num[4] = string.IsNullOrEmpty(a.Degree) ? 0 : int.Parse(a.Degree);
                            //num[4] = int.Parse(a.Degree);
                            num[5] = a.errCode != "0" || a.errCode2 != "0" || a.faildCode != "0" ? 2 : 0;
 
                            //if (a.errCode != "0" || a.errCode2 != "0" || a.faildCode != "0") num[5] = 2;
 
                            CMMLog.Info($"车辆状态处理:写入车辆数据:车号:{a.forkliftNo},ip:{stateInfo.ip},port:{stateInfo.port},data:{JsonConvert.SerializeObject(num)}");
                            var wirteal = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti
                            {
                                addr = stateInfo.writeAddr,
                                host = stateInfo.ip,
                                port = stateInfo.port,
                                data = num
                            });
                        }
                        else CMMLog.Info($"车辆状态处理:小车{a.forkliftNo}未配置");
                    });
                }
                else CMMLog.Info("车辆状态处理:AGV_STATE 车辆状态表未获取到车辆状态数据!");
            }
            catch(Exception ex)
            {
                CMMLog.Info($"车辆状态处理-异常:{ex.Message}!");
            }
            
            CMMLog.Info("车辆状态处理:执行结束!");
            Thread.Sleep(1000);
        }
 
        /// <summary>
        /// 返料站台送物料
        /// </summary>
        /// <param name="device"></param>
        internal static void LiftAction(Settings.PlcInfo plc, string taskNo, bool result)
        {
            if(plc.device == "24")
            {
                if (result)
                {
                    //返料站台任务
                    //ASM写入通道0小车动作,1-取料
                    var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                    {
                        host = plc.ip,
                        port = plc.port,
                        addr = plc.writeAddr,
                        data = 1
                    });
                    if (writeRes.errCode == 0)
                    {
                        //小车请求进入安全门交互
                        //小车请求进料,并且查询设备是否允许AGV进入
                        //读地址1设备是否  2-可以进入
                        var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                        {
                            dataNum = 1,
                            host = plc.ip,
                            addr = plc.readAddr,
                            port = plc.port
                        });
                        CMMLog.Debug($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}.");
                        Console.WriteLine($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}.");
                        if (readRes != null && readRes.errCode == 0)
                        {
                            if (readRes.result[0] == 2)
                            {
                                //设备允许进入,改参数通知AGV
                                WorkFlowAction.TrackLog(taskNo, 10, 1013, "success");
                                TSHelper.GoToAGV(taskNo, 10, 1);
                            }
                        }
                        else CMMLog.Debug($"入返料站台:1013,readRes:{readRes},readRes.errCode:{readRes.errCode}");
                    }
                }
                else
                {
                    var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                    {
                        host = plc.ip,
                        port = plc.port,
                        addr = plc.writeAddr,
                        data = 3
                    });
                }
            }
        }
 
        /// <summary>
        /// 3楼包装机取料至复称平台流程——任务开始前,判断包装机有没有下线信号,并判断当前复称平台是否NG以及有没有任务在进行中(四钴)
        /// </summary>
        /// <param name="a"></param>
        internal static void CheckPackingMachineTetracobalt(Settings.PlcInfo pmInfo)
        {
            //var count = MongoDBSingleton.Instance.FindOne<>//occupy
            var count = MongoDBSingleton.Instance.FindOne<MachineInfoTetracobalt>(Query.EQ("occupy", "1"), "MachineInfoTetracobalt");
 
            //检查包装机通道0是否有下料信号,如果有生成下料任务。线程循环读取,无需设置循环。——通道0参数为1
            if (count == null)
            {
                // var secondInfo = MongoDBSingleton.Instance.FindOne<secondResult>(Query.EQ("Bit", pmInfo.location), "secondResult");
                // if(secondInfo == null || secondInfo.Status == "OK")
                // {
                if (PickUpStartFree(pmInfo.location) && PickUpEndFree(pmInfo.location))
                {
                    try
                    {
                        var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                        {
                            dataNum = 1,
                            addr = pmInfo.readAddr,
                            host = pmInfo.ip,
                            port = pmInfo.port
                        });
                        if (result != null && result.errCode == 0)
                        {
                            CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:读取包装机通道号为:{pmInfo.readAddr}里面的值为{result.result[0]}");
                            //Console.WriteLine($"包装下线流程:读取包装机通道号为:{pmInfo.readAddr}里面的值为{result.result[0]}");
                            if (result != null && result.errCode == 0 && result.result.Count() > 0)
                            {
                                //1:取料
                                if (result.result[0] == 1)
                                {
                                    string PlcBit02 = Settings.GetPlcInfo().Where(a => a.deviceType == "22").FirstOrDefault().location;
                                    //包装下线任务,终点为复称位置(这里判断包装机起点,复称起点终点均无任务,则推送任务)
                                    if (ProcessHelper.PickUpStartFree(pmInfo.location) && ProcessHelper.CheckEndFree(PlcBit02) && ProcessHelper.PickUpStartFree(PlcBit02))
                                    {
                                        //判断复称位置,没有货并且不为NG(ng参数0,1,2——0,不确定(包装机刚开始工作时)1,ok 2,ng)
                                        var state = GetSecondWeighStateTetracobalt(pmInfo.location);
                                        CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:判断复称位置空满状态为:{state.full},ng状态为:{state.ng}");
                                        if (state.full == 0)
                                        {
                                            if (Settings.mesOpen == "1")
                                            {
                                                //可以生成任务,调mes接口获取生产信息       2022.7.28   变更  MES可能需要1~2年才能上线,先把MES功能注销
                                                CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:获取MES之后进入info判断!");
                                                var trayCode = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                                                {
                                                    addr = pmInfo.readAddr + 5,
                                                    dataNum = 105,////ERP变更:90  原先:80
                                                    host = pmInfo.ip,
                                                    port = pmInfo.port
                                                });
                                                if (trayCode.errCode == 0 && trayCode.result.Length == 105)
                                                {
                                                    //获取托盘码等信息  读取通道 11、12、13的数据作为托盘码   读取其它通道 重量 叠包等信息 所有数据存入MachineInfo表
                                                    string timeStamp = "";
                                                    GetMachineDataTetracobalt(trayCode.result, ref timeStamp, false);
                                                    var tray = MongoDBSingleton.Instance.FindOne<MachineInfoTetracobalt>(Query.EQ("machineNo", pmInfo.location), "MachineInfoTetracobalt");
                                                    if (tray != null)
                                                    {
                                                        CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:MachineInfoTetracobalt数据插入成功!");
                                                        bool IsContLaterCode = true;
                                                        if (ERPService.ERPSwitch01 == "1")
                                                        {
                                                            string employeeId = "G" + tray.empCode.PadLeft(7, '0');
                                                            CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:员工编码:{employeeId}");
                                                            // 判断当前【员工编号】通道信息读出来的员工编号是否已经存在于我们的员工信息表-ERPEmployeeTable(查询字段-employee_id)
                                                            var erpEmployeeInfo = MongoDBSingleton.Instance.FindOne<ERPEmployeeTable>(Query.EQ("employee_id", employeeId), "ERPEmployeeTable");
                                                            var erpItemInfo = MongoDBSingleton.Instance.FindOne<ERPItemTable>(Query.EQ("item_code", tray.materialCode), "ERPItemTable");
                                                            if (string.IsNullOrEmpty(tray.empCode) || string.IsNullOrEmpty(tray.materialCode) || erpEmployeeInfo == null || erpEmployeeInfo == null)
                                                            {
                                                                CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:人员表或者物料表未找到该数据,员工编号:{employeeId},物料编码:{tray.materialCode}!");
                                                                IsContLaterCode = false;
                                                            }
                                                            else CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:员工编码查询成功");
                                                        }
 
                                                        if (IsContLaterCode)
                                                        {
                                                            //检索员工信息成功
                                                            var wirteall01 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                                                            {
                                                                addr = pmInfo.writeAddr,
                                                                host = pmInfo.ip,
                                                                port = pmInfo.port,
                                                                data = 4
                                                            });
 
                                                            //string timeStamp = ProcessHelper.GetTimeStamp(31, 1, 1);
                                                            //tray.trayCode = "VW" + timeStamp;
                                                            //MongoDBSingleton.Instance.Update<MachineInfoTetracobalt>(Query.EQ("_id", tray._id), Update.Set("trayCode", tray.trayCode), UpdateFlags.None);
                                                            //MongoDBSingleton.Instance.Update<MachineInfoTwoTetracobalt>(Query.EQ("_id", tray._id), Update.Set("trayCode", tray.trayCode), UpdateFlags.None);
                                                            CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:托盘码:{tray.trayCode}");
                                                            if (tray.addState == 0)
                                                            {
                                                                MongoDBSingleton.Instance.Remove<MachineInfoTetracobalt>(Query.EQ("machineNo", pmInfo.location), "MachineInfoTetracobalt", RemoveFlags.None);
                                                                if (ERPService.ERPSwitch01 == "0") MongoDBSingleton.Instance.Remove<MachineInfoTetracobalt>(Query.EQ("machineNo", pmInfo.location), "MachineInfoTwo", RemoveFlags.None);
                                                                CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:叠托层数为0,不允许生成任务");
                                                            }
                                                            else if (tray.trayCode != "0" && !string.IsNullOrEmpty(tray.trayCode))
                                                            {
                                                                HHAmsExecuteResult req = AMSHelper.CreateTask(DateTime.Now.Ticks.ToString(), pmInfo.location, PlcBit02, "3楼包装取料(四钴)", 0, tray.trayCode, timeStamp);
                                                                if (req.success)
                                                                {
                                                                    string weight = (double.Parse(tray.oneTrayWeight) / 100).ToString();
                                                                    ERPService.packageInfo(tray.machineNo, tray.trayCode, tray.lotNo, weight);
 
                                                                    //记录托盘类型
                                                                    var trayTypeInfo = MongoDBSingleton.Instance.FindOne<trayTypeTable>(Query.EQ("locCode", pmInfo.location), "trayTypeTable");
                                                                    if(trayTypeInfo == null)
                                                                    {
                                                                        MongoDBSingleton.Instance.Insert<trayTypeTable>(new trayTypeTable
                                                                        {
                                                                            locCode = pmInfo.location,
                                                                            trayType = tray.trayType
                                                                        });
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        else
                                                        {
                                                            CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:员工编码查询失败");
                                                            //检索员工信息失败写入对方通道值1 读取地址+3 
                                                            var wirteall01 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                                                            {
                                                                addr = pmInfo.readAddr + 3,
                                                                host = pmInfo.ip,
                                                                port = pmInfo.port,
                                                                data = 1
                                                            });
                                                        }
                                                    }
                                                    else CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:tray==null!");
                                                }
                                                else CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:3楼包装至复称获取MODBUS值错误!trayCode.errCode:{trayCode.errCode},trayCode.result.Length:{trayCode.result.Length}");
                                            }
                                        }
                                        else
                                        {
                                            var task = MongoDBSingleton.Instance.FindOne<TN_I_TASK_MST>(Query.Or(Query.EQ("CN_S_START_BIT", PlcBit02), Query.EQ("CN_S_END_BIT", PlcBit02)), "TN_I_TASK_MST");
                                            if (task != null) CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:检查复称中间表是否为空,不为空则检查其数据full以及ng是否为0和1!任务号为:{task.CN_S_TASK_NO}");
                                            else CMMLog.Debug("(四钴车间)包装下线流程-{pmInfo.location}:检查复称中间表是否为空,不为空则检查其数据full以及ng是否为0和1!");
                                        }
                                    }
                                    else CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:包装机起点或者复称起点终点有任务在执行中!包装机点位:{pmInfo.location},复称点位:{PlcBit02}");
                                }
                                else CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:包装机通道0里面的数据不为1!result为:{JsonConvert.SerializeObject(result)}");
                            }
                            else CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:未读取到包装机通道0里面的数据!!!result:{JsonConvert.SerializeObject(result)}");
                        }
                        else CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:未读取到包装机通道0里面的数据!!!result:{JsonConvert.SerializeObject(result)}");
                    }
                    catch (Exception ex)
                    {
                        CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:" + ex.Message);
                    }
                }
                else
                {
                    //var taskNo = MongoDBSingleton.Instance.FindOne<TN_I_TASK_MST>(Query.Or(Query.EQ("CN_S_START_BIT", pmInfo.location), Query.EQ("CN_S_END_BIT", pmInfo.location)), "TN_I_TASK_MST");
                    //string task = "", startbit = "", endbit = "";
                    //if (taskNo != null)
                    //{
                    //    task = taskNo.CN_S_TASK_NO;
                    //    startbit = taskNo.CN_S_START_BIT;
                    //    endbit = taskNo.CN_S_END_BIT;
                    //}
                    CMMLog.Debug($"当前包装机起点终点有任务正在执行,请确认当前包装机的任务是否取货卸货完成!包装机货位编码:{pmInfo.location}");
                }
                // }
                // else CMMLog.Info($"当前包装机出现故障,无法生成任务。包装机号:{pmInfo.location}");
            }
            else CMMLog.Debug($"当前MachineInfoTetracobalt中间表已存在数据:{JsonConvert.SerializeObject(count)}");
        }
 
 
        static object PickUpBlankCompleteLock = new object();
        internal static void PickUpBlankCompleteTetracobalt(string PlcBitCache01, string Extend = "")
        {
            lock (PickUpBlankCompleteLock)
            {
                CMMLog.Info("包装线补空流程任务处理");
                try
                {
                    if (Extend != "")
                    {
                        CMMLog.Info($"当前周转托盘位:{Extend}");
                        int addr = 0;
                        int qun = 0;
                        //修改中间表的值,若中间表的数量为0,则调用解绑方法
                        var bzEmp = MongoDBSingleton.Instance.FindOne<BZEmptyPoint>(Query.EQ("Bit", Extend), "BZEmptyPoint");
                        if (bzEmp != null)
                        {
                            CMMLog.Info($"当前货位的数量:{bzEmp.Quantity}");
                            if (bzEmp.Quantity == 1)
                            {
                                CMMLog.Info($"调用解绑接口");
                                //解绑
                                if (WMSHelper.WMSEmptyUnbind(Extend))
                                {
                                    //删除中间表数据
                                    MongoDBSingleton.Instance.Remove<BZEmptyPoint>(Query.EQ("Bit", Extend), RemoveFlags.Single);
                                }
                            }
                            else
                            {
                                var quantity = bzEmp.Quantity - 1;
                                var updateBuider = Update.Set("Quantity", quantity);
                                MongoDBSingleton.Instance.Update<BZEmptyPoint>(Query.EQ("Bit", Extend), updateBuider, UpdateFlags.None);
                                CMMLog.Info($"减去当前周转托盘位数量之后,数量为:{quantity}");
                                qun = quantity;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    CMMLog.Error($"PickUpBlankComplete Error:" + ex.Message);
                }
            }
        }
 
        #region 包装线补空任务
        internal static void PackingLine(string ip, string taskNo)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.ip == ip).FirstOrDefault();
            //ASM写入小车动作,1:上料复称;2:上砝码教称;3:取复称NG托盘,4:取砝码到包装机
            int[] num = new int[3] { 3, 0, 1 };
            var writeRes0 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti
            {
                host = ip,
                addr = plc.writeAddr,
                data = num,//原先是1,单个写入
                port = plc.port
            });
            CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 2}里面数据为{JsonConvert.SerializeObject(num)}.");
            //Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr + 2}里面数据为1.");
            ///小车和复称位对接
            //小车请求进料,并且查询设备是否允许AGV进入
            var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
            {
                dataNum = 1,
                host = plc.ip,
                addr = plc.readAddr + 2,
                port = plc.port
            });
            CMMLog.Debug($"读取设备{plc.location}通道{plc.readAddr + 2}里面数据为{readRes.result[0]}.");
            //Console.WriteLine($"读取设备{plc.location}通道{plc.readAddr + 2}里面数据为{readRes.result[0]}.");
            if (readRes != null && readRes.errCode == 0)
            {
                if (readRes.result[0] == 2)
                {
                    var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                    {
                        host = ip,
                        addr = plc.writeAddr + 2,
                        data = 2,
                        port = plc.port
                    });
                    CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 2}里面数据为2.");
                    //Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr + 2}里面数据为2.");
 
                    WorkFlowAction.TrackLog(taskNo, 10, 1013, "success");
                    TSHelper.GoToAGV(taskNo, 10, 3);
                }
            }
        }
 
        internal static void PackingLineComplete(string ip)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.ip == ip).FirstOrDefault();
            if (plc != null)
            {
                //写入包装机--安全门关门指令
                var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                {
                    host = ip,
                    addr = plc.writeAddr + 2,
                    data = 3,
                    port = plc.port
                });
                CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 2}里面数据为3.");
            }
            else CMMLog.Debug($"包装线空托上线,3,ip=null!");
        }
 
        internal static void PickUpBlankTwo(Settings.PlcInfo pmInfo)
        {
            //四钴车间 从地堆缓存位获取空托
            if (ProcessHelper.PickUpEndFree(pmInfo.location))
            {
                CMMLog.Info($"包装机补空托流程:查询包装机:{pmInfo.location}");
                //读取输入寄存器还是输出寄存器待定,读取通道0的数据
                var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                {
                    dataNum = 1,
                    addr = pmInfo.readAddr,
                    host = pmInfo.ip,
                    port = pmInfo.port
                });
 
                CMMLog.Info($"包装机补空托流程:判断包装机通道{pmInfo.readAddr}值为{JsonConvert.SerializeObject(result)},ip:{pmInfo.ip}");
                if (result != null && result.errCode == 0)
                {
                    //3:人工叫空托盘(川字)  4:人工叫空托盘(田字)
                    if (result.result[0] == 3 || result.result[0] == 4)
                    {
                        //包装线补空流程
                        //判断空托缓存点(5 对 2)是否有空托,有空托判断是否有任务,有任务则判断另一个点位
 
                        var locInfo = Settings.GetAGVLocationList().Where(b => b.machineLoc.Contains(pmInfo.location) && b.Enabel == 1).First();
                        if (locInfo != null)
                        {
                            CMMLog.Info($"包装机点位:{pmInfo.location}");
                            foreach (var a in locInfo.ddLoc)
                            {
                                CMMLog.Info($"周转托盘位:{a}");
                                var ddInfo = Settings.GetDDSiteList().Where(b => b.ddLoc == a && b.Enable == 1).FirstOrDefault();
                                if(ddInfo != null)
                                {
                                    if(ddInfo.trayType == result.result[0] - 2)
                                    {
                                        if (ProcessHelper.PickUpEndFree(pmInfo.location) && ProcessHelper.PickUpStartFree(a))
                                        {
                                            var empInfo = MongoDBSingleton.Instance.FindOne<BZEmptyPoint>(Query.EQ("Bit", a), "BZEmptyPoint");
                                            if (empInfo != null && empInfo.Quantity > 0)
                                            {
                                                CMMLog.Info($"包装线补空任务,包装机点位:{pmInfo.location},周转托盘位:{a},周转托盘位数量:{empInfo.Quantity}");
                                                HHAmsExecuteResult amsResult = AMSHelper.CreateTask(DateTime.Now.Ticks.ToString(), a, pmInfo.location, "包装线补空", 0, "");
                                                if (amsResult.success)
                                                {
                                                    CMMLog.Info($"包装线补空任务生成成功");
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            CMMLog.Info("配置文件未配置包装机空托缓存点");
                        }
                    }
                    //流程未对接好,是否这样使用 待定
                    //if (result.result[0] == 4)
                    //{
                    //    //调用wms出库接口
                    //    var tasks = MongoDBSingleton.Instance.Find<TN_I_TASK_MST>(Query.EQ("CN_S_END_BIT", pmInfo.location), "TN_I_TASK_MST");
                    //    //判断一下当前叠托点是否有任务占用
                    //    if (ProcessHelper.CheckEndFree(pmInfo.location) && tasks.Count == 0)
                    //    {
                    //        //可以生成任务,调WMS接口获取任务信息
                    //        bool req = WMSHelper.WMSOut(pmInfo.location, "");
                    //        if (req) CMMLog.Debug($"调用WMS获取辅材出库生成任务成功!");//现在任务由WMS自己下发,AMS做拦截处理(查询ext1里面对应的任务类型,并更改任务类型)
                    //        else CMMLog.Debug($"调用WMS获取辅材出库生成任务失败!");
                    //    }
                    //    else CMMLog.Debug($"检查当前叠托点是否有任务占用,或者MST主表中有缓存架入叠盘机的任务!");
                    //}
                }
                else
                {
                    CMMLog.Info($"err  包装机通道{pmInfo.readAddr}  ip:{pmInfo.ip}");
                }
            }
        }
 
        internal static void productOutWare(string endBit, string taskNo, bool action)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.location == endBit).FirstOrDefault();
            if (plc != null)
            {
                if (action)
                {
                    int[] num = new int[3] {1,1,1};
                    var writeRes = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti
                    {
                        host = plc.ip,
                        addr = plc.writeAddr,
                        data = num,
                        port = plc.port
                    });
                    CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr}里面数据为{JsonConvert.SerializeObject(num)}.");
 
                    var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                    {
                        dataNum = 3,
                        addr = plc.readAddr,
                        host = plc.ip,
                        port = plc.port
                    });
                    CMMLog.Debug($"查询设备{plc.location}通道{plc.readAddr}里面数据为{result.result[0]}、{result.result[1]}、{result.result[2]}.");
                    if (result != null && result.errCode == 0)
                    {
                        if (result.result[0] == 1 && result.result[1] == 1 && result.result[2] == 2)
                        {
                            //推送小车进入
                            TSHelper.GoToAGV(taskNo, 10, 3);
                            writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                            {
                                host = plc.ip,
                                addr = plc.writeAddr + 2,
                                data = 2,
                                port = plc.port
                            });
                            CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 2}里面数据为2.");
                        }
                    }
                }
                else
                {
                    var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                    {
                        host = plc.ip,
                        addr = plc.writeAddr + 2,
                        data = 3,
                        port = plc.port
                    });
                    CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 2}里面数据为3.");
                }
            }
        }
 
        internal static void productInWare(string startBit, string taskNo, bool action)
        {
            var plc = Settings.GetPlcInfo().Where(a => a.location == startBit).FirstOrDefault();
            if (plc != null)
            {
                if (action)
                {
                    var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                    {
                        dataNum = 1,
                        addr = plc.readAddr + 3,
                        host = plc.ip,
                        port = plc.port
                    });
                    CMMLog.Debug($"查询设备{plc.location}通道{plc.readAddr + 3}里面数据为{result.result[0]}.");
                    if (result.result[0] == 1)
                    {
                        int[] num = new int[4] { 2, 0, 0, 1 };
                        var writeRes = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti
                        {
                            host = plc.ip,
                            addr = plc.writeAddr,
                            data = num,
                            port = plc.port
                        });
                        CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr}里面数据为{JsonConvert.SerializeObject(num)}.");
                    }
 
                    result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel
                    {
                        dataNum = 4,
                        addr = plc.readAddr,
                        host = plc.ip,
                        port = plc.port
                    });
                    CMMLog.Debug($"查询设备{plc.location}通道{plc.readAddr}里面数据为{result.result[0]}、{result.result[1]}、{result.result[3]}.");
                    if (result != null && result.errCode == 0)
                    {
                        if (result.result[0] == 2 && result.result[1] == 2 && result.result[3] == 2)
                        {
                            //推送小车进入
                            TSHelper.GoToAGV(taskNo, 10, 1);
                            var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                            {
                                host = plc.ip,
                                addr = plc.writeAddr + 3,
                                data = 2,
                                port = plc.port
                            });
                            CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 3}里面数据为2.");
                        }
                    }
                }
                else
                {
                    var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                    {
                        host = plc.ip,
                        addr = plc.writeAddr + 3,
                        data = 3,
                        port = plc.port
                    });
                    CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 3}里面数据为3.");
                }
            }
        }
 
        internal static void SendHeartBeat()
        {
            var taskInfo = MongoDBSingleton.Instance.Find<TN_I_TASK_MST>(Query.Or(Query.EQ("CN_S_BUSS_TYPE", "电梯取货"), Query.EQ("CN_S_BUSS_TYPE", "电梯卸货")), "TN_I_TASK_MST");
            if (taskInfo.Count > 0)
            {
                foreach (var item in taskInfo)
                {
                    if (item.CN_S_BUSS_TYPE == "电梯卸货" || (item.CN_S_BUSS_TYPE == "电梯取货" && !WorkFlowAction.ExistsTrackLogs(item.CN_S_TASK_NO, 1, 4)))
                    {
                        //发送心跳信号
                        string DTBit = item.CN_S_BUSS_TYPE == "电梯卸货" ? item.CN_S_END_BIT : item.CN_S_START_BIT;
                        var plcInfo = Settings.GetPlcInfo().Where(a => a.Extend == DTBit).FirstOrDefault();
                        if(plcInfo != null)
                        {
                            var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel
                            {
                                addr = plcInfo.writeAddr + 12,
                                data = 1,
                                host = plcInfo.ip,
                                port = plcInfo.port
                            });
                        }
                        else
                        {
                            CMMLog.Info($"{DTBit}电梯未配置");
                        }
                    }
                }
            }
            Thread.Sleep(100000);
        }
        #endregion
    }
}