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