HamBook – Rev 7

Subversion Repositories:
Rev:
using HamBook.Radios.Generic;
using HamBook.Radios.Generic.CAT;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
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
{
    [Radio("Yaesu FT-891")]
    public class PS : HamBook.Radios.Generic.CAT.PS
    {
        private static readonly Regex readRegex = new Regex(@"^PS(?<state>[0,1])$", RegexOptions.Compiled);

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

        public override async Task<bool> Set(PowerState state)
        {
            var taskCompletionSource = new TaskCompletionSource<bool>();
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            Task.Delay(Constants.POWER_ON_DELAY).ContinueWith(ca =>
            {
                switch(state)
                {
                    case PowerState.ON:
                        SerialPort.Write($"{Name}1{Generic.Constants.EOT}");
                        Task.Delay(Constants.TIME_TO_WARM).ContinueWith(cb =>
                        {
                            taskCompletionSource.TrySetResult(true);
                        });
                        return;
                    case PowerState.OFF:
                        SerialPort.Write($"{Name}0{Generic.Constants.EOT}");
                        break;
                }
                taskCompletionSource.TrySetResult(true);
            });
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

            SerialPort.Write($"POWER");
            await taskCompletionSource.Task;
            return await Read() == state;
        }

        public override async Task<PowerState> Read()
        {
            var taskCompletionSource = new TaskCompletionSource<int>();
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            Task.Delay(Constants.POWER_ON_DELAY).ContinueWith(_ =>
            {
                SerialPort.Write($"{Name}{Generic.Constants.EOT}");
                var result = SerialPort.ReadTo(Generic.Constants.EOT);
                var match = readRegex.Match(result);
                var state =  int.Parse(match.Result("${state}"));

                taskCompletionSource.TrySetResult(state);
            });
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

            SerialPort.Write($"POWER");
            var onoff = await taskCompletionSource.Task;
            switch(onoff)
            {
                case 1:
                    return PowerState.ON;
                case 0:
                    return PowerState.OFF;
            }

            return PowerState.NONE;
        }
    }
}