Zzz – Blame information for rev 2
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.ComponentModel; |
||
3 | using System.Drawing; |
||
4 | using System.Runtime.InteropServices; |
||
5 | using System.Threading; |
||
6 | using System.Windows.Forms; |
||
7 | using InTheHand.Net.Bluetooth; |
||
8 | using InTheHand.Net.Sockets; |
||
9 | using MQTTnet.Client.Subscribing; |
||
10 | using Serilog; |
||
11 | using Zzz.Clients; |
||
12 | using Zzz.Properties; |
||
13 | using Zzz.Utilities; |
||
14 | |||
15 | namespace Zzz |
||
16 | { |
||
17 | public partial class SettingsForm : Form |
||
18 | { |
||
19 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
||
20 | |||
21 | private BindingSource _bluetoothWatchListBindingSource; |
||
22 | |||
23 | private readonly MqttClient _mqttClient; |
||
24 | |||
25 | private readonly Configuration.Configuration _configuration; |
||
26 | |||
27 | private BindingSource _windowsWatchListBindingSource; |
||
28 | |||
29 | private BluetoothClient _bluetoothClient; |
||
30 | |||
31 | private BluetoothComponent _bluetoothComponent; |
||
32 | |||
33 | private ScheduledContinuation MqttRestartContinuation; |
||
34 | |||
35 | private CancellationTokenSource CancellationTokenSource; |
||
36 | |||
37 | private CancellationToken CancellationToken; |
||
38 | |||
39 | #endregion |
||
40 | |||
41 | #region Constructors, Destructors and Finalizers |
||
42 | |||
43 | public SettingsForm() |
||
44 | { |
||
45 | InitializeComponent(); |
||
46 | |||
47 | CancellationTokenSource = new CancellationTokenSource(); |
||
48 | CancellationToken = CancellationTokenSource.Token; |
||
49 | |||
50 | MqttRestartContinuation = new ScheduledContinuation(); |
||
51 | |||
52 | try |
||
53 | { |
||
54 | _bluetoothClient = new BluetoothClient(); |
||
55 | _bluetoothComponent = new BluetoothComponent(_bluetoothClient); |
||
56 | |||
57 | _bluetoothComponent.DiscoverDevicesProgress += LocalComponent_DiscoverDevicesProgress; |
||
58 | _bluetoothComponent.DiscoverDevicesComplete += LocalComponent_DiscoverDevicesComplete; |
||
59 | } |
||
60 | catch(Exception ex) |
||
61 | { |
||
62 | Log.Warning(ex, $"Bluetooth stack is not supported"); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | public SettingsForm(MqttClient mqttClient, Configuration.Configuration configuration) : this() |
||
67 | { |
||
68 | _mqttClient = mqttClient; |
||
69 | _configuration = configuration; |
||
70 | } |
||
71 | |||
72 | /// <summary> |
||
73 | /// Clean up any resources being used. |
||
74 | /// </summary> |
||
75 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
||
76 | protected override void Dispose(bool disposing) |
||
77 | { |
||
78 | if (disposing && components != null) |
||
79 | { |
||
80 | components.Dispose(); |
||
81 | } |
||
82 | |||
83 | if (_bluetoothComponent != null) |
||
84 | { |
||
85 | _bluetoothComponent.DiscoverDevicesProgress -= LocalComponent_DiscoverDevicesProgress; |
||
86 | _bluetoothComponent.DiscoverDevicesComplete -= LocalComponent_DiscoverDevicesComplete; |
||
87 | } |
||
88 | |||
89 | _mqttClient.MqttConnectionFailed -= MqttClient_MqttConnectionFailed; |
||
90 | _mqttClient.MqttConnectionSucceeded -= MqttClient_MqttConnectionSucceeded; |
||
91 | _mqttClient.MqttDisconnected -= MqttClient_MqttDisconnected; |
||
92 | _mqttClient.MqttSubscribeFailed -= MqttClient_MqttSubscribeFailed; |
||
93 | _mqttClient.MqttSubscribeSucceeded -= MqttClient_MqttSubscribeSucceeded; |
||
94 | |||
95 | _bluetoothComponent?.Dispose(); |
||
96 | _bluetoothComponent = null; |
||
97 | |||
98 | _bluetoothClient?.Dispose(); |
||
99 | _bluetoothClient = null; |
||
100 | |||
101 | base.Dispose(disposing); |
||
102 | } |
||
103 | |||
104 | #endregion |
||
105 | |||
106 | #region Event Handlers |
||
107 | |||
108 | private void MqttSettings_Changed(object sender, EventArgs e) |
||
109 | { |
||
110 | MqttRestartContinuation.Schedule(TimeSpan.FromSeconds(1), async () => |
||
111 | { |
||
112 | switch (_configuration.MqttEnable) |
||
113 | { |
||
114 | case false: |
||
115 | await _mqttClient.Stop(); |
||
116 | break; |
||
117 | default: |
||
118 | await _mqttClient.Restart(); |
||
119 | break; |
||
120 | } |
||
121 | }, CancellationToken); |
||
122 | |||
123 | } |
||
124 | |||
125 | private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e) |
||
126 | { |
||
127 | CancellationTokenSource.Cancel(); |
||
128 | } |
||
129 | |||
130 | private void SettingsForm_Load(object sender, EventArgs e) |
||
131 | { |
||
2 | office | 132 | Utilities.WindowState.FormTracker.Track(this); |
133 | |||
1 | office | 134 | pictureBox1.BackColor = _mqttClient.Connected ? Color.Green : Color.Red; |
135 | pictureBox2.BackColor = _mqttClient.Subscribed ? Color.Green : Color.Red; |
||
136 | |||
137 | _mqttClient.MqttConnectionFailed += MqttClient_MqttConnectionFailed; |
||
138 | _mqttClient.MqttConnectionSucceeded += MqttClient_MqttConnectionSucceeded; |
||
139 | _mqttClient.MqttDisconnected += MqttClient_MqttDisconnected; |
||
140 | _mqttClient.MqttSubscribeFailed += MqttClient_MqttSubscribeFailed; |
||
141 | _mqttClient.MqttSubscribeSucceeded += MqttClient_MqttSubscribeSucceeded; |
||
142 | |||
143 | // Bind the bluetooth watch list binding source. |
||
144 | _bluetoothWatchListBindingSource = new BindingSource(_configuration.BluetoothWatchList, ""); ; |
||
145 | listBox1.DataSource = _bluetoothWatchListBindingSource; |
||
146 | |||
147 | // Bind the windows watch list binding source. |
||
148 | _windowsWatchListBindingSource = new BindingSource(_configuration.WindowsWatchList, ""); |
||
149 | listBox2.DataSource = _windowsWatchListBindingSource; |
||
150 | |||
151 | // Bind the rest of the settings. |
||
152 | checkBox1.DataBindings.Add(nameof(checkBox1.Checked), _configuration, nameof(_configuration.LaunchOnBoot), true, DataSourceUpdateMode.OnPropertyChanged); |
||
153 | trackBar2.DataBindings.Add(nameof(trackBar2.Value), _configuration, nameof(_configuration.Timeout), true, DataSourceUpdateMode.OnPropertyChanged); |
||
154 | numericUpDown3.DataBindings.Add(nameof(numericUpDown3.Value), _configuration, nameof(_configuration.Timeout), true, DataSourceUpdateMode.OnPropertyChanged); |
||
155 | numericUpDown5.DataBindings.Add(nameof(numericUpDown5.Value), _configuration, nameof(_configuration.HibernateTimeout), true, DataSourceUpdateMode.OnPropertyChanged); |
||
156 | comboBox3.DataBindings.Add(nameof(comboBox3.Text), _configuration, nameof(_configuration.Action), true, DataSourceUpdateMode.OnPropertyChanged); |
||
157 | comboBox1.DataBindings.Add(nameof(comboBox1.Text), _configuration, nameof(_configuration.ActionClick), true, DataSourceUpdateMode.OnPropertyChanged); |
||
158 | comboBox2.DataBindings.Add(nameof(comboBox2.Text), _configuration, nameof(_configuration.ActionDoubleClick), true, DataSourceUpdateMode.OnPropertyChanged); |
||
159 | trackBar4.DataBindings.Add(nameof(trackBar4.Value), _configuration, nameof(_configuration.ClickActionDelay), true, DataSourceUpdateMode.OnPropertyChanged); |
||
160 | checkBox2.DataBindings.Add(nameof(checkBox2.Checked), _configuration, nameof(_configuration.MonitorMouse), true, DataSourceUpdateMode.OnPropertyChanged); |
||
161 | checkBox3.DataBindings.Add(nameof(checkBox3.Checked), _configuration, nameof(_configuration.MonitorKeyboard), true, DataSourceUpdateMode.OnPropertyChanged); |
||
162 | checkBox5.DataBindings.Add(nameof(checkBox5.Checked), _configuration, nameof(_configuration.MonitorBluetooth), true, DataSourceUpdateMode.OnPropertyChanged); |
||
163 | checkBox6.DataBindings.Add(nameof(checkBox6.Checked), _configuration, nameof(_configuration.MonitorWindows), true, DataSourceUpdateMode.OnPropertyChanged); |
||
164 | trackBar1.DataBindings.Add(nameof(trackBar1.Value), _configuration, nameof(_configuration.MouseMoveTolerance), true, DataSourceUpdateMode.OnPropertyChanged); |
||
165 | numericUpDown1.DataBindings.Add(nameof(numericUpDown1.Value), _configuration, nameof(_configuration.MouseMoveTolerance), true, DataSourceUpdateMode.OnPropertyChanged); |
||
166 | trackBar3.DataBindings.Add(nameof(trackBar3.Value), _configuration, nameof(_configuration.BluetoothScanInterval), true, DataSourceUpdateMode.OnPropertyChanged); |
||
167 | numericUpDown4.DataBindings.Add(nameof(numericUpDown4.Value), _configuration, nameof(_configuration.BluetoothScanInterval), true, DataSourceUpdateMode.OnPropertyChanged); |
||
168 | checkBox4.DataBindings.Add(nameof(checkBox4.Checked), _configuration, nameof(_configuration.MqttEnable), true, DataSourceUpdateMode.OnPropertyChanged); |
||
169 | textBox1.DataBindings.Add(nameof(textBox1.Text), _configuration, nameof(_configuration.MqttServer), true, DataSourceUpdateMode.OnPropertyChanged); |
||
170 | numericUpDown2.DataBindings.Add(nameof(numericUpDown2.Value), _configuration, nameof(_configuration.MqttPort), true, DataSourceUpdateMode.OnPropertyChanged); |
||
171 | textBox2.DataBindings.Add(nameof(textBox2.Text), _configuration, nameof(_configuration.MqttUsername), true, DataSourceUpdateMode.OnPropertyChanged); |
||
172 | textBox3.DataBindings.Add(nameof(textBox3.Text), _configuration, nameof(_configuration.MqttPassword), true, DataSourceUpdateMode.OnPropertyChanged); |
||
173 | textBox4.DataBindings.Add(nameof(textBox4.Text), _configuration, nameof(_configuration.MqttTopic), true, DataSourceUpdateMode.OnPropertyChanged); |
||
174 | } |
||
175 | |||
176 | private void MqttClient_MqttSubscribeSucceeded(object sender, MqttClientSubscribeResultCode e) |
||
177 | { |
||
178 | pictureBox2.BackColor = Color.Green; |
||
179 | } |
||
180 | |||
181 | private void MqttClient_MqttSubscribeFailed(object sender, MqttClientSubscribeResultCode e) |
||
182 | { |
||
183 | pictureBox2.BackColor = Color.Red; |
||
184 | } |
||
185 | |||
186 | private void MqttClient_MqttDisconnected(object sender, EventArgs e) |
||
187 | { |
||
188 | pictureBox1.BackColor = Color.Red; |
||
189 | pictureBox2.BackColor = Color.Red; |
||
190 | } |
||
191 | |||
192 | private void MqttClient_MqttConnectionSucceeded(object sender, EventArgs e) |
||
193 | { |
||
194 | pictureBox1.BackColor = Color.Green; |
||
195 | } |
||
196 | |||
197 | private void MqttClient_MqttConnectionFailed(object sender, EventArgs e) |
||
198 | { |
||
199 | pictureBox1.BackColor = Color.Red; |
||
200 | pictureBox2.BackColor = Color.Red; |
||
201 | } |
||
202 | |||
203 | private void Button2_Click(object sender, EventArgs e) |
||
204 | { |
||
205 | foreach (var item in listBox1.SelectedItems) |
||
206 | { |
||
207 | _configuration.BluetoothWatchList.Remove((string) item); |
||
208 | } |
||
209 | |||
210 | _bluetoothWatchListBindingSource.ResetBindings(true); |
||
211 | } |
||
212 | |||
213 | private void Button1_Click(object sender, EventArgs e) |
||
214 | { |
||
215 | if (string.IsNullOrEmpty(textBox5.Text)) |
||
216 | { |
||
217 | return; |
||
218 | } |
||
219 | |||
220 | if (_configuration.BluetoothWatchList.Contains(textBox5.Text)) |
||
221 | { |
||
222 | return; |
||
223 | } |
||
224 | |||
225 | _configuration.BluetoothWatchList.Add(textBox5.Text); |
||
226 | |||
227 | _bluetoothWatchListBindingSource.ResetBindings(true); |
||
228 | } |
||
229 | |||
230 | private void Button3_Click(object sender, EventArgs e) |
||
231 | { |
||
232 | if (!BluetoothRadio.IsSupported) |
||
233 | { |
||
234 | Log.Information("Bluetooth radio is not supported."); |
||
235 | return; |
||
236 | } |
||
237 | |||
238 | _bluetoothComponent.DiscoverDevicesAsync(255, true, true, true, true, null); |
||
239 | |||
240 | Log.Information("Bluetooth scan started."); |
||
241 | } |
||
242 | |||
243 | private void LocalComponent_DiscoverDevicesComplete(object sender, DiscoverDevicesEventArgs e) |
||
244 | { |
||
245 | Log.Information("Bluetooth scan finished."); |
||
246 | |||
247 | _bluetoothWatchListBindingSource.ResetBindings(true); |
||
248 | } |
||
249 | |||
250 | private void LocalComponent_DiscoverDevicesProgress(object sender, DiscoverDevicesEventArgs e) |
||
251 | { |
||
252 | foreach (var deviceInfo in e.Devices) |
||
253 | { |
||
254 | if (!_configuration.BluetoothWatchList.Contains(deviceInfo.DeviceName)) |
||
255 | { |
||
256 | _configuration.BluetoothWatchList.Add(deviceInfo.DeviceName); |
||
257 | |||
258 | _bluetoothWatchListBindingSource.ResetBindings(true); |
||
259 | } |
||
260 | } |
||
261 | } |
||
262 | |||
263 | private void Button5_Click(object sender, EventArgs e) |
||
264 | { |
||
265 | foreach (var item in listBox2.SelectedItems) |
||
266 | { |
||
267 | _configuration.WindowsWatchList.Remove((string) item); |
||
268 | } |
||
269 | |||
270 | |||
271 | _windowsWatchListBindingSource.ResetBindings(true); |
||
272 | } |
||
273 | |||
274 | private void Button4_Click(object sender, EventArgs e) |
||
275 | { |
||
276 | foreach (var item in listBox3.SelectedItems) |
||
277 | { |
||
278 | if (_configuration.WindowsWatchList.Contains((string) item)) |
||
279 | { |
||
280 | continue; |
||
281 | } |
||
282 | |||
283 | _configuration.WindowsWatchList.Add((string) item); |
||
284 | } |
||
285 | |||
286 | _windowsWatchListBindingSource.ResetBindings(true); |
||
287 | } |
||
288 | |||
289 | private void Button6_Click(object sender, EventArgs e) |
||
290 | { |
||
291 | listBox3.Items.Clear(); |
||
292 | |||
293 | foreach (var handle in Helpers.FindWindows((wnd, param) => true)) |
||
294 | { |
||
295 | var title = Helpers.GetWindowTitle(handle); |
||
296 | if (string.IsNullOrEmpty(title)) |
||
297 | { |
||
298 | continue; |
||
299 | } |
||
300 | |||
301 | listBox3.Items.Add(title); |
||
302 | } |
||
303 | } |
||
304 | |||
305 | private void SettingsForm_ResizeBegin(object sender, EventArgs e) |
||
306 | { |
||
307 | SuspendLayout(); |
||
308 | } |
||
309 | |||
310 | private void SettingsForm_ResizeEnd(object sender, EventArgs e) |
||
311 | { |
||
312 | ResumeLayout(true); |
||
313 | } |
||
314 | |||
315 | #endregion |
||
316 | } |
||
317 | } |