yirongjin
2025-07-23 f4c6ac315102c3a64da3a8f2d12b2d9042b5effd
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using Newtonsoft.Json;
using EnumType;
using IWareDataAccess.EF;
using Seagull.BarTender.Print;
using System.IO;
 
namespace DeviceWCS
{
    public partial class Form1 : Form
    {
        srmSER.SrmService srmOPC = new srmSER.SrmService();
        rgvSRM.RgvService rgvOPC = new rgvSRM.RgvService();
        tranSRM.SrmTranService tranOPC = new tranSRM.SrmTranService();
        srmInfo SrmInfo = new srmInfo();
        rgvINFO Rgvinfo = new rgvINFO();
        tranInfo TranInfo = new tranInfo();
        int srmNO = 1;
        int rgvNO = 1;
        int tranNO = 1;
 
        //static  Engine btEngine = new Engine();
        //LabelFormatDocument btFormatEu = btEngine.Documents.Open(Path.Combine(@"F:\", "EUPrint.btw"));//这里是Bartender软件生成的模板文件,你需要先把模板文件做好。
        //LabelFormatDocument btFormatIs = btEngine.Documents.Open(Path.Combine(@"F:\", "IsOkPrint.btw"));//这里是Bartender软件生成的模板文件,你需要先把模板文件做好。
        public Form1()
        {
            InitializeComponent();
            this.dGVQuery.AutoGenerateColumns = false;
 
            Control.CheckForIllegalCrossThreadCalls = false;
            Thread thsrm = new Thread(srmInfo);
            thsrm.Start();
            Thread thrgv = new Thread(rgvinfo);
            thrgv.Start();
            Thread thtran = new Thread(traninfo);
            thtran.Start();
 
            //Thread thprint = new Thread(thprintinfo);
            // thprint.Start();
 
        }
 
        //private void thprintinfo(object obj)
        //{
        //    while (true) 
        //    {
        //     using (Model md = new Model())
        //    {
        //        var printinfo = md.BASE_PRINT.OrderBy(x => x.CREATETIME).FirstOrDefault(x=>x.STATE==0);
        //        int type = printinfo.TYPE;
        //        LabelFormatDocument btFormat;
        //        if (type == 1)
        //        {
 
        //            btFormat = btFormatEu;
        //        }
        //        else 
        //        {
        //            btFormat = btFormatIs;
        //        }
 
        //        btEngine.Start();
        //        btFormat.PrintSetup.PrinterName = printinfo.PRINTDEVNAME;// "80mmThermalPrinter";//打印机名称
        //        btFormat.PrintSetup.IdenticalCopiesOfLabel = 1; //打印份数
        //        Messages messages;
        //        int waitout = 1000; // 10秒 超时
 
        //        if (type == 1)
        //        {
        //            btFormat.SubStrings["ItemNum"].Value = printinfo.ITEMNUM;//为Bartender里的数据源(文本框、条码等等)传值
        //            btFormat.SubStrings["ItemTypeNum"].Value = printinfo.ITEMTYPENUM;//为Bartender里的数据源(文本框、条码等等)传值
        //            btFormat.SubStrings["CVIQRCode"].Value = printinfo.CVIQRCODE;//为Bartender里的数据源(文本框、条码等等)传值
        //        }
        //        else
        //        {
        //            btFormat.SubStrings["Num"].Value = printinfo.NUM;//为Bartender里的数据源(文本框、条码等等)传值
        //            btFormat.SubStrings["ItemCode"].Value = printinfo.ITEMCODE;//为Bartender里的数据源(文本框、条码等等)传值
        //            btFormat.SubStrings["ItemName"].Value = printinfo.ITEMNAME;//为Bartender里的数据源(文本框、条码等等)传值
        //            btFormat.SubStrings["ProductLine"].Value = printinfo.BACKUP1;//为Bartender里的数据源(文本框、条码等等)传值
        //        }
        //        btFormat.SubStrings["CVICode"].Value = printinfo.CVICODE;//为Bartender里的数据源(文本框、条码等等)传值
        //        btFormat.SubStrings["IsOk"].Value = printinfo.ISOK; ;//为Bartender里的数据源(文本框、条码等等)传值
 
