HamBook – Blame information for rev 17

Subversion Repositories:
Rev:
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;
3 office 21  
22 namespace HamBook
23 {
24 public class BandScan
25 {
26 private CancellationTokenSource _scanningCancellationTokenSource;
27  
28 private CancellationToken _scanningCancellationToken;
29 private CatAssemblies _catAssemblies;
30 private int _minFrequency;
31 private int _maxFrequency;
9 office 32 private SerialPortStream _serialPort;
12 office 33 private Configuration.Configuration _configuration;
5 office 34 private int _currentFrequency;
9 office 35 private Task _scanTask;
3 office 36  
37 private BandScan()
38 {
39 }
40  
12 office 41 public BandScan(CatAssemblies catAssemblies, int min, int max, SerialPortStream serialPort, Configuration.Configuration configuration) : this()
3 office 42 {
43 _catAssemblies = catAssemblies;
44 _minFrequency = min;
45 _maxFrequency = max;
46 _serialPort = serialPort;
12 office 47 _configuration = configuration;
3 office 48  
49 }
50  
9 office 51 public void Start(int step, int pause, int detect)
3 office 52 {
53 _scanningCancellationTokenSource = new CancellationTokenSource();
54 _scanningCancellationToken = _scanningCancellationTokenSource.Token;
5 office 55  
9 office 56 _scanTask = Scan(step, pause, detect);
3 office 57 }
58  
9 office 59 public async Task Stop()
3 office 60 {
61 if (_scanningCancellationTokenSource != null)
62 {
63 _scanningCancellationTokenSource.Cancel();
64 }
9 office 65  
66 if(_scanTask != null)
67 {
68 await _scanTask;
69 _scanTask = null;
70 }
3 office 71 }
72  
9 office 73 private async Task Scan(int step, int pause, int detect)
3 office 74 {
9 office 75 if (!_serialPort.IsOpen)
76 {
77 _serialPort.Open();
78 }
79  
80 _serialPort.DiscardInBuffer();
81  
82 _catAssemblies.CatWrite<InformationState>("AI", new object[] { InformationState.ON });
83  
3 office 84 try
85 {
86  
5 office 87 _currentFrequency = _minFrequency;
3 office 88  
89 do
90 {
5 office 91 var taskCompletionSource = new TaskCompletionSource<bool>();
3 office 92  
5 office 93 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
9 office 94 Task.Delay(TimeSpan.FromSeconds(pause), _scanningCancellationToken)
95 .ContinueWith(_ => taskCompletionSource.TrySetResult(true), CancellationToken.None);
5 office 96 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
3 office 97  
7 office 98 using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav")))
99 {
100 soundPlayer.Play();
9 office 101 await _catAssemblies.CatWriteAsync<int>("FA", new object[] { _currentFrequency }, _scanningCancellationToken);
7 office 102 }
5 office 103  
9 office 104 using (var memoryStream = new MemoryStream())
105 {
106 _serialPort.DiscardInBuffer();
5 office 107  
9 office 108 await taskCompletionSource.Task;
109  
110 var count = _serialPort.BaudRate / sizeof(byte) * pause;
111 await _serialPort.CopyToAsync(memoryStream, count, _scanningCancellationToken);
112  
113 memoryStream.Position = 0L;
114  
11 office 115 // TODO: radios
9 office 116 var result = Encoding.ASCII.GetString(memoryStream.ToArray());
11 office 117 foreach (var split in result.Split(Radios.Generic.Constants.EOT))
9 office 118 {
17 office 119 if(string.IsNullOrEmpty(split))
120 {
121 continue;
122 }
123  
11 office 124 var command = $"{split}{Radios.Generic.Constants.EOT}";
125  
17 office 126 try
9 office 127 {
17 office 128 switch (_catAssemblies.CatParse<BusyState>("BY", new object[] { command }))
129 {
130 case BusyState.ON:
131 if (!_configuration.Notifications.TryGetNotificationState(NotificationType.SignalScanDetect, out var notificationState))
132 {
133 var toastFrom = new ToastForm(
134 $"{Resources.Signal_detected_during_scan}",
135 $"{Resources.Frequency}: {_currentFrequency}Hz",
136 notificationState.LingerTime,
137 Constants.AssemblyIcon);
12 office 138  
17 office 139 toastFrom.Show();
12 office 140  
17 office 141 await Task.Delay(TimeSpan.FromSeconds(detect), _scanningCancellationToken);
142 }
143 continue;
144 }
9 office 145 }
17 office 146 catch(UnmatchedRadioResponseException exception)
147 {
148 // suppress
149 var b = exception;
150 }
151 catch(Exception exception)
152 {
153 var a = exception;
154 }
9 office 155 }
156 }
157  
5 office 158 _currentFrequency = _currentFrequency + step;
159 if(_currentFrequency > _maxFrequency)
3 office 160 {
5 office 161 _currentFrequency = _minFrequency;
3 office 162 }
5 office 163 if(_currentFrequency < _minFrequency)
3 office 164 {
5 office 165 _currentFrequency = _minFrequency;
3 office 166 }
167  
5 office 168 } while(!_scanningCancellationToken.IsCancellationRequested);
3 office 169 }
170 catch(Exception exception)
171 {
172 Log.Error(exception, Resources.Scanning_aborted);
173 }
9 office 174 finally
175 {
176 _catAssemblies.CatWrite<InformationState>("AI", new object[] { InformationState.OFF });
177  
178 if (_serialPort.IsOpen)
179 {
180 _serialPort.Close();
181  
182 _serialPort.DiscardInBuffer();
183 }
184 }
3 office 185 }
186 }
187 }