HamBook – Blame information for rev 46

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