HamBook – Diff between revs 15 and 26
?pathlinks?
Rev 15 | Rev 26 | |||
---|---|---|---|---|
Line 23... | Line 23... | |||
23 | private int _count; |
23 | private int _count; |
|
24 | private SerialPortStream _serialPort; |
24 | private SerialPortStream _serialPort; |
|
25 | private string _radio; |
25 | private string _radio; |
|
Line 26... | Line 26... | |||
26 | |
26 | |
|
- | 27 | private ConcurrentDictionary<string, Cat> _catCommands; |
||
Line 27... | Line 28... | |||
27 | private ConcurrentDictionary<string, Cat> _catCommands; |
28 | private SemaphoreSlim _semaphoreSlim; |
|
28 | |
29 | |
|
29 | private CatAssemblies() |
30 | private CatAssemblies() |
|
- | 31 | { |
||
30 | { |
32 | _catCommands = new ConcurrentDictionary<string, Cat>(); |
|
Line 31... | Line 33... | |||
31 | _catCommands = new ConcurrentDictionary<string, Cat>(); |
33 | _semaphoreSlim = new SemaphoreSlim(1, 1); |
|
32 | } |
34 | } |
|
33 | |
35 | |
|
Line 121... | Line 123... | |||
121 | } |
123 | } |
|
Line 122... | Line 124... | |||
122 | |
124 | |
|
123 | _catCommands.Clear(); |
125 | _catCommands.Clear(); |
|
Line 124... | Line 126... | |||
124 | } |
126 | } |
|
125 | |
127 | |
|
126 | public T CatGetDefault<T>(string command, object[] param) |
128 | public T CatParse<T>(string command, object[] param) |
|
127 | { |
129 | { |
|
- | 130 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
128 | if (!_serialPort.IsOpen) |
131 | { |
|
- | 132 | var methodInfo = catCommand.GetType().GetMethod("Parse"); |
||
- | 133 | if (methodInfo != null) |
||
- | 134 | { |
||
- | 135 | return (T)methodInfo.Invoke(catCommand, param); |
||
- | 136 | } |
||
129 | { |
137 | |
|
Line -... | Line 138... | |||
- | 138 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "Parse"); |
||
- | 139 | } |
||
- | 140 | |
||
- | 141 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
||
- | 142 | } |
||
- | 143 | |
||
- | 144 | public T CatGetDefault<T>(string command, object[] param) |
||
130 | _serialPort.Open(); |
145 | { |
|
131 | } |
146 | _semaphoreSlim.Wait(); |
|
- | 147 | |
||
- | 148 | try |
||
- | 149 | { |
||
- | 150 | if (!_serialPort.IsOpen) |
||
- | 151 | { |
||
- | 152 | _serialPort.Open(); |
||
- | 153 | } |
||
132 | |
154 | |
|
- | 155 | try |
||
- | 156 | { |
||
- | 157 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
- | 158 | { |
||
- | 159 | var methodInfo = catCommand.GetType().GetMethod("GetDefault"); |
||
- | 160 | if (methodInfo != null) |
||
- | 161 | { |
||
- | 162 | return (T)methodInfo.Invoke(catCommand, param); |
||
- | 163 | } |
||
133 | try |
164 | } |
|
134 | { |
- | ||
135 | if (_catCommands.TryGetValue(command, out var catCommand)) |
165 | } |
|
136 | { |
166 | finally |
|
- | 167 | { |
||
137 | var methodInfo = catCommand.GetType().GetMethod("GetDefault"); |
168 | if (_serialPort.IsOpen) |
|
138 | if (methodInfo != null) |
169 | { |
|
139 | { |
170 | _serialPort.Flush(); |
|
140 | return (T)methodInfo.Invoke(catCommand, param); |
171 | _serialPort.Close(); |
|
141 | } |
172 | } |
|
142 | } |
173 | } |
|
143 | } |
- | ||
144 | finally |
- | ||
145 | { |
- | ||
146 | if (_serialPort.IsOpen) |
174 | } |
|
147 | { |
- | ||
148 | _serialPort.Flush(); |
175 | finally |
|
Line 149... | Line 176... | |||
149 | _serialPort.Close(); |
176 | { |
|
150 | } |
177 | _semaphoreSlim.Release(); |
|
Line 151... | Line 178... | |||
151 | } |
178 | } |
|
152 | |
179 | |
|
153 | return default; |
180 | return default; |
|
- | 181 | } |
||
- | 182 | |
||
154 | } |
183 | public void CatSet<T>(string command, object[] param) |
|
155 | |
- | ||
156 | public T CatParse<T>(string command, object[] param) |
184 | { |
|
157 | { |
185 | _semaphoreSlim.Wait(); |
|
158 | if (_catCommands.TryGetValue(command, out var catCommand)) |
186 | |
|
159 | { |
187 | try |
|
Line -... | Line 188... | |||
- | 188 | { |
||
- | 189 | if (!_serialPort.IsOpen) |
||
160 | var methodInfo = catCommand.GetType().GetMethod("Parse"); |
190 | { |
|
161 | if (methodInfo != null) |
191 | _serialPort.Open(); |
|
- | 192 | } |
||
- | 193 | |
||
- | 194 | try |
||
- | 195 | { |
||
Line 162... | Line 196... | |||
162 | { |
196 | if (_catCommands.TryGetValue(command, out var catCommand)) |
|
163 | return (T)methodInfo.Invoke(catCommand, param); |
197 | { |
|
Line 164... | Line 198... | |||
164 | } |
198 | var methodInfo = catCommand.GetType().GetMethod("Set"); |
|
165 | |
- | ||
166 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "Parse"); |
- | ||
167 | } |
- | ||
168 | |
- | ||
169 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
199 | if (methodInfo != null) |
|
Line -... | Line 200... | |||
- | 200 | { |
||
170 | } |
201 | methodInfo.Invoke(catCommand, param); |
|
171 | |
202 | |
|
172 | public void CatSet<T>(string command, object[] param) |
- | ||
173 | { |
203 | return; |
|
174 | if (!_serialPort.IsOpen) |
- | ||
175 | { |
204 | } |
|
176 | _serialPort.Open(); |
205 | |
|
177 | } |
206 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "Set"); |
|
178 | |
- | ||
179 | try |
207 | } |
|
180 | { |
208 | |
|
181 | if (_catCommands.TryGetValue(command, out var catCommand)) |
- | ||
182 | { |
- | ||
183 | var methodInfo = catCommand.GetType().GetMethod("Set"); |
209 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
|
184 | if (methodInfo != null) |
- | ||
185 | { |
- | ||
186 | methodInfo.Invoke(catCommand, param); |
210 | } |
|
187 | |
211 | finally |
|
188 | return; |
212 | { |
|
189 | } |
- | ||
190 | |
- | ||
191 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "Set"); |
- | ||
192 | } |
213 | if (_serialPort.IsOpen) |
|
193 | |
- | ||
194 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
214 | { |
|
195 | } |
215 | _serialPort.Flush(); |
|
Line 196... | Line 216... | |||
196 | finally |
216 | _serialPort.Close(); |
|
197 | { |
217 | } |
|
198 | if (_serialPort.IsOpen) |
- | ||
199 | { |
- | ||
200 | _serialPort.Flush(); |
218 | } |
|
201 | _serialPort.Close(); |
- | ||
Line 202... | Line 219... | |||
202 | } |
219 | } |
|
203 | } |
220 | finally |
|
204 | } |
221 | { |
|
- | 222 | _semaphoreSlim.Release(); |
||
- | 223 | } |
||
- | 224 | } |
||
- | 225 | |
||
- | 226 | public async Task<U> CatSetAsync<T, U>(string command, object[] param, CancellationToken cancellationToken) |
||
205 | |
227 | { |
|
206 | public async Task<U> CatSetAsync<T, U>(string command, object[] param, CancellationToken cancellationToken) |
228 | await _semaphoreSlim.WaitAsync(cancellationToken); |
|
207 | { |
- | ||
208 | if (!_serialPort.IsOpen) |
229 | |
|
209 | { |
230 | try |
|
- | 231 | { |
||
210 | _serialPort.Open(); |
232 | if (!_serialPort.IsOpen) |
|
- | 233 | { |
||
- | 234 | _serialPort.Open(); |
||
211 | } |
235 | } |
|
212 | |
236 | |
|
213 | try |
237 | try |
|
Line 214... | Line 238... | |||
214 | { |
238 | { |
|
Line 215... | Line 239... | |||
215 | if (_catCommands.TryGetValue(command, out var catCommand)) |
239 | if (_catCommands.TryGetValue(command, out var catCommand)) |
|
- | 240 | { |
||
216 | { |
241 | var methodInfo = catCommand.GetType().GetMethod("SetAsync"); |
|
- | 242 | if (methodInfo != null) |
||
217 | var methodInfo = catCommand.GetType().GetMethod("SetAsync"); |
243 | { |
|
218 | if (methodInfo != null) |
- | ||
219 | { |
- | ||
Line 220... | Line 244... | |||
220 | if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
244 | if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
|
- | 245 | { |
||
- | 246 | var parameters = new List<object>(); |
||
- | 247 | parameters.AddRange(param); |
||
- | 248 | parameters.Add(cancellationToken); |
||
- | 249 | |
||
- | 250 | var result = await (Task<U>)methodInfo.Invoke(catCommand, parameters.ToArray()); |
||
- | 251 | |
||
- | 252 | return result; |
||
- | 253 | } |
||
221 | { |
254 | } |
|
222 | var parameters = new List<object>(); |
255 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "SetAsync"); |
|
223 | parameters.AddRange(param); |
256 | } |
|
224 | parameters.Add(cancellationToken); |
- | ||
225 | |
- | ||
226 | var result = await (Task<U>)methodInfo.Invoke(catCommand, parameters.ToArray()); |
- | ||
227 | |
257 | |
|
228 | return result; |
- | ||
229 | } |
258 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
|
230 | } |
259 | } |
|
Line 231... | Line 260... | |||
231 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "SetAsync"); |
260 | finally |
|
232 | } |
261 | { |
|
233 | |
- | ||
234 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
- | ||
235 | } |
262 | if (_serialPort.IsOpen) |
|
236 | finally |
- | ||
Line 237... | Line 263... | |||
237 | { |
263 | { |
|
238 | if (_serialPort.IsOpen) |
264 | await _serialPort.FlushAsync(cancellationToken); |
|
239 | { |
265 | _serialPort.Close(); |
|
240 | await _serialPort.FlushAsync(cancellationToken); |
266 | } |
|
241 | _serialPort.Close(); |
267 | } |
|
- | 268 | } |
||
- | 269 | finally |
||
- | 270 | { |
||
- | 271 | _semaphoreSlim.Release(); |
||
242 | } |
272 | } |
|
243 | } |
273 | } |
|
- | 274 | |
||
- | 275 | public void CatWrite<T>(string command, object[] param) |
||
- | 276 | { |
||
244 | } |
277 | _semaphoreSlim.Wait(); |
|
- | 278 | |
||
- | 279 | try |
||
- | 280 | { |
||
Line 245... | Line 281... | |||
245 | |
281 | if (!_serialPort.IsOpen) |
|
246 | public void CatWrite<T>(string command, object[] param) |
282 | { |
|
Line 247... | Line 283... | |||
247 | { |
283 | _serialPort.Open(); |
|
- | 284 | } |
||
- | 285 | |
||
- | 286 | try |
||
- | 287 | { |
||
- | 288 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
- | 289 | { |
||
- | 290 | var methodInfo = catCommand.GetType().GetMethod("Write"); |
||
248 | if (!_serialPort.IsOpen) |
291 | if (methodInfo != null) |
|
249 | { |
- | ||
250 | _serialPort.Open(); |
- | ||
251 | } |
292 | { |
|
252 | |
293 | methodInfo.Invoke(catCommand, param); |
|
253 | try |
294 | |
|
254 | { |
- | ||
255 | if (_catCommands.TryGetValue(command, out var catCommand)) |
- | ||
256 | { |
295 | return; |
|
257 | var methodInfo = catCommand.GetType().GetMethod("Write"); |
- | ||
258 | if (methodInfo != null) |
296 | } |
|
259 | { |
297 | |
|
Line 260... | Line 298... | |||
260 | methodInfo.Invoke(catCommand, param); |
298 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "Write"); |
|
261 | |
299 | } |
|
262 | return; |
- | ||
263 | } |
- | ||
264 | |
300 | |
|
265 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "Write"); |
- | ||
Line 266... | Line 301... | |||
266 | } |
301 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
|
267 | |
302 | } |
|
268 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
303 | finally |
|
269 | } |
304 | { |
|
270 | finally |
305 | if (_serialPort.IsOpen) |
|
- | 306 | { |
||
- | 307 | _serialPort.Flush(); |
||
- | 308 | } |
||
- | 309 | } |
||
271 | { |
310 | } |
|
272 | if (_serialPort.IsOpen) |
311 | finally |
|
273 | { |
312 | { |
|
- | 313 | _semaphoreSlim.Release(); |
||
274 | _serialPort.Flush(); |
314 | } |
|
- | 315 | } |
||
- | 316 | |
||
275 | } |
317 | public async Task CatWriteAsync<T>(string command, object[] param, CancellationToken cancellationToken) |
|
276 | } |
318 | { |
|
277 | } |
319 | await _semaphoreSlim.WaitAsync(cancellationToken); |
|
Line 278... | Line 320... | |||
278 | |
320 | |
|
Line 279... | Line 321... | |||
279 | public async Task CatWriteAsync<T>(string command, object[] param, CancellationToken cancellationToken) |
321 | try |
|
- | 322 | { |
||
280 | { |
323 | if (!_serialPort.IsOpen) |
|
- | 324 | { |
||
- | 325 | _serialPort.Open(); |
||
281 | if (!_serialPort.IsOpen) |
326 | } |
|
Line 282... | Line 327... | |||
282 | { |
327 | |
|
- | 328 | try |
||
- | 329 | { |
||
- | 330 | if (_catCommands.TryGetValue(command, out var catCommand)) |
||
- | 331 | { |
||
- | 332 | var methodInfo = catCommand.GetType().GetMethod("WriteAsync"); |
||
- | 333 | if (methodInfo != null) |
||
- | 334 | { |
||
283 | _serialPort.Open(); |
335 | if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
|
284 | } |
- | ||
285 | |
- | ||
286 | try |
336 | { |
|
287 | { |
337 | var parameters = new List<object>(); |
|
288 | if (_catCommands.TryGetValue(command, out var catCommand)) |
338 | parameters.AddRange(param); |
|
289 | { |
339 | parameters.Add(cancellationToken); |
|
290 | var methodInfo = catCommand.GetType().GetMethod("WriteAsync"); |
- | ||
291 | if (methodInfo != null) |
- | ||
292 | { |
- | ||
293 | if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |
340 | |
|
294 | { |
341 | await (Task)methodInfo.Invoke(catCommand, parameters.ToArray()); |
|
Line 295... | Line 342... | |||
295 | var parameters = new List<object>(); |
342 | |
|
296 | parameters.AddRange(param); |
343 | return; |
|
297 | parameters.Add(cancellationToken); |
- | ||
298 | |
- | ||
299 | await (Task)methodInfo.Invoke(catCommand, parameters.ToArray()); |
344 | } |
|
300 | |
- | ||
Line 301... | Line 345... | |||
301 | return; |
345 | } |
|
302 | } |
346 | |
|
303 | } |
347 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "WriteAsync"); |
|
- | 348 | } |
||
- | 349 | |
||
- | 350 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
||
- | 351 | } |
||
- | 352 | finally |
||
304 | |
353 | { |
|
305 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "WriteAsync"); |
354 | if (_serialPort.IsOpen) |
|
306 | } |
- | ||
307 | |
355 | { |
|
- | 356 | await _serialPort.FlushAsync(cancellationToken); |
||
- | 357 | } |
||
- | 358 | } |
||
308 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
359 | } |
|
- | 360 | finally |
||
- | 361 | { |
||
- | 362 | _semaphoreSlim.Release(); |
||
309 | } |
363 | } |
|
Line 310... | Line 364... | |||
310 | finally |
364 | } |
|
- | 365 | |
||
- | 366 | public T CatRead<T>(string command, object[] param) |
||
- | 367 | { |
||
- | 368 | _semaphoreSlim.Wait(); |
||
- | 369 | |
||
- | 370 | try |
||
- | 371 | { |
||
- | 372 | if (!_serialPort.IsOpen) |
||
311 | { |
373 | { |
|
312 | if (_serialPort.IsOpen) |
- | ||
313 | { |
- | ||
314 | await _serialPort.FlushAsync(cancellationToken); |
374 | _serialPort.Open(); |
|
315 | } |
375 | } |
|
316 | } |
376 | |
|
317 | } |
- | ||
318 | |
- | ||
319 | public T CatRead<T>(string command, object[] param) |
- | ||
320 | { |
377 | try |
|
321 | if (!_serialPort.IsOpen) |
- | ||
322 | { |
378 | { |
|
323 | _serialPort.Open(); |
379 | if (_catCommands.TryGetValue(command, out var catCommand)) |
|
Line 324... | Line 380... | |||
324 | } |
380 | { |
|
325 | |
381 | var methodInfo = catCommand.GetType().GetMethod("Read"); |
|
326 | try |
- | ||
327 | { |
- | ||
328 | if (_catCommands.TryGetValue(command, out var catCommand)) |
382 | if (methodInfo != null) |
|
329 | { |
- | ||
Line 330... | Line 383... | |||
330 | var methodInfo = catCommand.GetType().GetMethod("Read"); |
383 | { |
|
331 | if (methodInfo != null) |
384 | return (T)methodInfo.Invoke(catCommand, param); |
|
332 | { |
385 | } |
|
- | 386 | |
||
- | 387 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "Read"); |
||
- | 388 | } |
||
- | 389 | |
||
- | 390 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
||
333 | return (T)methodInfo.Invoke(catCommand, param); |
391 | } |
|
334 | } |
392 | finally |
|
335 | |
- | ||
336 | throw new CatCommandException(Resources.CAT_command_method_not_found, command, "Read"); |
393 | { |
|
337 | } |
394 | if (_serialPort.IsOpen) |
|
- | 395 | { |
||
338 | |
396 | _serialPort.Flush(); |
|
- | 397 | _serialPort.Close(); |
||
- | 398 | } |
||
339 | throw new CatCommandException(Resources.CAT_command_not_found, command); |
399 | } |
|
340 | } |
400 | } |
|
341 | finally |
401 | finally |
|
Line 342... | Line 402... | |||
342 | { |
402 | { |
|
- | 403 | _semaphoreSlim.Release(); |
||
343 | if (_serialPort.IsOpen) |
404 | } |
|
- | 405 | } |
||
- | 406 | |
||
344 | { |
407 | public async Task<T> CatReadAsync<T>(string command, object[] param, CancellationToken cancellationToken) |
|
Line 345... | Line 408... | |||
345 | _serialPort.Flush(); |
408 | { |
|
- | 409 | await _semaphoreSlim.WaitAsync(cancellationToken); |
||
- | 410 | |
||
- | 411 | try |
||
- | 412 | { |
||
- | 413 | if (!_serialPort.IsOpen) |
||
- | 414 | { |
||
- | 415 | _serialPort.Open(); |
||
- | 416 | } |
||
346 | _serialPort.Close(); |
417 | |
|
347 | } |
- | ||
348 | } |
- | ||
349 | } |
418 | try |
|
350 | |
419 | { |
|
351 | public async Task<T> CatReadAsync<T>(string command, object[] param, CancellationToken cancellationToken) |
420 | if (_catCommands.TryGetValue(command, out var catCommand)) |
|
352 | { |
- | ||
353 | if (!_serialPort.IsOpen) |
- | ||
354 | { |
- | ||
355 | _serialPort.Open(); |
421 | { |
|
356 | } |
- | ||
357 | |
422 | var methodInfo = catCommand.GetType().GetMethod("ReadAsync"); |
|
358 | try |
423 | if (methodInfo != null) |
|
359 | { |
424 | { |
|
360 | if (_catCommands.TryGetValue(command, out var catCommand)) |
425 | if (methodInfo.GetCustomAttribute<AsyncStateMachineAttribute>() != null) |