HamBook – Blame information for rev 3
?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; |
||
13 | |||
14 | namespace HamBook |
||
15 | { |
||
16 | public class BandScan |
||
17 | { |
||
18 | private CancellationTokenSource _scanningCancellationTokenSource; |
||
19 | |||
20 | private CancellationToken _scanningCancellationToken; |
||
21 | private Task _aiTask; |
||
22 | private CatAssemblies _catAssemblies; |
||
23 | private int _minFrequency; |
||
24 | private int _maxFrequency; |
||
25 | private SerialPort _serialPort; |
||
26 | |||
27 | private BandScan() |
||
28 | { |
||
29 | } |
||
30 | |||
31 | public BandScan(CatAssemblies catAssemblies, int min, int max, SerialPort serialPort) : this() |
||
32 | { |
||
33 | _catAssemblies = catAssemblies; |
||
34 | _minFrequency = min; |
||
35 | _maxFrequency = max; |
||
36 | _serialPort = serialPort; |
||
37 | |||
38 | } |
||
39 | |||
40 | public void Start() |
||
41 | { |
||
42 | if(_scanningCancellationTokenSource != null) |
||
43 | { |
||
44 | _scanningCancellationTokenSource.Cancel(); |
||
45 | |||
46 | } |
||
47 | |||
48 | if(_aiTask != null) |
||
49 | { |
||
50 | _aiTask.Wait(); |
||
51 | } |
||
52 | |||
53 | _scanningCancellationTokenSource = new CancellationTokenSource(); |
||
54 | _scanningCancellationToken = _scanningCancellationTokenSource.Token; |
||
55 | |||
56 | _aiTask = Task.Run(Ai, _scanningCancellationToken); |
||
57 | } |
||
58 | |||
59 | public void Stop() |
||
60 | { |
||
61 | if (_scanningCancellationTokenSource != null) |
||
62 | { |
||
63 | _scanningCancellationTokenSource.Cancel(); |
||
64 | } |
||
65 | |||
66 | if (_aiTask != null) |
||
67 | { |
||
68 | _aiTask.Wait(); |
||
69 | } |
||
70 | |||
71 | try |
||
72 | { |
||
73 | _catAssemblies.CatSet<ScanState>("SC", new object[] { ScanState.OFF }); |
||
74 | } |
||
75 | catch(Exception exception) |
||
76 | { |
||
77 | Log.Warning(exception, Resources.Could_not_set_scan_state); |
||
78 | } |
||
79 | |||
80 | try |
||
81 | { |
||
82 | _catAssemblies.CatSet<InformationState>("AI", new object[] { InformationState.OFF }); |
||
83 | } |
||
84 | catch(Exception exception) |
||
85 | { |
||
86 | Log.Warning(exception, Resources.Could_not_set_auto_information_state); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | private string ReadCat() |
||
91 | { |
||
92 | if (!_serialPort.IsOpen) |
||
93 | { |
||
94 | _serialPort.Open(); |
||
95 | } |
||
96 | |||
97 | try |
||
98 | { |
||
99 | return _serialPort.ReadTo(Radios.Generic.Constants.EOT); |
||
100 | } |
||
101 | finally |
||
102 | { |
||
103 | if (_serialPort.IsOpen) |
||
104 | { |
||
105 | _serialPort.Close(); |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | |||
110 | private void Ai() |
||
111 | { |
||
112 | try |
||
113 | { |
||
114 | |||
115 | _catAssemblies.CatSet<ScanState>("SC", new object[] { ScanState.OFF }); |
||
116 | |||
117 | var squelch = _catAssemblies.CatRead<int>("SQ", new object[] { }); |
||
118 | if (squelch == 0) |
||
119 | { |
||
120 | var @default = _catAssemblies.CatGetDefault<int>("SQ", new object[] { }); |
||
121 | |||
122 | _catAssemblies.CatSet<int>("SQ", new object[] { @default }); |
||
123 | } |
||
124 | |||
125 | _catAssemblies.CatSet<int>("FA", new object[] { _minFrequency }); |
||
126 | _catAssemblies.CatSet<ScanState>("SC", new object[] { ScanState.UP }); |
||
127 | _catAssemblies.CatSet<InformationState>("AI", new object[] { InformationState.ON }); |
||
128 | |||
129 | do |
||
130 | { |
||
131 | var data = ReadCat(); |
||
132 | |||
133 | var information = _catAssemblies.CatParse<Information>("AI", new object[] { data }); |
||
134 | |||
135 | if(information == null) |
||
136 | { |
||
137 | continue; |
||
138 | } |
||
139 | |||
140 | if(information.Frequency > _maxFrequency) |
||
141 | { |
||
142 | _catAssemblies.CatSet<int>("FA", new object[] { _minFrequency }); |
||
143 | continue; |
||
144 | } |
||
145 | |||
146 | if(information.Frequency < _minFrequency) |
||
147 | { |
||
148 | _catAssemblies.CatSet<int>("FA", new object[] { _minFrequency }); |
||
149 | continue; |
||
150 | } |
||
151 | } while (!_scanningCancellationToken.IsCancellationRequested); |
||
152 | } |
||
153 | catch(Exception exception) |
||
154 | { |
||
155 | Log.Error(exception, Resources.Scanning_aborted); |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | } |