quanzhou
2 天以前 86f899fa91e811415614dff1a699141144bfc802
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
using Bro.Common.Base;
using Bro.Common.Factory;
using Bro.Common.Helper;
using Bro.Common.Interface;
using Bro.Common.Model;
using Bro.M135.Common;
using Bro.M141.Process;
using Bro.UI.Model.Winform;
using HalconDotNet;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NPOI.OpenXmlFormats.Vml;
using NPOI.POIFS.Crypt.Dsig;
using NPOI.POIFS.FileSystem;
using NPOI.SS.Formula.Functions;
using NPOI.XSSF.Streaming.Values;
using ScottPlot.Drawing.Colormaps;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using Windows.Media.AppBroadcasting;
using static Bro.Common.Helper.EnumHelper;
using static NPOI.HSSF.Util.HSSFColor;
using static Org.BouncyCastle.Crypto.Engines.SM2Engine;
using static Org.BouncyCastle.Math.EC.ECCurve;
using static ScottPlot.Plottable.PopulationPlot;
 
namespace Bro.M141_AOI1.Process
{
    [Process("AOI1", EnumHelper.DeviceAttributeType.Device)]
    public class AOI1Process : M141Process
    {
        #region constructor
        public AOI1Process() : base() { }
        public AOI1Process(string productCode) : base(productCode) { }
        #endregion
 
        AOI1Config ConfigAOI1 => Config as AOI1Config;
 
