HamBook – Blame information for rev 9

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