HamBook – Blame information for rev 7
?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; |
||
3 | office | 15 | |
16 | namespace HamBook |
||
17 | { |
||
18 | public class BandScan |
||
19 | { |
||
20 | private CancellationTokenSource _scanningCancellationTokenSource; |
||
21 | |||
22 | private CancellationToken _scanningCancellationToken; |
||
23 | private CatAssemblies _catAssemblies; |
||
24 | private int _minFrequency; |
||
25 | private int _maxFrequency; |
||
26 | private SerialPort _serialPort; |
||
5 | office | 27 | private int _currentFrequency; |
3 | office | 28 | |
29 | private BandScan() |
||
30 | { |
||
31 | } |
||
32 | |||
33 | public BandScan(CatAssemblies catAssemblies, int min, int max, SerialPort serialPort) : this() |
||
34 | { |
||
35 | _catAssemblies = catAssemblies; |
||
36 | _minFrequency = min; |
||
37 | _maxFrequency = max; |
||
38 | _serialPort = serialPort; |
||
39 | |||
40 | } |
||
41 | |||
5 | office | 42 | public async Task Start(int step, int pause) |
3 | office | 43 | { |
44 | if(_scanningCancellationTokenSource != null) |
||
45 | { |
||
46 | _scanningCancellationTokenSource.Cancel(); |
||
47 | |||
48 | } |
||
49 | |||
50 | _scanningCancellationTokenSource = new CancellationTokenSource(); |
||
51 | _scanningCancellationToken = _scanningCancellationTokenSource.Token; |
||
5 | office | 52 | |
53 | await Scan(step, pause); |
||
3 | office | 54 | } |
55 | |||
56 | public void Stop() |
||
57 | { |
||
58 | if (_scanningCancellationTokenSource != null) |
||
59 | { |
||
60 | _scanningCancellationTokenSource.Cancel(); |
||
61 | } |
||
62 | } |
||
63 | |||
5 | office | 64 | private async Task Scan(int step, int pause) |
3 | office | 65 | { |
66 | try |
||
67 | { |
||
68 | var squelch = _catAssemblies.CatRead<int>("SQ", new object[] { }); |
||
69 | if (squelch == 0) |
||
70 | { |
||
71 | var @default = _catAssemblies.CatGetDefault<int>("SQ", new object[] { }); |
||
72 | |||
73 | _catAssemblies.CatSet<int>("SQ", new object[] { @default }); |
||
74 | } |
||
75 | |||
5 | office | 76 | _currentFrequency = _minFrequency; |
3 | office | 77 | |
78 | do |
||
79 | { |
||
5 | office | 80 | var taskCompletionSource = new TaskCompletionSource<bool>(); |
3 | office | 81 | |
5 | office | 82 | #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
83 | Task.Delay(TimeSpan.FromSeconds(pause)).ContinueWith(_ => taskCompletionSource.TrySetResult(true), _scanningCancellationToken); |
||
84 | #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
||
3 | office | 85 | |
7 | office | 86 | using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav"))) |
87 | { |
||
88 | soundPlayer.Play(); |
||
89 | _catAssemblies.CatSet<int>("FA", new object[] { _currentFrequency }); |
||
90 | } |
||
5 | office | 91 | |
92 | await taskCompletionSource.Task; |
||
93 | |||
94 | _currentFrequency = _currentFrequency + step; |
||
95 | if(_currentFrequency > _maxFrequency) |
||
3 | office | 96 | { |
5 | office | 97 | _currentFrequency = _minFrequency; |
3 | office | 98 | } |
5 | office | 99 | if(_currentFrequency < _minFrequency) |
3 | office | 100 | { |
5 | office | 101 | _currentFrequency = _minFrequency; |
3 | office | 102 | } |
103 | |||
5 | office | 104 | } while(!_scanningCancellationToken.IsCancellationRequested); |
3 | office | 105 | } |
106 | catch(Exception exception) |
||
107 | { |
||
108 | Log.Error(exception, Resources.Scanning_aborted); |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | } |