HamBook – Blame information for rev 45

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