HamBook – Rev 46

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

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

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

        public ST(SerialPortStream serialPort) : base(serialPort)
        {
            readRegex = new Regex($"^{Name}(?<state>[012]){Constants.EOT}$", RegexOptions.Compiled);
        }

        public override SplitState Read()
        {
            SerialPort.Write($"{Name}{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 state = int.Parse(match.Result("${state}"));
            return state;
        }

        public override async Task<SplitState> ReadAsync(CancellationToken cancellationToken)
        {
            var payload = Constants.Encoding.GetBytes($"{Name}{Constants.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 = Constants.Encoding.GetString(buffer);
            var match = readRegex.Match(answer);
            if (!match.Success)
            {
                throw new UnmatchedRadioResponseException(Name, answer);
            }

            var state = int.Parse(match.Result("${state}"));
            return state;
        }

        public override bool Set(SplitState state)
        {
            SerialPort.Write($"{Name}{(int)state}{Constants.EOT}");

            return Read() == state;
        }

        public override async Task<bool> SetAsync(SplitState state, CancellationToken cancellationToken)
        {
            var payload = Constants.Encoding.GetBytes($"{Name}{(int)state}{Constants.EOT}");
            await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);

            var result = await ReadAsync(cancellationToken);

            return result == state;
        }

        public override void Write(SplitState state)
        {
            SerialPort.Write($"{Name}{(int)state}{Constants.EOT}");
        }

        public override async Task WriteAsync(SplitState state, CancellationToken cancellationToken)
        {
            var payload = Constants.Encoding.GetBytes($"{Name}{(int)state}{Constants.EOT}");

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