HamBook – Blame information for rev 1

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