HamBook – Blame information for rev 11
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using HamBook.Properties; |
2 | using Org.BouncyCastle.Utilities; |
||
9 | office | 3 | using RJCP.IO.Ports; |
1 | office | 4 | using Serilog; |
5 | using System; |
||
6 | using System.Collections.Concurrent; |
||
7 | using System.Collections.Generic; |
||
8 | using System.IO.Ports; |
||
9 | using System.Linq; |
||
10 | using System.Reflection; |
||
11 | using System.Runtime.CompilerServices; |
||
12 | using System.Text; |
||
13 | using System.Threading; |
||
14 | using System.Threading.Tasks; |
||
15 | using System.Windows.Input; |
||
16 | |||
17 | namespace HamBook.Radios |
||
18 | { |
||
19 | public class CatAssemblies : IDisposable |
||
20 | { |
||
21 | public int Count => _count; |
||
22 | |||
23 | private int _count; |
||
9 | office | 24 | private SerialPortStream _serialPort; |
1 | office | 25 | private string _radio; |
26 | |||
3 | office | 27 | private ConcurrentDictionary<string, Cat> _catCommands; |
1 | office | 28 | |
29 | private CatAssemblies() |
||
30 | { |
||
31 | _catCommands = new ConcurrentDictionary<string, Cat>(); |
||
32 | } |
||
33 | |||
9 | office | 34 | public CatAssemblies(SerialPortStream serialPort, string radio) : this() |
1 | office | 35 | { |
36 | _serialPort = serialPort; |
||
37 | _radio = radio; |
||
38 | |||
39 | Load(_radio); |
||
40 | } |
||
41 | public void Dispose() |
||
42 | { |
||
43 | Unload(); |
||
44 | } |
||
45 | |||
46 | private void Load(string radio) |
||
47 | { |
||
48 | Interlocked.Exchange(ref _count, 0); |
||
49 | |||
50 | foreach (var type in Assembly.GetExecutingAssembly() |
||
51 | .GetTypes() |
||
52 | .Where(type => type.IsSubclassOf(typeof(Cat))) |
||
53 | .Where(type => type.GetCustomAttribute<RadioAttribute>() !=null && type.GetCustomAttribute<RadioAttribute>().Radio == radio)) |
||
54 | { |
||
55 | try |
||
56 | { |
||
57 | var catCommand = (Cat)type |
||
9 | office | 58 | .GetConstructor(new[] { typeof(SerialPortStream) }) |
1 | office | 59 | .Invoke(new [] { _serialPort }); |
60 | |||
61 | if (catCommand == null) |
||
62 | { |
||
63 | throw new ArgumentException(Resources.Could_not_create_assembly); |
||
64 | } |
||
65 | |||
66 | if (!_catCommands.TryAdd(catCommand.Name, catCommand)) |
||
67 | { |
||
68 | Log.Warning(Resources.Unable_to_register_assembly, catCommand.Name); |
||
69 | |||
70 | catCommand.Dispose(); |
||
71 | |||
72 | continue; |
||
73 | } |
||
74 | |||
75 | //BindEventHandlers(command); |
||
76 | |||
77 | Interlocked.Increment(ref _count); |
||
78 | } |
||
79 | catch (Exception exception) |
||
80 | { |
||
81 | Log.Error(exception, Resources.Could_not_instantiate_assembly, type); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | Log.Information(Resources.Registering_commands, Count); |
||
86 | } |
||
87 | |||
88 | private void Unload() |
||
89 | { |
||
90 | // Dispose all command assemblies. |
||
91 | var list = new List<Cat>(_catCommands.Values); |
||
92 | |||
93 | for (var i = 0; i < list.Count; ++i) |
||
94 | { |
||
95 | try |
||
96 | { |
||
97 | //UnbindEventHandlers(list[i]); |
||
98 | |||
99 | if (list[i] != null) |
||
100 | { |
||
101 | Log.Debug(Resources.Disposing_assembly, list[i].GetType().Name); |
||
102 | |||
103 | list[i] |
||
104 | .Dispose(); |
||
105 | |||
106 | list[i] = null; |
||
107 | } |
||
108 | } |
||
109 | catch (NullReferenceException) |
||
110 | { |
||
111 | // Already removed. |
||
112 | } |
||
113 | catch (ObjectDisposedException) |
||
114 | { |
||
115 | list[i] = null; |
||
116 | } |
||
117 | catch (Exception exception) |
||
118 | { |
||
119 | Log.Error(exception, Resources.Error_encountered_while_disposing_object); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | _catCommands.Clear(); |
||
124 | } |
||
125 | |||
9 | office | 126 | public T CatGetDefault<T>(string command, object[] param) |
1 | office | 127 | { |
3 | office | 128 | if (!_serialPort.IsOpen) |
129 | { |
||
130 | _serialPort.Open(); |
||
131 | } |
||
132 | |||
1 | office | 133 | try |
134 | { |
||
135 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
136 | { |
||
9 | office | 137 | var methodInfo = catCommand.GetType().GetMethod("GetDefault"); |
1 | office | 138 | if (methodInfo != null) |
139 | { |
||
9 | office | 140 | return (T)methodInfo.Invoke(catCommand, param); |
1 | office | 141 | } |
142 | } |
||
143 | } |
||
144 | finally |
||
145 | { |
||
3 | office | 146 | if (_serialPort.IsOpen) |
147 | { |
||
9 | office | 148 | _serialPort.Flush(); |
3 | office | 149 | _serialPort.Close(); |
150 | } |
||
1 | office | 151 | } |
9 | office | 152 | |
153 | return default; |
||
1 | office | 154 | } |
155 | |||
9 | office | 156 | public T CatParse<T>(string command, object[] param) |
157 | { |
||
158 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
159 | { |
||
160 | var methodInfo = catCommand.GetType().GetMethod("Parse"); |
||
161 | if (methodInfo != null) |
||
162 | { |
||
163 | return (T)methodInfo.Invoke(catCommand, param); |
||
164 | } |
||
11 | office | 165 | |
166 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "Parse"); |
||
9 | office | 167 | } |
168 | |||
11 | office | 169 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
9 | office | 170 | } |
171 | |||
1 | office | 172 | public void CatSet<T>(string command, object[] param) |
173 | { |
||
3 | office | 174 | if (!_serialPort.IsOpen) |
175 | { |
||
176 | _serialPort.Open(); |
||
177 | } |
||
178 | |||
1 | office | 179 | try |
180 | { |
||
181 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
182 | { |
||
183 | var methodInfo = catCommand.GetType().GetMethod("Set"); |
||
184 | if (methodInfo != null) |
||
185 | { |
||
186 | methodInfo.Invoke(catCommand, param); |
||
11 | office | 187 | |
188 | return; |
||
1 | office | 189 | } |
11 | office | 190 | |
191 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "Set"); |
||
1 | office | 192 | } |
11 | office | 193 | |
194 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
||
1 | office | 195 | } |
196 | finally |
||
197 | { |
||
3 | office | 198 | if (_serialPort.IsOpen) |
199 | { |
||
9 | office | 200 | _serialPort.Flush(); |
3 | office | 201 | _serialPort.Close(); |
202 | } |
||
1 | office | 203 | } |
204 | } |
||
205 | |||
9 | office | 206 | public async Task CatSetAsync<T>(string command, object[] param, CancellationToken cancellationToken) |
3 | office | 207 | { |
208 | if (!_serialPort.IsOpen) |
||
209 | { |
||
210 | _serialPort.Open(); |
||
211 | } |
||
212 | |||
213 | try |
||
214 | { |
||
215 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
216 | { |
||
9 | office | 217 | var methodInfo = catCommand.GetType().GetMethod("SetAsync"); |
3 | office | 218 | if (methodInfo != null) |
219 | { |
||
9 | office | 220 | if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
221 | { |
||
222 | var parameters = new List<object>(); |
||
223 | parameters.AddRange(param); |
||
224 | parameters.Add(cancellationToken); |
||
225 | |||
226 | await (Task)methodInfo.Invoke(catCommand, parameters.ToArray()); |
||
11 | office | 227 | |
228 | return; |
||
9 | office | 229 | } |
3 | office | 230 | } |
11 | office | 231 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "SetAsync"); |
3 | office | 232 | } |
11 | office | 233 | |
234 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
||
3 | office | 235 | } |
236 | finally |
||
237 | { |
||
238 | if (_serialPort.IsOpen) |
||
239 | { |
||
9 | office | 240 | await _serialPort.FlushAsync(cancellationToken); |
3 | office | 241 | _serialPort.Close(); |
242 | } |
||
243 | } |
||
9 | office | 244 | } |
3 | office | 245 | |
9 | office | 246 | public void CatWrite<T>(string command, object[] param) |
247 | { |
||
248 | if (!_serialPort.IsOpen) |
||
249 | { |
||
250 | _serialPort.Open(); |
||
251 | } |
||
252 | |||
253 | try |
||
254 | { |
||
255 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
256 | { |
||
257 | var methodInfo = catCommand.GetType().GetMethod("Write"); |
||
258 | if (methodInfo != null) |
||
259 | { |
||
260 | methodInfo.Invoke(catCommand, param); |
||
11 | office | 261 | |
262 | return; |
||
9 | office | 263 | } |
11 | office | 264 | |
265 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "Write"); |
||
9 | office | 266 | } |
11 | office | 267 | |
268 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
||
9 | office | 269 | } |
270 | finally |
||
271 | { |
||
272 | if (_serialPort.IsOpen) |
||
273 | { |
||
274 | _serialPort.Flush(); |
||
275 | } |
||
276 | } |
||
3 | office | 277 | } |
278 | |||
9 | office | 279 | public async Task CatWriteAsync<T>(string command, object[] param, CancellationToken cancellationToken) |
3 | office | 280 | { |
281 | if (!_serialPort.IsOpen) |
||
282 | { |
||
283 | _serialPort.Open(); |
||
284 | } |
||
285 | |||
286 | try |
||
287 | { |
||
288 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
289 | { |
||
9 | office | 290 | var methodInfo = catCommand.GetType().GetMethod("WriteAsync"); |
3 | office | 291 | if (methodInfo != null) |
292 | { |
||
9 | office | 293 | if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
294 | { |
||
295 | var parameters = new List<object>(); |
||
296 | parameters.AddRange(param); |
||
297 | parameters.Add(cancellationToken); |
||
298 | |||
299 | await (Task)methodInfo.Invoke(catCommand, parameters.ToArray()); |
||
11 | office | 300 | |
301 | return; |
||
9 | office | 302 | } |
3 | office | 303 | } |
11 | office | 304 | |
305 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "WriteAsync"); |
||
3 | office | 306 | } |
11 | office | 307 | |
308 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
||
3 | office | 309 | } |
310 | finally |
||
311 | { |
||
312 | if (_serialPort.IsOpen) |
||
313 | { |
||
9 | office | 314 | await _serialPort.FlushAsync(cancellationToken); |
3 | office | 315 | } |
316 | } |
||
317 | } |
||
318 | |||
1 | office | 319 | public T CatRead<T>(string command, object[] param) |
320 | { |
||
3 | office | 321 | if (!_serialPort.IsOpen) |
322 | { |
||
323 | _serialPort.Open(); |
||
324 | } |
||
325 | |||
1 | office | 326 | try |
327 | { |
||
328 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
329 | { |
||
330 | var methodInfo = catCommand.GetType().GetMethod("Read"); |
||
331 | if (methodInfo != null) |
||
332 | { |
||
333 | return (T)methodInfo.Invoke(catCommand, param); |
||
334 | } |
||
11 | office | 335 | |
336 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "Read"); |
||
1 | office | 337 | } |
11 | office | 338 | |
339 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
||
1 | office | 340 | } |
341 | finally |
||
342 | { |
||
3 | office | 343 | if (_serialPort.IsOpen) |
344 | { |
||
9 | office | 345 | _serialPort.Flush(); |
3 | office | 346 | _serialPort.Close(); |
347 | } |
||
1 | office | 348 | } |
349 | } |
||
350 | |||
9 | office | 351 | public async Task<T> CatReadAsync<T>(string command, object[] param, CancellationToken cancellationToken) |
1 | office | 352 | { |
3 | office | 353 | if (!_serialPort.IsOpen) |
354 | { |
||
355 | _serialPort.Open(); |
||
356 | } |
||
357 | |||
1 | office | 358 | try |
359 | { |
||
360 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
361 | { |
||
9 | office | 362 | var methodInfo = catCommand.GetType().GetMethod("ReadAsync"); |
1 | office | 363 | if (methodInfo != null) |
364 | { |
||
365 | if(methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
||
366 | { |
||
9 | office | 367 | var parameters = new List<object>(); |
368 | parameters.AddRange(param); |
||
369 | parameters.Add(cancellationToken); |
||
370 | |||
371 | return await (Task<T>)methodInfo.Invoke(catCommand, parameters.ToArray()); |
||
1 | office | 372 | } |
373 | } |
||
11 | office | 374 | |
375 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "ReadAsync"); |
||
1 | office | 376 | } |
11 | office | 377 | |
378 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
||
1 | office | 379 | } |
380 | finally |
||
381 | { |
||
3 | office | 382 | if (_serialPort.IsOpen) |
383 | { |
||
9 | office | 384 | await _serialPort.FlushAsync(cancellationToken); |
3 | office | 385 | _serialPort.Close(); |
386 | } |
||
1 | office | 387 | } |
388 | } |
||
389 | } |
||
390 | } |