HamBook – Diff between revs 3 and 9

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 3 Rev 9
Line 1... Line 1...
1 using HamBook.Properties; 1 using HamBook.Properties;
2 using Org.BouncyCastle.Utilities; 2 using Org.BouncyCastle.Utilities;
-   3 using RJCP.IO.Ports;
3 using Serilog; 4 using Serilog;
4 using System; 5 using System;
5 using System.Collections.Concurrent; 6 using System.Collections.Concurrent;
6 using System.Collections.Generic; 7 using System.Collections.Generic;
7 using System.IO.Ports; 8 using System.IO.Ports;
Line 18... Line 19...
18 public class CatAssemblies : IDisposable 19 public class CatAssemblies : IDisposable
19 { 20 {
20 public int Count => _count; 21 public int Count => _count;
Line 21... Line 22...
21   22  
22 private int _count; 23 private int _count;
23 private SerialPort _serialPort; 24 private SerialPortStream _serialPort;
Line 24... Line 25...
24 private string _radio; 25 private string _radio;
Line 25... Line 26...
25   26  
26 private ConcurrentDictionary<string, Cat> _catCommands; 27 private ConcurrentDictionary<string, Cat> _catCommands;
27   28  
28 private CatAssemblies() 29 private CatAssemblies()
Line 29... Line 30...
29 { 30 {
30 _catCommands = new ConcurrentDictionary<string, Cat>(); 31 _catCommands = new ConcurrentDictionary<string, Cat>();
31 } 32 }
32   33  
Line 33... Line 34...
33 public CatAssemblies(SerialPort serialPort, string radio) : this() 34 public CatAssemblies(SerialPortStream serialPort, string radio) : this()
Line 52... Line 53...
52 .Where(type => type.GetCustomAttribute<RadioAttribute>() !=null && type.GetCustomAttribute<RadioAttribute>().Radio == radio)) 53 .Where(type => type.GetCustomAttribute<RadioAttribute>() !=null && type.GetCustomAttribute<RadioAttribute>().Radio == radio))
53 { 54 {
54 try 55 try
55 { 56 {
56 var catCommand = (Cat)type 57 var catCommand = (Cat)type
57 .GetConstructor(new[] { typeof(SerialPort) }) 58 .GetConstructor(new[] { typeof(SerialPortStream) })
58 .Invoke(new [] { _serialPort }); 59 .Invoke(new [] { _serialPort });
Line 59... Line 60...
59   60  
60 if (catCommand == null) 61 if (catCommand == null)
61 { 62 {
Line 120... Line 121...
120 } 121 }
Line 121... Line 122...
121   122  
122 _catCommands.Clear(); 123 _catCommands.Clear();
Line 123... Line 124...
123 } 124 }
124   125  
125 public async Task CatSet<T>(string command, object[] param, CancellationToken cancellationToken) 126 public T CatGetDefault<T>(string command, object[] param)
126 { 127 {
127 if (!_serialPort.IsOpen) 128 if (!_serialPort.IsOpen)
128 { 129 {
Line 129... Line 130...
129 _serialPort.Open(); 130 _serialPort.Open();
130 } 131 }
131   132  
132 try 133 try
133 { 134 {
134 if (_catCommands.TryGetValue(command, out var catCommand)) 135 if (_catCommands.TryGetValue(command, out var catCommand))
135 { 136 {
136 var methodInfo = catCommand.GetType().GetMethod("Set"); -  
137 if (methodInfo != null) -  
138 { 137 var methodInfo = catCommand.GetType().GetMethod("GetDefault");
139 if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) -  
140 { 138 if (methodInfo != null)
141 await (Task)methodInfo.Invoke(catCommand, param); 139 {
142 } 140 return (T)methodInfo.Invoke(catCommand, param);
143 } 141 }
144 } 142 }
145 } 143 }
146 finally 144 finally
-   145 {
147 { 146 if (_serialPort.IsOpen)
-   147 {
-   148 _serialPort.Flush();
148 if (_serialPort.IsOpen) 149 _serialPort.Close();
149 { 150  
-   151 _serialPort.DiscardInBuffer();
-   152 }
-   153 }
-   154  
-   155 return default;
-   156 }
-   157  
-   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 }
150 _serialPort.Close(); 167 }
Line 151... Line 168...
151 } 168  
152 } 169 return default;
153 } 170 }
Line 172... Line 189...
172 } 189 }
173 finally 190 finally
174 { 191 {
175 if (_serialPort.IsOpen) 192 if (_serialPort.IsOpen)
176 { 193 {
-   194 _serialPort.Flush();
177 _serialPort.Close(); 195 _serialPort.Close();
-   196  
-   197 _serialPort.DiscardInBuffer();
178 } 198 }
179 } 199 }
180 } 200 }
Line 181... Line 201...
181   201  
182 public T CatGetDefault<T>(string command, object[] param) 202 public async Task CatSetAsync<T>(string command, object[] param, CancellationToken cancellationToken)
183 { 203 {
184 if (!_serialPort.IsOpen) 204 if (!_serialPort.IsOpen)
185 { 205 {
186 _serialPort.Open(); 206 _serialPort.Open();
Line 187... Line 207...
187 } 207 }
188   208  
189 try 209 try
190 { 210 {
191 if (_catCommands.TryGetValue(command, out var catCommand)) 211 if (_catCommands.TryGetValue(command, out var catCommand))
192 { 212 {
193 var methodInfo = catCommand.GetType().GetMethod("GetDefault"); 213 var methodInfo = catCommand.GetType().GetMethod("SetAsync");
-   214 if (methodInfo != null)
-   215 {
-   216 if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null)
-   217 {
-   218 var parameters = new List<object>();
-   219 parameters.AddRange(param);
194 if (methodInfo != null) 220 parameters.Add(cancellationToken);
-   221  
195 { 222 await (Task)methodInfo.Invoke(catCommand, parameters.ToArray());
196 return (T)methodInfo.Invoke(catCommand, param); 223 }
197 } 224 }
198 } 225 }
199 } 226 }
200 finally 227 finally
201 { 228 {
-   229 if (_serialPort.IsOpen)
202 if (_serialPort.IsOpen) 230 {
-   231 await _serialPort.FlushAsync(cancellationToken);
-   232 _serialPort.Close();
203 { 233  
204 _serialPort.Close(); 234 _serialPort.DiscardInBuffer();
205 } -  
206 } -  
207   235 }
Line 208... Line 236...
208 return default; 236 }
209 } 237 }
210   238  
211 public T CatParse<T>(string command, object[] param) 239 public void CatWrite<T>(string command, object[] param)
212 { 240 {
213 if (!_serialPort.IsOpen) 241 if (!_serialPort.IsOpen)
Line 214... Line 242...
214 { 242 {
215 _serialPort.Open(); 243 _serialPort.Open();
216 } 244 }
217   245  
218 try 246 try
219 { 247 {
220 if (_catCommands.TryGetValue(command, out var catCommand)) 248 if (_catCommands.TryGetValue(command, out var catCommand))
221 { 249 {
222 var methodInfo = catCommand.GetType().GetMethod("Parse"); 250 var methodInfo = catCommand.GetType().GetMethod("Write");
223 if (methodInfo != null) 251 if (methodInfo != null)
224 { 252 {
225 return (T)methodInfo.Invoke(catCommand, param); 253 methodInfo.Invoke(catCommand, param);
226 } 254 }
227 } 255 }
228 } 256 }
229 finally 257 finally
230 { 258 {
231 if (_serialPort.IsOpen) 259 if (_serialPort.IsOpen)
-   260 {
Line -... Line 261...
-   261 _serialPort.Flush();
-   262 }
-   263 }
-   264 }
-   265  
-   266 public async Task CatWriteAsync<T>(string command, object[] param, CancellationToken cancellationToken)
-   267 {
-   268 if (!_serialPort.IsOpen)
-   269 {
-   270 _serialPort.Open();
-   271 }
-   272  
-   273 try
-   274 {
-   275 if (_catCommands.TryGetValue(command, out var catCommand))
-   276 {
-   277 var methodInfo = catCommand.GetType().GetMethod("WriteAsync");
-   278 if (methodInfo != null)
-   279 {
-   280 if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null)
-   281 {
-   282 var parameters = new List<object>();
-   283 parameters.AddRange(param);
-   284 parameters.Add(cancellationToken);
-   285  
232 { 286 await (Task)methodInfo.Invoke(catCommand, parameters.ToArray());
-   287 }
-   288 }
-   289 }
-   290 }
-   291 finally
-   292 {
233 _serialPort.Close(); 293 if (_serialPort.IsOpen)
Line 234... Line 294...
234 } 294 {
235 } 295 await _serialPort.FlushAsync(cancellationToken);
236   296 }
Line 257... Line 317...
257 } 317 }
258 finally 318 finally
259 { 319 {
260 if (_serialPort.IsOpen) 320 if (_serialPort.IsOpen)
261 { 321 {
-   322 _serialPort.Flush();
262 _serialPort.Close(); 323 _serialPort.Close();
-   324  
-   325 _serialPort.DiscardInBuffer();
263 } 326 }
264 } 327 }
Line 265... Line 328...
265   328  
266 return default; 329 return default;
Line 267... Line 330...
267 } 330 }
268   331  
269 public async Task<T> CatRead<T>(string command, object[] param, CancellationToken cancellationToken) 332 public async Task<T> CatReadAsync<T>(string command, object[] param, CancellationToken cancellationToken)
270 { 333 {
271 if (!_serialPort.IsOpen) 334 if (!_serialPort.IsOpen)
272 { 335 {
Line 273... Line 336...
273 _serialPort.Open(); 336 _serialPort.Open();
274 } 337 }
275   338  
276 try 339 try
277 { 340 {
278 if (_catCommands.TryGetValue(command, out var catCommand)) 341 if (_catCommands.TryGetValue(command, out var catCommand))
279 { 342 {
280 var methodInfo = catCommand.GetType().GetMethod("Read"); 343 var methodInfo = catCommand.GetType().GetMethod("ReadAsync");
281 if (methodInfo != null) 344 if (methodInfo != null)
-   345 {
-   346 if(methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null)
-   347 {
-   348 var parameters = new List<object>();
282 { 349 parameters.AddRange(param);
283 if(methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) 350 parameters.Add(cancellationToken);
284 { 351  
285 return await (Task<T>)methodInfo.Invoke(catCommand, param); 352 return await (Task<T>)methodInfo.Invoke(catCommand, parameters.ToArray());
286 } 353 }
287 } 354 }
288 } 355 }
289 } 356 }
290 finally 357 finally
-   358 {
291 { 359 if (_serialPort.IsOpen)
-   360 {
-   361 await _serialPort.FlushAsync(cancellationToken);
292 if (_serialPort.IsOpen) 362 _serialPort.Close();
293 { 363  
Line 294... Line 364...
294 _serialPort.Close(); 364 _serialPort.DiscardInBuffer();
295 } 365 }