HamBook – Blame information for rev 21
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
3 | office | 1 | using HamBook.Radios.Generic; |
2 | using HamBook.Radios; |
||
3 | using System; |
||
4 | using System.Collections.Generic; |
||
5 | using System.IO.Ports; |
||
6 | using System.Linq; |
||
7 | using System.Text; |
||
8 | using System.Threading; |
||
9 | using System.Threading.Tasks; |
||
10 | using Serilog; |
||
11 | using HamBook.Properties; |
||
12 | using Serilog.Events; |
||
7 | office | 13 | using System.Media; |
14 | using System.Reflection; |
||
9 | office | 15 | using RJCP.IO.Ports; |
16 | using System.Diagnostics; |
||
17 | using System.IO; |
||
12 | office | 18 | using Toasts; |
19 | using System.Drawing; |
||
20 | using Configuration; |
||
21 | office | 21 | using System.Security.Cryptography; |
3 | office | 22 | |
23 | namespace HamBook |
||
24 | { |
||
25 | public class BandScan |
||
26 | { |
||
27 | private CancellationTokenSource _scanningCancellationTokenSource; |
||
21 | office | 28 | private CancellationToken _scanningCancellationToken; |
3 | office | 29 | |
30 | private CatAssemblies _catAssemblies; |
||
31 | private int _minFrequency; |
||
32 | private int _maxFrequency; |
||
9 | office | 33 | private SerialPortStream _serialPort; |
12 | office | 34 | private Configuration.Configuration _configuration; |
5 | office | 35 | private int _currentFrequency; |
9 | office | 36 | private Task _scanTask; |
3 | office | 37 | |
38 | private BandScan() |
||
39 | { |
||
40 | } |
||
41 | |||
12 | office | 42 | public BandScan(CatAssemblies catAssemblies, int min, int max, SerialPortStream serialPort, Configuration.Configuration configuration) : this() |
3 | office | 43 | { |
44 | _catAssemblies = catAssemblies; |
||
45 | _minFrequency = min; |
||
46 | _maxFrequency = max; |
||
47 | _serialPort = serialPort; |
||
12 | office | 48 | _configuration = configuration; |
3 | office | 49 | |
50 | } |
||
51 | |||
21 | office | 52 | public async void Start(int stepFrequency, int pauseTime) |
3 | office | 53 | { |
21 | office | 54 | if(_scanTask != null) |
55 | { |
||
56 | if (!_scanningCancellationToken.IsCancellationRequested) |
||
57 | { |
||
58 | _scanningCancellationTokenSource.Cancel(); |
||
59 | } |
||
60 | |||
61 | await _scanTask; |
||
62 | _scanTask = null; |
||
63 | } |
||
64 | |||
3 | office | 65 | _scanningCancellationTokenSource = new CancellationTokenSource(); |
66 | _scanningCancellationToken = _scanningCancellationTokenSource.Token; |
||
5 | office | 67 | |
21 | office | 68 | _scanTask = Scan(stepFrequency, pauseTime); |
3 | office | 69 | } |
70 | |||
21 | office | 71 | public async void Start(int stepFrequency, int pauseTime, int pauseDetectTime) |
3 | office | 72 | { |
21 | office | 73 | if (_scanTask != null) |
3 | office | 74 | { |
21 | office | 75 | if (!_scanningCancellationToken.IsCancellationRequested) |
76 | { |
||
77 | _scanningCancellationTokenSource.Cancel(); |
||
78 | } |
||
79 | |||
80 | await _scanTask; |
||
81 | _scanTask = null; |
||
3 | office | 82 | } |
9 | office | 83 | |
21 | office | 84 | _scanningCancellationTokenSource = new CancellationTokenSource(); |
85 | _scanningCancellationToken = _scanningCancellationTokenSource.Token; |
||
86 | |||
87 | _scanTask = Scan(stepFrequency, pauseTime, pauseDetectTime); |
||
88 | } |
||
89 | |||
90 | public async Task Stop() |
||
91 | { |
||
92 | if (_scanTask != null) |
||
9 | office | 93 | { |
21 | office | 94 | if (!_scanningCancellationToken.IsCancellationRequested) |
95 | { |
||
96 | _scanningCancellationTokenSource.Cancel(); |
||
97 | } |
||
98 | |||
9 | office | 99 | await _scanTask; |
100 | _scanTask = null; |
||
101 | } |
||
3 | office | 102 | } |
103 | |||
21 | office | 104 | private async Task Scan(int stepFrequency, int pauseTime) |
3 | office | 105 | { |
9 | office | 106 | if (!_serialPort.IsOpen) |
107 | { |
||
108 | _serialPort.Open(); |
||
109 | } |
||
110 | |||
111 | _serialPort.DiscardInBuffer(); |
||
112 | |||
113 | _catAssemblies.CatWrite<InformationState>("AI", new object[] { InformationState.ON }); |
||
114 | |||
3 | office | 115 | try |
116 | { |
||
117 | |||
5 | office | 118 | _currentFrequency = _minFrequency; |
3 | office | 119 | |
120 | do |
||
121 | { |
||
5 | office | 122 | var taskCompletionSource = new TaskCompletionSource<bool>(); |
3 | office | 123 | |
5 | office | 124 | #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
21 | office | 125 | Task.Delay(TimeSpan.FromSeconds(pauseTime), _scanningCancellationToken) |
9 | office | 126 | .ContinueWith(_ => taskCompletionSource.TrySetResult(true), CancellationToken.None); |
5 | office | 127 | #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
3 | office | 128 | |
7 | office | 129 | using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav"))) |
130 | { |
||
131 | soundPlayer.Play(); |
||
9 | office | 132 | await _catAssemblies.CatWriteAsync<int>("FA", new object[] { _currentFrequency }, _scanningCancellationToken); |
7 | office | 133 | } |
5 | office | 134 | |
21 | office | 135 | await taskCompletionSource.Task; |
136 | |||
137 | _currentFrequency = _currentFrequency + stepFrequency; |
||
138 | if (_currentFrequency > _maxFrequency) |
||
139 | { |
||
140 | _currentFrequency = _minFrequency; |
||
141 | } |
||
142 | if (_currentFrequency < _minFrequency) |
||
143 | { |
||
144 | _currentFrequency = _minFrequency; |
||
145 | } |
||
146 | |||
147 | } while (!_scanningCancellationToken.IsCancellationRequested); |
||
148 | } |
||
149 | catch (Exception exception) |
||
150 | { |
||
151 | Log.Error(exception, Resources.Scanning_aborted); |
||
152 | } |
||
153 | finally |
||
154 | { |
||
155 | _catAssemblies.CatWrite<InformationState>("AI", new object[] { InformationState.OFF }); |
||
156 | |||
157 | if (_serialPort.IsOpen) |
||
158 | { |
||
159 | _serialPort.Close(); |
||
160 | |||
161 | _serialPort.DiscardInBuffer(); |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | |||
166 | private async Task Scan(int stepFrequency, int pauseTime, int pauseDetectTime) |
||
167 | { |
||
168 | if (!_serialPort.IsOpen) |
||
169 | { |
||
170 | _serialPort.Open(); |
||
171 | } |
||
172 | |||
173 | _serialPort.DiscardInBuffer(); |
||
174 | |||
175 | _catAssemblies.CatWrite<InformationState>("AI", new object[] { InformationState.ON }); |
||
176 | |||
177 | try |
||
178 | { |
||
179 | |||
180 | _currentFrequency = _minFrequency; |
||
181 | |||
182 | do |
||
183 | { |
||
184 | var taskCompletionSource = new TaskCompletionSource<bool>(); |
||
185 | |||
186 | #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
||
187 | Task.Delay(TimeSpan.FromSeconds(pauseTime), _scanningCancellationToken) |
||
188 | .ContinueWith(_ => taskCompletionSource.TrySetResult(true), CancellationToken.None); |
||
189 | #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
||
190 | |||
191 | using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav"))) |
||
192 | { |
||
193 | soundPlayer.Play(); |
||
194 | await _catAssemblies.CatWriteAsync<int>("FA", new object[] { _currentFrequency }, _scanningCancellationToken); |
||
195 | } |
||
196 | |||
9 | office | 197 | using (var memoryStream = new MemoryStream()) |
198 | { |
||
199 | _serialPort.DiscardInBuffer(); |
||
5 | office | 200 | |
9 | office | 201 | await taskCompletionSource.Task; |
202 | |||
21 | office | 203 | var count = _serialPort.BaudRate / sizeof(byte) * pauseTime; |
9 | office | 204 | await _serialPort.CopyToAsync(memoryStream, count, _scanningCancellationToken); |
205 | |||
206 | memoryStream.Position = 0L; |
||
207 | |||
11 | office | 208 | // TODO: radios |
9 | office | 209 | var result = Encoding.ASCII.GetString(memoryStream.ToArray()); |
11 | office | 210 | foreach (var split in result.Split(Radios.Generic.Constants.EOT)) |
9 | office | 211 | { |
17 | office | 212 | if(string.IsNullOrEmpty(split)) |
213 | { |
||
214 | continue; |
||
215 | } |
||
216 | |||
217 | try |
||
9 | office | 218 | { |
18 | office | 219 | switch (_catAssemblies.CatParse<BusyState>("BY", new object[] { $"{split}{Radios.Generic.Constants.EOT}" })) |
17 | office | 220 | { |
221 | case BusyState.ON: |
||
21 | office | 222 | if (_configuration.Notifications.TryGetNotificationState(NotificationType.SignalScanDetect, out var notificationState)) |
17 | office | 223 | { |
21 | office | 224 | continue; |
225 | } |
||
12 | office | 226 | |
21 | office | 227 | var toastFrom = new ToastForm( |
228 | $"{Resources.Signal_detected_during_scan}", |
||
229 | $"{Resources.Frequency}: {_currentFrequency}Hz", |
||
230 | notificationState.LingerTime, |
||
231 | Constants.AssemblyIcon); |
||
12 | office | 232 | |
21 | office | 233 | toastFrom.Show(); |
234 | |||
235 | await Task.Delay(TimeSpan.FromSeconds(pauseDetectTime), _scanningCancellationToken); |
||
17 | office | 236 | continue; |
237 | } |
||
9 | office | 238 | } |
18 | office | 239 | catch (TargetInvocationException exception) when (exception.InnerException is UnmatchedRadioResponseException) |
17 | office | 240 | { |
241 | // suppress |
||
242 | } |
||
18 | office | 243 | catch (Exception exception) |
17 | office | 244 | { |
18 | office | 245 | Log.Warning(exception, Resources.Unexpected_failure_while_scanning); |
17 | office | 246 | } |
9 | office | 247 | } |
248 | } |
||
249 | |||
21 | office | 250 | _currentFrequency = _currentFrequency + stepFrequency; |
5 | office | 251 | if(_currentFrequency > _maxFrequency) |
3 | office | 252 | { |
5 | office | 253 | _currentFrequency = _minFrequency; |
3 | office | 254 | } |
5 | office | 255 | if(_currentFrequency < _minFrequency) |
3 | office | 256 | { |
5 | office | 257 | _currentFrequency = _minFrequency; |
3 | office | 258 | } |
259 | |||
5 | office | 260 | } while(!_scanningCancellationToken.IsCancellationRequested); |
3 | office | 261 | } |
262 | catch(Exception exception) |
||
263 | { |
||
264 | Log.Error(exception, Resources.Scanning_aborted); |
||
265 | } |
||
9 | office | 266 | finally |
267 | { |
||
268 | _catAssemblies.CatWrite<InformationState>("AI", new object[] { InformationState.OFF }); |
||
269 | |||
270 | if (_serialPort.IsOpen) |
||
271 | { |
||
272 | _serialPort.Close(); |
||
273 | |||
274 | _serialPort.DiscardInBuffer(); |
||
275 | } |
||
276 | } |
||
3 | office | 277 | } |
278 | } |
||
279 | } |