-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
260 lines (232 loc) · 8.6 KB
/
MainWindow.xaml.cs
File metadata and controls
260 lines (232 loc) · 8.6 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
using System;
using System.Threading.Tasks;
using System.Windows;
using Auth0.Windows;
using System.Windows.Interop;
using System.Configuration;
using System.Collections.ObjectModel;
using System.Net.Http;
namespace LockInitClient
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Auth0Client auth0 = new Auth0Client(ConfigurationManager.AppSettings["auth0:Domain"],
ConfigurationManager.AppSettings["auth0:ClientId"]);
private Auth0User loggedInUser;
private LockInitHandler handler;
private LockService lockService;
private bool isListingRegisteredLocks = false;
ObservableCollection<String> deviceIds = new ObservableCollection<String>();
ObservableCollection<String> logs = new ObservableCollection<String>();
public MainWindow()
{
InitializeComponent();
this.LogList.ItemsSource = logs;
}
private bool GetSelectedLock(out string device)
{
device = this.DiscoveredDevicesList.SelectedItem as string;
if (isListingRegisteredLocks && device != null)
{
return true;
}
else
{
AddStatus("Please List and select a lock");
return false;
}
}
private bool GetInitLock(out string device)
{
device = this.DiscoveredDevicesList.SelectedItem as string;
if (!isListingRegisteredLocks && device != null)
{
return true;
}
else
{
AddStatus("Please find new locks and select a lock");
return false;
}
}
private void OnLock(object sender, RoutedEventArgs e)
{
string deviceId;
if (GetSelectedLock(out deviceId))
{
this.lockService.LockDeviceAsync(deviceId, true, (succeeded, message, errCode) =>
{
if (succeeded)
{
this.AddStatus("locked device: " + message);
}
else
{
this.AddStatus(string.Format("lock device failed, error: {0}", errCode));
}
});
}
}
private void OnUnlock(object sender, RoutedEventArgs e)
{
string deviceId;
if (GetSelectedLock(out deviceId))
{
this.lockService.LockDeviceAsync(deviceId, false, (succeeded, message, errCode) =>
{
if (succeeded)
{
this.AddStatus("unlocked device: " + message);
}
else
{
this.AddStatus(string.Format("unlock device failed, error: {0}", errCode));
}
});
}
}
private void OnClearDevice(object sender, RoutedEventArgs e)
{
//TODO: should reset device as well? If so how is it safe to do that? From the
// serivce with encrpted command? Or should assume device reset happens via physical
// interaction with device?
string device;
if (GetSelectedLock(out device))
{
lockService.DeviceDeregistrationAsync(device, (succeeded, errCode) =>
{
if (succeeded)
{
this.AddStatus("deregistered device");
Dispatcher.BeginInvoke(new Action(() =>
{
this.deviceIds.Remove(device);
}));
}
else
{
this.AddStatus(string.Format("deregistered failed, error: {0}", errCode));
}
});
}
}
private void OnListDevices(object sender, RoutedEventArgs e)
{
lockService.ListLocksAync((succeeded, locks, errCode) =>
{
if (succeeded)
{
isListingRegisteredLocks = true;
Dispatcher.BeginInvoke(new Action(() =>
{
this.AddStatus("Got configured locks");
this.DeviceListTitle.Content = "Configured Locks";
this.deviceIds.Clear();
foreach(var lockid in locks)
{
this.deviceIds.Add(lockid);
}
}));
}
else
{
this.AddStatus("Init failed to service, error: " + errCode);
}
});
}
private void OnInitializeDevice(object sender, RoutedEventArgs e)
{
string device;
string mqttServer;
int mqttPort;
if (GetInitLock(out device))
{
if (GetServerAndPort(out mqttServer, out mqttPort))
{
lockService.DeviceRegistrationAsync(device, (succeeded, key, errCode) =>
{
if (succeeded)
{
this.AddStatus("initialized with service, setting device");
handler.InitDevice(device, mqttServer, mqttPort, key);
}
else
{
this.AddStatus("Init failed to service, error: " + errCode);
}
});
}
}
}
private void OnLogin(object sender, RoutedEventArgs e)
{
auth0.LoginAsync(new WindowWrapper(new WindowInteropHelper(this).Handle)).ContinueWith(loggedInResult =>
{
if (loggedInResult.IsFaulted)
{
AddStatus("failure: " + loggedInResult.Exception.InnerException.ToString());
}
else
{
loggedInUser = loggedInResult.Result;
AddStatus("\nlogged in to Auth0 as: "
+ (string)loggedInUser.Profile["email"] +
" id: " + (string) loggedInUser.Profile["user_id"]);
lockService = new LockService(loggedInUser, MqttServerText.Text + ":" + "3000");
this.LoginButton.Visibility = System.Windows.Visibility.Hidden;
InitiateDiscover();
this.InitializeButton.IsEnabled = true;
this.FindDevicesButton.IsEnabled = true;
this.ListCurrentLocksButton.IsEnabled = true;
this.ClearLockButton.IsEnabled = true;
}
},
TaskScheduler.FromCurrentSynchronizationContext());
}
private void OnFindDevices(object sender, RoutedEventArgs e)
{
isListingRegisteredLocks = false;
this.deviceIds.Clear();
handler.QueryDevices();
}
private void AddStatus(string status)
{
Dispatcher.BeginInvoke(new Action(() => {
logs.Insert(0, status);
}));
}
private void InitiateDiscover()
{
handler = new LockInitHandler(
device =>
{
Dispatcher.BeginInvoke(new Action(() => {
if (this.deviceIds.Contains(device) == false)
{
this.deviceIds.Add(device);
}
}));
},
AddStatus
);
this.DiscoveredDevicesList.ItemsSource = deviceIds;
handler.QueryDevices();
}
private bool GetServerAndPort(out string mqttServer, out int mqttPort)
{
string device = this.DiscoveredDevicesList.SelectedItem as string;
mqttServer = MqttServerText.Text;
var mqttPortStr = MqttPortText.Text;
bool isValidPort = int.TryParse(mqttPortStr, out mqttPort);
if (!string.IsNullOrWhiteSpace(mqttServer) && isValidPort)
{
return true;
}
AddStatus("Server and port not configured");
return false;
}
}
}