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