HamBook – Diff between revs 54 and 58
?pathlinks?
Rev 54 | Rev 58 | |||
---|---|---|---|---|
Line 10... | Line 10... | |||
10 | using HamBook.Radios; |
10 | using HamBook.Radios; |
|
11 | using HamBook.Radios.Generic; |
11 | using HamBook.Radios.Generic; |
|
12 | using RJCP.IO.Ports; |
12 | using RJCP.IO.Ports; |
|
13 | using Serilog; |
13 | using Serilog; |
|
14 | using Toasts; |
14 | using Toasts; |
|
- | 15 | using static System.Windows.Forms.AxHost; |
||
- | 16 | using Toasts = Toasts.Toasts; |
||
Line 15... | Line 17... | |||
15 | |
17 | |
|
16 | namespace HamBook |
18 | namespace HamBook |
|
17 | { |
19 | { |
|
18 | public class BandScan |
20 | public class BandScan |
|
Line 24... | Line 26... | |||
24 | private readonly SerialPortStream _serialPort; |
26 | private readonly SerialPortStream _serialPort; |
|
25 | private int _currentFrequency; |
27 | private int _currentFrequency; |
|
26 | private CancellationToken _scanningCancellationToken; |
28 | private CancellationToken _scanningCancellationToken; |
|
27 | private CancellationTokenSource _scanningCancellationTokenSource; |
29 | private CancellationTokenSource _scanningCancellationTokenSource; |
|
28 | private Thread _scanThread; |
30 | private Thread _scanThread; |
|
- | 31 | private global::Toasts.Toasts _toasts; |
||
Line 29... | Line 32... | |||
29 | |
32 | |
|
30 | private BandScan() |
33 | private BandScan() |
|
- | 34 | { |
||
31 | { |
35 | |
|
Line 32... | Line 36... | |||
32 | } |
36 | } |
|
33 | |
37 | |
|
34 | public BandScan(CatAssemblies catAssemblies, int min, int max, SerialPortStream serialPort, |
38 | public BandScan(CatAssemblies catAssemblies, int min, int max, SerialPortStream serialPort, |
|
Line 52... | Line 56... | |||
52 | } |
56 | } |
|
Line 53... | Line 57... | |||
53 | |
57 | |
|
54 | _scanningCancellationTokenSource = new CancellationTokenSource(); |
58 | _scanningCancellationTokenSource = new CancellationTokenSource(); |
|
Line -... | Line 59... | |||
- | 59 | _scanningCancellationToken = _scanningCancellationTokenSource.Token; |
||
- | 60 | |
||
55 | _scanningCancellationToken = _scanningCancellationTokenSource.Token; |
61 | _toasts = new global::Toasts.Toasts(_scanningCancellationToken); |
|
56 | |
62 | |
|
57 | _scanThread = new Thread(Scan); |
63 | _scanThread = new Thread(Scan); |
|
Line 58... | Line 64... | |||
58 | _scanThread.Start(new BandScanParameters(stepFrequency, pauseTime, pauseDetectTime, autoTune)); |
64 | _scanThread.Start(new BandScanParameters(stepFrequency, pauseTime, pauseDetectTime, autoTune)); |
|
Line 65... | Line 71... | |||
65 | if (!_scanningCancellationToken.IsCancellationRequested) _scanningCancellationTokenSource.Cancel(); |
71 | if (!_scanningCancellationToken.IsCancellationRequested) _scanningCancellationTokenSource.Cancel(); |
|
Line 66... | Line 72... | |||
66 | |
72 | |
|
67 | _scanThread.Join(); |
73 | _scanThread.Join(); |
|
68 | _scanThread = null; |
74 | _scanThread = null; |
|
- | 75 | } |
||
- | 76 | |
||
- | 77 | if (_toasts != null) |
||
- | 78 | { |
||
- | 79 | _toasts.Dispose(); |
||
- | 80 | _toasts = null; |
||
69 | } |
81 | } |
|
Line 70... | Line 82... | |||
70 | } |
82 | } |
|
71 | |
83 | |
|
72 | private async void Scan(object obj) |
84 | private async void Scan(object obj) |
|
Line 81... | Line 93... | |||
81 | { |
93 | { |
|
82 | _currentFrequency = _minFrequency; |
94 | _currentFrequency = _minFrequency; |
|
Line 83... | Line 95... | |||
83 | |
95 | |
|
84 | do |
96 | do |
|
85 | { |
- | ||
86 | var taskCompletionSource = new TaskCompletionSource<bool>(); |
- | ||
87 | |
97 | { |
|
88 | using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly() |
98 | using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly() |
|
89 | .GetManifestResourceStream("HamBook.Effects.pot.wav"))) |
99 | .GetManifestResourceStream("HamBook.Effects.pot.wav"))) |
|
90 | { |
100 | { |
|
Line 135... | Line 145... | |||
135 | // retry |
145 | // retry |
|
136 | } |
146 | } |
|
137 | } while (!_scanningCancellationToken.IsCancellationRequested); |
147 | } while (!_scanningCancellationToken.IsCancellationRequested); |
|
138 | } |
148 | } |
|
Line 139... | Line 149... | |||
139 | |
149 | |
|
Line 140... | Line 150... | |||
140 | _catAssemblies.CatWrite<InformationState>("AI", new object[] { InformationState.ON }); |
150 | await _catAssemblies.CatWriteAsyncManual<InformationState>("AI", new object[] { InformationState.ON }, _scanningCancellationToken); |
|
141 | |
151 | |
|
142 | try |
152 | try |
|
143 | { |
153 | { |
|
- | 154 | using (var memoryStream = new MemoryStream()) |
||
- | 155 | { |
||
- | 156 | do |
||
- | 157 | { |
||
144 | using (var memoryStream = new MemoryStream()) |
158 | var taskCompletionSource = new TaskCompletionSource<bool>(); |
|
145 | { |
159 | |
|
146 | #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
160 | #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
|
147 | Task.Delay(TimeSpan.FromSeconds(bandScanParameters.PauseTime), _scanningCancellationToken) |
161 | Task.Delay(TimeSpan.FromSeconds(bandScanParameters.PauseTime), _scanningCancellationToken) |
|
Line 148... | Line 162... | |||
148 | .ContinueWith(_ => taskCompletionSource.TrySetResult(true), CancellationToken.None); |
162 | .ContinueWith(_ => taskCompletionSource.TrySetResult(true), CancellationToken.None); |
|
149 | #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
- | ||
150 | |
- | ||
151 | await taskCompletionSource.Task; |
- | ||
Line 152... | Line 163... | |||
152 | |
163 | #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
|
Line 153... | Line 164... | |||
153 | await _serialPort.CopyToAsync(memoryStream, _serialPort.BytesToRead, |
164 | |
|
- | 165 | await taskCompletionSource.Task; |
||
Line 154... | Line 166... | |||
154 | _scanningCancellationToken); |
166 | |
|
155 | |
167 | memoryStream.Position = 0L; |
|
156 | memoryStream.Position = 0L; |
168 | |
|
157 | |
169 | await _serialPort.CopyToAsync(memoryStream, _serialPort.BytesToRead, |
|
158 | var result = Encoding.ASCII.GetString(memoryStream.ToArray()); |
- | ||
159 | |
- | ||
160 | Parallel.ForEach(result.Split(Radios.Yaesu.FT_891.Constants.Eot), |
- | ||
161 | new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, |
- | ||
162 | async (split, state) => |
170 | _scanningCancellationToken); |
|
163 | { |
171 | |
|
164 | if (string.IsNullOrEmpty(split)) state.Stop(); |
- | ||
165 | |
172 | var signal = false; |
|
166 | try |
- | ||
167 | { |
- | ||
168 | switch (_catAssemblies.CatParse<BusyState>("BY", |
- | ||
169 | new object[] { $"{split}{Radios.Yaesu.FT_891.Constants.Eot}" })) |
- | ||
170 | { |
- | ||
171 | case BusyState.ON: |
- | ||
172 | if (_configuration.Notifications.TryGetNotificationState( |
- | ||
173 | NotificationType.SignalScanDetect, out var notificationState)) |
- | ||
174 | state.Stop(); |
- | ||
175 | |
- | ||
176 | var toastFrom = new ToastForm( |
- | ||
177 | $"{Resources.Signal_detected_during_scan}", |
- | ||
178 | $"{Resources.Frequency}: {_currentFrequency}Hz", |
- | ||
179 | notificationState.LingerTime, |
- | ||
180 | Constants.AssemblyIcon); |
- | ||
181 | |
- | ||
182 | toastFrom.Show(); |
- | ||
183 | |
- | ||
184 | await Task.Delay( |
- | ||
185 | TimeSpan.FromSeconds(bandScanParameters.PauseDetectTime), |
- | ||
186 | _scanningCancellationToken); |
- | ||
187 | state.Stop(); |
173 | var result = Encoding.ASCII.GetString(memoryStream.ToArray()); |
|
188 | break; |
174 | foreach (var split in result.Split(Radios.Yaesu.FT_891.Constants.Eot)) |
|
189 | } |
175 | { |
|
- | 176 | if (!_catAssemblies.CatParse<BusyState>("BY", |
||
190 | } |
177 | new object[] { $"{split}{Radios.Yaesu.FT_891.Constants.Eot}" }, |
|
191 | catch (TargetInvocationException exception) when |
178 | out var busyState)) |
|
192 | (exception.InnerException is UnmatchedRadioResponseException) |
179 | { |
|
193 | { |
180 | continue; |
|
194 | // suppress |
181 | } |
|
- | 182 | |
||
- | 183 | if (busyState == BusyState.ON) |
||
- | 184 | { |
||
- | 185 | signal = true; |
||
- | 186 | } |
||
- | 187 | } |
||
- | 188 | |
||
- | 189 | |
||
- | 190 | if (!signal) |
||
- | 191 | { |
||
- | 192 | break; |
||
- | 193 | } |
||
- | 194 | |
||
- | 195 | if (!_configuration.Notifications.TryGetNotificationState( |
||
- | 196 | NotificationType.SignalScanDetect, out var notificationState) || |
||
- | 197 | !notificationState.Enabled) |
||
- | 198 | { |
||
- | 199 | continue; |
||
- | 200 | } |
||
- | 201 | |
||
- | 202 | var toastFrom = new ToastForm( |
||
- | 203 | $"{Resources.Signal_detected_during_scan}", |
||
- | 204 | $"{Resources.Frequency}: {_currentFrequency}Hz", |
||
195 | } |
205 | notificationState.LingerTime, |
|
196 | catch (Exception exception) |
206 | Constants.AssemblyIcon); |
|
197 | { |
207 | |
|
198 | Log.Warning(exception, Resources.Error_encountered_while_scanning_for_signal); |
208 | System.Windows.Forms.Application.Run(toastFrom); |
|
199 | } |
209 | |
|
200 | }); |
210 | } while (!_scanningCancellationToken.IsCancellationRequested); |
|
201 | } |
211 | } |
|
202 | } |
212 | } |
|
203 | catch (Exception exception) |
213 | catch (Exception exception) |
|
204 | { |
214 | { |
|
Line 205... | Line 215... | |||
205 | Log.Warning(exception, Resources.Error_encountered_while_scanning_for_signal); |
215 | Log.Warning(exception, Resources.Error_encountered_while_scanning_for_signal); |
|
- | 216 | } |
||
206 | } |
217 | finally |
|
207 | finally |
218 | { |
|
- | 219 | await _catAssemblies.CatWriteAsyncManual<InformationState>("AI", new object[] { InformationState.OFF }, _scanningCancellationToken); |
||
208 | { |
220 | } |
|
209 | _catAssemblies.CatWrite<InformationState>("AI", new object[] { InformationState.OFF }); |
221 | |
|
210 | } |
222 | _currentFrequency = _currentFrequency + bandScanParameters.StepFrequency; |
|
211 | |
223 | |
|
212 | _currentFrequency = _currentFrequency + bandScanParameters.StepFrequency; |
224 | if (_currentFrequency > _maxFrequency) _currentFrequency = _minFrequency; |