HamBook – Rev 54

Subversion Repositories:
Rev:
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using RJCP.IO.Ports;
using static HamBook.Radios.Yaesu.FT_891.Constants;

namespace HamBook.Radios.Yaesu.FT_891.CAT
{
    [Radio("Yaesu FT-891")]
    public class Mc : Generic.CAT.Mc
    {
        private readonly Regex _readRegex;

        public Mc(SerialPortStream serialPort) : base(serialPort)
        {
            _readRegex = new Regex($"^{Name}(?<channel>[0-9PLU]{{3}}){Eot}$", RegexOptions.Compiled);
        }

        public override CatLength CatLength => new CatLength { Set = 6, Read = 3, Answer = 6 };

        public override string Read()
        {
            SerialPort.Write($"{Name}{Eot}");
            var buffer = new byte[CatLength.Answer];
            if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer)
                throw new UnexpectedRadioResponseException(Name, buffer);

            var answer = Encoding.GetString(buffer);
            var match = _readRegex.Match(answer);
            if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer);
            return $"{match.Result("${channel}")}";
        }

        public override async Task<string> ReadAsync(CancellationToken cancellationToken)
        {
            var payload = Encoding.GetBytes($"{Name}{Eot}");
            await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
            var buffer = new byte[CatLength.Answer];
            if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer, cancellationToken) != CatLength.Answer)
                throw new UnexpectedRadioResponseException(Name, buffer);

            var answer = Encoding.GetString(buffer);
            var match = _readRegex.Match(answer);
            if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer);

            return $"{match.Result("${channel}")}";
        }

        public override bool Set(string channel)
        {
            var payload = Encoding.GetBytes($"{Name}{channel}{Eot}");

            SerialPort.Write(payload, 0, payload.Length);

            return Read() == channel;
        }

        public override async Task<bool> SetAsync(string channel, CancellationToken cancellationToken)
        {
            var payload = Encoding.GetBytes($"{Name}{channel}{Eot}");

            await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);

            return await ReadAsync(cancellationToken) == channel;
        }

        public override void Write(string channel)
        {
            SerialPort.Write($"{Name}{channel}{Eot}");
        }

        public override async Task WriteAsync(string channel, CancellationToken cancellationToken)
        {
            var payload = Encoding.GetBytes($"{Name}{channel}{Eot}");

            await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
        }
    }
}

Generated by GNU Enscript 1.6.5.90.