HamBook – Blame information for rev 54

Subversion Repositories:
Rev:
Rev Author Line No. Line
54 office 1 using System.Text.RegularExpressions;
9 office 2 using System.Threading;
3 office 3 using System.Threading.Tasks;
54 office 4 using HamBook.Radios.Generic;
5 using RJCP.IO.Ports;
3 office 6 using static HamBook.Radios.Yaesu.FT_891.Constants;
7  
8 namespace HamBook.Radios.Yaesu.FT_891.CAT
9 {
10 [Radio("Yaesu FT-891")]
54 office 11 public class Ai : Generic.CAT.Ai
3 office 12 {
54 office 13 private readonly Regex _readRegex;
3 office 14  
54 office 15 public Ai(SerialPortStream serialPort) : base(serialPort)
3 office 16 {
54 office 17 _readRegex = new Regex($"^{Name}(?<state>[01]){Eot}$", RegexOptions.Compiled);
3 office 18 }
19  
54 office 20 public override CatLength CatLength => new CatLength { Set = 4, Read = 3, Answer = 4 };
21  
3 office 22 public override InformationState Read()
23 {
54 office 24 SerialPort.Write($"{Name}{Eot}");
9 office 25 var buffer = new byte[CatLength.Answer];
26 if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer)
11 office 27 throw new UnexpectedRadioResponseException(Name, buffer);
9 office 28  
54 office 29 var answer = Encoding.GetString(buffer);
30 var match = _readRegex.Match(answer);
31 if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer);
3 office 32 var state = (InformationState)int.Parse(match.Result("${state}"));
33  
34 return state;
35 }
54 office 36  
9 office 37 public override async Task<InformationState> ReadAsync(CancellationToken cancellationToken)
38 {
54 office 39 var payload = Encoding.GetBytes($"{Name}{Eot}");
9 office 40 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
41 var buffer = new byte[CatLength.Answer];
42 if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer, cancellationToken) != CatLength.Answer)
11 office 43 throw new UnexpectedRadioResponseException(Name, buffer);
3 office 44  
54 office 45 var answer = Encoding.GetString(buffer);
46 var match = _readRegex.Match(answer);
47 if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer);
9 office 48  
49 var state = (InformationState)int.Parse(match.Result("${state}"));
50 return state;
51 }
52  
53 public override bool Set(InformationState state)
3 office 54 {
54 office 55 SerialPort.Write($"{Name}{(int)state}{Eot}");
9 office 56 return Read() == state;
3 office 57 }
54 office 58  
9 office 59 public override async Task<bool> SetAsync(InformationState state, CancellationToken cancellationToken)
60 {
54 office 61 var payload = Encoding.GetBytes($"{Name}{(int)state}{Eot}");
9 office 62 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
63 return await ReadAsync(cancellationToken) == state;
64 }
3 office 65  
66 public override Information Parse(string input)
67 {
68 var match = InformationRegex.Match(input);
69  
54 office 70 if (!match.Success) throw new UnmatchedRadioResponseException(Name, input);
3 office 71  
72 var radioBank = int.Parse(match.Result("${radioBank}"));
73 var frequency = int.Parse(match.Result("${frequency}"));
74 var clarifierDirection = char.Parse(match.Result("${clarifierDirection}"));
75 var clarifierOffset = int.Parse(match.Result("${clarifierOffset}"));
15 office 76 var clar = int.Parse(match.Result("${clar}")) == 0 ? false : true;
3 office 77 var radioMode = char.Parse(match.Result("${mode}"));
15 office 78 var storage = match.Result("${storage}");
46 office 79 var ctcss = int.Parse(match.Result("${ctcssMode}"));
80 var phase = int.Parse(match.Result("${phase}"));
3 office 81  
82 var information = new Information
83 {
84 RadioBank = radioBank,
85 Frequency = frequency,
86 ClarifierDirection = clarifierDirection,
87 ClarifierOffset = clarifierOffset,
88 Clar = clar,
43 office 89 RadioMode = new RadioMode(radioMode),
3 office 90 Storage = storage,
46 office 91 Ctcss = new Ctcss(ctcss),
92 Phase = new Phase(phase)
3 office 93 };
94  
95 return information;
96 }
9 office 97  
98 public override void Write(InformationState state)
99 {
54 office 100 SerialPort.Write($"{Name}{(int)state}{Eot}");
9 office 101 }
102  
103 public override async Task WriteAsync(InformationState state, CancellationToken cancellationToken)
104 {
54 office 105 var payload = Encoding.GetBytes($"{Name}{(int)state}{Eot}");
9 office 106  
107 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
108 }
3 office 109 }
54 office 110 }