-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
2716 lines (2117 loc) · 86.4 KB
/
MainWindow.xaml.cs
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Arthas.Controls;
using Arthas.Utility.Media;
using BodDetect.BodDataManage;
using BodDetect.DataBaseInteractive.Sqlite;
using BodDetect.Event;
using BodDetect.PagerDataModels;
using BodDetect.UDP;
using MahApps.Metro.Controls.Dialogs;
using System.Configuration;
using BodDetect.DataBaseInteractive;
namespace BodDetect
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : MahApps.Metro.Controls.MetroWindow, IDisposable
{
private Dictionary<byte, MetroProgressBar> metroProgressBars = new Dictionary<byte, MetroProgressBar>();
private Dictionary<byte, EventHandler> ProcessHandlers = new Dictionary<byte, EventHandler>();
private Dictionary<byte, MetroSwitch> ValveDic = new Dictionary<byte, MetroSwitch>();
private Dictionary<byte, MetroSwitch> MultiValveDic = new Dictionary<byte, MetroSwitch>();
DispatcherTimer timer = new DispatcherTimer();
DispatcherTimer UpdataStatusTimer = new DispatcherTimer();
DispatcherTimer StandWaterTimer = new DispatcherTimer();
DispatcherTimer RunTimer = new DispatcherTimer();
DispatcherTimer CodTimer = new DispatcherTimer();
public Process kbpr;
private BodHelper bodHelper;
public BodData bodData = new BodData();
public ProgressDialogController progressDialog;
private CancellationTokenSource StopCts = new CancellationTokenSource();
MainWindow_Model mainWindow_Model = new MainWindow_Model();
ConfigData configData = new ConfigData();
private bool disposedValue;
public bool DataGridIsEdit = false;
public Task BodCurrentRunTask;
public MainWindow()
{
InitializeComponent();
metroProgressBars.Add(PLCConfig.WaterValveBit, WaterView);
metroProgressBars.Add(PLCConfig.bufferValveBit, CacheView);
metroProgressBars.Add(PLCConfig.StandardValveBit, StandardView);
metroProgressBars.Add(Convert.ToByte((ushort)100), PumpView);
metroProgressBars.Add(PLCConfig.AirValveBit, AirView);
metroProgressBars.Add(PLCConfig.NormalValveBit, NormalView);
metroProgressBars.Add(PLCConfig.SampleValveBit, SampleView);
metroProgressBars.Add(PLCConfig.DepositValveBit, StoreWaterView);
metroProgressBars.Add(Convert.ToByte((ushort)101), WaterSampleView);
ProcessHandlers.Add(PLCConfig.WaterValveBit, RefeshWaterProcessEvent);
ProcessHandlers.Add(PLCConfig.bufferValveBit, RefeshCachProcessEvent);
ProcessHandlers.Add(PLCConfig.StandardValveBit, RefeshStandProcessEvent);
ProcessHandlers.Add(Convert.ToByte((ushort)100), RefeshPumpProcessEvent);
ProcessHandlers.Add(PLCConfig.AirValveBit, RefeshAirProcessEvent);
ProcessHandlers.Add(PLCConfig.NormalValveBit, RefeshNormalProcessEvent);
ProcessHandlers.Add(PLCConfig.SampleValveBit, RefeshSampleProcessEvent);
ProcessHandlers.Add(PLCConfig.DepositValveBit, RefeshStoreWaterProcessEvent);
ProcessHandlers.Add(Convert.ToByte((ushort)101), RefeshWaterSampleProcessEvent);
ValveDic.Add(PLCConfig.WaterValveBit, WaterValve);
ValveDic.Add(PLCConfig.bufferValveBit, CacheValve);
ValveDic.Add(PLCConfig.StandardValveBit, StandValve);
ValveDic.Add(PLCConfig.AirValveBit, AirValve);
ValveDic.Add(PLCConfig.NormalValveBit, NormalValve);
ValveDic.Add(PLCConfig.SampleValveBit, Valve);
ValveDic.Add(PLCConfig.DepositValveBit, StoreValve);
//ValveDic.Add(PLCConfig.CisternValveBit, RowValve);
//ValveDic.Add(PLCConfig.WashValveBit, WashValve);
//ValveDic.Add(PLCConfig.BodDrainValveBit, BodRowValve);
initAsync();
this.DataContext = mainWindow_Model;
}
public async void initAsync()
{
try
{
string PLCip = ConfigurationManager.AppSettings["PLCip"];
string PLCport = ConfigurationManager.AppSettings["PLCport"];
LogUtil.Log(PLCip + PLCport);
string[] value = PLCip.Split('.');
if (value.Length < 4)
{
await this.ShowMessageAsync("Error", "异常ip!", MessageDialogStyle.Affirmative);
}
int port = Convert.ToInt32(PLCport, 10);
bodHelper = new BodHelper(PLCip, port);
bodHelper.Init();
bodHelper.refreshProcess = new BodHelper.RefreshUI(RefeshProcess);
bodHelper.refreshStaus = new BodHelper.RefreshStaus(RefreshStatus);
bodHelper.refreshData = new BodHelper.RefreshData(RefreshData);
bodHelper.refreshProcessStatus = new BodHelper.RefreshProcessStatus(RefreshProcessStatus);
bodHelper.addAlramInfo = new BodHelper.AddAlramInfo(AddAlarmInfo);
bodHelper.mainWindow = this;
initConfig();
UpdataBodStatus();
if (!bodHelper.IsConnectPlc)
{
LogUtil.LogError("连接PLC异常");
await this.ShowMessageAsync("Error", "连接PLC异常!");
return;
}
if (!bodHelper.ConnectSeri)
{
LogUtil.LogError("连接串口异常");
await this.ShowMessageAsync("Error", "连接串口异常!");
return;
}
await this.Dispatcher.InvokeAsync(() => Start());
}
catch (Exception ex)
{
LogUtil.LogError(ex, "开机启动initAsync");
await this.ShowMessageAsync("Error", "连接PLC异常!");
}
}
private async void Start()
{
try
{
UpdataStatusTimer.Tick += UpdateDevStatus;
int times = Convert.ToInt32(UpdataStatus.Text);
UpdataStatusTimer.Interval = new TimeSpan(0, times, 0);
int SpaceHour = Convert.ToInt32(sampleSpac.Text);
start.IsChecked = true;
if (!bodHelper.IsSampling)
{
initConfig();
Task initTask = await Task.Factory.StartNew(() => bodHelper.PreInitAsync());
if (!System.IO.File.Exists(XmlHelp.Xmlpath))
{
XmlHelp.createXml(XmlHelp.Xmlpath);
}
Task.WaitAll(initTask);
StandWaterTimer.Tick += StartStandWaterAsync;
StandWaterTimer.Interval = new TimeSpan(0, 1, 0, 0);
StandWaterTimer.Start();
//Task StandTask = Task.Factory.StartNew(() => bodHelper.StartBodStandWater());
//TimeSpan timeSpan = new TimeSpan(SpaceHour, 0, 0);
//Task.WaitAll(StandTask);
if (RunTimer == null || !RunTimer.IsEnabled)
{
RunTimer.Tick += BodRun;
RunTimer.Interval = new TimeSpan(SpaceHour, 0, 0);
RunTimer.Start();
}
BodCurrentRunTask = Task.Factory.StartNew(() => bodHelper.StartBodDetect(StopCts.Token), StopCts.Token);
_loading.Visibility = Visibility.Visible;
}
else
{
bodHelper.manualevent.Set();
_loading.Visibility = Visibility.Collapsed;
}
}
catch (Exception ex)
{
LogUtil.LogError(ex, "开机启动异步线程Start");
}
}
private void MetroButton_Click(object sender, RoutedEventArgs e)
{
float[] DoDota = bodHelper.GetDoData();
Thread.Sleep(3000);
uint[] TurbidityData = bodHelper.GetTurbidityData();
Thread.Sleep(3000);
float[] PHData = bodHelper.GetPHData();
Thread.Sleep(3000);
bodData.TemperatureData = DoDota[0];
bodData.DoData = DoDota[1];
bodData.TurbidityData = (float)TurbidityData[0] / 1000;
bodData.PHData = PHData[1];
//bodData.CodData = (float)CODData[0] / 100;
// if (finsClient == null)
// {
// MessageBox.Show("PLC 未连接");
// return;
// }
// ushort Address = Convert.ToUInt16(address_TextBox.Text);
// byte bitAddress = Convert.ToByte(Bit_TextBox.Text);
// ushort Count = Convert.ToUInt16(DataCount_TextBox.Text);
//// string area = MemoryAreaCode_combo.SelectedItem.ToString();
// byte AreaCode = 0X82;
// ushort[] data = finsClient.ReadData(Address, bitAddress, Count, AreaCode);
// foreach (var item in data)
// {
// string value = Convert.ToString(item) + "\r\n";
// Data_TextBox.AppendText(value);
// }
}
//private void MetroButton_Click_1(object sender, RoutedEventArgs e)
//{
// try
// {
// string ip = IP_textbox.Text;
// string[] value = ip.Split('.');
// if (value.Length < 4)
// {
// this.ShowMessageAsync("Error", "异常ip!");
// }
// int port = Convert.ToInt32(Port_TextBox.Text);
// bool success = bodHelper.ConnectPlc();
// if (success)
// {
// this.ShowMessageAsync("与PLC通讯", "连接成功!", MessageDialogStyle.Affirmative);
// }
// }
// catch (Exception ex)
// {
// LogUtil.LogError(ex);
// this.ShowMessageAsync("Error", "连接PLC异常!");
// }
//}
//private void ResetIp_Click(object sender, RoutedEventArgs e)
//{
// IP_textbox.Text = "192.168.0.174";
// Port_TextBox.Text = "9600";
//}
private void RefreshData(BodData data)
{
mainWindow_Model.BodData = data.Bod;
mainWindow_Model.CodData = data.CodData;
mainWindow_Model.DoData = data.DoData;
mainWindow_Model.PHData = data.PHData;
mainWindow_Model.TemperatureData = data.TemperatureData;
mainWindow_Model.TurbidityData = data.TurbidityData;
mainWindow_Model.Uv254Data = data.Uv254Data;
mainWindow_Model.HumidityDataData = data.HumidityData;
mainWindow_Model.AirTemperatureData = data.AirTemperatureData;
if (mainWindow_Model.BodData < 0 || mainWindow_Model.BodData > 1000)
{
//BOD.Foreground = new SolidColorBrush( Color.FromRgb(255,0,0));
BOD_.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0));
}
HisDatabase hisDatabase = new HisDatabase();
hisDatabase.DoData = mainWindow_Model.DoData;
hisDatabase.DoDataUnit = "mg/L";
hisDatabase.PHData = mainWindow_Model.PHData;
hisDatabase.TemperatureData = mainWindow_Model.TemperatureData;
hisDatabase.TemperatureUnit = "C";
hisDatabase.TurbidityData = mainWindow_Model.TurbidityData;
hisDatabase.TurbidityUnit = "mg/L";
hisDatabase.Bod = mainWindow_Model.BodData;
hisDatabase.CodData = mainWindow_Model.CodData;
hisDatabase.Uv254Data = mainWindow_Model.Uv254Data;
hisDatabase.BodElePot = data.BodElePot;
hisDatabase.BodElePotDrop = data.BodElePotDrop;
hisDatabase.CreateDate = data.CreateDate;
hisDatabase.CreateTime = data.CreateTime;
mainWindow_Model.HisParamData.AddData(hisDatabase);
HisDataBaseModel hisDataBaseModel = new HisDataBaseModel();
hisDatabase.CopyToHisDataBaseModel(hisDataBaseModel);
Task.Factory.StartNew(() => BodSqliteHelp.InsertHisBodData(hisDataBaseModel));
// Task.Factory.StartNew(() => MySqlHelper.InsertBodData(hisDataBaseModel));
mainWindow_Model.UpdateSensorStatus();
}
private void MetroButton_Click_2(object sender, RoutedEventArgs e)
{
float[] floatData = bodHelper.GetDoData();
floatData = bodHelper.GetPHData();
uint[] value = bodHelper.GetTurbidityData();
byte[] IOCmd = { PLCConfig.WashValveBit, PLCConfig.BodDrainValveBit };
bodHelper.ValveControl(PLCConfig.Valve2Address, IOCmd);
byte[] IOCmd2 = { PLCConfig.DepositValveBit, PLCConfig.StandardValveBit };
bodHelper.ValveControl(PLCConfig.Valve1Address, IOCmd2);
}
#region 委托处理
public void RefeshProcess(DelegateParam param)
{
try
{
if (metroProgressBars.Count < 0)
return;
switch (param.State)
{
case ProcessState.ShowData:
metroProgressBars[param.Uid].Value = (double)param.Data;
break;
case ProcessState.AutoAdd:
ProcessAutoAdd(param);
break;
case ProcessState.AutoRed:
break;
case ProcessState.Hidden:
break;
case ProcessState.Show:
break;
}
}
catch (Exception ex)
{
LogUtil.LogError(ex);
return;
}
}
/// <summary>
/// 多通阀进度条控制
/// </summary>
/// <param name="param"></param>
public void ProcessAutoAdd(DelegateParam param)
{
while (true)
{
if (!timer.IsEnabled)
{
timer.Tick += ProcessHandlers[param.Uid];
timer.Interval = new TimeSpan(0, 0, 0, 0, 20);
timer.Start();
return;
}
}
}
///// <summary>
///// Bod部分的进度条委托
///// </summary>
///// <param name="param"></param>
//public void BodProcessCtrl(DelegateParam param)
//{
//}
public void RefeshWaterProcessEvent(object sender, EventArgs e)
{
if (WaterView.Value >= WaterView.Maximum)
{
timer.Stop();
timer.Tick -= RefeshWaterProcessEvent;
}
WaterView.Value++;
}
public void RefeshCachProcessEvent(object sender, EventArgs e)
{
if (CacheView.Value >= CacheView.Maximum)
{
timer.Stop();
timer.Tick -= RefeshCachProcessEvent;
}
CacheView.Value++;
}
public void RefeshStandProcessEvent(object sender, EventArgs e)
{
if (StandardView.Value >= StandardView.Maximum)
{
timer.Stop();
timer.Tick -= RefeshStandProcessEvent;
}
StandardView.Value++;
}
public void RefeshPumpProcessEvent(object sender, EventArgs e)
{
if (PumpView.Value >= PumpView.Maximum)
{
timer.Stop();
timer.Tick -= RefeshPumpProcessEvent;
}
PumpView.Value++;
}
public void RefeshAirProcessEvent(object sender, EventArgs e)
{
if (AirView.Value >= AirView.Maximum)
{
timer.Stop();
timer.Tick -= RefeshAirProcessEvent;
}
AirView.Value++;
}
public void RefeshNormalProcessEvent(object sender, EventArgs e)
{
if (NormalView.Value >= NormalView.Maximum)
{
timer.Stop();
timer.Tick -= RefeshNormalProcessEvent;
}
NormalView.Value++;
}
public void RefeshSampleProcessEvent(object sender, EventArgs e)
{
if (SampleView.Value >= SampleView.Maximum)
{
timer.Stop();
timer.Tick -= RefeshSampleProcessEvent;
}
SampleView.Value++;
}
public void RefeshStoreWaterProcessEvent(object sender, EventArgs e)
{
if (StoreWaterView.Value >= StoreWaterView.Maximum)
{
timer.Stop();
timer.Tick -= RefeshStoreWaterProcessEvent;
}
StoreWaterView.Value++;
}
public void RefeshWaterSampleProcessEvent(object sender, EventArgs e)
{
if (WaterSampleView.Value >= WaterSampleView.Maximum)
{
timer.Stop();
timer.Tick -= RefeshWaterSampleProcessEvent;
}
WaterSampleView.Value++;
}
public void RefreshStatus(SysStatus sysStatus)
{
switch (sysStatus)
{
case SysStatus.Sampling:
start.IsChecked = true;
break;
case SysStatus.Pause:
start.IsChecked = false;
break;
case SysStatus.Complete:
start.IsChecked = false;
break;
default:
break;
}
}
public void RefreshProcessStatus(ProcessType processType)
{
string status = Tool.GetProcessTypeToString(processType);
SysStatusData sysStatusData = new SysStatusData();
sysStatusData.Status = status;
sysStatusData.id = mainWindow_Model.SysStatusDataModel.AllSysStatusData.Count + 1;
sysStatusData.num = 0;
DateTime dateTime = DateTime.Now;
sysStatusData.CreateDate = dateTime.ToLongDateString();
sysStatusData.CreateTime = dateTime.ToLongTimeString();
SysStatusInfoModel sysStatusInfoModel = new SysStatusInfoModel();
sysStatusData.CopyToSysStatusInfoModel(sysStatusInfoModel);
Task refeshTask = Task.Factory.StartNew(() => BodSqliteHelp.InsertSysStatusData(sysStatusInfoModel));
// refeshTask.Dispose();
this.Dispatcher.BeginInvoke(new Action(delegate ()
{
mainWindow_Model.SysStatusDataModel.AddData(sysStatusData);
SysStaus.Content = status;
LogUtil.Log("状态更新成功:" + status);
}));
}
public void AddAlarmInfo(AlarmData alarmData)
{
alarmData.id = mainWindow_Model.AlramPagerModels.AllAlarmData.Count + 1;
mainWindow_Model.AlramPagerModels.AddData(alarmData);
AlramInfoModel alramInfoModel = new AlramInfoModel();
alarmData.CopyToAlramInfoModel(alramInfoModel);
BodSqliteHelp.InsertAlramInfo(alramInfoModel);
HisAlarmList.UpdateLayout();
}
#endregion
private void Valve_Checked(object sender, RoutedEventArgs e)
{
try
{
if (Valve.IsChecked == true)
{
byte[] data = { PLCConfig.SampleValveBit };
bodHelper.ValveControl(PLCConfig.Valve2Address, data);
}
}
catch (Exception ex)
{
LogUtil.LogError(ex);
Valve.IsChecked = false;
this.ShowMessageAsync("Error", "送样(样液)阀门打开失败.");
}
}
private void NormalValve_Checked(object sender, RoutedEventArgs e)
{
try
{
if (NormalValve.IsChecked == true)
{
byte[] data = { PLCConfig.NormalValveBit };
bodHelper.ValveControl(PLCConfig.Valve2Address, data);
}
}
catch (Exception ex)
{
LogUtil.LogError(ex);
NormalValve.IsChecked = false;
this.ShowMessageAsync("Error", "送样(标液)阀门打开失败.");
}
}
private void Valves_Checked(object sender, RoutedEventArgs e)
{
try
{
MetroSwitch valve = (MetroSwitch)sender;
if (bodHelper.IsSampling == true)
{
this.ShowMessageAsync("Error", "现在正在采样过程中,禁止相关操作.");
valve.IsChecked = false;
return;
}
if (valve.IsChecked == true)
{
foreach (var item in ValveDic)
{
if (item.Value != valve)
{
item.Value.IsChecked = false;
}
}
var key = ValveDic.FirstOrDefault(t => t.Value == valve).Key;
byte[] data = { key };
bodHelper.ValveControl(PLCConfig.Valve2Address, data);
}
}
catch (Exception ex)
{
LogUtil.LogError(ex);
StoreValve.IsChecked = false;
this.ShowMessageAsync("Error", "阀门打开失败.");
}
}
private void MetroButton_Click_3(object sender, RoutedEventArgs e)
{
try
{
if (bodHelper.IsSampling == true)
{
this.ShowMessageAsync("Error", "现在正在采样过程中,禁止相关操作..");
return;
}
var CheckedVavle = ValveDic.Where(t => t.Value.IsChecked == true).ToList();
if (CheckedVavle == null)
{
this.ShowMessageAsync("Error", "只能打开一个阀门,请关闭阀门.");
return;
}
if (CheckedVavle.Count > 2)
{
this.ShowMessageAsync("Error", "只能打开一个阀门,请关闭阀门.");
return;
}
var key = CheckedVavle[0].Key;
timer.Tick += ProcessHandlers[key];
bodHelper.PunpAbsorb(PunpCapType.fiveml);
timer.Start();
}
catch (Exception ex)
{
LogUtil.LogError(ex);
}
}
private void PumpDrain_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
try
{
if (bodHelper.IsSampling == true)
{
this.ShowMessageAsync("Error", "现在正在采样过程中,禁止相关操作.");
return;
}
var CheckedVavle = ValveDic.Where(t => t.Value.IsChecked == true).ToList();
if (CheckedVavle == null)
{
this.ShowMessageAsync("Error", "请打开任意一个阀门.");
return;
}
if (CheckedVavle.Count > 2)
{
this.ShowMessageAsync("Error", "只能打开一个阀门,请关闭阀门.");
return;
}
bodHelper.PumpDrain();
}
catch (Exception ex)
{
LogUtil.LogError(ex);
}
}
private void PumpDrain_Click(object sender, RoutedEventArgs e)
{
try
{
if (bodHelper.IsSampling == true)
{
this.ShowMessageAsync("Error", "现在正在采样过程中,禁止相关操作.");
return;
}
var CheckedVavle = ValveDic.Where(t => t.Value.IsChecked == true).ToList();
if (CheckedVavle == null)
{
this.ShowMessageAsync("Error", "请打开任意一个阀门.");
return;
}
if (CheckedVavle.Count > 2)
{
this.ShowMessageAsync("Error", "只能打开一个阀门,请关闭阀门.");
return;
}
bodHelper.PumpDrain();
}
catch (Exception ex)
{
LogUtil.LogError(ex);
}
}
private void PumpWaterButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (bodHelper.IsSampling == true)
{
this.ShowMessageAsync("Error", "现在正在采样过程中,禁止相关操作.");
return;
}
PumpWaterButton.Visibility = Visibility.Collapsed;
PumpStopButton.Visibility = Visibility.Visible;
byte[] data = { PLCConfig.CisternPumpBit };
bool success = bodHelper.ValveControl(PLCConfig.Valve1Address, data);
}
catch (Exception ex)
{
LogUtil.LogError(ex);
}
}
private void PumpStopButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (bodHelper.IsSampling == true)
{
this.ShowMessageAsync("Error", "现在正在采样过程中,禁止相关操作.");
return;
}
PumpWaterButton.Visibility = Visibility.Visible;
PumpStopButton.Visibility = Visibility.Collapsed;
byte[] data = { 0 };
bool success = bodHelper.ValveControl(PLCConfig.Valve1Address, data);
}
catch (Exception ex)
{
LogUtil.LogError(ex);
}
}
private void RowValve_Checked(object sender, RoutedEventArgs e)
{
try
{
if (bodHelper.IsSampling == true)
{
this.ShowMessageAsync("Error", "现在正在采样过程中,禁止相关操作.");
return;
}
byte[] data = { PLCConfig.DepositValveBit };
bodHelper.ValveControl(PLCConfig.Valve2Address, data);
}
catch (Exception ex)
{
LogUtil.LogError(ex);
}
}
private void StandCap_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
}
private void MetroComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void PunpStand_ButtonClick(object sender, EventArgs e)
{
try
{
string cap = PunpStand.Text;
if (string.IsNullOrEmpty(cap))
{
this.ShowMessageAsync("Tips", "请输入抽取容量.");
}
int capData = Convert.ToInt32(cap);
int times = capData / 5;
int extraTimes = capData % 5;
byte[] StandValve = { PLCConfig.StandardValveBit };
byte[] StandBodValve = { PLCConfig.NormalValveBit };
List<byte[]> data = new List<byte[]>();
List<ushort> address = new List<ushort>();
data.Add(StandValve);
data.Add(StandBodValve);
address.Add(PLCConfig.Valve2Address);
address.Add(PLCConfig.Valve2Address);
while (times > 0)
{
PumpProcess(data, address, PunpCapType.fiveml);
times--;
}
while (extraTimes > 0)
{
PumpProcess(data, address, PunpCapType.oneml);
extraTimes--;
}
byte[] data1 = { 0 };
bodHelper.ValveControl(PLCConfig.Valve2Address, data1);
}
catch (Exception ex)
{
LogUtil.LogError(ex);
}
}
private void PumpProcess(List<byte[]> data, List<ushort> address, PunpCapType punpCapType)
{
if (data == null || data.Count < 2 || address == null || address.Count < 2)
{
return;
}
bool success = false;
success = bodHelper.ValveControl(address[0], data[0]);
Thread.Sleep(1000);
if (!success)
{
MessageBox.Show(" 阀门打开失败.", "提示", MessageBoxButton.OK);
}
success = bodHelper.PunpAbsorb(punpCapType);
if (!success)
{
MessageBox.Show(" 注射泵抽水失败.", "提示", MessageBoxButton.OK);
}
Thread.Sleep(7000);
success = bodHelper.ValveControl(address[1], data[1]);
Thread.Sleep(1000);
if (!success)
{
MessageBox.Show(" 阀门打开失败.", "提示", MessageBoxButton.OK);
}
success = bodHelper.PumpDrain();
if (!success)
{
MessageBox.Show(" 注射泵放水失败.", "提示", MessageBoxButton.OK);
}
Thread.Sleep(7000);
}
private async void PumpCache_ButtonClick(object sender, EventArgs e)
{
await this.Dispatcher.InvokeAsync(() =>
{
try
{
string cap = PumpCache.Text;
if (string.IsNullOrEmpty(cap))
{
MessageBox.Show(" 请输入抽取容量.", "提示", MessageBoxButton.OK);
}
int capData = Convert.ToInt32(cap);
int times = capData / 5;
int extraTimes = capData % 5;
byte[] StandValve = { PLCConfig.WaterValveBit };
byte[] StandBodValve = { PLCConfig.NormalValveBit };
List<byte[]> data = new List<byte[]>();
List<ushort> address = new List<ushort>();
data.Add(StandValve);
data.Add(StandBodValve);
address.Add(PLCConfig.Valve2Address);
address.Add(PLCConfig.Valve2Address);
for (int i = 0; i < times; i++)
{
PumpProcess(data, address, PunpCapType.fiveml);
}
for (int i = 0; i < extraTimes; i++)
{
PumpProcess(data, address, PunpCapType.oneml);
}
byte[] data1 = { 0 };
bodHelper.ValveControl(PLCConfig.Valve2Address, data1);
}