HamBook – Blame information for rev 7

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;
12  
13 namespace HamBook.Radios.Yaesu.FT_891
14 {
15 [Radio("Yaesu FT-891")]
3 office 16 public class PS : HamBook.Radios.Generic.CAT.PS
1 office 17 {
18 private static readonly Regex readRegex = new Regex(@"^PS(?<state>[0,1])$", RegexOptions.Compiled);
19  
20 public PS(SerialPort serialPort) : base(serialPort)
21 {
22 }
23  
7 office 24 public override async Task<bool> Set(PowerState state)
1 office 25 {
26 var taskCompletionSource = new TaskCompletionSource<bool>();
27 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
28 Task.Delay(Constants.POWER_ON_DELAY).ContinueWith(ca =>
29 {
30 switch(state)
31 {
32 case PowerState.ON:
3 office 33 SerialPort.Write($"{Name}1{Generic.Constants.EOT}");
1 office 34 Task.Delay(Constants.TIME_TO_WARM).ContinueWith(cb =>
35 {
36 taskCompletionSource.TrySetResult(true);
37 });
38 return;
39 case PowerState.OFF:
3 office 40 SerialPort.Write($"{Name}0{Generic.Constants.EOT}");
1 office 41 break;
42 }
43 taskCompletionSource.TrySetResult(true);
44 });
45 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
46  
47 SerialPort.Write($"POWER");
48 await taskCompletionSource.Task;
7 office 49 return await Read() == state;
1 office 50 }
51  
52 public override async Task<PowerState> Read()
53 {
54 var taskCompletionSource = new TaskCompletionSource<int>();
55 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
56 Task.Delay(Constants.POWER_ON_DELAY).ContinueWith(_ =>
57 {
3 office 58 SerialPort.Write($"{Name}{Generic.Constants.EOT}");
59 var result = SerialPort.ReadTo(Generic.Constants.EOT);
1 office 60 var match = readRegex.Match(result);
61 var state = int.Parse(match.Result("${state}"));
62  
63 taskCompletionSource.TrySetResult(state);
64 });
65 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
66  
67 SerialPort.Write($"POWER");
68 var onoff = await taskCompletionSource.Task;
69 switch(onoff)
70 {
71 case 1:
72 return PowerState.ON;
73 case 0:
74 return PowerState.OFF;
75 }
76  
77 return PowerState.NONE;
78 }
79 }
80 }