HamBook – Diff between revs 7 and 9
?pathlinks?
Rev 7 | Rev 9 | |||
---|---|---|---|---|
Line 10... | Line 10... | |||
10 | using Serilog; |
10 | using Serilog; |
|
11 | using HamBook.Properties; |
11 | using HamBook.Properties; |
|
12 | using Serilog.Events; |
12 | using Serilog.Events; |
|
13 | using System.Media; |
13 | using System.Media; |
|
14 | using System.Reflection; |
14 | using System.Reflection; |
|
- | 15 | using RJCP.IO.Ports; |
||
- | 16 | using System.Diagnostics; |
||
- | 17 | using System.IO; |
||
Line 15... | Line 18... | |||
15 | |
18 | |
|
16 | namespace HamBook |
19 | namespace HamBook |
|
17 | { |
20 | { |
|
18 | public class BandScan |
21 | public class BandScan |
|
Line 21... | Line 24... | |||
21 | |
24 | |
|
22 | private CancellationToken _scanningCancellationToken; |
25 | private CancellationToken _scanningCancellationToken; |
|
23 | private CatAssemblies _catAssemblies; |
26 | private CatAssemblies _catAssemblies; |
|
24 | private int _minFrequency; |
27 | private int _minFrequency; |
|
25 | private int _maxFrequency; |
28 | private int _maxFrequency; |
|
26 | private SerialPort _serialPort; |
29 | private SerialPortStream _serialPort; |
|
- | 30 | private int _currentFrequency; |
||
Line 27... | Line 31... | |||
27 | private int _currentFrequency; |
31 | private Task _scanTask; |
|
28 | |
32 | |
|
29 | private BandScan() |
33 | private BandScan() |
|
Line 30... | Line 34... | |||
30 | { |
34 | { |
|
31 | } |
35 | } |
|
32 | |
36 | |
|
33 | public BandScan(CatAssemblies catAssemblies, int min, int max, SerialPort serialPort) : this() |
37 | public BandScan(CatAssemblies catAssemblies, int min, int max, SerialPortStream serialPort) : this() |
|
34 | { |
38 | { |
|
35 | _catAssemblies = catAssemblies; |
39 | _catAssemblies = catAssemblies; |
|
Line 36... | Line 40... | |||
36 | _minFrequency = min; |
40 | _minFrequency = min; |
|
Line 37... | Line 41... | |||
37 | _maxFrequency = max; |
41 | _maxFrequency = max; |
|
38 | _serialPort = serialPort; |
42 | _serialPort = serialPort; |
|
39 | |
- | ||
40 | } |
- | ||
41 | |
- | ||
42 | public async Task Start(int step, int pause) |
- | ||
43 | { |
- | ||
44 | if(_scanningCancellationTokenSource != null) |
- | ||
45 | { |
43 | |
|
46 | _scanningCancellationTokenSource.Cancel(); |
44 | } |
|
Line 47... | Line 45... | |||
47 | |
45 | |
|
48 | } |
46 | public void Start(int step, int pause, int detect) |
|
Line 49... | Line 47... | |||
49 | |
47 | { |
|
50 | _scanningCancellationTokenSource = new CancellationTokenSource(); |
48 | _scanningCancellationTokenSource = new CancellationTokenSource(); |
|
51 | _scanningCancellationToken = _scanningCancellationTokenSource.Token; |
49 | _scanningCancellationToken = _scanningCancellationTokenSource.Token; |
|
52 | |
50 | |
|
53 | await Scan(step, pause); |
51 | _scanTask = Scan(step, pause, detect); |
|
54 | } |
52 | } |
|
- | 53 | |
||
- | 54 | public async Task Stop() |
||
- | 55 | { |
||
- | 56 | if (_scanningCancellationTokenSource != null) |
||
- | 57 | { |
||
- | 58 | _scanningCancellationTokenSource.Cancel(); |
||
55 | |
59 | } |
|
Line 56... | Line 60... | |||
56 | public void Stop() |
60 | |
|
57 | { |
61 | if(_scanTask != null) |
|
58 | if (_scanningCancellationTokenSource != null) |
62 | { |
|
59 | { |
63 | await _scanTask; |
|
60 | _scanningCancellationTokenSource.Cancel(); |
- | ||
61 | } |
64 | _scanTask = null; |
|
62 | } |
65 | } |
|
63 | |
- | ||
Line -... | Line 66... | |||
- | 66 | } |
||
- | 67 | |
||
64 | private async Task Scan(int step, int pause) |
68 | private async Task Scan(int step, int pause, int detect) |
|
- | 69 | { |
||
65 | { |
70 | if (!_serialPort.IsOpen) |
|
- | 71 | { |
||
Line 66... | Line 72... | |||
66 | try |
72 | _serialPort.Open(); |
|
Line 67... | Line 73... | |||
67 | { |
73 | } |
|
68 | var squelch = _catAssemblies.CatRead<int>("SQ", new object[] { }); |
74 | |
|
69 | if (squelch == 0) |
75 | _serialPort.DiscardInBuffer(); |
|
Line 70... | Line 76... | |||
70 | { |
76 | |
|
- | 77 | _catAssemblies.CatWrite<InformationState>("AI", new object[] { InformationState.ON }); |
||
71 | var @default = _catAssemblies.CatGetDefault<int>("SQ", new object[] { }); |
78 | |
|
72 | |
79 | try |
|
Line 73... | Line 80... | |||
73 | _catAssemblies.CatSet<int>("SQ", new object[] { @default }); |
80 | { |
|
74 | } |
81 | |
|
75 | |
82 | _currentFrequency = _minFrequency; |
|
76 | _currentFrequency = _minFrequency; |
83 | |
|
77 | |
84 | do |
|
Line -... | Line 85... | |||
- | 85 | { |
||
- | 86 | var taskCompletionSource = new TaskCompletionSource<bool>(); |
||
- | 87 | |
||
- | 88 | #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
||
78 | do |
89 | Task.Delay(TimeSpan.FromSeconds(pause), _scanningCancellationToken) |
|
- | 90 | .ContinueWith(_ => taskCompletionSource.TrySetResult(true), CancellationToken.None); |
||
- | 91 | #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
||
- | 92 | |
||
- | 93 | using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav"))) |
||
- | 94 | { |
||
- | 95 | soundPlayer.Play(); |
||
- | 96 | await _catAssemblies.CatWriteAsync<int>("FA", new object[] { _currentFrequency }, _scanningCancellationToken); |
||
- | 97 | } |
||
- | 98 | |
||
- | 99 | using (var memoryStream = new MemoryStream()) |
||
- | 100 | { |
||
- | 101 | _serialPort.DiscardInBuffer(); |
||
- | 102 | |
||
- | 103 | await taskCompletionSource.Task; |
||
- | 104 | |
||
- | 105 | var count = _serialPort.BaudRate / sizeof(byte) * pause; |
||
- | 106 | await _serialPort.CopyToAsync(memoryStream, count, _scanningCancellationToken); |
||
Line 79... | Line 107... | |||
79 | { |
107 | |
|
80 | var taskCompletionSource = new TaskCompletionSource<bool>(); |
108 | memoryStream.Position = 0L; |
|
81 | |
109 | |
|
82 | #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
110 | var result = Encoding.ASCII.GetString(memoryStream.ToArray()); |
|
Line 105... | Line 133... | |||
105 | } |
133 | } |
|
106 | catch(Exception exception) |
134 | catch(Exception exception) |
|
107 | { |
135 | { |
|
108 | Log.Error(exception, Resources.Scanning_aborted); |
136 | Log.Error(exception, Resources.Scanning_aborted); |
|
109 | } |
137 | } |
|
- | 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 | } |
||
110 | } |
149 | } |
|
111 | } |
150 | } |
|
112 | } |
151 | } |