-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
86 lines (72 loc) · 2.67 KB
/
Copy pathApp.xaml.cs
File metadata and controls
86 lines (72 loc) · 2.67 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
using System;
using System.IO;
using System.Windows;
namespace DoorLockTray
{
public partial class App : System.Windows.Application
{
private System.Windows.Forms.NotifyIcon? trayIcon;
private HaWsClient? client;
protected override async void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
trayIcon = new System.Windows.Forms.NotifyIcon
{
Text = "Door Lock",
Icon = IconLoader.Load("locked.ico"),
Visible = true
};
var menu = new System.Windows.Forms.ContextMenuStrip();
var exitItem = new System.Windows.Forms.ToolStripMenuItem("Exit");
exitItem.Click += (_, __) => Shutdown();
menu.Items.Add(exitItem);
trayIcon.ContextMenuStrip = menu;
// TODO: Set these
var haUrl = "wss://x/api/websocket";
var token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var lockEntityId = "lock.office_door";
client = new HaWsClient(haUrl, token, lockEntityId);
client.LockStateChanged += state =>
{
Dispatcher.Invoke(() =>
{
if (trayIcon is null) return;
if (string.Equals(state, "locked", StringComparison.OrdinalIgnoreCase))
{
trayIcon.Icon = IconLoader.Load("locked.ico");
trayIcon.Text = "Door: Locked";
}
else if (string.Equals(state, "unlocked", StringComparison.OrdinalIgnoreCase))
{
trayIcon.Icon = IconLoader.Load("unlocked.ico");
trayIcon.Text = "Door: Unlocked";
}
else
{
trayIcon.Text = $"Door: {state}";
}
});
};
await client.StartAsync();
}
protected override void OnExit(ExitEventArgs e)
{
client?.Dispose();
if (trayIcon is not null)
{
trayIcon.Visible = false;
trayIcon.Dispose();
trayIcon = null;
}
base.OnExit(e);
}
}
internal static class IconLoader
{
public static System.Drawing.Icon Load(string fileName)
{
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets", fileName);
return new System.Drawing.Icon(path);
}
}
}