HamBook – Rev 3

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;


namespace HamBook.Radios.Yaesu.FT_891.CAT
{
    [Radio("Yaesu FT-891")]
    public class AI : Generic.CAT.AI
    {
        private static readonly Regex readRegex = new Regex(@"^AI(?<state>[0,1])$", RegexOptions.Compiled);

        public AI(SerialPort serialPort) : base(serialPort)
        {
        }

        public override InformationState Read()
        {
            SerialPort.Write($"{Name}{Generic.Constants.EOT}");
            var result = SerialPort.ReadTo(Generic.Constants.EOT);
            var match = readRegex.Match(result);
            var state = (InformationState)int.Parse(match.Result("${state}"));

            return state;
        }

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

        public override Information Parse(string input)
        {
            var match = InformationRegex.Match(input);

            if(!match.Success)
            {
                return null;
            }

            var radioBank = int.Parse(match.Result("${radioBank}"));
            var frequency = int.Parse(match.Result("${frequency}"));
            var clarifierDirection = char.Parse(match.Result("${clarifierDirection}"));
            var clarifierOffset = int.Parse(match.Result("${clarifierOffset}"));
            var clar = int.Parse(match.Result("${clar}"));
            var radioMode = char.Parse(match.Result("${mode}"));
            var storage = (RadioStorage)int.Parse(match.Result("${storage}"));
            var ctcssMode = (CtcssMode)int.Parse(match.Result("${ctcssMode}"));
            var phase = (RadioPhase)int.Parse(match.Result("${phase}"));

            var information = new Information
            {
                RadioBank = radioBank,
                Frequency = frequency,
                ClarifierDirection = clarifierDirection,
                ClarifierOffset = clarifierOffset,
                Clar = clar,
                RadioMode = radioMode,
                Storage = storage,
                CtcssMode = ctcssMode,
                Phase =phase
            };

            return information;
        }
    }
}