-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
420 lines (332 loc) · 15 KB
/
script.js
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
const navigateToFormStep = (stepNumber) => {
document.querySelectorAll(".form-step").forEach((formStepElement) => {
formStepElement.classList.add("d-none");
});
document.querySelectorAll(".form-stepper-list").forEach((formStepHeader) => {
formStepHeader.classList.add("form-stepper-unfinished");
formStepHeader.classList.remove("form-stepper-active", "form-stepper-completed");
});
document.querySelector("#step-" + stepNumber).classList.remove("d-none");
const formStepCircle = document.querySelector('li[step="' + stepNumber + '"]');
formStepCircle.classList.remove("form-stepper-unfinished", "form-stepper-completed");
formStepCircle.classList.add("form-stepper-active");
for (let index = 0; index < stepNumber; index++) {
const formStepCircle = document.querySelector('li[step="' + index + '"]');
if (formStepCircle) {
formStepCircle.classList.remove("form-stepper-unfinished", "form-stepper-active");
formStepCircle.classList.add("form-stepper-completed");
}
}
};
document.querySelectorAll(".btn-navigate-form-step").forEach((formNavigationBtn) => {
formNavigationBtn.addEventListener("click", () => {
const stepNumber = parseInt(formNavigationBtn.getAttribute("step_number"));
navigateToFormStep(stepNumber);
});
});
var Logger = {
log: function () {
var line = Array.prototype.slice.call(arguments).map(function (argument) {
return typeof argument === 'string' ? argument : JSON.stringify(argument);
}).join(' ');
document.querySelector('#log').textContent += '> ' + line + '\n';
},
clearLog: function () {
document.querySelector('#log').textContent = '';
},
};
var argon_ble_device = null; // BLE connection instance
var ssids = [];
const uuid_wifi_config_service = 'b4ad5b8d-d2db-44d6-9d35-5d43b9e5060c';
const uuid_wifi_config_tx = '3c673f3a-382a-4835-8433-c1c1b6b65346';
const uuid_wifi_config_rx = '226285d5-7a5a-448d-8317-dae1fd2d6c36';
var encoder = new TextEncoder();
function refreshPage() {
// Reload the current page
location.reload();
}
// Function to display the help window
function showHelp() {
document.getElementById('help_window').style.display = 'block';
}
// Function to close the help window
function closeHelp() {
document.getElementById('help_window').style.display = 'none';
}
// Attach the showHelp function to the click event of the help button
document.getElementById('help_btn').addEventListener('click', showHelp);
function handleConnectButtonClick() {
document.getElementById('userAccountSetupForm').submit();
}
function onScanButtonClick() {
if (argon_ble_device) {
document.getElementById('loader_show').style.display = 'block';
argon_ble_device.gatt.getPrimaryService(uuid_wifi_config_service)
// Set up notifications
.then(service => {
// Logger.log('Getting WiFi Configuration Service...');
service.getCharacteristic(uuid_wifi_config_rx)
.then(characteristic => {
characteristic.startNotifications();
// Logger.log('RX notifications enabled');
return characteristic;
})
.then(characteristic => {
characteristic.addEventListener('characteristicvaluechanged', rxDataHandler);
// Logger.log('Added \'rx\' service listener');
})
.catch(error => {
document.getElementById('loader_show').style.display = 'none';
document.getElementById('scan_try_again').style.display = 'block';
Logger.log('Try Again! ');
});
return service;
})
// Initiate WiFi scan
.then(service => {
return service.getCharacteristic(uuid_wifi_config_tx)
})
.then(characteristic => {
// Logger.log('Initiate WiFi Scan');
var scan_msg = {
msg_type: "scan",
}
var message_str = JSON.stringify(scan_msg);
var bytes = encoder.encode(message_str);
// Logger.log('TX: ' + JSON.stringify(scan_msg));
return characteristic.writeValue(bytes);
// return characteristic.writeValue(cmd_wifi_scan)
})
.catch(error => {
console.log('Try again-1')
document.getElementById('loader_show').style.display = 'none';
document.getElementById('scan_try_again').style.display = 'block';
Logger.log('Try Again! ');
})
} else {
console.log('Try again-2')
document.getElementById('loader_show').style.display = 'none';
document.getElementById('scan_try_again').style.display = 'block';
Logger.log('Try Again! Not connected to Nunam Device!');
}
}
function onSubmitCredentialsButtonClick() {
if (argon_ble_device) {
argon_ble_device.gatt.getPrimaryService(uuid_wifi_config_service)
.then(service => {
return service.getCharacteristic(uuid_wifi_config_tx)
})
// Send credentials to device
.then(characteristic => {
var credentials_msg = {
msg_type: "set_creds",
ssid: document.getElementById('ssid').value,
password: document.getElementById('password').value,
}
// Logger.log('Configure WiFi Credentials for network \"' + credentials_msg.ssid + '\"');
var message_str = JSON.stringify(credentials_msg);
var bytes = encoder.encode(message_str);
// Logger.log('TX: ' + JSON.stringify(credentials_msg));
var conn_wifi_message = credentials_msg.ssid
var ssid_pass = credentials_msg.password
document.getElementById('scan_wifi_next_btn').style.display = "block";
document.getElementById('config_complete').style.display = "block";
document.getElementById('scan_wifi_disable').style.display = "none";
var connectionMsgElement = document.getElementById('wifi_connection-msg');
connectionMsgElement.style.display = 'block';
connectionMsgElement.textContent = conn_wifi_message;
var connectionMsgElement = document.getElementById('wifi_pass');
connectionMsgElement.style.display = 'block';
connectionMsgElement.textContent = ssid_pass;
return characteristic.writeValue(bytes);
})
.catch(error => {
Logger.log('Try Again! ' + error.name);
})
} else {
Logger.log('Try Again! Not connected to Nunam Device!');
}
}
function onConnectButtonClick() {
Logger.log('Requesting Bluetooth Device...');
var message = ''
navigator.bluetooth.requestDevice({
filters: [{
services: [uuid_wifi_config_service], // Only show Argons that have the WiFi config service enabled (this doesn't work??)
// namePrefix: "Argon", // Argons advertise their name as "Argon-XXXXXX"
}],
})
.then(device => {
// Logger.log('Connecting to ' + device.name + '...');
message = 'Connected With Nunam Device: ' + device.name
// device.addEventListener('gattserverdisconnected', onDisconnected);
argon_ble_device = device;
return device.gatt.connect();
})
.then(server => {
// Logger.log('Connected!');
document.getElementById('form_wifi_scan').style.display = "block";
document.getElementById('form_wifi_config').style.display = "block";
// document.getElementById('wifi_scan_btn').disabled = false;
// document.getElementById('connect_btn').value = "Disconnect from Argon";
var connectionMsgElement = document.getElementById('connection-msg');
connectionMsgElement.style.display = 'block';
connectionMsgElement.textContent = message;
document.getElementById('permissions_text').style.display = "none";
document.getElementById('form_connect').style.display = "none";
document.getElementById('first_next_btn').style.display = "block";
Logger.clearLog();
})
.catch(error => {
Logger.log('Try Again! ' + error.name);
argon_ble_device = null;
});
}
function onDisconnectButtonClick() {
if (argon_ble_device) {
Logger.log('Disconnecting...');
argon_ble_device.gatt.disconnect();
argon_ble_device = null;
// Clear SSID list
var ssid_list = document.getElementById("ssid");
while (ssid_list.firstChild) {
ssid_list.removeChild(ssid_list.firstChild);
}
var password = document.getElementById("password");
password.value = "";
// document.getElementById('connect_btn').value = "Connect to Argon";
ssids = [];
console.log(ssids)
console.log(password.value)
console.log(argon_ble_device)
}
}
// Handler for when data is received from the BLE device
function rxDataHandler(event) {
var decoder = new TextDecoder("utf-8");
var result_str = decoder.decode(event.target.value);
try {
var obj = JSON.parse(result_str);
if (obj) {
if (obj.msg_t == "scan_resp") {
// Logger.log('RX: \"' + result_str + '\"');
if (obj.ssid != "") {
var ssid_list = document.getElementById("ssid");
var ssid_string = obj.ssid + ' (' + obj.sec + ')';
ssid_list.appendChild(new Option(ssid_string, obj.ssid));
ssids.push(obj);
if (ssids.length < 1) {
Logger.log('Device not found. Try Again! ');
}
// Hacky fix to only enable SSID and Password fields after we get an SSID response from the Argon
if (ssids.length == 1) { // Added our first SSID to the list
// Enable SSID and Password fields on the web form
document.getElementById('ssid').disabled = false;
document.getElementById('password').disabled = false;
document.getElementById('ssid').dispatchEvent(new Event('change')); // Force update the submit button
document.getElementById('after_scan_wifi').style.display = "block";
document.getElementById('form_wifi_scan').style.display = "none";
document.getElementById('loader_show').style.display = 'none';
document.getElementById('scan_try_again').style.display = 'none';
}
}
else {
Logger.log('Device not found. Try Again! ');
}
} else {
Logger.log('Try Again!!');
}
}
} catch (error) {
Logger.log('Try Again! ' + error.name);
}
}
function onDisconnected(event) {
const device = event.target;
// Logger.log(`Device ${device.name} is disconnected.`);
argon_ble_device = null;
resetUI();
}
function resetUI() {
document.getElementById('form_wifi_scan').style.display = "none";
document.getElementById('form_wifi_config').style.display = "none";
document.getElementById('ssid').disabled = true;
document.getElementById('password').disabled = true;
document.getElementById('submit_credentials_btn').disabled = true;
// document.getElementById('wifi_scan_btn').disabled = true;
// Clear SSID list
var ssid_list = document.getElementById("ssid");
while (ssid_list.firstChild) {
ssid_list.removeChild(ssid_list.firstChild);
}
var password = document.getElementById("password");
password.value = "";
// document.getElementById('connect_btn').value = "Connect to Argon";
ssids = [];
}
// Connect to Argon button handler
document.querySelector('#form_connect').addEventListener('click', function (event) {
event.stopPropagation();
event.preventDefault();
if (navigator.bluetooth) {
if (argon_ble_device) {
// Disconnect
onDisconnectButtonClick();
} else {
// Connect
onConnectButtonClick();
}
} else {
// Logger.log('Error: Web Bluetooth API is not available. Please make sure the "Experimental Web Platform features" flag is enabled.');
Logger.log('Web Bluetooth API is not available in your Device.');
}
});
// Scan for networks button handler
document.querySelector('#form_wifi_scan').addEventListener('click', function (event) {
event.stopPropagation();
event.preventDefault();
// Reset UI State if button is clicked (don't want to duplicate networks)
// Logger.clearLog();
document.getElementById('ssid').disabled = true;
document.getElementById('password').disabled = true;
document.getElementById('submit_credentials_btn').disabled = true;
// Clear list
var ssid_list = document.getElementById("ssid");
while (ssid_list.firstChild) {
ssid_list.removeChild(ssid_list.firstChild);
}
ssids = [];
var password = document.getElementById("password");
password.value = "";
onScanButtonClick();
});
// Submit credentials button handler
document.querySelector('#form_wifi_config').addEventListener('click', function (event) {
event.stopPropagation();
event.preventDefault();
// Logger.clearLog();
onSubmitCredentialsButtonClick();
});
// Handler to only enable submit credentials button when password length >0
document.getElementById('password').addEventListener('input', function (event) {
if (document.getElementById('ssid').options.length > 0 && document.getElementById('password').value.length > 0) {
document.getElementById('submit_credentials_btn').disabled = false;
} else {
document.getElementById('submit_credentials_btn').disabled = true;
}
});
document.getElementById('ssid').addEventListener('change', function (event) {
var selected_ssid = ssids[event.target.options.selectedIndex];
if (selected_ssid.sec == "Unsecured") {
document.getElementById('submit_credentials_btn').disabled = false;
document.getElementById('password_p').style.display = "none";
document.getElementById('password').value = "";
} else {
document.getElementById('password_p').style.display = "block";
if (document.getElementById('ssid').options.length > 0 && document.getElementById('password').value.length > 0) {
document.getElementById('submit_credentials_btn').disabled = false;
} else {
document.getElementById('submit_credentials_btn').disabled = true;
}
}
});