HamBook – Blame information for rev 9

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>();
32 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
33 Task.Delay(Constants.POWER_ON_DELAY).ContinueWith(ca =>
34 {
35 switch(state)
36 {
37 case PowerState.ON:
3 office 38 SerialPort.Write($"{Name}1{Generic.Constants.EOT}");
1 office 39 Task.Delay(Constants.TIME_TO_WARM).ContinueWith(cb =>
40 {
41 taskCompletionSource.TrySetResult(true);
42 });
43 return;
44 case PowerState.OFF:
3 office 45 SerialPort.Write($"{Name}0{Generic.Constants.EOT}");
1 office 46 break;
47 }
48 taskCompletionSource.TrySetResult(true);
49 });
50 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
51  
52 SerialPort.Write($"POWER");
53 await taskCompletionSource.Task;
9 office 54 return await ReadAsync(cancellationToken) == state;
1 office 55 }
56  
9 office 57 public override async Task<PowerState> ReadAsync(CancellationToken cancellationToken)
1 office 58 {
59 var taskCompletionSource = new TaskCompletionSource<int>();
60 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
61 Task.Delay(Constants.POWER_ON_DELAY).ContinueWith(_ =>
62 {
3 office 63 SerialPort.Write($"{Name}{Generic.Constants.EOT}");
9 office 64 var result = SerialPort.ReadTo($"{Generic.Constants.EOT}");
1 office 65 var match = readRegex.Match(result);
66 var state = int.Parse(match.Result("${state}"));
67  
68 taskCompletionSource.TrySetResult(state);
69 });
70 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
71  
72 SerialPort.Write($"POWER");
73 var onoff = await taskCompletionSource.Task;
74 switch(onoff)
75 {
76 case 1:
77 return PowerState.ON;
78 case 0:
79 return PowerState.OFF;
80 }
81  
82 return PowerState.NONE;
83 }
84 }
85 }