        public override void Open()
        {
 
            //string configPath = @"C:\Users\30263\Desktop\666.txt";
 
            //string _configBackupStr = "";
            //using (StreamReader reader = new StreamReader(configPath, System.Text.Encoding.UTF8))
            //{
            //    _configBackupStr = reader.ReadToEnd();
 
            //}
 
            //ProductModel p = JsonConvert.DeserializeObject<ProductModel>(_configBackupStr, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All });
 
            base.Open();
        }
 
 
        [ProcessMethod("", "PositionCheck_P1", "工位1检测", InvokeType.TestInvoke)]
        public ResponseMessage PositionCheck_P1(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            var positionSet = M141Config.WorkPositionCollection.Where(u => u.IsEnabled).FirstOrDefault(u => u.TriggerValue == "1#");
            if (positionSet == null)
            {
                throw new ProcessException($"P1检测未能获取对应的可用工位信息");
            }
            var plcnum = Plc1.Read(positionSet.plcnum, 1, out _);
 
            config.TriggerStr = "1#,1,Str," + plcnum[0];
            ResponseMessage msg = RunImageCheck_plc(config);
            msg.IsReply = false;
 
            return msg;
        }
 
 
        [ProcessMethod("", "PositionCheck_P2", "工位2检测", InvokeType.TestInvoke)]
        public ResponseMessage PositionCheck_P2(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            var positionSet = M141Config.WorkPositionCollection.Where(u => u.IsEnabled).FirstOrDefault(u => u.TriggerValue == "2#");
            if (positionSet == null)
            {
                throw new ProcessException($"P2检测未能获取对应的可用工位信息");
            }
            var plcnum = Plc1.Read(positionSet.plcnum, 1, out _);
 
            config.TriggerStr = "2#,1,Str," + plcnum[0];
            ResponseMessage msg = RunImageCheck_plc(config);
            msg.IsReply = false;
 
            return msg;
        }
 
 
        [ProcessMethod("", "PositionCheck_P3", "工位3检测", InvokeType.TestInvoke)]
        public ResponseMessage PositionCheck_P3(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            var positionSet = M141Config.WorkPositionCollection.Where(u => u.IsEnabled).FirstOrDefault(u => u.TriggerValue == "3#");
            if (positionSet == null)
            {
                throw new ProcessException($"P3检测未能获取对应的可用工位信息");
            }
            var plcnum = Plc1.Read(positionSet.plcnum, 1, out _);
 
            config.TriggerStr = "3#,1,Str," + plcnum[0];
            ResponseMessage msg = RunImageCheck_plc(config);
            msg.IsReply = false;
 
            return msg;
        }
 
 
        [ProcessMethod("ImageCheck", "CheckProduction", "产品点检", InvokeType.TestInvoke)]
        public ResponseMessage CheckProduction(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
 
            if (config is IImageCheckOperationConfig opConfig)
            {
                var results1 = GetSpecListFromConfigSelection(opConfig.SpecCollection);
                IImageSet imageSet = null;
                string Head = "时间,";
                string data = $"{DateTime.Now.ToString("yyyyMMddHHmmss")}T,";               
                try
                {
                    var messure = M141Config.MeasureBindCollection_Check.FirstOrDefault(u => u.CameraId == invokeDevice.Id);
                    var camera = DeviceCollection.FirstOrDefault(u => u.Id == messure.CameraId) as CameraBase;          
                    try
                    {
                        imageSet = CollectHImage(camera, messure.SnapshotOpConfig);
                    }
                    catch (Exception exx)
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"点检功能获取图像失败{exx.ToString()}");
                    }
                    var tool = GetHalconTool(null, "", opConfig.AlgorithemPath);
                    var ret = tool.RunProcedure(null, new Dictionary<string, HalconDotNet.HObject>() { { "INPUT_Image", imageSet.HImage } }, new List<string>() { "OUTPUT_Results" }, null);
                   // var ret = tool.RunProcedure(null, null, new List<string>() { "OUTPUT_Results" }, null);
                    List<IShapeElement> eleList = new List<IShapeElement>();
                    TextDisplay text = new TextDisplay();
                    text.LineLimit = M141Config.LineLimit_p;
                    text.FontSize = M141Config.FontSize_p;
                    eleList.Add(text);
                    text.StartX = text.StartY = 0;
                    text.AddText("点检运行成功", Color.Lime, Color.Transparent);
 
                    var b = ret.Item2["OUTPUT_Results"].HTupleToDouble();
 
                    if (results1 != null&& results1.Count== b.Count())
                    {
 
                        var itemDict = results1.ToDictionary(u => u.OutputIndex);
 
                        for (int i = 0; i < itemDict.Count(); i++)
                        {
                            double CValue;
                            if (itemDict[i].CompensationValue != null)
                            {
                                CValue = itemDict[i].CompensationValue[0];
                            }
                            else
                            {
                                CValue = 0;
                            }
                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"点检结果{itemDict[i].Code}算法输出值为{b[i]}");
                            double diff = b[i] + CValue;
                            double Upvalue = itemDict[i].StandardValue + itemDict[i].Tolrenance_Positive;
                            double Downvalue = itemDict[i].StandardValue - itemDict[i].Tolrenance_Negative;
                            bool isInTolerance = false;
 
                            if (diff >= Downvalue && diff <= Upvalue)
                            {
                                isInTolerance = true;
                            }
                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"点检结果{itemDict[i].Code}为{isInTolerance}");
                            text.AddText($"{itemDict[i].Code} {diff}", isInTolerance == true ? Color.Lime : Color.Red, Color.Transparent);
                            Head += $"{itemDict[i].Code},";
                            data += $"{diff},";
                        }                       
                    }                   
                    camera.SaveFitImage(eleList, imageSet);
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"点检功能运行成功");
                }
                catch (Exception ex)
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"点检功能失败,{ex.ToString()}");
                }
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"点检图片开始释放");
                imageSet.HImage?.Dispose();
                imageSet.HImage = null;
                imageSet.Dispose();
                imageSet = null;
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"图片已释放");
                //报表输出
                CSVRecordAsync($"相机{invokeDevice.Name}_点检数据记录.csv", data, Head);
            }                             
            return msg;
        }
 
        [ProcessMethod("ImageCheck", "ReadBarcode", "读码", InvokeType.TestInvoke)]
        public ResponseMessage ReadBarcode(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
            if (config is IImageCheckOperationConfig opConfig)
            {
                if (invokeDevice is TcpClientWrapBase BarcodeScanner)
                {
                    var results = opConfig.Products.Select(u =>
                    {
                        DetectResult result = new DetectResult();
                        result.PID = u.PID;
                        result.Specs = GetSpecListFromConfigSelection(opConfig.SpecCollection);
                        return result;
                    }).ToList();
 
                    msg.DataObj = results;
 
                    string barcode = BarcodeScannerCommunicate(BarcodeScanner);
 
                    List<double> ret = new List<double>();
                    if ("NOREAD".Equals(barcode.ToUpper()))
                    {
                        ret.Add(999);
                    }
                    else
                    {
                        if (barcode.Split('-').Length > 5)
                        {
                            int mh = Plc1.Read(4000, 1, out _)[0];
                            string codestr = barcode.Split('-')[4];
                            if (codestr == "L")
                            {
                                codestr = "10";
                            }
                            if (codestr.Equals(mh.ToString()))
                            {
                                ret.Add(1);
                            }
                            else
                            {
                                ret.Add(2);
                                Plc1.WriteSingleAddress(4010, 1, out _);//给plc报警
                            }
                        }
                        else
                        {
                            ret.Add(999);
                        }
                    }
 
                    FillSpecResults(results[0].PID, results[0].Specs, ret, opConfig.Products[0].SEQUENCE);
 
                    opConfig.Products[0].SN = barcode;
                    opConfig.Products[0].PID = barcode + "_1";
                    opConfig.Products[0].Details.ForEach(u =>
                    {
                        u.SN = barcode;
                        u.PID = barcode + "_1";
                        u.ResultList.ForEach(x =>
                    {
 
                        x.PID = barcode + "_1";
 
                    });
                    });
 
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"产品{opConfig.Products[0].PID}条码获取为{barcode}");
                }
            }
            return msg;
        }
 
        [ProcessMethod("ImageCheck", "ReadBarcode2", "读栏具码", InvokeType.TestInvoke)]
        public ResponseMessage ReadBarcode2(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
 
            if (invokeDevice is TcpClientWrapBase BarcodeScanner2)
            {
                string barcode = BarcodeScannerCommunicate(BarcodeScanner2);
                if (string.IsNullOrEmpty(barcode) || "noread".Equals(barcode.ToLower()))
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"栏具码获取失败");
                    Plc1.WriteSingleAddress(1524, 2, out _);
                    Plc1.WriteSingleAddress(1514, 1, out _);
                }
                else
                {
                    ConfigAOI1.basketcode = barcode;
                    int num = Plc1.Read(1577, 1, out _)[0];
 
                    if (ConfigAOI1.PlcAndBasketcodes.Count > 10)
                    {
                        ConfigAOI1.PlcAndBasketcodes.RemoveAt(0);
                    }
 
                    if (ConfigAOI1.PlcAndBasketcodes.Any(u => u.id == num))
                    {
                        ConfigAOI1.PlcAndBasketcodes.FirstOrDefault(u => u.id == num).code = barcode;
                    }
                    else
                    {
                        ConfigAOI1.PlcAndBasketcodes.Add(new PlcAndBasketcode
                        {
                            code = barcode,
                            id = num,
                        });
                    }
 
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"获取到栏具码为{barcode}");
 
                    mysqlhelper.NewBasketcode(barcode);
                    RerefreshBasketcode();
 
                    if (ConfigAOI1.ISupMES)
                    {
                        //string zword = mqtt.MESForBasketAsync("2", "DS02217", barcode, "1", num.ToString()).Result;
                        string zwordstr = Task.Run(() => mqtt.MESForBasketAsync("2", "DS02217", barcode, "1", ConfigAOI1.mesnum2.ToString())).Result;
 
                        ConfigAOI1.mesnum2++;
                        if (ConfigAOI1.ISokMes)
                        {
                            Plc1.WriteSingleAddress(1524, 1, out _);
                            Plc1.WriteSingleAddress(1514, 1, out _);
                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"已强制读栏具码运行设备");
                        }
                        else
                        {
                            try
                            {
                                var obj = JsonConvert.DeserializeObject<AutoLineMacQueueBak>(zwordstr);
                                ConfigAOI1.zwoid = obj.zwoid;
                                if (!string.IsNullOrEmpty(obj.zwoid) && obj.zstatus == "200")
                                {
                                    Plc1.WriteSingleAddress(1524, 1, out _);
                                    Plc1.WriteSingleAddress(1514, 1, out _);
                                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"RabbitMQ zwoid获取成功 {obj.zstatus} {obj.zwoid} ");
                                }
                                else
                                {
                                    try
                                    {
                                        if (zwordstr.Contains("version错误"))
                                        {
                                            Regex reg = new Regex("[0-9]+", RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(2));
                                            MatchCollection matches = reg.Matches(obj.zerrmsg);
                                            if (matches[0] != null && matches.Count == 3)
                                            {
                                                ConfigAOI1.mesnum2 = int.Parse(matches[0].Value)+1;
                                            }
                                            else
                                            {
                                                ConfigAOI1.mesnum2 = 0;
                                            }
                                        }
                                    }
                                    catch (Exception ex) 
                                    {
                                    }                                 
                                    Plc1.WriteSingleAddress(1524, 2, out _);
                                    Plc1.WriteSingleAddress(1514, 1, out _);
                                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"RabbitMQ zwoid获取失败 {zwordstr} ");
                                }
                            }
                            catch(Exception ex)
                            {
                                Plc1.WriteSingleAddress(1524, 2, out _);
                                Plc1.WriteSingleAddress(1514, 1, out _);
                                LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"RabbitMQ zwoid获取异常  返回数据为 {zwordstr},异常错误{ex.GetExceptionMessage()}");
                            }
                        }                       
                    }
                    else
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"已关闭mes连接");
                        Plc1.WriteSingleAddress(1524, 1, out _);
                        Plc1.WriteSingleAddress(1514, 1, out _);
                    }
                    SaveProcessConfig(ConfigAOI1);
                }
 
            }
 
            return msg;
        }
 
        private string BarcodeScannerCommunicate(TcpClientWrapBase client)
        {
            string barcode = "";
 
            for (int i = 0; i < 3; i++)
            {
                if (client.WriteAndRead("start", out string error, out barcode, true))
                {
                    barcode = barcode.Trim(' ', '\r', '\n');
                    if (CheckBarcodeValid(barcode))
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{client.Name}扫码完成,反馈{barcode}");
                        return barcode;
                    }
                    else
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{client.Name}第{i + 1}扫码完成,反馈{barcode},不是合法条码");
                        Thread.Sleep(200);
                    }
                }
                else
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"{client.Name}扫码失败,{error}");
                    break;
                }
            }
 
            return "NOREAD";
        }
 
        private bool CheckBarcodeValid(string barcode)
        {
            bool isBarcodeValid = true;
            if (string.IsNullOrWhiteSpace(barcode) || barcode == "NA" || barcode == "NoRead")
            {
                isBarcodeValid = false;
            }
            return isBarcodeValid;
        }
 
        [ProcessMethod("ImageCheck", "CheckLineProfile", "检测产品线轮廓度", InvokeType.TestInvoke)]
        public ResponseMessage CheckLineProfile(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
            try
            {
                if (config is IImageCheckOperationConfig opConfig)
                {
                    var results = opConfig.Products.Select(u =>
                    {
                        DetectResult result = new DetectResult();
                        result.PID = u.PID;
                        result.Specs = GetSpecListFromConfigSelection(opConfig.SpecCollection);
                        return result;
                    }).ToList();
 
                    List<ContourPoint> CheckPointList = GetPointListFromConfigSelection(opConfig.CheckPointList);
                    msg.DataObj = results;
                    CheckPointList = CheckPointList.OrderBy(u => u.index).ToList();
 
                    List<double> pointdataF = new List<double>();
                    foreach (var item in CheckPointList)
                    {
                        pointdataF.Add(item.X);
                        pointdataF.Add(item.Y);
                    }
 
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"检测产品线轮廓度输入点数据  {string.Join(',', pointdataF)}");
 
                    var tool = GetHalconTool(null, "", opConfig.AlgorithemPath);
                    var ret = tool.RunProcedure(new Dictionary<string, HalconDotNet.HTuple>() { { "INPUT_Points", pointdataF.ToArray() } }, new Dictionary<string, HalconDotNet.HObject>() { { "INPUT_Image", opConfig.ImageSet.HImage } }, new List<string>() { "OUTPUT_Results", "OUTPUT_PointZ" }, null);                   
                    if (ret == null)
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"脚本{opConfig.AlgorithemPath}运行异常,返回值为null");
                    }
                    else if (!ret.Item1)
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"脚本{opConfig.AlgorithemPath}运行异常,{ret.Item4}");
                    }
                    else
                    {
                        //List<double> datasX = ret.Item2["OUTPUT_PointX"].HTupleToDouble();
                        //List<double> datasY = ret.Item2["OUTPUT_PointY"].HTupleToDouble();
                        List<double> datasZ = ret.Item2["OUTPUT_PointZ"].HTupleToDouble();
 
                        //LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"{results[0].PID}获取数据OUTPUT_PointX   {string.Join(",", datasX.Select(u => u.ToString("f4")))}");
                        //LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"{results[0].PID}获取数据OUTPUT_PointY   {string.Join(",", datasY.Select(u => u.ToString("f4")))}");
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"{results[0].PID}获取数据OUTPUT_PointZ   {string.Join(",", datasZ.Select(u => u.ToString("f4")))}");
 
 
                        if (datasZ.Count == CheckPointList.Count)
                        {
                            string Postion = "P" + opConfig.TriggerStr.Split('#')[0];
                            string csvhead = "Time,Pid" +
                                ",Postion";
                            string csvdata = $"{DateTime.Now.ToString("yyyyMMddHHmmss")}T,{results[0].PID},{Postion}";
 
                            for (int i = 0; i < CheckPointList.Count; i++)
                            {
                                CheckPointList[i].ActualZ = datasZ[i] + CheckPointList[i].compensateZ;
 
                                csvhead += $",{CheckPointList[i].Name}_X";
                                csvhead += $",{CheckPointList[i].Name}_Y";
                                csvhead += $",{CheckPointList[i].Name}_Z";
 
                                csvdata += $",{CheckPointList[i].X.ToString("F4")}";
                                csvdata += $",{CheckPointList[i].Y.ToString("F4")}";
                                csvdata += $",{CheckPointList[i].ActualZ.ToString("F4")}";
                            }
 
                            foreach (var item in results[0].Specs.AsParallel())
                            {
                                var temcal = ConfigAOI1.MeasureItemBinds.FirstOrDefault(u => u.SpecCode == item.Code);
 
                                if (temcal == null)
                                {
                                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"检测项 {item.Code}未能获取计算公式");
                                }
                                else
                                {
                                    List<double> pointdata = new List<double>();
                                    foreach (var item1 in temcal.MeasurePointNameCollection)
                                    {
                                        var point = CheckPointList.FirstOrDefault(u => u.Name == item1.MeasurePointName);
                                        if (point == null)
                                        {
                                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"未能找到检测项{item.Code}的点数据{item1.MeasurePointName}");
                                        }
                                        else
                                        {
                                            if (item1.ContourEdge == ContourEdge.X)
                                            {
                                                pointdata.Add(point.X);
                                            }
                                            else if (item1.ContourEdge == ContourEdge.Y)
                                            {
                                                pointdata.Add(point.Y);
                                            }
                                            else if (item1.ContourEdge == ContourEdge.Z)
                                            {
                                                pointdata.Add(point.ActualZ);
                                            }
                                        }
 
                                    }
 
                                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"{results[0].PID}   {item.Code}输入计算公式数据 {string.Join(",", pointdata.Select(u => u.ToString("f4")))}");
                                    var toolcal = GetHalconTool(null, "", temcal.AlgorithemPath);
                                    var retcal = toolcal.RunProcedure(new Dictionary<string, HalconDotNet.HTuple>() { { "INPUT_Points", pointdata.ToArray() } }, null, new List<string>() { "OUTPUT_Results" }, null);
 
                                    if (retcal != null && !ret.Item1)
                                    {
                                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"未能找到检测项{item.Code}的计算公式算法运行异常{retcal?.Item4}");
                                    }
                                    else
                                    {
                                        item.ActualValue = retcal.Item2["OUTPUT_Results"].HTupleToDouble()[0];
 
                                        if (item.IsEnableCompensation && item.CompensationValue.Count > 1)
                                        {                                            
                                            string index = opConfig.TriggerStr.Split('#')[0];
                                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"{item.Code}进入双重补偿{index},初始值为{item.ActualValue}");
 
                                            if (index=="1")
                                            {
                                                item.ActualValue += item.CompensationValue[0];
                                                LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"{item.Code}进入双重补偿,补偿值为{item.CompensationValue[0]},最终结果为{item.ActualValue}");
                                            }
                                            else
                                            {
                                                item.ActualValue += item.CompensationValue[1];
                                                LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"{item.Code}进入双重补偿,补偿值为{item.CompensationValue[1]},最终结果为{item.ActualValue}");
                                            }
 
                                        }
 
 
                                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"{results[0].PID}获取计算公式数据{item.Code}  {item.GetMeasureValueStr()}");
                                    }
                                }
                            }
 
 
                            CSVRecordAsync($"ContourPointRecord_{Postion}.csv", csvdata, csvhead);
                        }
                        else
                        {
                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"脚本{opConfig.AlgorithemPath}输出数据长度异常");
                            throw new Exception();
                        }
                    }
 
                }
            }
            catch (Exception re)
            {
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"{re.ToString()}");
            }
 
            return msg;
        }
 
        [ProcessMethod("ImageCheck", "GetMatrix", "获取矩阵", InvokeType.TestInvoke)]
        public ResponseMessage GetMatrix(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
            if (config is IImageCheckOperationConfig opConfig)
            {
                var tool = GetHalconTool(null, "", opConfig.AlgorithemPath);
                var ret = tool.RunProcedure(null, new Dictionary<string, HalconDotNet.HObject>() { { "INPUT_Image", opConfig.ImageSet.HImage } }, new List<string>() { "OUTPUT_Results" }, null);
 
 
                if (ret == null)
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"脚本{opConfig.AlgorithemPath}运行异常,返回值为null");
                }
                else if (!ret.Item1)
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"脚本{opConfig.AlgorithemPath}运行异常,{ret.Item4}");
                }
                else
                {
                    List<double> datas = ret.Item2["OUTPUT_Results"].HTupleToDouble();
                    opConfig.Products[0].Centermatrix = datas;
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"产品{opConfig.Products[0].PID}矩阵获取为 {string.Join(',', datas)}");
                }
            }
            return msg;
        }
 
        [ProcessMethod("", "MESupdata", "MES正常上传单个OK产品获取条码", InvokeType.TestInvoke)]
        public ResponseMessage MESupdata(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
 
            Plc3 = invokeDevice as PLCBase;
            //上传反馈结果          
            bool Issuccess = false;
            bool Issuccess1 = false;
 
            string tary_label = string.Empty;
 
            string tary_label1 = string.Empty;
 
            var plcnum = Plc3.Read(2100, 1, out _);
            var plcnum1 = Plc3.Read(2102, 1, out _);
            if (plcnum[0] == 0)
            {
                Issuccess = true;
            }
            else
            {
                Issuccess = UpMES(plcnum[0] + "_1", "N", out tary_label);
                if (M141Config.Isprint && !string.IsNullOrEmpty(tary_label))
                {
                    StartPrint(tary_label, "Honeywell PX240S(300 dpi)");
                }
            }
            if (plcnum1[0] == 0)
            {
                Issuccess1 = true;               
            }
            else
            {
                Issuccess1 = UpMES(plcnum1[0] + "_1", "N", out tary_label1);
 
                if (M141Config.Isprint && !string.IsNullOrEmpty(tary_label1))
                {
                    StartPrint(tary_label1, "Honeywell PX240S(300 dpi)");
                }
            }                      
            if (Issuccess && Issuccess1)
            {
                Plc3.WriteSingleAddress(2120, 1, out _);
            }
            else
            {
                Plc3.WriteSingleAddress(2120, 2, out _);
            }
            return msg;
        }
 
        [ProcessMethod("", "MESupFinallydata", "MES上传单个尾盘OK产品获取条码", InvokeType.TestInvoke)]
        public ResponseMessage MESupFinallydata(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
 
            Plc3 = invokeDevice as PLCBase;
            //上传反馈结果          
            bool Issuccess = false;
            bool Issuccess1 = false;
 
            string tary_label = string.Empty;
 
            string tary_label1 = string.Empty;
 
            var plcnum = Plc3.Read(2100, 1, out _);
            var plcnum1 = Plc3.Read(2102, 1, out _);
            if (plcnum[0] != 0|| plcnum1[0] != 0)
            {
                if (plcnum[0] == 0)
                {
                    Issuccess1 = UpMES(plcnum1[0] + "_1", "Y", out tary_label1);
                    if (M141Config.Isprint && !string.IsNullOrEmpty(tary_label1))
                    {
                        StartPrint(tary_label1, "Honeywell PX240S(300 dpi)");
                    }
                }
                else if(plcnum1[0] == 0)
                {
                    Issuccess = UpMES(plcnum[0] + "_1", "Y", out tary_label);
                    if (M141Config.Isprint && !string.IsNullOrEmpty(tary_label))
                    {
                        StartPrint(tary_label, "Honeywell PX240S(300 dpi)");
                    }
                }
                else if (plcnum[0] != 0 && plcnum1[0] != 0)
                {
                    Issuccess = UpMES(plcnum[0] + "_1", "N", out tary_label);                   
                    if (M141Config.Isprint && !string.IsNullOrEmpty(tary_label1))
                    {
                        StartPrint(tary_label1, "Honeywell PX240S(300 dpi)");
                    }
                    Issuccess1 = UpMES(plcnum1[0] + "_1", "Y", out tary_label1);
                    if (M141Config.Isprint && !string.IsNullOrEmpty(tary_label))
                    {
                        StartPrint(tary_label, "Honeywell PX240S(300 dpi)");
                    }
                }
            }
           
            if (Issuccess && Issuccess1)
            {
                Plc3.WriteSingleAddress(2120, 1, out _);
            }
            else
            {
                Plc3.WriteSingleAddress(2120, 2, out _);
            }
            return msg;
        }
 
        private bool UpMES(string PLCNum ,string endtray , out string result )
        {
            var plist = mysqlhelper.GetProductList(PLCNum);
            ProductModel newp = new ProductModel();
            newp.SEQUENCE = plist[0].SEQUENCE;
            newp.PID = plist[0].PID;
            newp.BasketCode = plist[0].BasketCode;
            newp.Zword = plist[0].Zword;
            newp.Result = "OK";
            newp.SN = plist[0].SN;
            bool isok = UptoMES(newp, endtray, out result);
            return isok;
 
        }
 
        private bool UptoMES( ProductModel pro, string endtray,  out string result)
        {
            if (M141Config.numpro >= 50)
            {
                M141Config.numpro = 0;
            }
            M141Config.numpro++;
            var tems = Task.Run(() => mqtt.MESForProduceAsync(pro, M141Config.mesnum2.ToString(), M141Config.numpro, endtray)).Result;
            if (tems == null)
            {
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{pro.PID}数据上传MES异常 返回数据为null");
                result = string.Empty;
                return false;
            }
            else
            {
                try
                {
                    var obj = JsonConvert.DeserializeObject<AutoLineMacBarcodeQueueBak>(tems);
                    if (obj.zstatus == "200")
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"产品{pro.PID}数据上传MES成功 {tems}");
                        result = obj.tary_label;
                        return true;
                    }
                    else
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{pro.PID}数据上传MES失败  {tems}");
                        result = "obj.zerrmsg";
                        if (obj.zerrmsg.Contains("重复数据"))
                        {
                            return true;
                        }
                        return false;
                    }
                }
                catch (Exception)
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{pro.PID}数据上传MES失败  {tems}");
                    result = string.Empty;
                    return false;
                }
 
            }
        }
 
       [ProcessMethod("", "PLCtosaveproduction", "MES通过plc获取产品PID并保存", InvokeType.TestInvoke)]
        public ResponseMessage PLCtosaveproduction(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
 
            Plc3 = invokeDevice as PLCBase;
 
            bool Issuccess = false;
 
            bool Issuccess1 = false;
 
            LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"产品信息记录保存触发");
 
            var plcnum = Plc3.Read(2100, 1, out _);
            Issuccess = GetProImformation(plcnum[0]);
 
            var plcnum1 = Plc3.Read(2102, 1, out _);
            Issuccess1 = GetProImformation(plcnum1[0]);
            
            if (Issuccess && Issuccess1)
            {
                Plc3.WriteSingleAddress(2120, 1, out _);
            }
            else
            {
                Plc3.WriteSingleAddress(2120, 2, out _);
            }
            return msg;
        }
 
        private bool GetProImformation(int PLC_order)
        {
            if (PLC_order == 0)
            {
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"PLC反馈机械手吸盘无产品");
                return true;
            }
            else
            {
                var plist = mysqlhelper.GetProductList(PLC_order + "_1");
                if (plist != null)
                {
                    if (!M141Config.Productioncode.Any(u => u.PID == plist[0].PID))
                    {
                        int num = M141Config.Productioncode.Count + 1;
                        M141Config.Productioncode.Add(new ProductionID
                        {
                            Uptomesid = num,
                            PID = plist[0].PID,
                            SEQUENCE = plist[0].SEQUENCE,
                            BasketCode = plist[0].BasketCode,
                            Zword = plist[0].Zword,
                            //newp.Result = "OK";
                            SN = plist[0].SN
                        });
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"产品{plist[0].PID}数据记录成功");
                        return true;                       
                    }
                    else
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"产品{plist[0].PID}数据重复,记录失败");
                        return false;                      
                    }
                }
                else
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"PLC序号:{PLC_order}无法从数据库中获取数据");
                    return false;
                }
            }
           
        }
 
        [ProcessMethod("", "MEStogetcode", "MES上传整批数据并获取条码", InvokeType.TestInvoke)]
        public ResponseMessage MEStogetcode(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
            Plc3 = invokeDevice as PLCBase;
            bool Issuccess = false;
            bool Issuccess1 = false;
 
            var plcnum = Plc3.Read(2100, 1, out _);
            Issuccess = GetProImformation(plcnum[0]);
 
            var plcnum1 = Plc3.Read(2102, 1, out _);
            Issuccess1 = GetProImformation(plcnum1[0]);
 
            if (!Issuccess && !Issuccess1)
            {
                Plc3.WriteSingleAddress(2120, 2, out _);
                return msg;
            }
                      
            LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"产品信息上传MES触发");
 
            if (M141Config.Productioncode.Count == 50)
            {
                //产品正常上传
                while(M141Config.Productioncode.Count>0)
                {
                    ProductModel newp = new ProductModel();
                    newp.SEQUENCE = M141Config.Productioncode[0].SEQUENCE;
                    newp.PID = M141Config.Productioncode[0].PID;
                    newp.BasketCode = M141Config.Productioncode[0].BasketCode;
                    newp.Zword = M141Config.Productioncode[0].Zword;
                    newp.Result = "OK";
                    newp.SN = M141Config.Productioncode[0].SN;
                    string tary_ID = string.Empty;
                    bool isok = UptoMES(newp, "N", out tary_ID);
                    if (!isok)
                    {
                        Plc3.WriteSingleAddress(2120, 2, out _);
                    }
                    if (M141Config.Isprint && !string.IsNullOrEmpty(tary_ID))
                    {
                        StartPrint(tary_ID, "Honeywell PX240S(300 dpi)");
                        M141Config.MES_codes.Add(new MES_code
                        {
                            Printers_code = tary_ID
                        });
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"条码:{tary_ID}已触发S6打印并且保存");
                    }                
                    M141Config.Productioncode.RemoveAt(0);
                }
            }
            else if (M141Config.Productioncode.Count > 50)
            {
                //中途有产品被取走
                for (int i = 0; i < 50; i++)
                {
                    ProductModel newp = new ProductModel();
                    newp.SEQUENCE = M141Config.Productioncode[-1].SEQUENCE;
                    newp.PID = M141Config.Productioncode[-1].PID;
                    newp.BasketCode = M141Config.Productioncode[-1].BasketCode;
                    newp.Zword = M141Config.Productioncode[-1].Zword;
                    newp.Result = "OK";
                    newp.SN = M141Config.Productioncode[-1].SN;
                    string tary_ID = string.Empty;
                    bool isok = UptoMES(newp, "N", out tary_ID);
                    if (!isok)
                    {
                        Plc3.WriteSingleAddress(2120, 2, out _);
                    }
                    if (M141Config.Isprint && !string.IsNullOrEmpty(tary_ID))
                    {
                        StartPrint(tary_ID, "Honeywell PX240S(300 dpi)");
                        M141Config.MES_codes.Add(new MES_code
                        {
                            Printers_code = tary_ID
                        });
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"条码:{tary_ID}已触发S6打印并且保存");
                    }
                    M141Config.Productioncode.RemoveAt(-1);
                }
                M141Config.Productioncode.Clear();
            }
            else if (M141Config.Productioncode.Count < 50)
            {
                //产品缺少需要额外的条码补救
                while (M141Config.Productioncode.Count > 0)
                {
                    ProductModel newp = new ProductModel();
                    newp.SEQUENCE = M141Config.Productioncode[0].SEQUENCE;
                    newp.PID = M141Config.Productioncode[0].PID;
                    newp.BasketCode = M141Config.Productioncode[0].BasketCode;
                    newp.Zword = M141Config.Productioncode[0].Zword;
                    newp.Result = "OK";
                    newp.SN = M141Config.Productioncode[0].SN;
                    string tary_ID = string.Empty;
                    bool isok = false;
                                                        
                    if (M141Config.Productioncode.Count==1)
                    {
                        isok = UptoMES(newp, "Y", out tary_ID);
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"产品{newp.PID}触发零数尾盘");
                    }
                    else
                    {
                        isok = UptoMES(newp, "N", out tary_ID);
                    }
                    if (!isok)
                    {
                        Plc3.WriteSingleAddress(2120, 2, out _);
                    }
                    if (M141Config.Isprint && !string.IsNullOrEmpty(tary_ID))
                    {
                        StartPrint(tary_ID, "Honeywell PX240S(300 dpi)");
                        M141Config.MES_codes.Add(new MES_code
                        {
                            Printers_code = tary_ID
                        });
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"条码:{tary_ID}已触发S6打印并且保存");
                    }
                    M141Config.Productioncode.RemoveAt(0);
                }
            }
            if (Issuccess && Issuccess1)
            {
                Plc3.WriteSingleAddress(2120, 1, out _);
            }
            else
            {
                Plc3.WriteSingleAddress(2120, 2, out _);
            }
            return msg;
        }
 
        [ProcessMethod("", "MESup1", "MES上传进料口空篮", InvokeType.TestInvoke)]
        public ResponseMessage MESup1(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
 
            //int num = Plc1.Read(1575, 1, out _)[0];
            //mqtt.Send("1", "DS02216", "", "0", num.ToString());
            //Plc1.WriteSingleAddress(1590, 1, out _);
            if (ConfigAOI1.ISupMES)
            {
                string Msg = Task.Run(() => mqtt.MESForBasketAsync("1", "DS02216", "", "0", ConfigAOI1.mesnum1.ToString())).Result;
 
                if (ConfigAOI1.ISokMes)
                {
                    ConfigAOI1.mesnum1++;
                    Plc1.WriteSingleAddress(1590, 1, out _);
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"已强制MES上传进料口空篮");
                }
                else
                {
                    if (Msg != null)
                    {
                        try
                        {
                            var obj = JsonConvert.DeserializeObject<AutoLineMacQueueBak>(Msg);
                            if (obj?.zstatus == "200")
                            {
                                ConfigAOI1.mesnum1++;
                                Plc1.WriteSingleAddress(1590, 1, out _);
                            }
                            else
                            {
                                try
                                {
                                    if (Msg.Contains("version错误"))
                                    {
                                        Regex reg = new Regex("[0-9]+", RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(2));
                                        MatchCollection matches = reg.Matches(obj.zerrmsg);
                                        if (matches[0] != null && matches.Count == 3)
                                        {
                                            ConfigAOI1.mesnum1 = int.Parse(matches[0].Value)+ 1;
                                        }
                                        else
                                        {
                                            ConfigAOI1.mesnum1 = 0;
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                }
                                Plc1.WriteSingleAddress(1590, 2, out _);
                            }
                            LogAsync(DateTime.Now, obj?.zstatus == "200" ? EnumHelper.LogLevel.Information : EnumHelper.LogLevel.Exception, $"RabbitMQ 篮具  进料口空篮 上传{(obj?.zstatus == "200" ? "成功" : "失败")}   {obj?.zstatus}");
 
                        }
                        catch
                        {
                            
                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"RabbitMQ 篮具  进料口空篮 获取返回值解析异常  返回数据{Msg} ");
                            Plc1.WriteSingleAddress(1590, 2, out _);
                        }
                    }
                    else
                    {
                        Plc1.WriteSingleAddress(1590, 2, out _);
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"RabbitMQ 篮具  进料口空篮 获取返回值失败  返回数据null ");
                    }
                }                
            }
            else
            {
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"已关闭mes连接");
                Plc1.WriteSingleAddress(1590, 1, out _);
 
            }
 
            return msg;
        }
 
 
        [ProcessMethod("", "MESup2", "MES上传进料口满载", InvokeType.TestInvoke)]
        public ResponseMessage MESup2(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
            //int num = Plc1.Read(1575, 1, out _)[0];
 
            //mqtt.Send("1", "DS02216", "", "1", num.ToString());
            //Plc1.WriteSingleAddress(1590, 1, out _);
 
            if (ConfigAOI1.ISupMES)
            {
                string Msg = Task.Run(() => mqtt.MESForBasketAsync("1", "DS02216", "", "1", ConfigAOI1.mesnum1.ToString())).Result;
                if (ConfigAOI1.ISokMes) 
                {
                    ConfigAOI1.mesnum1++;
                    Plc1.WriteSingleAddress(1590, 1, out _);
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"已强制MES上传进料口满载");
                } 
                else
                {
                    if (Msg != null)
                    {
 
                        try
                        {
                            var obj = JsonConvert.DeserializeObject<AutoLineMacQueueBak>(Msg);
                            if (obj?.zstatus == "200")
                            {
                                ConfigAOI1.mesnum1++;
                                Plc1.WriteSingleAddress(1590, 1, out _);
                            }
                            else
                            {
                                try
                                {
                                    if (Msg.Contains("version错误"))
                                    {
                                        Regex reg = new Regex("[0-9]+", RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(2));
                                        MatchCollection matches = reg.Matches(obj.zerrmsg);
                                        if (matches[0] != null && matches.Count == 3)
                                        {
                                            ConfigAOI1.mesnum1 = int.Parse(matches[0].Value) + 1;
                                        }
                                        else
                                        {
                                            ConfigAOI1.mesnum1 = 0;
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                }
                                Plc1.WriteSingleAddress(1590, 2, out _);
                            }
                            LogAsync(DateTime.Now, obj?.zstatus == "200" ? EnumHelper.LogLevel.Information : EnumHelper.LogLevel.Exception, $"RabbitMQ 篮具  进料口满载 上传{(obj?.zstatus == "200" ? "成功" : "失败")}   {obj?.zstatus}");
                        }
                        catch
                        {
                            Plc1.WriteSingleAddress(1590, 2, out _);
                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"RabbitMQ 篮具  进料口满载 获取返回值解析异常  返回数据{Msg} ");
                        }
                    }
                    else
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"RabbitMQ 篮具  进料口满载 获取返回值解析异常  返回数据null ");
                        Plc1.WriteSingleAddress(1590, 2, out _);
                    }
                }                
            }
            else
            {
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"已关闭mes连接");
                Plc1.WriteSingleAddress(1590, 1, out _);
            }
            return msg;
        }
 
        [ProcessMethod("", "MESup3", "MES上传下料口空篮", InvokeType.TestInvoke)]
        public ResponseMessage MESup3(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
 
            if (ConfigAOI1.ISupMES)
            {
                int num = Plc1.Read(1578, 1, out _)[0];
 
                string Msg = Task.Run(() => mqtt.MESForBasketAsync("3", "DS02217", ConfigAOI1.PlcAndBasketcodes.Any(u => u.id == num) ? ConfigAOI1.PlcAndBasketcodes.FirstOrDefault(u => u.id == num).code : "NoRead", "0", ConfigAOI1.mesnum3.ToString())).Result;
                if (Msg != null)
                {
                    try
                    {
                        var obj = JsonConvert.DeserializeObject<AutoLineMacQueueBak>(Msg);
                        if (obj?.zstatus == "200")
                        {
                            ConfigAOI1.mesnum3++;
                            Plc1.WriteSingleAddress(1591, 1, out _);
                        }
                        else
                        {
                            try
                            {
                                if (Msg.Contains("version错误"))
                                {
                                    Regex reg = new Regex("[0-9]+", RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(2));
                                    MatchCollection matches = reg.Matches(obj.zerrmsg);
                                    if (matches[0] != null && matches.Count == 3)
                                    {
                                        ConfigAOI1.mesnum3 = int.Parse(matches[0].Value)+1;
                                    }
                                    else
                                    {
                                        ConfigAOI1.mesnum3 = 0;
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                            }
                            Plc1.WriteSingleAddress(1591, 2, out _);
                        }
                        LogAsync(DateTime.Now, obj?.zstatus == "200" ? EnumHelper.LogLevel.Information : EnumHelper.LogLevel.Exception, $"RabbitMQ 篮具  下料口空篮 上传{(obj?.zstatus == "200" ? "成功" : "失败")}   {obj?.zstatus}");
 
                    }
                    catch
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"RabbitMQ 篮具  下料口空篮 获取返回值解析异常  返回数据{Msg} ");
                        Plc1.WriteSingleAddress(1591, 2, out _);
                    }
                }
                else
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"RabbitMQ 篮具  进料口下料口空篮空篮 获取返回值解析异常  返回数据null ");
                    Plc1.WriteSingleAddress(1591, 2, out _);
                }
            }
            else
            {
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"已关闭mes连接");
                Plc1.WriteSingleAddress(1591, 1, out _);
            }
 
            return msg;
        }
 
        [ProcessMethod("", "MESup4", "MES上传下料口满载", InvokeType.TestInvoke)]
        public ResponseMessage MESup4(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
 
            if (ConfigAOI1.ISupMES)
            {
                int num = Plc1.Read(1578, 1, out _)[0];
                //mqtt.Send("3", "DS02217", ConfigAOI1.Dicbasketcode.ContainsKey(num) ? ConfigAOI1.Dicbasketcode[num] : ConfigAOI1.basketcode, "2", num.ToString());
 
                string Msg = Task.Run(() => mqtt.MESForBasketAsync("3", "DS02217", ConfigAOI1.PlcAndBasketcodes.Any(u => u.id == num) ? ConfigAOI1.PlcAndBasketcodes.FirstOrDefault(u => u.id == num).code : "NoRead", "2", ConfigAOI1.mesnum3.ToString())).Result;
                if (Msg != null)
                {
                    try
                    {
                        var obj = JsonConvert.DeserializeObject<AutoLineMacQueueBak>(Msg);
                        if (obj?.zstatus == "200")
                        {
                            ConfigAOI1.mesnum3++;
                        }
                        else
                        {
                            try
                            {
                                if (Msg.Contains("version错误"))
                                {
                                    Regex reg = new Regex("[0-9]+", RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(2));
                                    MatchCollection matches = reg.Matches(obj.zerrmsg);
                                    if (matches[0] != null && matches.Count == 3)
                                    {
                                        ConfigAOI1.mesnum3 = int.Parse(matches[0].Value) + 1;
                                    }
                                    else
                                    {
                                        ConfigAOI1.mesnum3 = 0;
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                            LogAsync(DateTime.Now, obj?.zstatus == "200" ? EnumHelper.LogLevel.Information : EnumHelper.LogLevel.Exception, $"RabbitMQ 篮具  下料口满载1 上传{(obj?.zstatus == "200" ? "成功" : "失败")}   {obj?.zstatus}");
                    }
                    catch
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"RabbitMQ 篮具  下料口满载1 获取返回值解析异常  返回数据{Msg} ");
                    }
                }
                else
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"RabbitMQ 篮具  下料口满载1 获取返回值解析异常  返回数据null ");
                }
                Thread.Sleep(1000);
                //mqtt.Send("4", "DS02217", ConfigAOI1.Dicbasketcode.ContainsKey(num) ? ConfigAOI1.Dicbasketcode[num] : ConfigAOI1.basketcode, "1", num.ToString());
                Msg = Task.Run(() => mqtt.MESForBasketAsync("4", "DS02217", ConfigAOI1.PlcAndBasketcodes.Any(u => u.id == num) ? ConfigAOI1.PlcAndBasketcodes.FirstOrDefault(u => u.id == num).code : "NoRead", "1", ConfigAOI1.mesnum4.ToString())).Result;
                if (Msg != null)
                {
                    try
                    {
                        var obj = JsonConvert.DeserializeObject<AutoLineMacQueueBak>(Msg);
                        if (obj?.zstatus == "200")
                        {
                            ConfigAOI1.mesnum4++;
                            Plc1.WriteSingleAddress(1591, 1, out _);
                        }
                        else
                        {
                            try
                            {
                                if (Msg.Contains("version错误"))
                                {
                                    Regex reg = new Regex("[0-9]+", RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(2));
                                    MatchCollection matches = reg.Matches(obj.zerrmsg);
                                    if (matches[0] != null && matches.Count == 3)
                                    {
                                        ConfigAOI1.mesnum4 = int.Parse(matches[0].Value) + 1;
                                    }
                                    else
                                    {
                                        ConfigAOI1.mesnum4 = 0;
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                            }
                            Plc1.WriteSingleAddress(1591, 2, out _);
                        }
                        LogAsync(DateTime.Now, obj?.zstatus == "200" ? EnumHelper.LogLevel.Information : EnumHelper.LogLevel.Exception, $"RabbitMQ 篮具  下料口满载 上传{(obj?.zstatus == "200" ? "成功" : "失败")}   {obj?.zstatus}");
                    }
                    catch
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"RabbitMQ 篮具  下料口满载 获取返回值解析异常  返回数据{Msg} ");
                        Plc1.WriteSingleAddress(1591, 2, out _);
                    }
                }
                else
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"RabbitMQ 篮具  下料口满载 获取返回值解析异常  返回数据null ");
                    Plc1.WriteSingleAddress(1591, 2, out _);
                }
            }
            else
            {
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"已关闭mes连接");
                Plc1.WriteSingleAddress(1591, 1, out _);
            }
            return msg;
        }
    }
}