HamBook – Diff between revs 18 and 21

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 18 Rev 21
Line 16... Line 16...
16 using System.Diagnostics; 16 using System.Diagnostics;
17 using System.IO; 17 using System.IO;
18 using Toasts; 18 using Toasts;
19 using System.Drawing; 19 using System.Drawing;
20 using Configuration; 20 using Configuration;
-   21 using System.Security.Cryptography;
Line 21... Line 22...
21   22  
22 namespace HamBook 23 namespace HamBook
23 { 24 {
24 public class BandScan 25 public class BandScan
25 { 26 {
26 private CancellationTokenSource _scanningCancellationTokenSource; -  
27   27 private CancellationTokenSource _scanningCancellationTokenSource;
-   28 private CancellationToken _scanningCancellationToken;
28 private CancellationToken _scanningCancellationToken; 29  
29 private CatAssemblies _catAssemblies; 30 private CatAssemblies _catAssemblies;
30 private int _minFrequency; 31 private int _minFrequency;
31 private int _maxFrequency; 32 private int _maxFrequency;
32 private SerialPortStream _serialPort; 33 private SerialPortStream _serialPort;
Line 46... Line 47...
46 _serialPort = serialPort; 47 _serialPort = serialPort;
47 _configuration = configuration; 48 _configuration = configuration;
Line 48... Line 49...
48   49  
Line 49... Line 50...
49 } 50 }
50   51  
-   52 public async void Start(int stepFrequency, int pauseTime)
-   53 {
-   54 if(_scanTask != null)
-   55 {
-   56 if (!_scanningCancellationToken.IsCancellationRequested)
-   57 {
-   58 _scanningCancellationTokenSource.Cancel();
-   59 }
-   60  
-   61 await _scanTask;
-   62 _scanTask = null;
51 public void Start(int step, int pause, int detect) 63 }
52 { 64  
Line 53... Line 65...
53 _scanningCancellationTokenSource = new CancellationTokenSource(); 65 _scanningCancellationTokenSource = new CancellationTokenSource();
54 _scanningCancellationToken = _scanningCancellationTokenSource.Token; 66 _scanningCancellationToken = _scanningCancellationTokenSource.Token;
Line 55... Line 67...
55   67  
56 _scanTask = Scan(step, pause, detect); 68 _scanTask = Scan(stepFrequency, pauseTime);
57 } 69 }
58   70  
-   71 public async void Start(int stepFrequency, int pauseTime, int pauseDetectTime)
-   72 {
59 public async Task Stop() 73 if (_scanTask != null)
-   74 {
-   75 if (!_scanningCancellationToken.IsCancellationRequested)
-   76 {
-   77 _scanningCancellationTokenSource.Cancel();
60 { 78 }
Line -... Line 79...
-   79  
-   80 await _scanTask;
-   81 _scanTask = null;
-   82 }
-   83  
-   84 _scanningCancellationTokenSource = new CancellationTokenSource();
-   85 _scanningCancellationToken = _scanningCancellationTokenSource.Token;
-   86  
61 if (_scanningCancellationTokenSource != null) 87 _scanTask = Scan(stepFrequency, pauseTime, pauseDetectTime);
62 { 88 }
-   89  
-   90 public async Task Stop()
-   91 {
-   92 if (_scanTask != null)
-   93 {
63 _scanningCancellationTokenSource.Cancel(); 94 if (!_scanningCancellationToken.IsCancellationRequested)
64 } 95 {
65   96 _scanningCancellationTokenSource.Cancel();
66 if(_scanTask != null) 97 }
Line 67... Line 98...
67 { 98  
68 await _scanTask; 99 await _scanTask;
69 _scanTask = null; 100 _scanTask = null;
70 } 101 }
71 } 102 }
72   103  
Line 89... Line 120...
89 do 120 do
90 { 121 {
91 var taskCompletionSource = new TaskCompletionSource<bool>(); 122 var taskCompletionSource = new TaskCompletionSource<bool>();
Line 92... Line 123...
92   123  
-   124 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
-   125 Task.Delay(TimeSpan.FromSeconds(pauseTime), _scanningCancellationToken)
-   126 .ContinueWith(_ => taskCompletionSource.TrySetResult(true), CancellationToken.None);
-   127 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
-   128  
-   129 using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav")))
-   130 {
-   131 soundPlayer.Play();
-   132 await _catAssemblies.CatWriteAsync<int>("FA", new object[] { _currentFrequency }, _scanningCancellationToken);
-   133 }
-   134  
-   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  
93 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed 186 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
94 Task.Delay(TimeSpan.FromSeconds(pause), _scanningCancellationToken) 187 Task.Delay(TimeSpan.FromSeconds(pauseTime), _scanningCancellationToken)
95 .ContinueWith(_ => taskCompletionSource.TrySetResult(true), CancellationToken.None); 188 .ContinueWith(_ => taskCompletionSource.TrySetResult(true), CancellationToken.None);
Line 96... Line 189...
96 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed 189 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
97   190  
Line 105... Line 198...
105 { 198 {
106 _serialPort.DiscardInBuffer(); 199 _serialPort.DiscardInBuffer();
Line 107... Line 200...
107   200  
Line 108... Line 201...
108 await taskCompletionSource.Task; 201 await taskCompletionSource.Task;
109   202  
Line 110... Line 203...
110 var count = _serialPort.BaudRate / sizeof(byte) * pause; 203 var count = _serialPort.BaudRate / sizeof(byte) * pauseTime;
Line 111... Line 204...
111 await _serialPort.CopyToAsync(memoryStream, count, _scanningCancellationToken); 204 await _serialPort.CopyToAsync(memoryStream, count, _scanningCancellationToken);
Line 124... Line 217...
124 try 217 try
125 { 218 {
126 switch (_catAssemblies.CatParse<BusyState>("BY", new object[] { $"{split}{Radios.Generic.Constants.EOT}" })) 219 switch (_catAssemblies.CatParse<BusyState>("BY", new object[] { $"{split}{Radios.Generic.Constants.EOT}" }))
127 { 220 {
128 case BusyState.ON: 221 case BusyState.ON:
129 if (!_configuration.Notifications.TryGetNotificationState(NotificationType.SignalScanDetect, out var notificationState)) 222 if (_configuration.Notifications.TryGetNotificationState(NotificationType.SignalScanDetect, out var notificationState))
130 { 223 {
131 var toastFrom = new ToastForm( -  
132 $"{Resources.Signal_detected_during_scan}", -  
133 $"{Resources.Frequency}: {_currentFrequency}Hz", -  
134 notificationState.LingerTime, 224 continue;
135 Constants.AssemblyIcon); 225 }
Line -... Line 226...
-   226  
-   227 var toastFrom = new ToastForm(
-   228 $"{Resources.Signal_detected_during_scan}",
-   229 $"{Resources.Frequency}: {_currentFrequency}Hz",
136   230 notificationState.LingerTime,
Line -... Line 231...
-   231 Constants.AssemblyIcon);
-   232  
137 toastFrom.Show(); 233 toastFrom.Show();
138   -  
139 await Task.Delay(TimeSpan.FromSeconds(detect), _scanningCancellationToken); 234  
140 } 235 await Task.Delay(TimeSpan.FromSeconds(pauseDetectTime), _scanningCancellationToken);
141 continue; 236 continue;
142 } 237 }
143 } 238 }
Line 150... Line 245...
150 Log.Warning(exception, Resources.Unexpected_failure_while_scanning); 245 Log.Warning(exception, Resources.Unexpected_failure_while_scanning);
151 } 246 }
152 } 247 }
153 } 248 }
Line 154... Line 249...
154   249  
155 _currentFrequency = _currentFrequency + step; 250 _currentFrequency = _currentFrequency + stepFrequency;
156 if(_currentFrequency > _maxFrequency) 251 if(_currentFrequency > _maxFrequency)
157 { 252 {
158 _currentFrequency = _minFrequency; 253 _currentFrequency = _minFrequency;
159 } 254 }