        //        btFormat.SubStrings["CreateTime"].Value = ((DateTime)printinfo.CREATETIME).ToString("yyyy-MM-dd");//为Bartender里的数据源(文本框、条码等等)传值
        //        Result nResult1 = btFormat.Print("标签打印软件", waitout, out messages);
        //        btFormat.PrintSetup.Cache.FlushInterval = CacheFlushInterval.Daily;
        //        Resolution rl = new Resolution(300, 300);
        //        //string name = DateTime.Now.ToLongDateString();
        //        // btFormat.ExportImageToFile(@"F:\"+"name.png", ImageType.PNG, ColorDepth.Mono, rl, OverwriteOptions.Overwrite);
        //        btFormat.Close(SaveOptions.DoNotSaveChanges);//不保存对打开模板的修改
        //        btEngine.Stop();
 
        //        if (nResult1.ToString() == "Success" || nResult1.ToString() == "0")
        //        {
        //            printinfo.STATE=1;
        //        }
        //        md.SaveChanges();  
        //    }
        //     Thread.Sleep(1000);
        //    }
        //}
        #region 枚举
 
        /// <summary>堆垛机号
        /// 
        /// </summary>
        public enum srmid
        {
            一号堆垛机 = 1,
            二号堆垛机 = 2,
            三号堆垛机 = 3,
            四号堆垛机 = 4,
            五号堆垛机 = 5,
            六号堆垛机 = 6,
            七号堆垛机 = 7,
            八号堆垛机 = 8,
            九号堆垛机 = 9
        }
 
        /// <summary>堆垛机命令
        /// 
        /// </summary>
        public enum command
        {
            搬运 = 1,
            单放 = 2,
            移动 = 3
        }
 
        /// <summary>堆垛机模式
        /// 
        /// </summary>
        public enum srmMode
        {
            自动模式 = 1,
            手动模式 = 2,
            半自动模式 = 3,
            维修模式 = 4,
            关机模式 = 0
        }
 
        /// <summary>堆垛机状态
        /// 
        /// </summary>
        public enum srmState
        {
            空闲 = 0,
            取货定位中 = 1,
            请求取货 = 2,
            取货中 = 3,
            取货完成放货定位中 = 4,
            请求放货 = 5,
            放货中 = 6,
            维修 = 98,
            报警 = 99
        }
 
        /// <summary>货叉位置
        /// 
        /// </summary>
        public enum liftPosition
        {
            货叉原位 = 0,
            货叉在左侧 = 1,
            货叉在右侧 = 2
        }
        /// <summary>RGVid枚举
        /// 
        /// </summary>
        public enum rgvid
        {
            一号RGV = 1,
            二号RGV = 2,
            三号RGV = 3,
            四号RGV = 4
        }
 
