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