-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonitor.cs
189 lines (174 loc) · 4.37 KB
/
Monitor.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
using ElitechLog.Models;
using ElitechLog.Monitor;
namespace ElitechLogCLI;
public class Monitor
{
public event Action<Parameters> Connected;
public event Action<int, int> Downloading;
public event Action<Parameters> Downloaded;
public event Action Updated;
public event Action Disconnected;
private readonly EventWaitHandle _stop = new ManualResetEvent(false);
private readonly CCOM _com = CCOM.GetInstance();
private readonly CUSB _usb = CUSB.GetInstance();
private readonly object _lockObj = new();
private Parameters _parms;
private string _dsn;
private Exception _ex;
public Monitor()
{
_com.EParametersLoaded += ParametersLoaded;
_com.EDownloadProgress += DownloadProgress;
_com.EDownloadComplete += DownloadComplete;
_com.EDeviceDisconnected += _ => DeviceDisconnected(true);
_com.ENotify += Notify;
_usb.EParametersLoaded += ParametersLoaded;
_usb.EDownloadProgress += DownloadProgress;
_usb.EDownloadComplete += DownloadComplete;
_usb.EDeviceDisconnected += _ => DeviceDisconnected(false);
_usb.ENotify += Notify;
}
public void Run()
{
_com.Run();
_usb.Run();
_stop.WaitOne();
if (_ex != null) throw _ex;
}
public void Stop()
{
_stop.Set();
}
private void ParametersLoaded(Parameters parms, string dsn)
{
lock (_lockObj)
{
try
{
if (_dsn == dsn)
{
_parms = parms;
Updated?.Invoke();
}
else if (_dsn == null)
{
_parms = parms;
_dsn = dsn;
Connected?.Invoke(parms);
}
}
catch (Exception ex)
{
_ex = ex;
_stop.Set();
}
}
}
public void Download()
{
lock (_lockObj)
{
switch (_parms)
{
case COMParameters parms:
_com.Download(parms);
return;
case USBParameters parms:
_usb.Download(parms);
return;
default:
return;
}
}
}
public void UpdateParameters()
{
lock (_lockObj)
{
switch (_parms)
{
case COMParameters p:
_com.SetParameters(p, 0);
return;
case USBParameters p:
_usb.SetParameters(p, 0);
return;
default:
return;
}
}
}
public void QuickReset()
{
lock (_lockObj)
{
switch (_parms)
{
case COMParameters p:
_com.SetParameters(p, 1);
return;
case USBParameters p:
_usb.SetParameters(p, 1);
return;
default:
return;
}
}
}
private void DownloadProgress(int current, int total, string msg)
{
try
{
Downloading?.Invoke(current, total);
}
catch (Exception ex)
{
_ex = ex;
_stop.Set();
}
}
private void DownloadComplete(Parameters parms)
{
try
{
Downloaded?.Invoke(parms);
}
catch (Exception ex)
{
_ex = ex;
_stop.Set();
}
}
private void DeviceDisconnected(bool com)
{
lock (_lockObj)
{
try
{
if ((com && _parms is COMParameters) || (!com && _parms is USBParameters))
{
_parms = null;
_dsn = null;
Disconnected?.Invoke();
}
}
catch (Exception ex)
{
_ex = ex;
_stop.Set();
}
}
}
private void Notify(bool error, string msg)
{
if (error)
{
_ex = new ApplicationException(msg);
_stop.Set();
}
else
{
Console.WriteLine(msg);
}
}
}