HamBook – Blame information for rev 9

Subversion Repositories:
Rev:
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();
9 office 150  
151 _serialPort.DiscardInBuffer();
3 office 152 }
1 office 153 }
9 office 154  
155 return default;
1 office 156 }
157  
9 office 158 public T CatParse<T>(string command, object[] param)
159 {
160 if (_catCommands.TryGetValue(command, out var catCommand))
161 {
162 var methodInfo = catCommand.GetType().GetMethod("Parse");
163 if (methodInfo != null)
164 {
165 return (T)methodInfo.Invoke(catCommand, param);
166 }
167 }
168  
169 return default;
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);
187 }
188 }
189 }
190 finally
191 {
3 office 192 if (_serialPort.IsOpen)
193 {
9 office 194 _serialPort.Flush();
3 office 195 _serialPort.Close();
9 office 196  
197 _serialPort.DiscardInBuffer();
3 office 198 }
1 office 199 }
200 }
201  
9 office 202 public async Task CatSetAsync<T>(string command, object[] param, CancellationToken cancellationToken)
3 office 203 {
204 if (!_serialPort.IsOpen)
205 {
206 _serialPort.Open();
207 }
208  
209 try
210 {
211 if (_catCommands.TryGetValue(command, out var catCommand))
212 {
9 office 213 var methodInfo = catCommand.GetType().GetMethod("SetAsync");
3 office 214 if (methodInfo != null)
215 {
9 office 216 if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null)
217 {
218 var parameters = new List<object>();
219 parameters.AddRange(param);
220 parameters.Add(cancellationToken);
221  
222 await (Task)methodInfo.Invoke(catCommand, parameters.ToArray());
223 }
3 office 224 }
225 }
226 }
227 finally
228 {
229 if (_serialPort.IsOpen)
230 {
9 office 231 await _serialPort.FlushAsync(cancellationToken);
3 office 232 _serialPort.Close();
9 office 233  
234 _serialPort.DiscardInBuffer();
3 office 235 }
236 }
9 office 237 }
3 office 238  
9 office 239 public void CatWrite<T>(string command, object[] param)
240 {
241 if (!_serialPort.IsOpen)
242 {
243 _serialPort.Open();
244 }
245  
246 try
247 {
248 if (_catCommands.TryGetValue(command, out var catCommand))
249 {
250 var methodInfo = catCommand.GetType().GetMethod("Write");
251 if (methodInfo != null)
252 {
253 methodInfo.Invoke(catCommand, param);
254 }
255 }
256 }
257 finally
258 {
259 if (_serialPort.IsOpen)
260 {
261 _serialPort.Flush();
262 }
263 }
3 office 264 }
265  
9 office 266 public async Task CatWriteAsync<T>(string command, object[] param, CancellationToken cancellationToken)
3 office 267 {
268 if (!_serialPort.IsOpen)
269 {
270 _serialPort.Open();
271 }
272  
273 try
274 {
275 if (_catCommands.TryGetValue(command, out var catCommand))
276 {
9 office 277 var methodInfo = catCommand.GetType().GetMethod("WriteAsync");
3 office 278 if (methodInfo != null)
279 {
9 office 280 if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null)
281 {
282 var parameters = new List<object>();
283 parameters.AddRange(param);
284 parameters.Add(cancellationToken);
285  
286 await (Task)methodInfo.Invoke(catCommand, parameters.ToArray());
287 }
3 office 288 }
289 }
290 }
291 finally
292 {
293 if (_serialPort.IsOpen)
294 {
9 office 295 await _serialPort.FlushAsync(cancellationToken);
3 office 296 }
297 }
298 }
299  
1 office 300 public T CatRead<T>(string command, object[] param)
301 {
3 office 302 if (!_serialPort.IsOpen)
303 {
304 _serialPort.Open();
305 }
306  
1 office 307 try
308 {
309 if (_catCommands.TryGetValue(command, out var catCommand))
310 {
311 var methodInfo = catCommand.GetType().GetMethod("Read");
312 if (methodInfo != null)
313 {
314 return (T)methodInfo.Invoke(catCommand, param);
315 }
316 }
317 }
318 finally
319 {
3 office 320 if (_serialPort.IsOpen)
321 {
9 office 322 _serialPort.Flush();
3 office 323 _serialPort.Close();
9 office 324  
325 _serialPort.DiscardInBuffer();
3 office 326 }
1 office 327 }
328  
329 return default;
330 }
331  
9 office 332 public async Task<T> CatReadAsync<T>(string command, object[] param, CancellationToken cancellationToken)
1 office 333 {
3 office 334 if (!_serialPort.IsOpen)
335 {
336 _serialPort.Open();
337 }
338  
1 office 339 try
340 {
341 if (_catCommands.TryGetValue(command, out var catCommand))
342 {
9 office 343 var methodInfo = catCommand.GetType().GetMethod("ReadAsync");
1 office 344 if (methodInfo != null)
345 {
346 if(methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null)
347 {
9 office 348 var parameters = new List<object>();
349 parameters.AddRange(param);
350 parameters.Add(cancellationToken);
351  
352 return await (Task<T>)methodInfo.Invoke(catCommand, parameters.ToArray());
1 office 353 }
354 }
355 }
356 }
357 finally
358 {
3 office 359 if (_serialPort.IsOpen)
360 {
9 office 361 await _serialPort.FlushAsync(cancellationToken);
3 office 362 _serialPort.Close();
9 office 363  
364 _serialPort.DiscardInBuffer();
3 office 365 }
1 office 366 }
367  
368 return default;
369 }
370 }
371 }