HamBook – Rev 46

Subversion Repositories:
Rev:
using HamBook.Radios.Generic;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static HamBook.Radios.Yaesu.FT_891.Constants;
using RJCP.IO.Ports;
using System.Threading;

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

        public override CatLength CatLength => new CatLength { Set = 5, Answer = 5, Read = 4 };

        public MD(SerialPortStream serialPort) : base(serialPort)
        {
            readRegex = new Regex($"^MD0(?<mode>[123456789ABCD]){Constants.EOT}$", RegexOptions.Compiled);
        }

        public override bool Set(Generic.RadioMode radioMode)
        {
            SerialPort.Write($"{Name}0{radioMode.Code}{Constants.EOT}");

            return Read() == radioMode;
        }

        public override async Task<bool> SetAsync(Generic.RadioMode radioMode, CancellationToken cancellationToken)
        {
            var payload = Constants.Encoding.GetBytes($"{Name}0{radioMode.Code}{Constants.EOT}");

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

            return (await ReadAsync(cancellationToken)).Code == radioMode.Code;
        }

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

            var answer = Constants.Encoding.GetString(buffer);
            var match = readRegex.Match(answer);
            if (!match.Success)
            {
                throw new UnmatchedRadioResponseException(Name, answer);
            }
            var radioMode = char.Parse(match.Result("${mode}"));
            return new RadioMode(radioMode);
        }
        public override async Task<Generic.RadioMode> ReadAsync(CancellationToken cancellationToken)
        {
            var payload = Constants.Encoding.GetBytes($"{Name}0{Constants.EOT}");
            await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
            var buffer = new byte[CatLength.Answer];
            if (await SerialPort.ReadAsync(buffer, 0, buffer.Length, cancellationToken) != CatLength.Answer)
            {
                throw new UnexpectedRadioResponseException(Name, buffer);
            }

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

            var radioMode = char.Parse(match.Result("${mode}"));
            return new RadioMode(radioMode);
        }
    }
}