-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.htm
94 lines (83 loc) · 2.63 KB
/
setup.htm
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Setup WiFi</title>
</head>
<body style="width:300px">
<h1>Setup WiFi</h1>
<div style="display:grid;grid-template-columns: 10ch auto;grid-gap:1ch;">
<label>Devicename:</label><span id="d">.</span>
<label>Network:</label><span><select id="n" style="width:12em">
<option selected disabled>scanning...</option>
</select></span>
<label>Passphrase:</label><input id="pass" type="password" style="width:12em"></input>
<label>format:</label><span><input id="fmt" type="checkbox"></input></span>
</div>
<div style="text-align: right;"><button id="b">Connect</button></div>
<script>
var d = document;
var s = 0; // state = 0: start, 1: get sysinfo, 2: sysinfo-done, 3: scan, 4: scan-done, 5: connect issued
var timer;
var oSel = d.getElementById('n');
var oBtn = d.getElementById('b');
var dn;
async function check() {
let r, t;
if (s == 0) {
// start sysinfo
s = 1;
r = await fetch('/api/sysinfo');
t = await r.text();
if (t.length > 0) {
dn = JSON.parse(t).devicename;
d.getElementById('d').textContent = dn;
s = 2;
} // if
} else if (s == 2) {
// start scan
s = 3;
r = await fetch('/api/scan');
t = await r.text();
if (t.length == 0) {
s = 2;
} else {
scanned(JSON.parse(t));
s = 4;
}
} else if (s == 4) {
window.clearInterval(timer);
timer = 0;
} // if
} // check()
function scanned(netList) {
oSel.innerHTML = '';
var o = d.createElement('option');
o.value = 0;
o.text = 'select...';
o.disabled = true;
oSel.options.add(o);
netList.forEach(function(n) {
var o = d.createElement('option');
o.value = o.text = n.id;
oSel.options.add(o);
});
} // scanned()
// connect using network name and password
oBtn.addEventListener('click', async () => {
if (s == 4) {
let url = `/api/connect?n=${oSel.value}&p=${d.getElementById('pass').value}`;
if (d.getElementById('fmt').checked) {
url += 'f=1';
}
try { await fetch(url); } catch (e) { }
s = 5;
oBtn.textContent = ">>>";
} else if (s == 5) {
location.href = `//${dn}/$update.htm`;
}
});
timer = window.setInterval(check, 300);
</script>
</body>