Zzz – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Threading; |
||
3 | using System.Threading.Tasks; |
||
4 | using InTheHand.Net.Bluetooth; |
||
5 | using InTheHand.Net.Sockets; |
||
6 | using Serilog; |
||
7 | using Zzz.Properties; |
||
8 | |||
9 | namespace Zzz.Idle |
||
10 | { |
||
11 | public class BluetoothScan : IDisposable |
||
12 | { |
||
13 | #region Public Events & Delegates |
||
14 | |||
15 | public event EventHandler<EventArgs> BluetoothDeviceDetected; |
||
16 | |||
17 | #endregion |
||
18 | |||
19 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
||
20 | |||
21 | private readonly CancellationToken _cancellationToken; |
||
22 | |||
23 | private readonly Task _scanTask; |
||
24 | private Configuration.Configuration _configuration; |
||
25 | private BluetoothClient _bluetoothClient; |
||
26 | |||
27 | private BluetoothComponent _bluetoothComponent; |
||
28 | |||
29 | private CancellationTokenSource _cancellationTokenSource; |
||
30 | |||
31 | #endregion |
||
32 | |||
33 | #region Constructors, Destructors and Finalizers |
||
34 | |||
35 | public BluetoothScan(Configuration.Configuration configuration) : this() |
||
36 | { |
||
37 | _configuration = configuration; |
||
38 | } |
||
39 | |||
40 | private BluetoothScan() |
||
41 | { |
||
42 | _bluetoothClient = new BluetoothClient(); |
||
43 | _bluetoothComponent = new BluetoothComponent(_bluetoothClient); |
||
44 | |||
45 | _bluetoothComponent.DiscoverDevicesComplete += BluetoothComponent_DiscoverDevicesComplete; |
||
46 | _bluetoothComponent.DiscoverDevicesProgress += BluetoothComponent_DiscoverDevicesProgress; |
||
47 | |||
48 | _cancellationTokenSource = new CancellationTokenSource(); |
||
49 | _cancellationToken = _cancellationTokenSource.Token; |
||
50 | |||
51 | _scanTask = Scan(_cancellationToken); |
||
52 | } |
||
53 | |||
54 | public void Dispose() |
||
55 | { |
||
56 | _cancellationTokenSource?.Cancel(); |
||
57 | |||
58 | _scanTask.Wait(CancellationToken.None); |
||
59 | |||
60 | _cancellationTokenSource = null; |
||
61 | |||
62 | if (_bluetoothComponent != null) |
||
63 | { |
||
64 | _bluetoothComponent.DiscoverDevicesComplete -= BluetoothComponent_DiscoverDevicesComplete; |
||
65 | _bluetoothComponent.DiscoverDevicesProgress -= BluetoothComponent_DiscoverDevicesProgress; |
||
66 | |||
67 | _bluetoothComponent?.Dispose(); |
||
68 | _bluetoothComponent = null; |
||
69 | } |
||
70 | |||
71 | _bluetoothClient?.Close(); |
||
72 | _bluetoothClient?.Dispose(); |
||
73 | _bluetoothClient = null; |
||
74 | } |
||
75 | |||
76 | #endregion |
||
77 | |||
78 | #region Event Handlers |
||
79 | |||
80 | private void BluetoothComponent_DiscoverDevicesProgress(object sender, DiscoverDevicesEventArgs e) |
||
81 | { |
||
82 | foreach (var deviceInfo in e.Devices) |
||
83 | { |
||
84 | Log.Information($"Found bluetooth device {deviceInfo.DeviceName}"); |
||
85 | |||
86 | if (_configuration.BluetoothWatchList.Contains(deviceInfo.DeviceName)) |
||
87 | { |
||
88 | Log.Information($"Matching bluetooth device {deviceInfo.DeviceName} detected."); |
||
89 | |||
90 | BluetoothDeviceDetected?.Invoke(this, EventArgs.Empty); |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | |||
95 | private void BluetoothComponent_DiscoverDevicesComplete(object sender, DiscoverDevicesEventArgs e) |
||
96 | { |
||
97 | Log.Information("Bluetooth scan completed."); |
||
98 | } |
||
99 | |||
100 | #endregion |
||
101 | |||
102 | #region Private Methods |
||
103 | |||
104 | private async Task Scan(CancellationToken cancellationToken) |
||
105 | { |
||
106 | try |
||
107 | { |
||
108 | do |
||
109 | { |
||
110 | try |
||
111 | { |
||
112 | await Task.Delay(TimeSpan.FromMinutes((int)_configuration.BluetoothScanInterval), |
||
113 | cancellationToken); |
||
114 | |||
115 | if (!BluetoothRadio.IsSupported) |
||
116 | { |
||
117 | Log.Information("Bluetooth radio is not supported."); |
||
118 | return; |
||
119 | } |
||
120 | |||
121 | _bluetoothComponent.DiscoverDevicesAsync(255, true, true, true, true, null); |
||
122 | |||
123 | Log.Information("Bluetooth scan started."); |
||
124 | } |
||
125 | catch (TaskCanceledException) |
||
126 | { |
||
127 | // ignore task cancellations |
||
128 | } |
||
129 | catch (Exception ex) |
||
130 | { |
||
131 | Log.Warning(ex, "Bluetooth scan failure."); |
||
132 | } |
||
133 | } while (!cancellationToken.IsCancellationRequested); |
||
134 | } |
||
135 | catch (Exception ex) |
||
136 | { |
||
137 | Log.Warning(ex, "Bluetooth thread terminated."); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | #endregion |
||
142 | } |
||
143 | } |