HamBook – Blame information for rev 54

Subversion Repositories:
Rev:
Rev Author Line No. Line
54 office 1 using System;
39 office 2 using System.Collections.Generic;
54 office 3 using System.Media;
4 using System.Reflection;
39 office 5 using System.Threading;
6 using System.Threading.Tasks;
7 using HamBook.Properties;
54 office 8 using HamBook.Radios;
9 using HamBook.Radios.Generic;
39 office 10 using RJCP.IO.Ports;
54 office 11 using Serilog;
39 office 12  
13 namespace HamBook
14 {
15 public class MemoryTune
16 {
54 office 17 private readonly CatAssemblies _catAssemblies;
18 private readonly Configuration.Configuration _configuration;
19 private readonly MemoryBanks _memoryBanks;
20 private CancellationToken _scanningCancellationToken;
39 office 21 private CancellationTokenSource _scanningCancellationTokenSource;
22 private Thread _scanThread;
23 private SerialPortStream _serialPort;
24  
25 private MemoryTune()
26 {
27 }
28  
54 office 29 public MemoryTune(CatAssemblies catAssemblies, SerialPortStream serialPort,
30 Configuration.Configuration configuration) : this()
39 office 31 {
32 _catAssemblies = catAssemblies;
33 _serialPort = serialPort;
34 _configuration = configuration;
35  
48 office 36 _memoryBanks = MemoryBanks.Create(_configuration.Radio);
39 office 37 }
38  
39 public void Start()
40 {
41 if (_scanThread != null)
42 {
54 office 43 if (!_scanningCancellationToken.IsCancellationRequested) _scanningCancellationTokenSource.Cancel();
39 office 44  
45 _scanThread.Join();
46 _scanThread = null;
47 }
48  
49 _scanningCancellationTokenSource = new CancellationTokenSource();
50 _scanningCancellationToken = _scanningCancellationTokenSource.Token;
51  
54 office 52 _scanThread = new Thread(Tune);
39 office 53  
45 office 54 var memoryBankQueue = new Queue<string>();
54 office 55 foreach (var bank in _memoryBanks.GetMemoryBanks()) memoryBankQueue.Enqueue(bank);
39 office 56  
57 _scanThread.Start(memoryBankQueue);
58 }
59  
60 public void Stop()
61 {
62 if (_scanThread != null)
63 {
54 office 64 if (!_scanningCancellationToken.IsCancellationRequested) _scanningCancellationTokenSource.Cancel();
39 office 65  
66 _scanThread.Join();
67 _scanThread = null;
68 }
69 }
70  
71 private async void Tune(object obj)
72 {
45 office 73 var memoryBankQueue = (Queue<string>)obj;
39 office 74  
75 do
76 {
77 try
78 {
79 var memoryBank = memoryBankQueue.Dequeue();
80  
54 office 81 using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly()
82 .GetManifestResourceStream("HamBook.Effects.pot.wav")))
39 office 83 {
84 soundPlayer.Play();
85  
54 office 86 if (!await _catAssemblies.CatSetAsync<int, bool>("MC", new object[] { memoryBank },
87 _scanningCancellationToken))
88 throw new MemoryTuneException(MemoryTuneExceptionCode.CouldNotChangeChannel,
89 Resources.Could_not_change_channel);
39 office 90 }
91  
54 office 92 if (!await _catAssemblies.CatSetAsync<TunerState, bool>("AC", new object[] { TunerState.TUNER_ON },
93 _scanningCancellationToken))
94 throw new MemoryTuneException(MemoryTuneExceptionCode.CouldNotEnableTuner,
95 Resources.Could_not_enable_tuner);
39 office 96  
97 do
98 {
99 await Task.Delay(TimeSpan.FromSeconds(1), _scanningCancellationToken);
100  
101 try
102 {
54 office 103 var tuneState = await _catAssemblies.CatReadAsync<TunerState>("AC", new object[] { },
104 _scanningCancellationToken);
39 office 105  
54 office 106 if (tuneState == TunerState.TUNER_ON) break;
39 office 107 }
108 catch (Exception)
109 {
110 // retry
111 }
112 } while (!_scanningCancellationToken.IsCancellationRequested);
113  
54 office 114 if (!await _catAssemblies.CatSetAsync<TunerState, bool>("AC",
115 new object[] { TunerState.TUNING_START }, _scanningCancellationToken))
116 throw new MemoryTuneException(MemoryTuneExceptionCode.CouldNotStartTuning,
117 Resources.Could_not_start_tuning);
39 office 118  
119 do
120 {
121 await Task.Delay(TimeSpan.FromSeconds(1), _scanningCancellationToken);
122  
123 try
124 {
54 office 125 var tuneState = await _catAssemblies.CatReadAsync<TunerState>("AC", new object[] { },
126 _scanningCancellationToken);
39 office 127  
54 office 128 if (tuneState != TunerState.TUNING_START) break;
39 office 129 }
130 catch (Exception)
131 {
132 // retry
133 }
134 } while (!_scanningCancellationToken.IsCancellationRequested);
135 }
54 office 136 catch (MemoryTuneException exception)
39 office 137 {
138 Log.Error(exception, exception.Message);
139 }
140 catch (Exception exception)
141 {
142 Log.Error(exception, Resources.Unknown_error_while_tuning);
143 }
144 } while (!_scanningCancellationToken.IsCancellationRequested && memoryBankQueue.Count != 0);
145 }
146 }
54 office 147 }