HamBook – Diff between revs 3 and 5

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 3 Rev 5
Line 21... Line 21...
21 private Task _aiTask; 21 private Task _aiTask;
22 private CatAssemblies _catAssemblies; 22 private CatAssemblies _catAssemblies;
23 private int _minFrequency; 23 private int _minFrequency;
24 private int _maxFrequency; 24 private int _maxFrequency;
25 private SerialPort _serialPort; 25 private SerialPort _serialPort;
-   26 private int _currentFrequency;
Line 26... Line 27...
26   27  
27 private BandScan() 28 private BandScan()
28 { 29 {
Line 35... Line 36...
35 _maxFrequency = max; 36 _maxFrequency = max;
36 _serialPort = serialPort; 37 _serialPort = serialPort;
Line 37... Line 38...
37   38  
Line 38... Line 39...
38 } 39 }
39   40  
40 public void Start() 41 public async Task Start(int step, int pause)
41 { 42 {
42 if(_scanningCancellationTokenSource != null) 43 if(_scanningCancellationTokenSource != null)
Line 50... Line 51...
50 _aiTask.Wait(); 51 _aiTask.Wait();
51 } 52 }
Line 52... Line 53...
52   53  
53 _scanningCancellationTokenSource = new CancellationTokenSource(); 54 _scanningCancellationTokenSource = new CancellationTokenSource();
54 _scanningCancellationToken = _scanningCancellationTokenSource.Token; 55 _scanningCancellationToken = _scanningCancellationTokenSource.Token;
55 56  
56 _aiTask = Task.Run(Ai, _scanningCancellationToken); 57 await Scan(step, pause);
Line 57... Line 58...
57 } 58 }
58   59  
59 public void Stop() 60 public void Stop()
Line 65... Line 66...
65   66  
66 if (_aiTask != null) 67 if (_aiTask != null)
67 { 68 {
68 _aiTask.Wait(); 69 _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 } 70 }
Line 88... Line 71...
88 } 71 }
89   72  
90 private string ReadCat() -  
91 { -  
92 if (!_serialPort.IsOpen) -  
93 { -  
94 _serialPort.Open(); -  
95 } 73 private async Task Scan(int step, int pause)
96   74 {
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 }); 75 try
116   76 {
117 var squelch = _catAssemblies.CatRead<int>("SQ", new object[] { }); 77 var squelch = _catAssemblies.CatRead<int>("SQ", new object[] { });
118 if (squelch == 0) 78 if (squelch == 0)
Line 119... Line 79...
119 { 79 {
120 var @default = _catAssemblies.CatGetDefault<int>("SQ", new object[] { }); 80 var @default = _catAssemblies.CatGetDefault<int>("SQ", new object[] { });
Line 121... Line 81...
121   81  
122 _catAssemblies.CatSet<int>("SQ", new object[] { @default }); -  
123 } -  
Line 124... Line 82...
124   82 _catAssemblies.CatSet<int>("SQ", new object[] { @default });
125 _catAssemblies.CatSet<int>("FA", new object[] { _minFrequency }); 83 }
126 _catAssemblies.CatSet<ScanState>("SC", new object[] { ScanState.UP }); 84  
Line -... Line 85...
-   85 _currentFrequency = _minFrequency;
127 _catAssemblies.CatSet<InformationState>("AI", new object[] { InformationState.ON }); 86  
-   87 do
Line 128... Line 88...
128   88 {
129 do -  
-   89 var taskCompletionSource = new TaskCompletionSource<bool>();
130 { 90  
131 var data = ReadCat(); -  
Line -... Line 91...
-   91 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
132   92 Task.Delay(TimeSpan.FromSeconds(pause)).ContinueWith(_ => taskCompletionSource.TrySetResult(true), _scanningCancellationToken);
133 var information = _catAssemblies.CatParse<Information>("AI", new object[] { data }); 93 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
134   94  
135 if(information == null) -  
136 { 95 _catAssemblies.CatSet<int>("FA", new object[] { _currentFrequency });
137 continue; -  
138 } 96  
139   97 await taskCompletionSource.Task;
140 if(information.Frequency > _maxFrequency) 98  
141 { -  
142 _catAssemblies.CatSet<int>("FA", new object[] { _minFrequency }); 99 _currentFrequency = _currentFrequency + step;
-   100 if(_currentFrequency > _maxFrequency)
143 continue; 101 {
144 } 102 _currentFrequency = _minFrequency;
145   103 }
146 if(information.Frequency < _minFrequency) 104 if(_currentFrequency < _minFrequency)
147 { 105 {
148 _catAssemblies.CatSet<int>("FA", new object[] { _minFrequency }); 106 _currentFrequency = _minFrequency;