HamBook – Blame information for rev 11

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