HamBook – Diff between revs 46 and 54

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 46 Rev 54
Line 1... Line -...
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; 1 using System.Text.RegularExpressions;
12 using System.Threading; 2 using System.Threading;
13 using System.Threading.Tasks; 3 using System.Threading.Tasks;
14 using static HamBook.Radios.Yaesu.FT_891.Constants; 4 using HamBook.Radios.Generic;
15 using RJCP.IO.Ports; 5 using RJCP.IO.Ports;
-   6 using static HamBook.Radios.Yaesu.FT_891.Constants;
Line 16... Line 7...
16   7  
17 namespace HamBook.Radios.Yaesu.FT_891.CAT 8 namespace HamBook.Radios.Yaesu.FT_891.CAT
18 { 9 {
19 [Radio("Yaesu FT-891")] 10 [Radio("Yaesu FT-891")]
20 public class AI : Generic.CAT.AI 11 public class Ai : Generic.CAT.Ai
21 { 12 {
22 private readonly Regex readRegex; -  
23   -  
Line 24... Line 13...
24 public override CatLength CatLength => new CatLength { Set = 4, Read = 3, Answer = 4 }; 13 private readonly Regex _readRegex;
25   14  
26 public AI(SerialPortStream serialPort) : base(serialPort) 15 public Ai(SerialPortStream serialPort) : base(serialPort)
27 { 16 {
Line -... Line 17...
-   17 _readRegex = new Regex($"^{Name}(?<state>[01]){Eot}$", RegexOptions.Compiled);
-   18 }
28 readRegex = new Regex($"^{Name}(?<state>[01]){Constants.EOT}$", RegexOptions.Compiled); 19  
29 } 20 public override CatLength CatLength => new CatLength { Set = 4, Read = 3, Answer = 4 };
30   21  
31 public override InformationState Read() 22 public override InformationState Read()
32 { 23 {
33 SerialPort.Write($"{Name}{Constants.EOT}"); -  
34 var buffer = new byte[CatLength.Answer]; 24 SerialPort.Write($"{Name}{Eot}");
35 if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer) -  
Line 36... Line 25...
36 { 25 var buffer = new byte[CatLength.Answer];
37 throw new UnexpectedRadioResponseException(Name, buffer); 26 if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer)
38 } -  
39   -  
40 var answer = Constants.Encoding.GetString(buffer); 27 throw new UnexpectedRadioResponseException(Name, buffer);
41 var match = readRegex.Match(answer); -  
42 if (!match.Success) 28  
Line 43... Line 29...
43 { 29 var answer = Encoding.GetString(buffer);
44 throw new UnmatchedRadioResponseException(Name, answer); 30 var match = _readRegex.Match(answer);
-   31 if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer);
45 } 32 var state = (InformationState)int.Parse(match.Result("${state}"));
46 var state = (InformationState)int.Parse(match.Result("${state}")); 33  
47   34 return state;
48 return state; 35 }
49 } 36  
50 public override async Task<InformationState> ReadAsync(CancellationToken cancellationToken) 37 public override async Task<InformationState> ReadAsync(CancellationToken cancellationToken)
51 { -  
52 var payload = Constants.Encoding.GetBytes($"{Name}{Constants.EOT}"); 38 {
53 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken); -  
Line 54... Line 39...
54 var buffer = new byte[CatLength.Answer]; 39 var payload = Encoding.GetBytes($"{Name}{Eot}");
55 if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer, cancellationToken) != CatLength.Answer) 40 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
56 { -  
57 throw new UnexpectedRadioResponseException(Name, buffer); -  
58 } 41 var buffer = new byte[CatLength.Answer];
59   -  
Line 60... Line 42...
60 var answer = Constants.Encoding.GetString(buffer); 42 if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer, cancellationToken) != CatLength.Answer)
61 var match = readRegex.Match(answer); 43 throw new UnexpectedRadioResponseException(Name, buffer);
62 if (!match.Success) 44  
Line 63... Line 45...
63 { 45 var answer = Encoding.GetString(buffer);
64 throw new UnmatchedRadioResponseException(Name, answer); 46 var match = _readRegex.Match(answer);
65 } 47 if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer);
66   48  
67 var state = (InformationState)int.Parse(match.Result("${state}")); 49 var state = (InformationState)int.Parse(match.Result("${state}"));
-   50 return state;
68 return state; 51 }
69 } 52  
70   53 public override bool Set(InformationState state)
71 public override bool Set(InformationState state) 54 {
72 { 55 SerialPort.Write($"{Name}{(int)state}{Eot}");
73 SerialPort.Write($"{Name}{(int)state}{Constants.EOT}"); 56 return Read() == state;
Line 74... Line 57...
74 return Read() == state; 57 }
75 } 58  
76 public override async Task<bool> SetAsync(InformationState state, CancellationToken cancellationToken) 59 public override async Task<bool> SetAsync(InformationState state, CancellationToken cancellationToken)
Line 77... Line -...
77 { -  
78 var payload = Constants.Encoding.GetBytes($"{Name}{(int)state}{Constants.EOT}"); -  
79 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken); 60 {
80 return await ReadAsync(cancellationToken) == state; -  
Line 81... Line 61...
81 } 61 var payload = Encoding.GetBytes($"{Name}{(int)state}{Eot}");
82   62 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
83 public override Information Parse(string input) 63 return await ReadAsync(cancellationToken) == state;
84 { 64 }
Line 115... Line 95...
115 return information; 95 return information;
116 } 96 }
Line 117... Line 97...
117   97  
118 public override void Write(InformationState state) 98 public override void Write(InformationState state)
119 { 99 {
120 SerialPort.Write($"{Name}{(int)state}{Constants.EOT}"); 100 SerialPort.Write($"{Name}{(int)state}{Eot}");
Line 121... Line 101...
121 } 101 }
122   102  
123 public override async Task WriteAsync(InformationState state, CancellationToken cancellationToken) 103 public override async Task WriteAsync(InformationState state, CancellationToken cancellationToken)
Line 124... Line 104...
124 { 104 {
125 var payload = Constants.Encoding.GetBytes($"{Name}{(int)state}{Constants.EOT}"); 105 var payload = Encoding.GetBytes($"{Name}{(int)state}{Eot}");
126   106  
127 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken); 107 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
128 } 108 }