HamBook – Blame information for rev 5

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