-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMainWindow.xaml.cs
193 lines (173 loc) · 6.43 KB
/
MainWindow.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Threading.Tasks;
using System.Windows;
namespace MediaSensor
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Core Core { get; }
private ConfigurationReader Configuration { get; }
private Sensor Sensor { get; }
public MainWindow()
{
InitializeComponent();
this.Configuration = new ConfigurationReader();
this.Sensor = new Sensor();
var apiEndpoint = new ApiEndpoint(this.Sensor, this.Configuration);
this.Core = new Core(this.Configuration, apiEndpoint, this.Sensor);
this.Core.StateUpdated += OnStateUpdated;
Application.Current.SessionEnding += OnSessionEnding;
this.Closing += OnWindowClosing;
this.PreviewKeyDown += OnPreviewKeyDown;
Task.Run(async () =>
{
try
{
await this.InitializeAsync();
}
catch (Exception ex)
{
this.HandleException(ex);
}
});
}
private async Task InitializeAsync()
{
this.Configuration.Initialize();
if (this.Configuration.Initialized == false)
{
// Configuration was just created. There is no point continuing.
throw new InvalidOperationException($"Please update {ConfigurationReader.ConfigurationFileName}");
}
await this.Core.InitializeAsync();
await this.Core.UpdateEndpointAsync();
}
/// <summary>
/// Reacts to new state by updating the UI
/// </summary>
private void OnStateUpdated(object? sender, UpdateArgs e)
{
if (e.Exception == null)
UpdateUi(e);
else
HandleException(e.Exception);
}
/// <summary>
/// Updates the text labels in the UI. This method may be called from any thread.
/// </summary>
/// <param name="args"><see cref="UpdateArgs"/> which contain the updated state.</param>
private void UpdateUi(UpdateArgs args)
{
if (System.Windows.Threading.Dispatcher.CurrentDispatcher != this.Dispatcher)
{
this.Dispatcher.BeginInvoke((Action)(() => this.UpdateUi(args)));
return;
}
this.StatusText.Text = args.MediaState switch
{
MediaState.Stopped => "Media: Stopped",
MediaState.Standby => "Media: Standby",
MediaState.Playing => "Media: Playing",
_ => "Media: error",
};
this.OverridingText.Text = args.OverrideState switch
{
TargetState.Off => "Switch: Off",
TargetState.On => "Switch: On",
TargetState.FromMedia => "Switch: Auto",
TargetState.Shutdown => "Switch: On for just a minute",
_ => "Switch: error",
};
this.StatusText.Visibility = this.Configuration.SoundSensor
? Visibility.Visible
: Visibility.Collapsed;
}
/// <summary>
/// Handle keyboard gestures, whether the button is focused or not
/// </summary>
private void OnPreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
switch (e.Key)
{
case System.Windows.Input.Key.Space:
// Space toggles the button
RunAsyncSafely(async () => await this.Core.ToggleOverrideAsync());
e.Handled = true;
break;
case System.Windows.Input.Key.Enter:
// Enter toggles the button and minimizes
RunAsyncSafely(async () => await this.Core.ToggleOverrideAsync());
this.WindowState = WindowState.Minimized;
e.Handled = true;
break;
case System.Windows.Input.Key.Escape:
// Escape minimizes
e.Handled = true;
this.WindowState = WindowState.Minimized;
break;
default:
e.Handled = false;
break;
}
}
/// <summary>
/// Handle mouse gesture for the button
/// </summary>
private void OnOverrideButtonClick(object sender, RoutedEventArgs e)
{
RunAsyncSafely(async () => await this.Core.ToggleOverrideAsync());
}
/// <summary>
/// Turn off the light after delay when user closes the app
/// </summary>
private void OnWindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
RunAsyncSafely(async () => await this.Core.ShutdownAsync())
.Wait();
}
/// <summary>
/// Turn off the light after delay when OS closes the app
/// </summary>
private void OnSessionEnding(object sender, SessionEndingCancelEventArgs e)
{
RunAsyncSafely(async () => await this.Core.ShutdownAsync())
.Wait();
}
/// <summary>
/// Dispatches async method to run. Returns immediately. Handles exceptions.
/// </summary>
private Task RunAsyncSafely(Func<Task> action)
{
return Task.Run(async () =>
{
try
{
await action();
}
catch (Exception ex)
{
this.HandleException(ex);
}
});
}
/// <summary>
/// Handle exception by permanently disabling the sensor and displaying the message.
/// </summary>
private void HandleException(Exception ex)
{
// Immediately stop further updates
this.Sensor.Stop();
// Switch to UI thread if needed
if (System.Windows.Threading.Dispatcher.CurrentDispatcher != this.Dispatcher)
{
this.Dispatcher.BeginInvoke((Action)(() => this.HandleException(ex)));
return;
}
// If possible, update the UI
this.StatusText.Text = ex.Message;
}
}
}