HamBook – Diff between revs 38 and 54
?pathlinks?
Rev 38 | Rev 54 | |||
---|---|---|---|---|
Line 1... | Line -... | |||
1 | using HamBook.Properties; |
- | ||
2 | using Org.BouncyCastle.Utilities; |
- | ||
3 | using RJCP.IO.Ports; |
- | ||
4 | using Serilog; |
- | ||
5 | using System; |
1 | using System; |
|
6 | using System.Collections.Concurrent; |
2 | using System.Collections.Concurrent; |
|
7 | using System.Collections.Generic; |
3 | using System.Collections.Generic; |
|
8 | using System.IO.Ports; |
- | ||
9 | using System.Linq; |
4 | using System.Linq; |
|
10 | using System.Reflection; |
5 | using System.Reflection; |
|
11 | using System.Runtime.CompilerServices; |
6 | using System.Runtime.CompilerServices; |
|
12 | using System.Text; |
- | ||
13 | using System.Threading; |
7 | using System.Threading; |
|
14 | using System.Threading.Tasks; |
8 | using System.Threading.Tasks; |
|
15 | using System.Windows.Input; |
9 | using HamBook.Properties; |
|
- | 10 | using RJCP.IO.Ports; |
||
- | 11 | using Serilog; |
||
Line 16... | Line 12... | |||
16 | |
12 | |
|
17 | namespace HamBook.Radios |
13 | namespace HamBook.Radios |
|
18 | { |
14 | { |
|
19 | public class CatAssemblies : IDisposable |
15 | public class CatAssemblies : IDisposable |
|
- | 16 | { |
||
20 | { |
17 | private readonly ConcurrentDictionary<string, Cat> _catCommands; |
|
- | 18 | private readonly string _radio; |
||
- | 19 | private readonly SemaphoreSlim _semaphoreSlim; |
||
Line 21... | Line 20... | |||
21 | public int Count => _count; |
20 | private readonly SerialPortStream _serialPort; |
|
22 | |
- | ||
23 | private int _count; |
- | ||
24 | private SerialPortStream _serialPort; |
- | ||
25 | private string _radio; |
- | ||
26 | |
- | ||
Line 27... | Line 21... | |||
27 | private ConcurrentDictionary<string, Cat> _catCommands; |
21 | |
|
28 | private SemaphoreSlim _semaphoreSlim; |
22 | private int _count; |
|
29 | |
23 | |
|
30 | private CatAssemblies() |
24 | private CatAssemblies() |
|
Line 38... | Line 32... | |||
38 | _serialPort = serialPort; |
32 | _serialPort = serialPort; |
|
39 | _radio = radio; |
33 | _radio = radio; |
|
Line 40... | Line 34... | |||
40 | |
34 | |
|
41 | Load(_radio); |
35 | Load(_radio); |
|
- | 36 | } |
||
- | 37 | |
||
- | 38 | public int Count => _count; |
||
42 | } |
39 | |
|
43 | public void Dispose() |
40 | public void Dispose() |
|
44 | { |
41 | { |
|
45 | Unload(); |
42 | Unload(); |
|
Line 46... | Line 43... | |||
46 | } |
43 | } |
|
47 | |
44 | |
|
48 | private void Load(string radio) |
45 | private void Load(string radio) |
|
Line 49... | Line 46... | |||
49 | { |
46 | { |
|
50 | Interlocked.Exchange(ref _count, 0); |
47 | Interlocked.Exchange(ref _count, 0); |
|
51 | |
48 | |
|
- | 49 | foreach (var type in Assembly.GetExecutingAssembly() |
||
52 | foreach (var type in Assembly.GetExecutingAssembly() |
50 | .GetTypes() |
|
53 | .GetTypes() |
51 | .Where(type => type.IsSubclassOf(typeof(Cat))) |
|
54 | .Where(type => type.IsSubclassOf(typeof(Cat))) |
52 | .Where(type => |
|
55 | .Where(type => type.GetCustomAttribute<RadioAttribute>() !=null && type.GetCustomAttribute<RadioAttribute>().Radio == radio)) |
53 | type.GetCustomAttribute<RadioAttribute>() != null && |
|
56 | { |
54 | type.GetCustomAttribute<RadioAttribute>().Radio == radio)) |
|
57 | try |
55 | try |
|
58 | { |
56 | { |
|
Line 59... | Line -... | |||
59 | var catCommand = (Cat)type |
- | ||
60 | .GetConstructor(new[] { typeof(SerialPortStream) }) |
- | ||
61 | .Invoke(new [] { _serialPort }); |
57 | var catCommand = (Cat)type |
|
62 | |
- | ||
Line 63... | Line 58... | |||
63 | if (catCommand == null) |
58 | .GetConstructor(new[] { typeof(SerialPortStream) }) |
|
64 | { |
59 | .Invoke(new[] { _serialPort }); |
|
65 | throw new ArgumentException(Resources.Could_not_create_assembly); |
60 | |
|
Line 73... | Line 68... | |||
73 | |
68 | |
|
74 | continue; |
69 | continue; |
|
Line 75... | Line 70... | |||
75 | } |
70 | } |
|
76 | |
- | ||
77 | //BindEventHandlers(command); |
71 | |
|
78 | |
72 | //BindEventHandlers(command); |
|
79 | Interlocked.Increment(ref _count); |
73 | Interlocked.Increment(ref _count); |
|
80 | } |
74 | } |
|
81 | catch (Exception exception) |
75 | catch (Exception exception) |
|
82 | { |
76 | { |
|
83 | Log.Error(exception, Resources.Could_not_instantiate_assembly, type); |
- | ||
Line 84... | Line 77... | |||
84 | } |
77 | Log.Error(exception, Resources.Could_not_instantiate_assembly, type); |
|
85 | } |
78 | } |
|
Line 86... | Line 79... | |||
86 | |
79 | |
|
87 | Log.Information(Resources.Registering_commands, Count); |
80 | Log.Information(Resources.Registering_commands, Count); |
|
88 | } |
81 | } |
|
89 | |
82 | |
|
Line 90... | Line 83... | |||
90 | private void Unload() |
83 | private void Unload() |
|
91 | { |
- | ||
92 | // Dispose all command assemblies. |
84 | { |
|
93 | var list = new List<Cat>(_catCommands.Values); |
85 | // Dispose all command assemblies. |
|
94 | |
86 | var list = new List<Cat>(_catCommands.Values); |
|
95 | for (var i = 0; i < list.Count; ++i) |
- | ||
96 | { |
87 | |
|
97 | try |
88 | for (var i = 0; i < list.Count; ++i) |
|
98 | { |
89 | try |
|
Line 99... | Line 90... | |||
99 | //UnbindEventHandlers(list[i]); |
90 | { |
|
Line 118... | Line 109... | |||
118 | } |
109 | } |
|
119 | catch (Exception exception) |
110 | catch (Exception exception) |
|
120 | { |
111 | { |
|
121 | Log.Error(exception, Resources.Error_encountered_while_disposing_object); |
112 | Log.Error(exception, Resources.Error_encountered_while_disposing_object); |
|
122 | } |
113 | } |
|
123 | } |
- | ||
Line 124... | Line 114... | |||
124 | |
114 | |
|
125 | _catCommands.Clear(); |
115 | _catCommands.Clear(); |
|
Line 126... | Line 116... | |||
126 | } |
116 | } |
|
127 | |
117 | |
|
128 | public T CatParse<T>(string command, object[] param) |
118 | public T CatParse<T>(string command, object[] param) |
|
129 | { |
119 | { |
|
130 | if (_catCommands.TryGetValue(command, out var catCommand)) |
120 | if (_catCommands.TryGetValue(command, out var catCommand)) |
|
131 | { |
- | ||
132 | var methodInfo = catCommand.GetType().GetMethod("Parse"); |
- | ||
133 | if (methodInfo != null) |
121 | { |
|
134 | { |
- | ||
Line 135... | Line 122... | |||
135 | return (T)methodInfo.Invoke(catCommand, param); |
122 | var methodInfo = catCommand.GetType().GetMethod("Parse"); |
|
136 | } |
123 | if (methodInfo != null) return (T)methodInfo.Invoke(catCommand, param); |
|
Line 137... | Line 124... | |||
137 | |
124 | |
|
Line 145... | Line 132... | |||
145 | { |
132 | { |
|
146 | _semaphoreSlim.Wait(); |
133 | _semaphoreSlim.Wait(); |
|
Line 147... | Line 134... | |||
147 | |
134 | |
|
148 | try |
135 | try |
|
149 | { |
136 | { |
|
150 | if (!_serialPort.IsOpen) |
- | ||
151 | { |
- | ||
152 | _serialPort.Open(); |
- | ||
Line 153... | Line 137... | |||
153 | } |
137 | if (!_serialPort.IsOpen) _serialPort.Open(); |
|
154 | |
138 | |
|
155 | try |
139 | try |
|
156 | { |
140 | { |
|
157 | if (_catCommands.TryGetValue(command, out var catCommand)) |
141 | if (_catCommands.TryGetValue(command, out var catCommand)) |
|
158 | { |
- | ||
159 | var methodInfo = catCommand.GetType().GetMethod("GetDefault"); |
- | ||
160 | if (methodInfo != null) |
142 | { |
|
161 | { |
- | ||
162 | return (T)methodInfo.Invoke(catCommand, param); |
143 | var methodInfo = catCommand.GetType().GetMethod("GetDefault"); |
|
163 | } |
144 | if (methodInfo != null) return (T)methodInfo.Invoke(catCommand, param); |
|
164 | } |
145 | } |
|
165 | } |
146 | } |
|
166 | finally |
147 | finally |
|
Line 184... | Line 165... | |||
184 | { |
165 | { |
|
185 | _semaphoreSlim.Wait(); |
166 | _semaphoreSlim.Wait(); |
|
Line 186... | Line 167... | |||
186 | |
167 | |
|
187 | try |
168 | try |
|
188 | { |
169 | { |
|
189 | if (!_serialPort.IsOpen) |
- | ||
190 | { |
- | ||
191 | _serialPort.Open(); |
- | ||
Line 192... | Line 170... | |||
192 | } |
170 | if (!_serialPort.IsOpen) _serialPort.Open(); |
|
193 | |
171 | |
|
194 | try |
172 | try |
|
195 | { |
173 | { |
|
Line 221... | Line 199... | |||
221 | { |
199 | { |
|
222 | _semaphoreSlim.Release(); |
200 | _semaphoreSlim.Release(); |
|
223 | } |
201 | } |
|
224 | } |
202 | } |
|
Line 225... | Line 203... | |||
225 | |
203 | |
|
226 | public async Task<U> CatSetAsync<T, U>(string command, object[] param, CancellationToken cancellationToken) |
204 | public async Task<TU> CatSetAsync<T, TU>(string command, object[] param, CancellationToken cancellationToken) |
|
227 | { |
205 | { |
|
Line 228... | Line 206... | |||
228 | await _semaphoreSlim.WaitAsync(cancellationToken); |
206 | await _semaphoreSlim.WaitAsync(cancellationToken); |
|
229 | |
207 | |
|
230 | try |
208 | try |
|
231 | { |
- | ||
232 | if (!_serialPort.IsOpen) |
- | ||
233 | { |
- | ||
Line 234... | Line 209... | |||
234 | _serialPort.Open(); |
209 | { |
|
235 | } |
210 | if (!_serialPort.IsOpen) _serialPort.Open(); |
|
236 | |
211 | |
|
237 | try |
212 | try |
|
238 | { |
213 | { |
|
239 | if (_catCommands.TryGetValue(command, out var catCommand)) |
214 | if (_catCommands.TryGetValue(command, out var catCommand)) |
|
240 | { |
- | ||
241 | var methodInfo = catCommand.GetType().GetMethod("SetAsync"); |
215 | { |
|
242 | if (methodInfo != null) |
216 | var methodInfo = catCommand.GetType().GetMethod("SetAsync"); |
|
243 | { |
217 | if (methodInfo != null) |
|
244 | if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
218 | if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
|
245 | { |
219 | { |
|
Line 246... | Line 220... | |||
246 | var parameters = new List<object>(); |
220 | var parameters = new List<object>(); |
|
Line 247... | Line 221... | |||
247 | parameters.AddRange(param); |
221 | parameters.AddRange(param); |
|
248 | parameters.Add(cancellationToken); |
222 | parameters.Add(cancellationToken); |
|
249 | |
- | ||
- | 223 | |
||
250 | var result = await (Task<U>)methodInfo.Invoke(catCommand, parameters.ToArray()); |
224 | var result = await (Task<TU>)methodInfo.Invoke(catCommand, parameters.ToArray()); |
|
251 | |
225 | |
|
Line 252... | Line 226... | |||
252 | return result; |
226 | return result; |
|
253 | } |
227 | } |
|
Line 276... | Line 250... | |||
276 | { |
250 | { |
|
277 | _semaphoreSlim.Wait(); |
251 | _semaphoreSlim.Wait(); |
|
Line 278... | Line 252... | |||
278 | |
252 | |
|
279 | try |
253 | try |
|
280 | { |
254 | { |
|
281 | if (!_serialPort.IsOpen) |
- | ||
282 | { |
- | ||
283 | _serialPort.Open(); |
- | ||
Line 284... | Line 255... | |||
284 | } |
255 | if (!_serialPort.IsOpen) _serialPort.Open(); |
|
285 | |
256 | |
|
286 | try |
257 | try |
|
287 | { |
258 | { |
|
Line 300... | Line 271... | |||
300 | |
271 | |
|
301 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
272 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
|
302 | } |
273 | } |
|
303 | finally |
274 | finally |
|
304 | { |
275 | { |
|
305 | if (_serialPort.IsOpen) |
- | ||
306 | { |
- | ||
307 | _serialPort.Flush(); |
- | ||
308 | } |
276 | if (_serialPort.IsOpen) _serialPort.Flush(); |
|
309 | } |
277 | } |
|
310 | } |
278 | } |
|
311 | finally |
279 | finally |
|
312 | { |
280 | { |
|
Line 318... | Line 286... | |||
318 | { |
286 | { |
|
319 | await _semaphoreSlim.WaitAsync(cancellationToken); |
287 | await _semaphoreSlim.WaitAsync(cancellationToken); |
|
Line 320... | Line 288... | |||
320 | |
288 | |
|
321 | try |
289 | try |
|
322 | { |
290 | { |
|
323 | if (!_serialPort.IsOpen) |
- | ||
324 | { |
- | ||
325 | _serialPort.Open(); |
- | ||
Line 326... | Line 291... | |||
326 | } |
291 | if (!_serialPort.IsOpen) _serialPort.Open(); |
|
327 | |
292 | |
|
328 | try |
293 | try |
|
329 | { |
294 | { |
|
330 | if (_catCommands.TryGetValue(command, out var catCommand)) |
295 | if (_catCommands.TryGetValue(command, out var catCommand)) |
|
331 | { |
296 | { |
|
332 | var methodInfo = catCommand.GetType().GetMethod("WriteAsync"); |
- | ||
333 | if (methodInfo != null) |
297 | var methodInfo = catCommand.GetType().GetMethod("WriteAsync"); |
|
334 | { |
298 | if (methodInfo != null) |
|
335 | if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
299 | if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
|
336 | { |
300 | { |
|
337 | var parameters = new List<object>(); |
301 | var parameters = new List<object>(); |
|
Line 338... | Line 302... | |||
338 | parameters.AddRange(param); |
302 | parameters.AddRange(param); |
|
Line 339... | Line 303... | |||
339 | parameters.Add(cancellationToken); |
303 | parameters.Add(cancellationToken); |
|
340 | |
304 | |
|
341 | await (Task)methodInfo.Invoke(catCommand, parameters.ToArray()); |
- | ||
Line 342... | Line 305... | |||
342 | |
305 | await (Task)methodInfo.Invoke(catCommand, parameters.ToArray()); |
|
343 | return; |
306 | |
|
Line 344... | Line 307... | |||
344 | } |
307 | return; |
|
345 | } |
308 | } |
|
346 | |
309 | |
|
347 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "WriteAsync"); |
310 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "WriteAsync"); |
|
348 | } |
- | ||
349 | |
- | ||
350 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
311 | } |
|
351 | } |
- | ||
352 | finally |
312 | |
|
353 | { |
313 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
|
354 | if (_serialPort.IsOpen) |
314 | } |
|
355 | { |
315 | finally |
|
356 | await _serialPort.FlushAsync(cancellationToken); |
316 | { |
|
Line 367... | Line 327... | |||
367 | { |
327 | { |
|
368 | await _semaphoreSlim.WaitAsync(cancellationToken); |
328 | await _semaphoreSlim.WaitAsync(cancellationToken); |
|
Line 369... | Line 329... | |||
369 | |
329 | |
|
370 | try |
330 | try |
|
371 | { |
331 | { |
|
372 | if (!_serialPort.IsOpen) |
- | ||
373 | { |
- | ||
374 | _serialPort.Open(); |
- | ||
Line 375... | Line 332... | |||
375 | } |
332 | if (!_serialPort.IsOpen) _serialPort.Open(); |
|
376 | |
333 | |
|
377 | try |
334 | try |
|
378 | { |
335 | { |
|
379 | if (_catCommands.TryGetValue(command, out var catCommand)) |
336 | if (_catCommands.TryGetValue(command, out var catCommand)) |
|
380 | { |
337 | { |
|
381 | var methodInfo = catCommand.GetType().GetMethod("WriteAsync"); |
- | ||
382 | if (methodInfo != null) |
338 | var methodInfo = catCommand.GetType().GetMethod("WriteAsync"); |
|
383 | { |
339 | if (methodInfo != null) |
|
384 | if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
340 | if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
|
385 | { |
341 | { |
|
386 | var parameters = new List<object>(); |
342 | var parameters = new List<object>(); |
|
Line 387... | Line 343... | |||
387 | parameters.AddRange(param); |
343 | parameters.AddRange(param); |
|
Line 388... | Line 344... | |||
388 | parameters.Add(cancellationToken); |
344 | parameters.Add(cancellationToken); |
|
389 | |
345 | |
|
390 | await (Task)methodInfo.Invoke(catCommand, parameters.ToArray()); |
- | ||
Line 391... | Line 346... | |||
391 | |
346 | await (Task)methodInfo.Invoke(catCommand, parameters.ToArray()); |
|
392 | return; |
347 | |
|
Line 393... | Line 348... | |||
393 | } |
348 | return; |
|
394 | } |
349 | } |
|
395 | |
350 | |
|
396 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "WriteAsync"); |
351 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "WriteAsync"); |
|
397 | } |
- | ||
398 | |
- | ||
399 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
352 | } |
|
400 | } |
- | ||
401 | finally |
353 | |
|
402 | { |
354 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
|
403 | if (_serialPort.IsOpen) |
355 | } |
|
404 | { |
356 | finally |
|
405 | await _serialPort.FlushAsync(cancellationToken); |
357 | { |
|
Line 416... | Line 368... | |||
416 | { |
368 | { |
|
417 | _semaphoreSlim.Wait(); |
369 | _semaphoreSlim.Wait(); |
|
Line 418... | Line 370... | |||
418 | |
370 | |
|
419 | try |
371 | try |
|
420 | { |
372 | { |
|
421 | if (!_serialPort.IsOpen) |
- | ||
422 | { |
- | ||
423 | _serialPort.Open(); |
- | ||
Line 424... | Line 373... | |||
424 | } |
373 | if (!_serialPort.IsOpen) _serialPort.Open(); |
|
425 | |
374 | |
|
426 | try |
375 | try |
|
427 | { |
376 | { |
|
428 | if (_catCommands.TryGetValue(command, out var catCommand)) |
377 | if (_catCommands.TryGetValue(command, out var catCommand)) |
|
429 | { |
- | ||
430 | var methodInfo = catCommand.GetType().GetMethod("Read"); |
- | ||
431 | if (methodInfo != null) |
378 | { |
|
432 | { |
- | ||
Line 433... | Line 379... | |||
433 | return (T)methodInfo.Invoke(catCommand, param); |
379 | var methodInfo = catCommand.GetType().GetMethod("Read"); |
|
434 | } |
380 | if (methodInfo != null) return (T)methodInfo.Invoke(catCommand, param); |
|
Line 435... | Line 381... | |||
435 | |
381 | |
|
Line 457... | Line 403... | |||
457 | { |
403 | { |
|
458 | await _semaphoreSlim.WaitAsync(cancellationToken); |
404 | await _semaphoreSlim.WaitAsync(cancellationToken); |
|
Line 459... | Line 405... | |||
459 | |
405 | |
|
460 | try |
406 | try |
|
461 | { |
407 | { |
|
462 | if (!_serialPort.IsOpen) |
- | ||
463 | { |
- | ||
464 | _serialPort.Open(); |
- | ||
Line 465... | Line 408... | |||
465 | } |
408 | if (!_serialPort.IsOpen) _serialPort.Open(); |
|
466 | |
409 | |
|
467 | try |
410 | try |
|
468 | { |
411 | { |
|
469 | if (_catCommands.TryGetValue(command, out var catCommand)) |
412 | if (_catCommands.TryGetValue(command, out var catCommand)) |
|
470 | { |
413 | { |
|
471 | var methodInfo = catCommand.GetType().GetMethod("ReadAsync"); |
- | ||
472 | if (methodInfo != null) |
414 | var methodInfo = catCommand.GetType().GetMethod("ReadAsync"); |
|
473 | { |
415 | if (methodInfo != null) |
|
474 | if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
416 | if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
|
475 | { |
417 | { |
|
476 | var parameters = new List<object>(); |
418 | var parameters = new List<object>(); |
|
Line 477... | Line 419... | |||
477 | parameters.AddRange(param); |
419 | parameters.AddRange(param); |
|
Line 478... | Line 420... | |||
478 | parameters.Add(cancellationToken); |
420 | parameters.Add(cancellationToken); |
|
479 | |
421 | |
|
480 | var result = await (Task<T>)methodInfo.Invoke(catCommand, parameters.ToArray()); |
- | ||
Line 481... | Line 422... | |||
481 | |
422 | var result = await (Task<T>)methodInfo.Invoke(catCommand, parameters.ToArray()); |
|
482 | return result; |
423 | |
|
Line 483... | Line 424... | |||
483 | } |
424 | return result; |
|
Line 501... | Line 442... | |||
501 | { |
442 | { |
|
502 | _semaphoreSlim.Release(); |
443 | _semaphoreSlim.Release(); |
|
503 | } |
444 | } |
|
504 | } |
445 | } |
|
505 | } |
446 | } |
|
506 | } |
447 | } |
|
507 | |
448 | |