HamBook – Blame information for rev 54

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