        #endregion
 
 
        /// <summary>自动选择任务号
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox2_SelectionChangeCommitted(object sender, EventArgs e)
        {
            int a = (int)Enum.Parse(typeof(srmid), comboBox2.Text);
            textBox1.Text = (a * 1111).ToString();
        }
        /// <summary>优化显示
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text == "单放" || comboBox1.Text == "移动")
            {
                textBox2.Enabled = false;
                //comboBox3.Enabled = false;
            }
            else
            {
                textBox2.Enabled = true;
                comboBox3.Enabled = true;
            }
        }
 
        /// <summary>发送堆垛机任务
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.Text == "搬运")
                {
                    int id = (int)Enum.Parse(typeof(srmid), comboBox2.Text);
                    int taskid = int.Parse(textBox1.Text);
                    int pallet = int.Parse(comboBox3.Text.Split('-')[0]);
                    int result = srmOPC.SendSrmTask(id, taskid, textBox2.Text, textBox3.Text, pallet, 1);
                    #region 判断返回值
                    if (result == 1)
                    {
                        label9.Text = id + "号堆垛机搬运成功发送";
                        label9.ForeColor = Color.Green;
                    }
                    else if (result == 2)
                    {
                        label9.Text = id + "号堆垛机搬运任务参数错误!!!!!!!!1";
                        label9.ForeColor = Color.Red;
                    }
                    else
                    {
                        label9.Text = id + "号堆垛机搬运发送失败!!!!!!!!1";
                        label9.ForeColor = Color.Red;
                    }
                    #endregion
                }
 
                else
                {
                    int id = (int)Enum.Parse(typeof(srmid), comboBox2.Text);
                    int taskid = int.Parse(textBox1.Text);
                    int comm = (int)Enum.Parse(typeof(command), comboBox1.Text);
                    int pallet = int.Parse(comboBox3.Text.Split('-')[0]);
                    int result = srmOPC.SendSrmMove(id, taskid, textBox3.Text, comm, pallet, 1);
                    #region 判断返回值
                    if (result == 1)
                    {
                        label9.Text = id + "号堆垛机单放或移动成功发送";
                        label9.ForeColor = Color.Green;
                    }
                    else if (result == 2)
                    {
                        label9.Text = id + "号堆垛机单放或移动任务参数错误!!!!!!!!1";
                        label9.ForeColor = Color.Red;
                    }
                    else
                    {
                        label9.Text = id + "号堆垛机单放或移动任务发送失败!!!!!!!!1";
                        label9.ForeColor = Color.Red;
                    }
                    #endregion
                }
            }
            catch (Exception)
            {
                label9.Text = "堆垛机单放或移动任务发送异常!!!!!!!!1";
                label9.ForeColor = Color.Red;
            }
 
 
        }
 
 
        /// <summary>确认任务
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                int id = (int)Enum.Parse(typeof(srmid), comboBox2.Text);
                int result = srmOPC.SendSrmTaskFinishConfirm(id);
                if (result == 1)
                {
                    label9.Text = id + "号堆垛机确认任务发送成功";
                    label9.ForeColor = Color.Green;
                }
                else
                {
                    label9.Text = id + "号堆垛机确认任务发送失败";
                    label9.ForeColor = Color.Red;
                }
            }
            catch (Exception)
            {
 
                label9.Text = "堆垛机确认任务发送异常";
                label9.ForeColor = Color.Red;
            }
 
 
        }
        /// <summary>堆垛机解除报警
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                int id = (int)Enum.Parse(typeof(srmid), comboBox2.Text);
                srmOPC.SrmRlsAlert(id);
            }
            catch (Exception)
            {
                label9.Text = "堆垛机解除报警发送异常";
                label9.ForeColor = Color.Red;
            }
 
 
        }
        /// <summary>删除任务
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                int id = (int)Enum.Parse(typeof(srmid), comboBox2.Text);
                int result = srmOPC.SendSrmDelete(id);
                if (result == 1)
                {
                    label9.Text = id + "号堆垛机删除任务发送成功";
                    label9.ForeColor = Color.Green;
                }
                else
                {
                    label9.Text = id + "号堆垛机删除任务发送失败";
                    label9.ForeColor = Color.Red;
                }
            }
            catch (Exception)
            {
 
                label9.Text = "堆垛机删除任务发送异常";
                label9.ForeColor = Color.Red;
            }
            MessageBox.Show("已单独删除此堆垛机任务");
        }
        /// <summary>急停任务
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                int id = (int)Enum.Parse(typeof(srmid), comboBox2.Text);
                int result = srmOPC.SendSrmEStop(id);
                if (result == 1)
                {
                    label9.Text = id + "号堆垛机急停任务发送成功";
                    label9.ForeColor = Color.Green;
                }
                else
                {
                    label9.Text = id + "号堆垛机急停任务发送失败";
                    label9.ForeColor = Color.Red;
                }
            }
            catch (Exception)
            {
 
                label9.Text = "堆垛机急停任务发送异常";
                label9.ForeColor = Color.Red;
            }
        }
 
        /// <summary>读取堆垛机状态
        /// 
        /// </summary>
        public void srmInfo()
        {
            while (true)
            {
                try
                {
                    Thread.Sleep(1000);
                    // var ff  =srmOPC.GetSrmInfo(srmNO);
                    SrmInfo = JsonConvert.DeserializeObject<srmInfo>(srmOPC.GetSrmInfo(srmNO));
                    label10.Text = "心跳:" + SrmInfo.RhandShake;
                    label11.Text = "模式:" + Enum.GetName(typeof(srmMode), SrmInfo.Rmode);
                    label18.Text = "状态:" + Enum.GetName(typeof(srmState), SrmInfo.Rstate);
                    label19.Text = "堆垛机名:" + SrmInfo.SrmName;
                    label17.Text = "系统任务号:" + SrmInfo.RtaskNO + " 设备任务号:" + SrmInfo.RDevicetaskNO;
                    label12.Text = "是否报警:" + (SrmInfo.Ralarm == 1 ? "报警状态" : "状态正常");
                    label13.Text = "是否有货:" + (SrmInfo.RliftFull == 1 ? "货叉上有货" : "货叉上无货");
                    label14.Text = "当前列:" + SrmInfo.Rposx + "列";
                    label15.Text = "当前层:" + SrmInfo.Rposy + "层";
                    label16.Text = "货叉位置:" + Enum.GetName(typeof(liftPosition), SrmInfo.Rposz);
                    label29.Text = "报警内容:" + Enum.GetName(typeof(ESrmAlarm), SrmInfo.RalarmNumber);
 
                    label54.Text = "任务是否完成:" + SrmInfo.RtaskFinish;
                }
                catch (Exception ex)
                {
                    label19.Text = "堆垛机名:" + SrmInfo.SrmName + "产生异常" + ex;
 
                }
 
 
            }
 
        }
 
        /// <summary>选择设备
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            srmNO = (int)Enum.Parse(typeof(srmid), comboBox4.Text);
        }
 
        /// <summary>发送RGV任务
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            int rgvID = (int)Enum.Parse(typeof(rgvid), comboBox5.Text);
            int pallet = int.Parse(comboBox8.Text.Split('-')[0]);
            int result = rgvOPC.SendRgvTask(rgvID, rgvID * 1111, Convert.ToInt32(textBox5.Text), Convert.ToInt32(textBox6.Text), (comboBox6.Text == "搬运" ? 1 : 2), pallet, 1);
            if (result == 1)
            {
                label1.Text = rgvID + "号RGV任务发送成功";
                label1.ForeColor = Color.Green;
            }
            else
            {
                label1.Text = rgvID + "号RGV任务发送失败";
                label1.ForeColor = Color.Red;
            }
 
        }
 
        /// <summary>自动设置任务号
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {
            int a = (int)Enum.Parse(typeof(rgvid), comboBox5.Text);
            textBox4.Text = (a * 1111).ToString();
        }
 
        /// <summary>优化显示
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
        {
 
        }
 
        /// <summary>RGV确认任务
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button10_Click(object sender, EventArgs e)
        {
            int rgvID = (int)Enum.Parse(typeof(rgvid), comboBox5.Text);
            bool isok = rgvOPC.SendRgvFinishConfirm(rgvID);
            if (isok)
            {
                label1.Text = rgvID + "号RGV任务确认成功";
                label1.ForeColor = Color.Green;
 
            }
            else
            {
                label1.Text = rgvID + "号RGV任务确认失败";
                label1.ForeColor = Color.Red;
            }
        }
 
        /// <summary>RGV解警
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button8_Click(object sender, EventArgs e)
        {
            int rgvID = (int)Enum.Parse(typeof(rgvid), comboBox5.Text);
            int result = rgvOPC.SendRgvRlsAlert(rgvID);
            if (result == 1)
            {
                label1.Text = rgvID + "号RGV解警成功";
                label1.ForeColor = Color.Green;
 
            }
            else
            {
                label1.Text = rgvID + "号RGV解警失败";
                label1.ForeColor = Color.Red;
            }
        }
        /// <summary>删除RGV任务
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_Click(object sender, EventArgs e)
        {
            int rgvID = (int)Enum.Parse(typeof(rgvid), comboBox5.Text);
            int result = rgvOPC.SendRgvDelete(rgvID);
            if (result == 1)
            {
                label1.Text = rgvID + "号RGV删除任务成功";
                label1.ForeColor = Color.Green;
 
            }
            else
            {
                label1.Text = rgvID + "号RGV删除任务失败";
                label1.ForeColor = Color.Red;
            }
        }
        /// <summary>选择RGV信息展示
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox7_SelectedIndexChanged(object sender, EventArgs e)
        {
            rgvNO = (int)Enum.Parse(typeof(rgvid), comboBox7.Text);
        }
        /// <summary>获取RGV信息
        /// 
        /// </summary>
        public void rgvinfo()
        {
            while (true)
            {
                try
                {
                    Thread.Sleep(1000);
                    Rgvinfo = JsonConvert.DeserializeObject<rgvINFO>(rgvOPC.GetRgvInfo(rgvNO));
                    label33.Text = "RGV名:" + Rgvinfo.RgvName;
                    label34.Text = "心跳:" + Rgvinfo.handShake;
                    label32.Text = "模式:" + (Rgvinfo.onLine == 3 ? "自动" : "非自动");
                    label25.Text = "状态:" + Rgvinfo.state;
                    label26.Text = "系统任务号:" + Rgvinfo.taskId + " 设备任务号:" + Rgvinfo.deviceTaskId;
                    label31.Text = "是否报警:" + (Rgvinfo.ararm == 1 ? "报警状态" : "状态正常");
                    label30.Text = "是否有货:" + (Rgvinfo.loaded == 1 ? "RGV有货" : "RGV无货");
                    label27.Text = "rgv当前位置:" + Rgvinfo.pos;
                    label28.Text = "报警代码:" + Enum.GetName(typeof(ESrgvAlarm), Rgvinfo.alarmCode);
                    label53.Text = "任务是否完成:" + Rgvinfo.taskFinish;
                    label57.Text = "RGV可用:" + Rgvinfo.useful;
 
                }
                catch (Exception)
                {
                    label33.Text = "RGV名:" + Rgvinfo.RgvName + "异常";
                }
 
 
            }
        }
        /// <summary>发送输送线任务
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button9_Click(object sender, EventArgs e)
        {
            try
            {
                int pallet = int.Parse(comboBox9.Text.Split('-')[0]);
                int result = tranOPC.SendGoodsReady(Convert.ToInt32(textBox9.Text), Convert.ToInt32(textBox8.Text), Convert.ToInt32(textBox7.Text), 1);
                if (result == 1)
                {
                    label40.Text = "信息提示:输送线任务发送成功";
                    label40.ForeColor = Color.Green;
                }
            }
            catch (Exception)
            {
                label40.Text = "信息提示:输送线任务发送异常";
                label40.ForeColor = Color.Red;
 
            }
 
 
        }
        /// <summary>获取输送线信息
        /// 
        /// </summary>
        public void traninfo()
        {
            while (true)
            {
                try
                {
                    Thread.Sleep(1000);
                    TranInfo = JsonConvert.DeserializeObject<tranInfo>(tranOPC.GetSrmConveyorStationInfo(tranNO));
                    label49.Text = "输送线编号:" + TranInfo.stationNo;
                    label50.Text = "心跳:" + TranInfo.handShake;
                    label48.Text = "模式:" + (TranInfo.auto == true ? "自动" : "非自动");
                    label42.Text = "故障:" + TranInfo.err;
                    label43.Text = "系统任务号:" + TranInfo.taskId + " 设备任务号:" + TranInfo.deviceTaskId;
                    label47.Text = "报警号:" + Enum.GetName(typeof(ETranAlarm), TranInfo.errId);
                    label46.Text = "是否有货:" + (TranInfo.goods == true ? "有货" : "无货");
                    label45.Text = "货物重量:" + TranInfo.weihgt;
 
                    label44.Text = "外形检测:" + (tranOPC.IsPassed(tranNO) == 0 ? "到位正常" : "不正常");
                    //label44.Text = "外形检测:" + (TranInfo.shapeCheck == 3 ? "到位正常" : "不正常");
                    label55.Text = "条码:" + TranInfo.Code;
                    label56.Text = "允许下发任务:" + TranInfo.TaskSendAllow;
                    //获取入口是否有新货(测试)
                    //tranOPC.HasTranGoods();
                    //tranOPC.ClearTranGoods(61);
                }
                catch (Exception)
                {
                    label33.Text = "RGV名:" + Rgvinfo.RgvName + "异常";
                }
 
 
            }
        }
 
        /// <summary>窗口关闭
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Environment.Exit(0);
        }
        /// <summary>输送线设备选择
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox10_TextChanged(object sender, EventArgs e)
        {
            try
            {
                tranNO = Convert.ToInt32(textBox10.Text);
            }
            catch
            {
 
            }
        }
 
        private void button11_Click(object sender, EventArgs e)
        {
            int rgvID = (int)Enum.Parse(typeof(rgvid), comboBox5.Text);
            int result = rgvOPC.SendRgvStop(rgvID);
            if (result == 1)
            {
                label1.Text = rgvID + "号RGV急停成功";
                label1.ForeColor = Color.Green;
 
            }
            else
            {
                label1.Text = rgvID + "号RGV急停失败";
                label1.ForeColor = Color.Red;
            }
        }
 
        private void button14_Click(object sender, EventArgs e)
        {
            int tranid = int.Parse(textBox10.Text);
            int result = tranOPC.SendTranReset(tranid);
            if (result == 1)
            {
                label40.Text = tranid + "号输送线解警成功";
                label40.ForeColor = Color.Green;
 
            }
            else
            {
                label40.Text = tranid + "号输送线解警失败";
                label40.ForeColor = Color.Red;
            }
        }
 
        private void button13_Click(object sender, EventArgs e)
        {
            int tranid = int.Parse(textBox10.Text);
            int result = tranOPC.SendTranDelete(tranid);
            if (result == 1)
            {
                label40.Text = tranid + "号输送线删除成功";
                label40.ForeColor = Color.Green;
 
            }
            else
            {
                label40.Text = tranid + "号输送线删除失败";
                label40.ForeColor = Color.Red;
            }
        }
 
        private void button12_Click(object sender, EventArgs e)
        {
            int tranid = int.Parse(textBox10.Text);
            int result = tranOPC.SendTranStop(tranid);
            if (result == 1)
            {
                label40.Text = tranid + "号输送线急停成功";
                label40.ForeColor = Color.Green;
 
            }
            else
            {
                label40.Text = tranid + "号输送线急停失败";
                label40.ForeColor = Color.Red;
            }
        }
 
        private void button15_Click_1(object sender, EventArgs e)
        {
            int rgvID = (int)Enum.Parse(typeof(rgvid), comboBox5.Text);
            bool result = rgvOPC.ChangeUseful(rgvID);
            if (result)
            {
                label1.Text = rgvID + "号RGV切换成功";
                label1.ForeColor = Color.Lime;
 
            }
            else
            {
                label1.Text = rgvID + "号RGV切换失败";
                label1.ForeColor = Color.Red;
            }
 
        }
 
        /// <summary>
        /// 删除关联任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button16_Click(object sender, EventArgs e)
        {
            //int rgvID = (int)Enum.Parse(typeof(rgvid), comboBox5.Text);
            //int result = rgvOPC.SendRgvDeleteAll(rgvID);
            //if (result == 1)
            //{
            //    label1.Text = rgvID + "号RGV删除任务成功";
            //    label1.ForeColor = Color.Green;
 
            //}
            //else
            //{
            //    label1.Text = rgvID + "号RGV删除任务失败";
            //    label1.ForeColor = Color.Red;
            //}
 
        }
 
        private void button17_Click(object sender, EventArgs e)
        {
            int id = (int)Enum.Parse(typeof(srmid), comboBox2.Text);
            int result = srmOPC.SendSrmDelete(id);
            if (result == 1)
            {
                label9.Text = id + "号堆垛机删除任务成功";
                label9.ForeColor = Color.Green;
 
            }
            else
            {
                label9.Text = id + "号堆垛机删除任务失败";
                label9.ForeColor = Color.Red;
            }
        }
 
        private void button18_Click(object sender, EventArgs e)
        {
            int tranid = int.Parse(textBox10.Text);
            int result = tranOPC.SendTranDelete(tranid);
            if (result == 1)
            {
                label40.Text = tranid + "号输送线删除任务成功";
                label40.ForeColor = Color.Green;
 
            }
            else if (result == 0)
            {
                label40.Text = tranid + "号输送线删除任务失败";
                label40.ForeColor = Color.Red;
            }
            else if (result == 2)
            {
                label40.Text = tranid + "号输送线删除任务失败,编号校验失败(可能其它线程访问中),请重发";
                label40.ForeColor = Color.Red;
            }
        }
 
        private void button19_Click(object sender, EventArgs e)
        {
            int[] srmid;
            int[] rgvid;
            int[] tranid;
            int dir = 0;
            string[] place;
            int mainid = int.Parse(textBox11.Text);
            string containerName = textBox11.Text.Trim();
            if (srmOPC.DeleteMainTask(mainid, out tranid, out rgvid, out srmid, out place, out dir))
            //if (srmOPC.DeleteMainTaskByContainerName(containerName, out tranid, out rgvid, out srmid, out place,out dir))
            {
                string text = "";
                foreach (var i in place)
                {
                    text = i + " ";
                }
                MessageBox.Show("拿出" + text);
            }
            else
            {
                MessageBox.Show("删除失败,未修改数据");
            }
 
            if (!tranOPC.UnlockTran(dir))
            {
                if (dir == 1)
                {
                    MessageBox.Show("输送线解锁失败,请开至安全位后手动解锁:东");
                }
                else if (dir == 0)
                {
                    MessageBox.Show("输送线解锁失败,请开至安全位后手动解锁:西");
                }
            }
        }
 
        private void button20_Click(object sender, EventArgs e)
        {
            int dir = 2;
            if (cbDir.Text == "东:冲压")
            {
                dir = 1;
            }
            else if (cbDir.Text == "西:焊装")
            {
                dir = 0;
            }
            else
            {
                MessageBox.Show("请选择东或者西");
            }
            bool result = tranOPC.UnlockTran(dir);
            if (result)
            {
                MessageBox.Show("解锁成功");
            }
            else
            {
                MessageBox.Show("未修改一条数据,请检查RGV是否开至安全位,可能原始数据即可用");
            }
        }
 
        private void button21_Click(object sender, EventArgs e)
        {
            int dir = 2;
            if (cbDir.Text == "东:冲压")
            {
                dir = 1;
            }
            else if (cbDir.Text == "西:焊装")
            {
                dir = 0;
            }
            else
            {
                MessageBox.Show("请选择东或者西");
            }
            bool result = tranOPC.UnFullTran(dir);
            if (result)
            {
                MessageBox.Show("解占用成功");
            }
            else
            {
                MessageBox.Show("未修改一条数据,可能原始数据即可用");
            }
        }
 
        private void button22_Click(object sender, EventArgs e)
        {
            int id = (int)Enum.Parse(typeof(srmid), comboBox2.Text);
            bool open = srmOPC.UseAutoMove(id);
            if (open)
            {
                button22.Text = "切换优化:开启";
            }
            else
            {
                button22.Text = "切换优化:关闭";
            }
        }
 
        private void btnSearch_Click(object sender, EventArgs e)
        {
            int taskid = 0;
            bool result = int.TryParse(tbTaskId.Text.Trim(), out taskid);
            if (result)
            {
                List<taskCommon> taskViewList = new List<taskCommon>();
                using (Model edm = new Model())
                {
                    TASK_TASK task = edm.TASK_TASK.FirstOrDefault(x => x.ID == taskid);
                    if (task.TASKTYPE == 2)
                    {
                        foreach (var i in task.SrmTask)
                        {
                            taskCommon taskCommon = new taskCommon();
                            taskCommon.id = i.ID;
                            taskCommon.device = "堆垛机";
                            taskCommon.deviceNum = i.USESRMID;
                            taskCommon.sourcePlace = i.SOURCEPLACE;
                            taskCommon.toPlace = i.TOPLACE;
                            taskCommon.isReleased = i.ISRELEASED == 1 ? "是" : "否";
                            taskCommon.hasFinished = i.HASFINISHED == 1 ? "是" : "否";
                            taskViewList.Add(taskCommon);
                        }
                        foreach (var i in task.RgvTask.OrderBy(x => x.ID))
                        {
                            taskCommon taskCommon = new taskCommon();
                            taskCommon.id = i.ID;
                            taskCommon.device = "RGV";
                            taskCommon.deviceNum = i.USERGVID;
                            taskCommon.sourcePlace = i.SOURCEPLACE.ToString();
                            taskCommon.toPlace = i.TOPLACE.ToString();
                            taskCommon.isReleased = i.ISRELEASED == 1 ? "是" : "否";
                            taskCommon.hasFinished = i.HASFINISHED == 1 ? "是" : "否";
                            taskViewList.Add(taskCommon);
                        }
                        //foreach (var i in task.TranTasks)
                        //{
                        //    taskCommon taskCommon = new taskCommon();
                        //    taskCommon.id = i.ID;
                        //    taskCommon.device = "输送线";
                        //    taskCommon.deviceNum = i.SOURCEPLACE;
                        //    taskCommon.sourcePlace = i.SOURCEPLACE.ToString();
                        //    taskCommon.toPlace = i.TOPLACE.ToString();
                        //    taskCommon.isReleased = i.ISRELEASED == 1 ? "是" : "否";
                        //    taskCommon.hasFinished = i.HASFINISHED == 1 ? "是" : "否";
                        //    taskViewList.Add(taskCommon);
                        //}
                    }
                    else if (task.TASKTYPE == 1)
                    {
                        foreach (var i in task.TranTask)
                        {
                            taskCommon taskCommon = new taskCommon();
                            taskCommon.id = i.ID;
                            taskCommon.device = "输送线";
                            taskCommon.deviceNum = i.SOURCEPLACE;
                            taskCommon.sourcePlace = i.SOURCEPLACE.ToString();
                            taskCommon.toPlace = i.TOPLACE.ToString();
                            taskCommon.isReleased = i.ISRELEASED == 1 ? "是" : "否";
                            taskCommon.hasFinished = i.HASFINISHED == 1 ? "是" : "否";
                            taskViewList.Add(taskCommon);
                        }
                        foreach (var i in task.RgvTask.OrderBy(x => x.ID))
                        {
                            taskCommon taskCommon = new taskCommon();
                            taskCommon.id = i.ID;
                            taskCommon.device = "RGV";
                            taskCommon.deviceNum = i.USERGVID;
                            taskCommon.sourcePlace = i.SOURCEPLACE.ToString();
                            taskCommon.toPlace = i.TOPLACE.ToString();
                            taskCommon.isReleased = i.ISRELEASED == 1 ? "是" : "否";
                            taskCommon.hasFinished = i.HASFINISHED == 1 ? "是" : "否";
                            taskViewList.Add(taskCommon);
                        }
                        foreach (var i in task.SrmTask)
                        {
                            taskCommon taskCommon = new taskCommon();
                            taskCommon.id = i.ID;
                            taskCommon.device = "堆垛机";
                            taskCommon.deviceNum = i.USESRMID;
                            taskCommon.sourcePlace = i.SOURCEPLACE;
                            taskCommon.toPlace = i.TOPLACE;
                            taskCommon.isReleased = i.ISRELEASED == 1 ? "是" : "否";
                            taskCommon.hasFinished = i.HASFINISHED == 1 ? "是" : "否";
                            taskViewList.Add(taskCommon);
                        }
                    }
                    else if (task.TASKTYPE == 3)
                    {
                        foreach (var i in task.SrmTask)
                        {
                            taskCommon taskCommon = new taskCommon();
                            taskCommon.id = i.ID;
                            taskCommon.device = "堆垛机";
                            taskCommon.deviceNum = i.USESRMID;
                            taskCommon.sourcePlace = i.SOURCEPLACE;
                            taskCommon.toPlace = i.TOPLACE;
                            taskCommon.isReleased = i.ISRELEASED == 1 ? "是" : "否";
                            taskCommon.hasFinished = i.HASFINISHED == 1 ? "是" : "否";
                            taskViewList.Add(taskCommon);
                        }
 
                    }
 
                    dGVQuery.DataSource = taskViewList;
                }
            }
            else
            {
                dGVQuery.DataSource = null;
                MessageBox.Show("输入正确格式任务号");
            }
        }
 
        private void button23_Click(object sender, EventArgs e)
        {
            int rgvID = (int)Enum.Parse(typeof(rgvid), comboBox5.Text);
            if (!rgvOPC.IsRgvReady(rgvID))
            {
                label1.Text = rgvID + "号RGV设备状态不空闲,不允许重发任务";
                label1.ForeColor = Color.Red;
            }
            RgvTask rgvTask = null;
            int palletType = 0;
            using (Model edm = new Model())
            {
                rgvTask = edm.RgvTask.AsNoTracking().OrderByDescending(u => u.ID).FirstOrDefault(x => x.ISRELEASED == 1 && x.HASFINISHED == 0 && x.USERGVID == rgvID);
                if (rgvTask != null)
                {
                    if (rgvTask.TASK_TASK.TASKSTATUS != "任务执行中")
                    {
                        label1.Text = rgvID + "号RGV没有需要重发的任务";
                        label1.ForeColor = Color.Red;
                        return;
                    }
                    try
                    {
                        palletType = rgvTask.TASK_TASK.BASE_CONTAINER.BASE_PALLET.PALLETCODE ?? 0;
                    }
                    catch
                    {
                        label1.Text = rgvID + "号器具类型转换异常";
                        label1.ForeColor = Color.Red;
                        return;
                    }
                }
 
            }
            if (rgvTask != null)
            {
                //int result = rgvOPC.SendRgvTask(rgvID, rgvID * 1111, Convert.ToInt32(textBox5.Text), Convert.ToInt32(textBox6.Text), (comboBox6.Text == "搬运" ? 1 : 2), pallet, 1);
                int result = rgvOPC.SendRgvTask(rgvTask.USERGVID, (rgvTask.ID % 32767) + 1, rgvTask.SOURCEPLACE, rgvTask.TOPLACE, rgvTask.TASKTYPE, palletType, (rgvTask.FASTHERTASKID % 32767) + 1);
 
                if (result == 1)
                {
                    label1.Text = rgvID + "号RGV任务发送成功";
                    label1.ForeColor = Color.Green;
                }
                else
                {
                    label1.Text = rgvID + "号RGV任务发送失败";
                    label1.ForeColor = Color.Red;
                }
            }
            else
            {
                label1.Text = rgvID + "号RGV没有需要重发的任务";
                label1.ForeColor = Color.Red;
            }
        }
    }
}