-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
739 lines (657 loc) · 26.8 KB
/
Program.cs
File metadata and controls
739 lines (657 loc) · 26.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using TapoSwitch.Properties;
using System.Configuration;
using System.Net.Http;
using System.Net.Sockets;
using Microsoft.Win32;
using System.Threading;
// Uses Unofficial TP-Link Tapo smart device library for C#: https://github.com/cwakefie27/TapoConnect
using TapoConnect;
using TapoConnect.Protocol;
using TapoConnect.Dto;
namespace TapoSwitch
{
/// <summary>
/// Contains the main entry point and application context for the TapoSwitch application.
/// </summary>
internal static class Program
{
/// <summary>
/// Gets the username from application settings.
/// </summary>
public static readonly string Username = ConfigurationManager.AppSettings["Username"];
/// <summary>
/// Gets the password from application settings.
/// </summary>
public static readonly string Password = ConfigurationManager.AppSettings["Password"];
/// <summary>
/// Gets the IP address of the Tapo device from application settings.
/// </summary>
public static readonly string IpAddress = ConfigurationManager.AppSettings["IpAddress"];
/// <summary>
/// Gets the shutdown timeout in seconds from application settings. Default is 2 seconds.
/// </summary>
public static readonly int ShutdownTimeoutSeconds =
int.TryParse(ConfigurationManager.AppSettings["ShutdownTimeoutSeconds"], out var timeout) && timeout > 0
? timeout
: 2;
/// <summary>
/// Gets the connection retry attempts from application settings. Default is 3.
/// </summary>
public static readonly int ConnectionRetryAttempts =
int.TryParse(ConfigurationManager.AppSettings["ConnectionRetryAttempts"], out var retries) && retries > 0
? retries
: 3;
/// <summary>
/// Gets the retry delay in milliseconds from application settings. Default is 2000ms.
/// </summary>
public static readonly int RetryDelayMilliseconds =
int.TryParse(ConfigurationManager.AppSettings["RetryDelayMilliseconds"], out var delay) && delay > 0
? delay
: 2000;
/// <summary>
/// Maximum delay in milliseconds for exponential backoff. Default is 30000ms (30 seconds).
/// </summary>
private const int MaxRetryDelayMilliseconds = 30000;
/// <summary>
/// Maximum length for Windows tooltip text (63 characters + null terminator).
/// </summary>
private const int MaxTooltipLength = 63;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// Validate configuration at startup
if (!ValidateConfiguration(out string validationError))
{
MessageBox.Show(
$"Configuration Error: {validationError}\n\nPlease check your App.config file.",
"TapoSwitch Configuration Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Set the default font for the application (example: Microsoft Sans Serif, 8.25pt)
using var font = new Font("Microsoft Sans Serif", 8.25F);
Application.SetDefaultFont(font);
var context = new CustomApplicationContext();
Application.Run(context);
}
/// <summary>
/// Validates critical configuration settings at startup.
/// </summary>
/// <param name="errorMessage">Error message if validation fails.</param>
/// <returns>True if configuration is valid, false otherwise.</returns>
private static bool ValidateConfiguration(out string errorMessage)
{
if (string.IsNullOrWhiteSpace(Username))
{
errorMessage = "Username is missing or empty.";
return false;
}
if (string.IsNullOrWhiteSpace(Password))
{
errorMessage = "Password is missing or empty.";
return false;
}
if (string.IsNullOrWhiteSpace(IpAddress))
{
errorMessage = "IpAddress is missing or empty.";
return false;
}
// Validate IP address format
if (!System.Net.IPAddress.TryParse(IpAddress, out _))
{
errorMessage = $"IpAddress '{IpAddress}' is not a valid IP address.";
return false;
}
errorMessage = null;
return true;
}
/// <summary>
/// Provides the application context for the TapoSwitch tray application, including tray icon and device control.
/// </summary>
public class CustomApplicationContext : ApplicationContext, IDisposable
{
/// <summary>
/// The tray icon displayed in the system tray.
/// </summary>
private NotifyIcon trayIcon;
/// <summary>
/// The client used to communicate with the Tapo device.
/// </summary>
private ITapoDeviceClient deviceClient;
/// <summary>
/// The authentication key for the Tapo device.
/// </summary>
private TapoDeviceKey deviceKey = null!;
/// <summary>
/// The information about the Tapo device.
/// </summary>
private DeviceGetInfoResult deviceInfo = null!;
/// <summary>
/// Indicates the current power state of the device.
/// </summary>
private volatile bool state = false;
/// <summary>
/// Indicates whether the application is shutting down.
/// </summary>
private volatile bool isShuttingDown = false;
/// <summary>
/// Indicates whether the device is currently connected.
/// </summary>
private volatile bool isConnected = false;
/// <summary>
/// Lock object for thread-safe operations.
/// </summary>
private readonly SemaphoreSlim connectionLock = new SemaphoreSlim(1, 1);
/// <summary>
/// Background timer for periodic connection checks.
/// </summary>
private System.Threading.Timer connectionCheckTimer;
/// <summary>
/// Synchronization context for UI thread marshaling.
/// </summary>
private readonly SynchronizationContext syncContext;
/// <summary>
/// Tracks whether Dispose has been called.
/// </summary>
private bool disposed = false;
/// <summary>
/// Initializes a new instance of the <see cref="CustomApplicationContext"/> class.
/// Sets up the tray icon and starts device initialization.
/// </summary>
public CustomApplicationContext()
{
// Capture UI synchronization context
syncContext = SynchronizationContext.Current ?? new WindowsFormsSynchronizationContext();
// Initialize Tray Icon
ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
contextMenuStrip.Items.Add("Exit", null, Exit);
string tooltipText = "Connecting to device...";
trayIcon = new NotifyIcon()
{
Icon = Resources.SwitchOff,
Text = tooltipText,
ContextMenuStrip = contextMenuStrip,
Visible = true
};
// Handle clicks
trayIcon.MouseClick += MouseClick;
// Handle application exit
Application.ApplicationExit += OnApplicationExit;
// Register for session change events (logoff, shutdown)
SystemEvents.SessionEnding += OnSessionEnding;
SystemEvents.SessionSwitch += OnSessionSwitch;
// Start async initialization after message loop starts
Task.Run(() => InitializeAsync());
// Start periodic connection check (every 30 seconds)
var connectionCheckInterval = TimeSpan.FromSeconds(30);
connectionCheckTimer = new System.Threading.Timer(
ConnectionCheckCallback,
null,
connectionCheckInterval,
connectionCheckInterval);
}
/// <summary>
/// Timer callback that safely handles connection checks.
/// </summary>
private async void ConnectionCheckCallback(object state)
{
try
{
await EnsureConnectionAsync().ConfigureAwait(false);
}
catch
{
// Prevent unhandled exceptions from crashing the app
}
}
/// <summary>
/// Asynchronously initializes the device connection and ensures the device is off at startup.
/// Retries on failure until successful or application shutdown.
/// </summary>
private async Task InitializeAsync()
{
int attempt = 0;
while (!isShuttingDown && !isConnected)
{
attempt++;
try
{
await InitializeConnection().ConfigureAwait(false);
await SyncDeviceStateAsync().ConfigureAwait(false);
await TurnOffAsync().ConfigureAwait(false);
isConnected = true;
InvokeOnUIThread(() =>
{
if (!isShuttingDown)
{
UpdateTrayIcon();
UpdateTooltip();
}
});
break;
}
catch (Exception ex) when (IsNetworkException(ex))
{
// Network issue - will retry
int delayMs;
if (attempt < ConnectionRetryAttempts)
{
// Exponential backoff capped at MaxRetryDelayMilliseconds
delayMs = Math.Min(RetryDelayMilliseconds * attempt, MaxRetryDelayMilliseconds);
}
else
{
// Keep retrying but with capped delay
delayMs = MaxRetryDelayMilliseconds;
}
await Task.Delay(delayMs).ConfigureAwait(false);
}
catch
{
// Non-network exception - wait and retry with capped delay
await Task.Delay(Math.Min(RetryDelayMilliseconds * 3, MaxRetryDelayMilliseconds)).ConfigureAwait(false);
}
}
}
/// <summary>
/// Asynchronously establishes a connection to the Tapo device and retrieves device information.
/// </summary>
public async Task InitializeConnection()
{
await connectionLock.WaitAsync().ConfigureAwait(false);
try
{
deviceClient = new TapoDeviceClient(new List<ITapoDeviceClient>
{
new SecurePassthroughDeviceClient(),
new KlapDeviceClient(),
});
deviceKey = await deviceClient.LoginByIpAsync(IpAddress, Username, Password).ConfigureAwait(false);
deviceInfo = await deviceClient.GetDeviceInfoAsync(deviceKey).ConfigureAwait(false);
}
finally
{
connectionLock.Release();
}
}
/// <summary>
/// Ensures the connection is active, reconnecting if necessary.
/// </summary>
private async Task EnsureConnectionAsync()
{
if (isShuttingDown)
return;
if (!isConnected)
{
await InitializeAsync().ConfigureAwait(false);
}
}
/// <summary>
/// Synchronizes local state with actual device state.
/// </summary>
private async Task SyncDeviceStateAsync()
{
try
{
var info = await deviceClient.GetDeviceInfoAsync(deviceKey).ConfigureAwait(false);
state = info.DeviceOn;
}
catch
{
// If we can't sync, assume off for safety
state = false;
}
}
/// <summary>
/// Asynchronously turns the Tapo device on.
/// </summary>
public async Task TurnOnAsync()
{
await ExecuteWithReconnectAsync(async () =>
{
await deviceClient.SetPowerAsync(deviceKey, true).ConfigureAwait(false);
state = true;
}).ConfigureAwait(false);
}
/// <summary>
/// Asynchronously turns the Tapo device off.
/// </summary>
public async Task TurnOffAsync()
{
await ExecuteWithReconnectAsync(async () =>
{
await deviceClient.SetPowerAsync(deviceKey, false).ConfigureAwait(false);
state = false;
}).ConfigureAwait(false);
}
/// <summary>
/// Executes an action with automatic reconnection on network errors.
/// Retries multiple times with exponential backoff.
/// </summary>
private async Task ExecuteWithReconnectAsync(Func<Task> action)
{
for (int attempt = 0; attempt < ConnectionRetryAttempts; attempt++)
{
try
{
await action().ConfigureAwait(false);
isConnected = true;
InvokeOnUIThread(() =>
{
if (!isShuttingDown)
{
UpdateTooltip();
}
});
return;
}
catch (Exception ex) when (IsNetworkException(ex) && !isShuttingDown)
{
isConnected = false;
// Last attempt failed
if (attempt == ConnectionRetryAttempts - 1)
{
InvokeOnUIThread(() =>
{
if (!isShuttingDown)
{
UpdateTooltip("Connection lost. Will retry automatically.");
}
});
// Trigger background reconnection
_ = Task.Run(() => InitializeAsync());
throw;
}
// Wait before retry with exponential backoff capped at MaxRetryDelayMilliseconds
int delayMs = Math.Min(RetryDelayMilliseconds * (attempt + 1), MaxRetryDelayMilliseconds);
await Task.Delay(delayMs).ConfigureAwait(false);
try
{
await InitializeConnection().ConfigureAwait(false);
}
catch
{
// Will retry in next iteration
}
}
catch (Exception) when (!isShuttingDown)
{
// Non-network exception
InvokeOnUIThread(() =>
{
if (!isShuttingDown)
{
UpdateTooltip("Device error occurred.");
}
});
throw;
}
}
}
/// <summary>
/// Marshals an action to the UI thread.
/// </summary>
private void InvokeOnUIThread(Action action)
{
if (syncContext != null && !isShuttingDown)
{
syncContext.Post(_ => action(), null);
}
else if (!isShuttingDown)
{
action();
}
}
/// <summary>
/// Determines if an exception is network-related.
/// </summary>
private bool IsNetworkException(Exception ex)
{
return ex is HttpRequestException ||
ex is SocketException ||
ex is TaskCanceledException ||
ex is OperationCanceledException ||
(ex.InnerException != null && IsNetworkException(ex.InnerException));
}
/// <summary>
/// Truncates tooltip text to Windows maximum length (63 characters).
/// </summary>
private string TruncateTooltip(string text)
{
if (string.IsNullOrEmpty(text))
return text;
if (text.Length <= MaxTooltipLength)
return text;
return text.Substring(0, MaxTooltipLength - 3) + "...";
}
/// <summary>
/// Updates the tray icon based on current state.
/// </summary>
private void UpdateTrayIcon()
{
if (isShuttingDown || disposed)
return;
trayIcon.Icon = state ? Resources.SwitchOn : Resources.SwitchOff;
}
/// <summary>
/// Updates the tooltip with device information or status message.
/// </summary>
private void UpdateTooltip(string customMessage = null)
{
if (isShuttingDown || disposed)
return;
string tooltipText;
if (customMessage != null)
{
tooltipText = customMessage;
}
else if (isConnected && deviceInfo != null)
{
tooltipText = $"Click to toggle {deviceInfo.Model} ({deviceInfo.Nickname}) switch.";
}
else
{
tooltipText = "Connecting to device...";
}
trayIcon.Text = TruncateTooltip(tooltipText);
}
/// <summary>
/// Handles mouse click events on the tray icon to toggle the device state.
/// </summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The mouse event arguments.</param>
private async void MouseClick(object sender, MouseEventArgs e)
{
// Left mouse button toggles
if (e.Button == MouseButtons.Left)
{
if (!isConnected)
{
// Trigger immediate reconnection attempt
UpdateTooltip("Reconnecting...");
_ = Task.Run(() => InitializeAsync());
return;
}
try
{
if (state)
{
await TurnOffAsync().ConfigureAwait(false);
}
else
{
await TurnOnAsync().ConfigureAwait(false);
}
InvokeOnUIThread(() =>
{
if (!isShuttingDown)
{
UpdateTrayIcon();
}
});
}
catch
{
// Error already handled in ExecuteWithReconnectAsync
}
}
}
/// <summary>
/// Handles session ending events (shutdown, logoff) to turn off the device.
/// This method temporarily cancels the shutdown to allow time for the device to turn off.
/// </summary>
private void OnSessionEnding(object sender, SessionEndingEventArgs e)
{
if (isShuttingDown)
return;
isShuttingDown = true;
// Cancel the shutdown temporarily to give us time to turn off the device
e.Cancel = true;
try
{
// Use Task.Run().Wait() to ensure the async operation completes
// before allowing shutdown to proceed. Use timeout to prevent hanging.
var turnOffTask = Task.Run(async () =>
{
try
{
await TurnOffAsync().ConfigureAwait(false);
}
catch
{
// Silently fail
}
});
// Wait up to configured seconds for the device to turn off
if (!turnOffTask.Wait(TimeSpan.FromSeconds(ShutdownTimeoutSeconds)))
{
// Timeout - proceed with shutdown anyway
}
}
catch
{
// Silently fail
}
finally
{
// Now allow the shutdown to proceed
e.Cancel = false;
// Clean up
Dispose();
// If this is a logoff, we can exit cleanly
if (e.Reason == SessionEndReasons.Logoff)
{
Application.Exit();
}
}
}
/// <summary>
/// Handles session switch events (lock, unlock, remote connect/disconnect).
/// </summary>
private async void OnSessionSwitch(object sender, SessionSwitchEventArgs e)
{
switch (e.Reason)
{
case SessionSwitchReason.SessionLogoff:
// Session logoff - turn off device
if (!isShuttingDown)
{
isShuttingDown = true;
try
{
await TurnOffAsync().ConfigureAwait(false);
InvokeOnUIThread(() =>
{
if (!isShuttingDown)
{
UpdateTrayIcon();
}
});
}
catch
{
// Silently fail
}
}
break;
}
}
/// <summary>
/// Handles application exit event to set shutdown flag.
/// </summary>
private void OnApplicationExit(object sender, EventArgs e)
{
isShuttingDown = true;
Dispose();
}
/// <summary>
/// Handles the exit menu item click event, turns off the device, and exits the application.
/// </summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private async void Exit(object sender, EventArgs e)
{
isShuttingDown = true;
try
{
await TurnOffAsync().ConfigureAwait(false);
}
catch
{
// Silently fail on exit
}
if (!disposed)
{
trayIcon.Visible = false;
}
Application.Exit();
}
/// <summary>
/// Disposes of managed resources.
/// </summary>
public new void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes of managed resources.
/// </summary>
/// <param name="disposing">True if called from Dispose(), false if called from finalizer.</param>
protected override void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// Dispose managed resources
connectionCheckTimer?.Dispose();
connectionLock?.Dispose();
if (trayIcon != null)
{
trayIcon.Visible = false;
trayIcon.Dispose();
}
// Unregister session events
SystemEvents.SessionEnding -= OnSessionEnding;
SystemEvents.SessionSwitch -= OnSessionSwitch;
Application.ApplicationExit -= OnApplicationExit;
}
disposed = true;
}
base.Dispose(disposing);
}
}
}
}