HamBook – Rev 54

Subversion Repositories:
Rev:
using System;
using System.Collections.Generic;
using System.Media;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using HamBook.Properties;
using HamBook.Radios;
using HamBook.Radios.Generic;
using RJCP.IO.Ports;
using Serilog;

namespace HamBook
{
    public class MemoryTune
    {
        private readonly CatAssemblies _catAssemblies;
        private readonly Configuration.Configuration _configuration;
        private readonly MemoryBanks _memoryBanks;
        private CancellationToken _scanningCancellationToken;
        private CancellationTokenSource _scanningCancellationTokenSource;
        private Thread _scanThread;
        private SerialPortStream _serialPort;

        private MemoryTune()
        {
        }

        public MemoryTune(CatAssemblies catAssemblies, SerialPortStream serialPort,
            Configuration.Configuration configuration) : this()
        {
            _catAssemblies = catAssemblies;
            _serialPort = serialPort;
            _configuration = configuration;

            _memoryBanks = MemoryBanks.Create(_configuration.Radio);
        }

        public void Start()
        {
            if (_scanThread != null)
            {
                if (!_scanningCancellationToken.IsCancellationRequested) _scanningCancellationTokenSource.Cancel();

                _scanThread.Join();
                _scanThread = null;
            }

            _scanningCancellationTokenSource = new CancellationTokenSource();
            _scanningCancellationToken = _scanningCancellationTokenSource.Token;

            _scanThread = new Thread(Tune);

            var memoryBankQueue = new Queue<string>();
            foreach (var bank in _memoryBanks.GetMemoryBanks()) memoryBankQueue.Enqueue(bank);

            _scanThread.Start(memoryBankQueue);
        }

        public void Stop()
        {
            if (_scanThread != null)
            {
                if (!_scanningCancellationToken.IsCancellationRequested) _scanningCancellationTokenSource.Cancel();

                _scanThread.Join();
                _scanThread = null;
            }
        }

        private async void Tune(object obj)
        {
            var memoryBankQueue = (Queue<string>)obj;

            do
            {
                try
                {
                    var memoryBank = memoryBankQueue.Dequeue();

                    using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly()
                               .GetManifestResourceStream("HamBook.Effects.pot.wav")))
                    {
                        soundPlayer.Play();

                        if (!await _catAssemblies.CatSetAsync<int, bool>("MC", new object[] { memoryBank },
                                _scanningCancellationToken))
                            throw new MemoryTuneException(MemoryTuneExceptionCode.CouldNotChangeChannel,
                                Resources.Could_not_change_channel);
                    }

                    if (!await _catAssemblies.CatSetAsync<TunerState, bool>("AC", new object[] { TunerState.TUNER_ON },
                            _scanningCancellationToken))
                        throw new MemoryTuneException(MemoryTuneExceptionCode.CouldNotEnableTuner,
                            Resources.Could_not_enable_tuner);

                    do
                    {
                        await Task.Delay(TimeSpan.FromSeconds(1), _scanningCancellationToken);

                        try
                        {
                            var tuneState = await _catAssemblies.CatReadAsync<TunerState>("AC", new object[] { },
                                _scanningCancellationToken);

                            if (tuneState == TunerState.TUNER_ON) break;
                        }
                        catch (Exception)
                        {
                            // retry
                        }
                    } while (!_scanningCancellationToken.IsCancellationRequested);

                    if (!await _catAssemblies.CatSetAsync<TunerState, bool>("AC",
                            new object[] { TunerState.TUNING_START }, _scanningCancellationToken))
                        throw new MemoryTuneException(MemoryTuneExceptionCode.CouldNotStartTuning,
                            Resources.Could_not_start_tuning);

                    do
                    {
                        await Task.Delay(TimeSpan.FromSeconds(1), _scanningCancellationToken);

                        try
                        {
                            var tuneState = await _catAssemblies.CatReadAsync<TunerState>("AC", new object[] { },
                                _scanningCancellationToken);

                            if (tuneState != TunerState.TUNING_START) break;
                        }
                        catch (Exception)
                        {
                            // retry
                        }
                    } while (!_scanningCancellationToken.IsCancellationRequested);
                }
                catch (MemoryTuneException exception)
                {
                    Log.Error(exception, exception.Message);
                }
                catch (Exception exception)
                {
                    Log.Error(exception, Resources.Unknown_error_while_tuning);
                }
            } while (!_scanningCancellationToken.IsCancellationRequested && memoryBankQueue.Count != 0);
        }
    }
}

Generated by GNU Enscript 1.6.5.90.