HamBook – Blame information for rev 54

Subversion Repositories:
Rev:
Rev Author Line No. Line
54 office 1 using System.Text.RegularExpressions;
2 using System.Threading;
50 office 3 using System.Threading.Tasks;
4 using HamBook.Radios.Generic;
54 office 5 using RJCP.IO.Ports;
50 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 Lk : Generic.CAT.Lk
50 office 12 {
54 office 13 private readonly Regex _readRegex;
50 office 14  
54 office 15 public Lk(SerialPortStream serialPort) : base(serialPort)
50 office 16 {
54 office 17 _readRegex = new Regex($"^{Name}(?<state>[01]{{1}}){Eot}$", RegexOptions.Compiled);
50 office 18 }
19  
54 office 20 public override CatLength CatLength => new CatLength { Set = 4, Read = 3, Answer = 4 };
21  
50 office 22 public override LockState Read()
23 {
54 office 24 SerialPort.Write($"{Name}{Eot}");
50 office 25 var buffer = new byte[CatLength.Answer];
26 if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer)
27 throw new UnexpectedRadioResponseException(Name, buffer);
28  
54 office 29 var answer = Encoding.GetString(buffer);
30 var match = _readRegex.Match(answer);
31 if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer);
50 office 32 return (LockState)int.Parse(match.Result("${state}"));
33 }
34  
35 public override async Task<LockState> ReadAsync(CancellationToken cancellationToken)
36 {
54 office 37 var payload = Encoding.GetBytes($"{Name}{Eot}");
50 office 38 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
39 var buffer = new byte[CatLength.Answer];
40 if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer, cancellationToken) != CatLength.Answer)
41 throw new UnexpectedRadioResponseException(Name, buffer);
42  
54 office 43 var answer = Encoding.GetString(buffer);
44 var match = _readRegex.Match(answer);
45 if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer);
50 office 46  
47 return (LockState)int.Parse(match.Result("${state}"));
48 }
49  
50 public override bool Set(LockState state)
51 {
54 office 52 var payload = Encoding.GetBytes($"{Name}{(int)state}{Eot}");
50 office 53  
54 SerialPort.Write(payload, 0, payload.Length);
55  
56 return Read() == state;
57 }
58  
59 public override async Task<bool> SetAsync(LockState state, CancellationToken cancellationToken)
60 {
54 office 61 var payload = Encoding.GetBytes($"{Name}{(int)state}{Eot}");
50 office 62  
63 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
64  
65 var result = await ReadAsync(cancellationToken);
66  
67 return result == state;
68 }
69  
70 public override void Write(LockState state)
71 {
54 office 72 SerialPort.Write($"{Name}{(int)state}{Eot}");
50 office 73 }
74  
75 public override async Task WriteAsync(LockState state, CancellationToken cancellationToken)
76 {
54 office 77 var payload = Encoding.GetBytes($"{Name}{(int)state}{Eot}");
50 office 78  
79 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
80 }
81 }
54 office 82 }