-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.xaml.cs
175 lines (145 loc) · 5.17 KB
/
App.xaml.cs
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
using System;
using System.Windows;
using Forms = System.Windows.Forms;
using CloudFlightMonitor.model;
using System.Timers;
using System.Drawing;
namespace CloudFlightMonitor
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private readonly Forms.NotifyIcon notifyIcon;
private System.Drawing.Icon[] chargeIcons;
private CloudFlightHeadset headset = null;
private Timer updateTimer;
private const int UPDATE_TIMER_MS = 60000; // 5 mins = 300000 ms
private const string BASE_ICON_TEXT = "Cloud Flight Monitor";
private Forms.ToolStripLabel batLabel;
public App()
{
notifyIcon = new Forms.NotifyIcon();
}
protected override void OnStartup(StartupEventArgs e)
{
InitIcons();
InitHIDDev();
OnUpdate(null, null); // force update first time
base.OnStartup(e);
Console.WriteLine("Started!");
// Start update Timer
updateTimer = new Timer(UPDATE_TIMER_MS);
updateTimer.Elapsed += OnUpdate;
updateTimer.AutoReset = true;
updateTimer.Enabled = true;
}
protected override void OnExit(ExitEventArgs e)
{
notifyIcon.Dispose();
base.OnExit(e);
}
private void OnUpdate(Object source, ElapsedEventArgs _)
{
try
{
//Get current batteryCharge
int batteryCharge = headset.ReadBattery();
if (batteryCharge <= 100)
{
string text = "Battery: " + batteryCharge + "%";
batLabel.Text = text;
notifyIcon.Text = BASE_ICON_TEXT + "\n" + text;
//Update Tray Icon
notifyIcon.Icon = chargeIcons[batteryCharge];
}
else if (batteryCharge == 199)
{
// headset is charging
string text = "Headset Charging";
batLabel.Text = text;
notifyIcon.Text = BASE_ICON_TEXT + "\n" + text;
//Update Tray Icon
notifyIcon.Icon = chargeIcons[100];
}
else
{
// headset returned a strange/unknown value...
string text = "Unknown Status";
batLabel.Text = text;
notifyIcon.Text = BASE_ICON_TEXT + "\n" + text;
//Update Tray Icon
notifyIcon.Icon = chargeIcons[101];
}
}
catch (Exception e)
{
Console.WriteLine("Error while updating battery charge, message: ", e.Message);
const string txt = "Couldn't connect to headset";
batLabel.Text = txt;
notifyIcon.Text = BASE_ICON_TEXT + "\n" + txt;
//Update Tray Icon
notifyIcon.Icon = chargeIcons[0];
// Reset connection...
InitHIDDev();
}
}
private void InitIcons()
{
chargeIcons = new Icon[102];
for (int i = 0; i <= 100; i++)
{
try
{
chargeIcons[i] = LoadIcon("icos\\" + i + ".ico");
}
catch (Exception)
{
Console.WriteLine("At least one Icon was not found (" + i + ".ico). Process exiting.");
ExitProgram();
}
}
chargeIcons[101] = LoadIcon("icos\\Headset.ico");
notifyIcon.Text = BASE_ICON_TEXT + "\n" + "Waiting for connection...";
Forms.ContextMenuStrip trayMenu = new Forms.ContextMenuStrip();
batLabel = new Forms.ToolStripLabel("Battery Level: Unknown");
trayMenu.Items.Add(batLabel);
Forms.ToolStripMenuItem exitItem = new Forms.ToolStripMenuItem();
exitItem.Text = "Exit";
exitItem.Click += new EventHandler(ExitProgram);
trayMenu.Items.Add(exitItem);
notifyIcon.ContextMenuStrip = trayMenu;
notifyIcon.Icon = chargeIcons[0];
notifyIcon.Visible = true;
}
private Icon LoadIcon(string path)
{
try
{
return new Icon(path);
}
catch (Exception)
{
Console.WriteLine("At least one Icon was not found ({0}). Process exiting.", path);
ExitProgram();
return null; // unreachable
}
}
private void InitHIDDev()
{
try
{
headset = new CloudFlightHeadset();
}
catch (Exception e)
{
Console.WriteLine("Cloud Flight Device not found! Message: " + e.Message);
}
}
private void ExitProgram(object sender = null, EventArgs e = null)
{
Application.Current.Shutdown();
}
}
}