-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.html
449 lines (345 loc) · 13.4 KB
/
connect.html
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<html>
<body ontouchstart="">
<div id="content">
<title>%menu.connect%</title>
<script type="module">
window.deviceID = Essentials.getURLParams('id');
window.deviceIP = Essentials.getURLParams('ip');
if(deviceID != null && deviceIP != null)
{
PageManager.setHeader('%connect.network%', '%connect.choose_network%');
var main = document.createElement('div');
main.style.display = 'flex';
main.innerHTML = '<input id="back-btn-wifi" type="button" value="%menu.back%" onclick="Essentials.leavePage(' + "'/device?id=" + deviceID + "&settings'" + ')"><input id="scan-btn" class="gradient-blue loading-loop" style="margin-left: 20px;" type="button" value="%general.please_wait% .." onclick="scanWifi(this)">';
PageManager.setFooter(main);
PageManager.showFooter();
PageManager.switchPage('check-name', 'wifi', true);
pingESP();
}
else
{
PageManager.setHeader('%connect.rename%', '%connect.choose_name%');
var main = document.createElement('div');
main.style.display = 'flex';
main.innerHTML = '<input type="button" value="%menu.back%" onclick="Essentials.leavePage(' + "'/'" + ')"><div style="display: block; width: 100%; margin-left: 20px"><input id="check-name-btn" type="submit" form="name-form" value="%menu.forward%"></div>';
PageManager.setFooter(main);
PageManager.showFooter();
}
Preloader.finish();
</script>
<div id="check-name" class="page">
<form id="name-form" class="main" onsubmit="event.preventDefault(); setName();">
<p style="margin-top: 0"><b>%general.name%: <input id="device-name" autofocus pattern="[a-zA-ZäÄöÖüÜß0-9-#()&/ ]+" title="%pattern.letters_numbers_spaces_special%: # / ( )" required name="name" type="text" style="max-width: 325px"></b></p>
<p style="margin: 100px 0">%connect.name_choise%</p>
</form>
</div>
<div id="connect" class="page hidden">
<div class="main" style="margin-bottom: 132px">
<img src="/img/wifi.png" class="setup-png">
<input class="gradient-blue loading-loop" style="margin: 100px auto 100px auto; max-width: 400px!important; display: block; pointer-events: none" type="button" value="%connect.searching_for_device% ..">
<p>%connect.connect_to_ap%</p>
</div>
</div>
<div id="wifi" class="page hidden">
<div class="main">
<form onsubmit="event.preventDefault(); sendWiFiSettings();">
<div id="networks">
<p class="loading-loop">%general.loading% ..</p>
</div>
<input type="password" id="wifipass" placeholder="%connect.wifi_password%" style="margin-bottom: 20px" required>
<br>
<input id="connect-btn" type="submit" value="%connect.connect%">
</form>
</div>
</div>
<div id="connecting" class="page hidden">
<div class="main">
<img src="/img/connecting.png" class="setup-png">
<div style="background: rgb(15, 15, 25); width: calc(100% - 100px); margin-top: 0px; margin-bottom: 100px; margin-right: 50px; margin-left: 50px; border-radius: 5px">
<div class="progress" style="background: hsl(210, 85%, 65%); width: 0%; height: 10px; border-radius: 5px"></div>
</div>
<p>%connect.trying_to_connect%</p>
</div>
</div>
<div id="success" class="page hidden">
<div class="main">
<img src="/img/success.png" class="setup-png">
<input class="gradient-green loading-loop" style="margin: 100px auto 100px auto; max-width: 400px!important; display: block; pointer-events: none" type="button" value="%general.waiting_for_bridge% ..">
<p>%connect.device_ready%!</p>
<p>%connect.automated_redirect%.</p>
</div>
</div>
<div id="error" class="page hidden">
<div class="main">
<img src="/img/error.png" class="setup-png">
<input class="gradient-red loading-loop" style="margin: 100px auto 100px auto; max-width: 400px!important; display: block; pointer-events: none" type="button" value="%general.waiting_for_bridge% ..">
<p>%connect.please_retry%!</p>
<p>%connect.incorrect%</p>
</div>
</div>
<div id="check-connection" class="page hidden">
<div class="main">
<img src="/img/success.png" class="setup-png">
<input class="gradient-yellow loading-loop" style="margin: 100px auto 100px auto; max-width: 400px!important; display: block; pointer-events: none" type="button" value="%general.waiting_for_bridge% ..">
<p>%connect.device_connecting%!</p>
<p>%connect.retry%</p>
</div>
</div>
<script>
// NOTE: Set Name
window.deviceName = null;
function setName()
{
deviceName = document.getElementById('device-name').value;
PageManager.setHeader('%connect.connect%', '%connect.with_smart_home_device%');
var main = document.createElement('div');
if(deviceID != null & deviceIP != null)
{
main.innerHTML = '<input id="back-btn" type="button" value="%menu.back%" onclick="Essentials.leavePage(' + "'/device?id=" + deviceID + "&settings'" + ')">';
}
else
{
main.innerHTML = '<input id="back-btn" type="button" value="%menu.back%" onclick="Essentials.leavePage(' + "'/'" + ')">';
}
PageManager.setFooter(main);
PageManager.showFooter();
PageManager.switchPage('check-name', 'connect', false);
pingESP();
}
// NOTE: Search for Device
window.pingActive = false;
window.deviceURL = 'http://192.168.4.1/';
async function pingESP()
{
if(!pingActive)
{
if(deviceIP != null)
{
Query.fetchURL('http://' + deviceIP + '/', 2000).then((echo) => {
if(echo != null)
{
pingActive = true;
deviceURL = 'http://' + deviceIP + '/';
scanWifi(document.getElementById('scan-btn'));
}
});
}
Query.fetchURL('http://192.168.4.1/', 2000).then((echo) => {
if(echo != null)
{
pingActive = true;
console.log('PING SUCCESS', deviceURL);
PageManager.setHeader('%connect.network%', '%connect.choose_network%');
var main = document.createElement('div');
main.style.display = 'flex';
if(deviceID != null & deviceIP != null)
{
main.innerHTML = '<input id="back-btn-wifi" type="button" value="%menu.back%" onclick="Essentials.leavePage(' + "'/device?id=" + deviceID + "&settings'" + ')"><input id="scan-btn" class="gradient-blue loading-loop" style="margin-left: 20px;" type="button" value="%general.please_wait% .." onclick="scanWifi(this)">';
}
else
{
main.innerHTML = '<input id="back-btn-wifi" type="button" value="%menu.back%" onclick="Essentials.leavePage(' + "'/'" + ')"><input id="scan-btn" class="gradient-blue loading-loop" style="margin-left: 20px;" type="button" value="%general.please_wait% .." onclick="scanWifi(this)">';
}
PageManager.setFooter(main);
PageManager.showFooter();
PageManager.switchPage('connect', 'wifi', false);
setTimeout(() => scanWifi(document.getElementById('scan-btn')), 2000);
}
else
{
setTimeout(() => pingESP(), 2000);
}
});
}
}
// NOTE: Scan WiFi Networks
var searching = false;
async function scanWifi(btn)
{
if(!searching)
{
searching = true;
if(document.getElementById('scan-btn') != null)
{
document.getElementById('scan-btn').value = '%general.search%';
document.getElementById('scan-btn').classList.remove('pulse');
}
document.getElementById('networks').innerHTML = '<p class="loading-loop">%general.loading% ..</p>';
var network = await Query.complexFetch(deviceURL + 'wifi', 10000, 3, {}, false);
try
{
network = JSON.parse(network);
}
catch(e)
{
network = null;
console.error(e);
}
if(network != null && network.available.length > 0)
{
document.getElementById('networks').innerHTML = '';
var found = false;
network.available.sort((a, b) => (a.rssi < b.rssi) ? 1 : (a.rssi > b.rssi) ? -1 : 0);
for(const i in network.available)
{
var label = document.createElement('label');
label.setAttribute('for', 'wifi-' + i);
label.setAttribute('style', 'margin-bottom: 20px; text-align: left; cursor: pointer');
var input = document.createElement('input');
input.setAttribute('id', 'wifi-' + i);
input.setAttribute('type', 'radio');
input.setAttribute('name', 'wifissid');
input.setAttribute('value', network.available[i].ssid);
if(!found && network.connected != null && network.available[i].ssid == network.connected.ssid)
{
input.setAttribute('checked', '');
found = true;
}
label.appendChild(input);
label.innerHTML += network.available[i].ssid + ' • ' + Math.round(100 - (network.available[i].rssi * network.available[i].rssi) / 100) + '%';
document.getElementById('networks').appendChild(label);
}
}
else if(network == null)
{
document.getElementById('networks').innerHTML = '<p>%connect.device_connection_error%</p>';
if(document.getElementById('scan-btn') != null)
{
document.getElementById('scan-btn').classList.add('pulse');
}
}
else
{
document.getElementById('networks').innerHTML = "<p>%connect.no_networks%<br>%connect.press_search_button%</p>";
if(document.getElementById('scan-btn') != null)
{
document.getElementById('scan-btn').classList.add('pulse');
}
}
if(document.getElementById('scan-btn') != null)
{
document.getElementById('scan-btn').value = '%general.search%';
}
searching = false;
}
}
// NOTE: Send WiFi Configuration to Device
window.bridgeIP = '<%bridge-ip%>';
window.success = false;
async function sendWiFiSettings()
{
var formData = {};
if(bridgeIP != 'null')
{
formData['bridge-ip'] = 'http://' + bridgeIP;
}
if(deviceName != null)
{
formData['device-name'] = deviceName;
}
if(document.getElementById('wifipass').value != '')
{
formData['wifipass'] = document.getElementById('wifipass').value;
}
var networks = document.getElementById('networks').getElementsByTagName('input');
for(var i = 0; i < networks.length; i++)
{
if(networks[i].checked)
{
formData['wifissid'] = networks[i].value;
}
}
if(formData['bridge-ip'] != null && formData['wifissid'] != null && formData['wifipass'] != null)
{
var overlays = {
root : document.getElementById('connect-btn'),
id : 'connecting',
pending : { value : '%general.please_wait% ..' },
connectionError : { value : '%connect.device_connection_error%!' }
};
var mac = await Query.complexFetch(deviceURL + 'settings', 20000, 1, overlays, false, JSON.stringify(formData));
if(mac != null)
{
if(deviceID != null && deviceIP != null)
{
PageManager.setHeader('%general.sent%', '%connect.test_connection%');
PageManager.hideFooter();
PageManager.switchPage('wifi', 'check-connection', false);
redirectCountdown('/device?id=' + mac);
}
else
{
PageManager.setHeader('%general.please_wait%', '%connect.test_connection%');
PageManager.hideFooter();
PageManager.switchPage('wifi', 'connecting', false);
setTimer(60000);
var ping = false;
setTimeout(async function()
{
if(await Query.fetchURL('/serverside/version', 10000) != null)
{
ping = true;
}
}, 20000);
setTimeout(function()
{
if(ping == false)
{
document.getElementById('connecting').getElementsByTagName('p')[0].innerHTML = '%connect.connect_to_wifi%';
}
}, 30000);
setTimeout(function()
{
if(success == false)
{
PageManager.setHeader('%general.error%', '%general.no_connection%');
PageManager.hideFooter();
PageManager.switchPage('connecting', 'error', false);
redirectCountdown('/connect');
}
}, 60000);
checkDB(mac);
}
console.log('MAC', mac);
}
}
return false;
}
// NOTE: Connecting Progress Bar
window.timeRemaining = 0;
async function setTimer(time)
{
timeRemaining = time - 100;
await Essentials.newTimeout(100);
document.getElementById('connecting').getElementsByClassName('progress')[0].style.width = (time / 600) + '%';
setTimer(timeRemaining);
}
// NOTE: Device Setup Check
async function checkDB(id)
{
if(await Query.fetchURL('/serverside/check-device?id=' + id, 5000) == 'Success')
{
success = true;
PageManager.setHeader('%connect.success%', '%connect.connect_success%');
PageManager.hideFooter();
PageManager.switchPage('connecting', 'success', false);
redirectCountdown('/device?id=' + id);
}
else
{
setTimeout(() => checkDB(id), 2000);
}
}
// NOTE: Redirect Delayed
async function redirectCountdown(url)
{
await Essentials.newTimeout(4000);
if(await Essentials.checkRestart('/serverside/check-restart'))
{
Essentials.leavePage(url);
}
}
</script>
</div>
</body>
</html>