HamBook – Diff between revs 46 and 54

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 46 Rev 54
Line 1... Line -...
1 using HamBook.Radios.Generic; -  
2 using HamBook.Radios.Generic.CAT; -  
3 using System; -  
4 using System.Collections.Generic; 1 using System.Text.RegularExpressions;
5 using System.Drawing; 2 using System.Threading;
6 using System.IO.Ports; -  
7 using System.Linq; -  
8 using System.Text; -  
9 using System.Text.RegularExpressions; -  
10 using System.Threading.Tasks; 3 using System.Threading.Tasks;
11 using static HamBook.Radios.Yaesu.FT_891.Constants; 4 using HamBook.Radios.Generic;
12 using RJCP.IO.Ports; 5 using RJCP.IO.Ports;
13 using System.Threading; -  
14 using System.Diagnostics; 6 using static HamBook.Radios.Yaesu.FT_891.Constants;
Line 15... Line 7...
15   7  
16 namespace HamBook.Radios.Yaesu.FT_891.CAT 8 namespace HamBook.Radios.Yaesu.FT_891.CAT
17 { 9 {
18 [Radio("Yaesu FT-891")] 10 [Radio("Yaesu FT-891")]
19 public class PS : HamBook.Radios.Generic.CAT.PS 11 public class Ps : Generic.CAT.Ps
20 { 12 {
21 private readonly Regex readRegex; -  
22   -  
Line 23... Line 13...
23 public override CatLength CatLength => new CatLength { Set = 4, Answer = 4, Read = 3 }; 13 private readonly Regex _readRegex;
24   14  
25 public PS(SerialPortStream serialPort) : base(serialPort) 15 public Ps(SerialPortStream serialPort) : base(serialPort)
26 { 16 {
Line -... Line 17...
-   17 _readRegex = new Regex($"^{Name}(?<state>[01]){Eot}$", RegexOptions.Compiled);
-   18 }
27 readRegex = new Regex($"^{Name}(?<state>[01]){Constants.EOT}$", RegexOptions.Compiled); 19  
28 } 20 public override CatLength CatLength => new CatLength { Set = 4, Answer = 4, Read = 3 };
29   21  
30 public override async Task<bool> SetAsync(PowerState state, CancellationToken cancellationToken) 22 public override async Task<bool> SetAsync(PowerState state, CancellationToken cancellationToken)
Line 31... Line 23...
31 { 23 {
Line 32... Line 24...
32 SerialPort.Write(new byte[1] { 0 }, 0, 1); 24 SerialPort.Write(new byte[1] { 0 }, 0, 1);
33 SerialPort.Read(new byte[2] { 0, 0 }, 0, 2); 25 SerialPort.Read(new byte[2] { 0, 0 }, 0, 2);
34   26  
35 var taskCompletionSource = new TaskCompletionSource<bool>(); 27 var taskCompletionSource = new TaskCompletionSource<bool>();
36   28  
37 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed 29 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
38 Task.Delay(POWER_ON_DELAY).ContinueWith(async task1 => 30 Task.Delay(PowerOnDelay).ContinueWith(async task1 =>
39 { 31 {
40 byte[] payload = null; 32 byte[] payload = null;
41 switch(state) -  
42 { -  
43 case PowerState.ON: 33 switch (state)
44 payload = Constants.Encoding.GetBytes($"{Name}1{Constants.EOT}"); -  
45 await SerialPort.WriteAsync(payload, 0, payload.Length); 34 {
46 Task.Delay(TIME_TO_WARM).ContinueWith(task2 => 35 case PowerState.ON:
47 { 36 payload = Encoding.GetBytes($"{Name}1{Eot}");
48 taskCompletionSource.TrySetResult(true); 37 await SerialPort.WriteAsync(payload, 0, payload.Length);
49 }); 38 Task.Delay(TimeToWarm).ContinueWith(task2 => { taskCompletionSource.TrySetResult(true); });
50 return; 39 return;
-   40 case PowerState.OFF:
51 case PowerState.OFF: 41 payload = Encoding.GetBytes($"{Name}0{Eot}");
52 payload = Constants.Encoding.GetBytes($"{Name}0{Constants.EOT}"); 42 await SerialPort.WriteAsync(payload, 0, payload.Length);
53 await SerialPort.WriteAsync(payload, 0, payload.Length); 43 break;
Line 54... Line 44...
54 break; 44 }
Line 67... Line 57...
67 SerialPort.Read(new byte[2] { 0, 0 }, 0, 2); 57 SerialPort.Read(new byte[2] { 0, 0 }, 0, 2);
Line 68... Line 58...
68   58  
Line 69... Line 59...
69 var taskCompletionSource = new TaskCompletionSource<int>(); 59 var taskCompletionSource = new TaskCompletionSource<int>();
70   60  
71 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed 61 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
72 Task.Delay(POWER_ON_DELAY).ContinueWith(async _ => 62 Task.Delay(PowerOnDelay).ContinueWith(async _ =>
Line 73... Line 63...
73 { 63 {
Line 74... Line 64...
74 var payload = Constants.Encoding.GetBytes($"{Name}{Constants.EOT}"); 64 var payload = Encoding.GetBytes($"{Name}{Eot}");
75   65  
76 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken); 66 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
77   67  
Line 78... Line 68...
78 var buffer = new byte[CatLength.Answer]; 68 var buffer = new byte[CatLength.Answer];
79 if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer, cancellationToken) != CatLength.Answer) 69 if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer, cancellationToken) != CatLength.Answer)
-   70 {
80 { 71 taskCompletionSource.TrySetResult(-1);
81 taskCompletionSource.TrySetResult(-1); 72  
82   73 throw new UnexpectedRadioResponseException(Name, buffer);
83 throw new UnexpectedRadioResponseException(Name, buffer); 74 }
84 } 75  
Line 85... Line 76...
85 var answer = Constants.Encoding.GetString(buffer); 76 var answer = Encoding.GetString(buffer);
86 var match = readRegex.Match(answer); 77 var match = _readRegex.Match(answer);
Line 87... Line 78...
87 if (!match.Success) 78 if (!match.Success)
88 { 79 {
89 taskCompletionSource.TrySetResult(-1); -  
90   80 taskCompletionSource.TrySetResult(-1);
91 throw new UnmatchedRadioResponseException(Name, answer); 81  
92 } 82 throw new UnmatchedRadioResponseException(Name, answer);
93   83 }
94 var state = int.Parse(match.Result("${state}")); 84  
95 taskCompletionSource.TrySetResult(state); 85 var state = int.Parse(match.Result("${state}"));
96   86 taskCompletionSource.TrySetResult(state);
97 }); 87 });
98 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed 88 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
99 var onoff = await taskCompletionSource.Task; 89 var onoff = await taskCompletionSource.Task;
Line 100... Line 90...
100 switch(onoff) 90 switch (onoff)
101 { 91 {
102 case 1: 92 case 1:
103 return PowerState.ON; 93 return PowerState.ON;
104 case 0: 94 case 0: