Zzz – Blame information for rev 1
?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 | { |
||
132 | pictureBox1.BackColor = _mqttClient.Connected ? Color.Green : Color.Red; |
||
133 | pictureBox2.BackColor = _mqttClient.Subscribed ? Color.Green : Color.Red; |
||
134 | |||
135 | _mqttClient.MqttConnectionFailed += MqttClient_MqttConnectionFailed; |
||
136 | _mqttClient.MqttConnectionSucceeded += MqttClient_MqttConnectionSucceeded; |
||
137 | _mqttClient.MqttDisconnected += MqttClient_MqttDisconnected; |
||
138 | _mqttClient.MqttSubscribeFailed += MqttClient_MqttSubscribeFailed; |
||
139 | _mqttClient.MqttSubscribeSucceeded += MqttClient_MqttSubscribeSucceeded; |
||
140 | |||
141 | // Bind the bluetooth watch list binding source. |
||
142 | _bluetoothWatchListBindingSource = new BindingSource(_configuration.BluetoothWatchList, ""); ; |
||
143 | listBox1.DataSource = _bluetoothWatchListBindingSource; |
||
144 | |||
145 | // Bind the windows watch list binding source. |
||
146 | _windowsWatchListBindingSource = new BindingSource(_configuration.WindowsWatchList, ""); |
||
147 | listBox2.DataSource = _windowsWatchListBindingSource; |
||
148 | |||
149 | // Bind the rest of the settings. |
||
150 | checkBox1.DataBindings.Add(nameof(checkBox1.Checked), _configuration, nameof(_configuration.LaunchOnBoot), true, DataSourceUpdateMode.OnPropertyChanged); |
||
151 | trackBar2.DataBindings.Add(nameof(trackBar2.Value), _configuration, nameof(_configuration.Timeout), true, DataSourceUpdateMode.OnPropertyChanged); |
||
152 | numericUpDown3.DataBindings.Add(nameof(numericUpDown3.Value), _configuration, nameof(_configuration.Timeout), true, DataSourceUpdateMode.OnPropertyChanged); |
||
153 | numericUpDown5.DataBindings.Add(nameof(numericUpDown5.Value), _configuration, nameof(_configuration.HibernateTimeout), true, DataSourceUpdateMode.OnPropertyChanged); |
||
154 | comboBox3.DataBindings.Add(nameof(comboBox3.Text), _configuration, nameof(_configuration.Action), true, DataSourceUpdateMode.OnPropertyChanged); |
||
155 | comboBox1.DataBindings.Add(nameof(comboBox1.Text), _configuration, nameof(_configuration.ActionClick), true, DataSourceUpdateMode.OnPropertyChanged); |
||
156 | comboBox2.DataBindings.Add(nameof(comboBox2.Text), _configuration, nameof(_configuration.ActionDoubleClick), true, DataSourceUpdateMode.OnPropertyChanged); |
||
157 | trackBar4.DataBindings.Add(nameof(trackBar4.Value), _configuration, nameof(_configuration.ClickActionDelay), true, DataSourceUpdateMode.OnPropertyChanged); |
||
158 | checkBox2.DataBindings.Add(nameof(checkBox2.Checked), _configuration, nameof(_configuration.MonitorMouse), true, DataSourceUpdateMode.OnPropertyChanged); |
||
159 | checkBox3.DataBindings.Add(nameof(checkBox3.Checked), _configuration, nameof(_configuration.MonitorKeyboard), true, DataSourceUpdateMode.OnPropertyChanged); |
||
160 | checkBox5.DataBindings.Add(nameof(checkBox5.Checked), _configuration, nameof(_configuration.MonitorBluetooth), true, DataSourceUpdateMode.OnPropertyChanged); |
||
161 | checkBox6.DataBindings.Add(nameof(checkBox6.Checked), _configuration, nameof(_configuration.MonitorWindows), true, DataSourceUpdateMode.OnPropertyChanged); |
||
162 | trackBar1.DataBindings.Add(nameof(trackBar1.Value), _configuration, nameof(_configuration.MouseMoveTolerance), true, DataSourceUpdateMode.OnPropertyChanged); |
||
163 | numericUpDown1.DataBindings.Add(nameof(numericUpDown1.Value), _configuration, nameof(_configuration.MouseMoveTolerance), true, DataSourceUpdateMode.OnPropertyChanged); |
||
164 | trackBar3.DataBindings.Add(nameof(trackBar3.Value), _configuration, nameof(_configuration.BluetoothScanInterval), true, DataSourceUpdateMode.OnPropertyChanged); |
||
165 | numericUpDown4.DataBindings.Add(nameof(numericUpDown4.Value), _configuration, nameof(_configuration.BluetoothScanInterval), true, DataSourceUpdateMode.OnPropertyChanged); |
||
166 | checkBox4.DataBindings.Add(nameof(checkBox4.Checked), _configuration, nameof(_configuration.MqttEnable), true, DataSourceUpdateMode.OnPropertyChanged); |
||
167 | textBox1.DataBindings.Add(nameof(textBox1.Text), _configuration, nameof(_configuration.MqttServer), true, DataSourceUpdateMode.OnPropertyChanged); |
||
168 | numericUpDown2.DataBindings.Add(nameof(numericUpDown2.Value), _configuration, nameof(_configuration.MqttPort), true, DataSourceUpdateMode.OnPropertyChanged); |
||
169 | textBox2.DataBindings.Add(nameof(textBox2.Text), _configuration, nameof(_configuration.MqttUsername), true, DataSourceUpdateMode.OnPropertyChanged); |
||
170 | textBox3.DataBindings.Add(nameof(textBox3.Text), _configuration, nameof(_configuration.MqttPassword), true, DataSourceUpdateMode.OnPropertyChanged); |
||
171 | textBox4.DataBindings.Add(nameof(textBox4.Text), _configuration, nameof(_configuration.MqttTopic), true, DataSourceUpdateMode.OnPropertyChanged); |
||
172 | } |
||
173 | |||
174 | private void MqttClient_MqttSubscribeSucceeded(object sender, MqttClientSubscribeResultCode e) |
||
175 | { |
||
176 | pictureBox2.BackColor = Color.Green; |
||
177 | } |
||
178 | |||
179 | private void MqttClient_MqttSubscribeFailed(object sender, MqttClientSubscribeResultCode e) |
||
180 | { |
||
181 | pictureBox2.BackColor = Color.Red; |
||
182 | } |
||
183 | |||
184 | private void MqttClient_MqttDisconnected(object sender, EventArgs e) |
||
185 | { |
||
186 | pictureBox1.BackColor = Color.Red; |
||
187 | pictureBox2.BackColor = Color.Red; |
||
188 | } |
||
189 | |||
190 | private void MqttClient_MqttConnectionSucceeded(object sender, EventArgs e) |
||
191 | { |
||
192 | pictureBox1.BackColor = Color.Green; |
||
193 | } |
||
194 | |||
195 | private void MqttClient_MqttConnectionFailed(object sender, EventArgs e) |
||
196 | { |
||
197 | pictureBox1.BackColor = Color.Red; |
||
198 | pictureBox2.BackColor = Color.Red; |
||
199 | } |
||
200 | |||
201 | private void Button2_Click(object sender, EventArgs e) |
||
202 | { |
||
203 | foreach (var item in listBox1.SelectedItems) |
||
204 | { |
||
205 | _configuration.BluetoothWatchList.Remove((string) item); |
||
206 | } |
||
207 | |||
208 | _bluetoothWatchListBindingSource.ResetBindings(true); |
||
209 | } |
||
210 | |||
211 | private void Button1_Click(object sender, EventArgs e) |
||
212 | { |
||
213 | if (string.IsNullOrEmpty(textBox5.Text)) |
||
214 | { |
||
215 | return; |
||
216 | } |
||
217 | |||
218 | if (_configuration.BluetoothWatchList.Contains(textBox5.Text)) |
||
219 | { |
||
220 | return; |
||
221 | } |
||
222 | |||
223 | _configuration.BluetoothWatchList.Add(textBox5.Text); |
||
224 | |||
225 | _bluetoothWatchListBindingSource.ResetBindings(true); |
||
226 | } |
||
227 | |||
228 | private void Button3_Click(object sender, EventArgs e) |
||
229 | { |
||
230 | if (!BluetoothRadio.IsSupported) |
||
231 | { |
||
232 | Log.Information("Bluetooth radio is not supported."); |
||
233 | return; |
||
234 | } |
||
235 | |||
236 | _bluetoothComponent.DiscoverDevicesAsync(255, true, true, true, true, null); |
||
237 | |||
238 | Log.Information("Bluetooth scan started."); |
||
239 | } |
||
240 | |||
241 | private void LocalComponent_DiscoverDevicesComplete(object sender, DiscoverDevicesEventArgs e) |
||
242 | { |
||
243 | Log.Information("Bluetooth scan finished."); |
||
244 | |||
245 | _bluetoothWatchListBindingSource.ResetBindings(true); |
||
246 | } |
||
247 | |||
248 | private void LocalComponent_DiscoverDevicesProgress(object sender, DiscoverDevicesEventArgs e) |
||
249 | { |
||
250 | foreach (var deviceInfo in e.Devices) |
||
251 | { |
||
252 | if (!_configuration.BluetoothWatchList.Contains(deviceInfo.DeviceName)) |
||
253 | { |
||
254 | _configuration.BluetoothWatchList.Add(deviceInfo.DeviceName); |
||
255 | |||
256 | _bluetoothWatchListBindingSource.ResetBindings(true); |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 | |||
261 | private void Button5_Click(object sender, EventArgs e) |
||
262 | { |
||
263 | foreach (var item in listBox2.SelectedItems) |
||
264 | { |
||
265 | _configuration.WindowsWatchList.Remove((string) item); |
||
266 | } |
||
267 | |||
268 | |||
269 | _windowsWatchListBindingSource.ResetBindings(true); |
||
270 | } |
||
271 | |||
272 | private void Button4_Click(object sender, EventArgs e) |
||
273 | { |
||
274 | foreach (var item in listBox3.SelectedItems) |
||
275 | { |
||
276 | if (_configuration.WindowsWatchList.Contains((string) item)) |
||
277 | { |
||
278 | continue; |
||
279 | } |
||
280 | |||
281 | _configuration.WindowsWatchList.Add((string) item); |
||
282 | } |
||
283 | |||
284 | _windowsWatchListBindingSource.ResetBindings(true); |
||
285 | } |
||
286 | |||
287 | private void Button6_Click(object sender, EventArgs e) |
||
288 | { |
||
289 | listBox3.Items.Clear(); |
||
290 | |||
291 | foreach (var handle in Helpers.FindWindows((wnd, param) => true)) |
||
292 | { |
||
293 | var title = Helpers.GetWindowTitle(handle); |
||
294 | if (string.IsNullOrEmpty(title)) |
||
295 | { |
||
296 | continue; |
||
297 | } |
||
298 | |||
299 | listBox3.Items.Add(title); |
||
300 | } |
||
301 | } |
||
302 | |||
303 | private void SettingsForm_ResizeBegin(object sender, EventArgs e) |
||
304 | { |
||
305 | SuspendLayout(); |
||
306 | } |
||
307 | |||
308 | private void SettingsForm_ResizeEnd(object sender, EventArgs e) |
||
309 | { |
||
310 | ResumeLayout(true); |
||
311 | } |
||
312 | |||
313 | #endregion |
||
314 | } |
||
315 | } |