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
|
function isObject(o) {
return Object.prototype.toString.call(o) === '[object Object]';
}
function ge(id) {
return document.getElementById(id)
}
function cancelEvent(evt) {
if (evt.preventDefault) evt.preventDefault();
if (evt.stopPropagation) evt.stopPropagation();
evt.cancelBubble = true;
evt.returnValue = false;
return false;
}
function errorText(e) {
return e instanceof Error ? e.message : e+''
}
(function() {
function request(method, url, data, callback) {
data = data || null;
if (typeof callback != 'function') {
throw new Error('callback must be a function');
}
if (!url)
throw new Error('no url specified');
switch (method) {
case 'GET':
if (isObject(data)) {
for (var k in data) {
if (data.hasOwnProperty(k))
url += (url.indexOf('?') === -1 ? '?' : '&')+encodeURIComponent(k)+'='+encodeURIComponent(data[k])
}
}
break;
case 'POST':
if (isObject(data)) {
var sdata = [];
for (var k in data) {
if (data.hasOwnProperty(k))
sdata.push(encodeURIComponent(k)+'='+encodeURIComponent(data[k]));
}
data = sdata.join('&');
}
break;
}
var xhr = new XMLHttpRequest();
xhr.open(method, url);
if (method === 'POST')
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if ('status' in xhr && !/^2|1223/.test(xhr.status))
throw new Error('http code '+xhr.status)
callback(null, JSON.parse(xhr.responseText));
}
};
xhr.onerror = function(e) {
callback(e, null);
};
xhr.send(method === 'GET' ? null : data);
return xhr;
}
window.ajax = {
get: request.bind(request, 'GET'),
post: request.bind(request, 'POST')
}
})();
function lock(el) {
el.setAttribute('disabled', 'disabled');
}
function unlock(el) {
el.removeAttribute('disabled');
}
function initNetworkSettings() {
function setupField(el, value) {
if (value !== null)
el.value = value;
unlock(el);
}
var doneRequestsCount = 0;
function onRequestDone() {
doneRequestsCount++;
if (doneRequestsCount === 2) {
ge('loading_label').style.display = 'none';
}
}
var form = document.forms.network_settings;
form.addEventListener('submit', function(e) {
if (!form.nid.value.trim()) {
alert('Введите node id');
return cancelEvent(e);
}
if (form.psk.value.length < 8) {
alert('Неверный пароль (минимальная длина - 8 символов)');
return cancelEvent(e);
}
if (form.ssid.selectedIndex == -1) {
alert('Не выбрана точка доступа');
return cancelEvent(e);
}
lock(form.submit)
})
form.show_psk.addEventListener('change', function(e) {
form.psk.setAttribute('type', e.target.checked ? 'text' : 'password');
});
form.ssid.addEventListener('change', function(e) {
var i = e.target.selectedIndex;
if (i !== -1) {
var opt = e.target.options[i];
if (opt)
form.psk.value = '';
}
});
ajax.get('/status', {}, function(error, response) {
try {
if (error)
throw error;
setupField(form.nid, response.node_id || null);
setupField(form.psk, null);
setupField(form.submit, null);
onRequestDone();
} catch (error) {
alert(errorText(error));
}
});
ajax.get('/scan', {}, function(error, response) {
try {
if (error)
throw error;
form.ssid.innerHTML = '';
for (var i = 0; i < response.list.length; i++) {
var ssid = response.list[i][0];
var rssi = response.list[i][1];
form.ssid.append(new Option(ssid + ' (' + rssi + ' dBm)', ssid));
}
unlock(form.ssid);
onRequestDone();
} catch (error) {
alert(errorText(error));
}
});
}
function initUpdateForm() {
var form = document.forms.update_settings;
form.addEventListener('submit', function(e) {
cancelEvent(e);
if (!form.file.files.length) {
alert('Файл обновления не выбран');
return false;
}
lock(form.submit);
var xhr = new XMLHttpRequest();
var fd = new FormData();
fd.append('file', form.file.files[0]);
xhr.upload.addEventListener('progress', function (e) {
var total = form.file.files[0].size;
var progress;
if (e.loaded < total) {
progress = Math.round(e.loaded / total * 100).toFixed(2);
} else {
progress = 100;
}
form.submit.innerHTML = progress + '%';
});
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
var response = JSON.parse(xhr.responseText);
if (response.result === 1) {
alert('Обновление завершено, устройство перезагружается');
} else {
alert('Ошибка обновления');
}
}
};
xhr.onerror = function(e) {
alert(errorText(e))
};
xhr.open('POST', e.target.action);
xhr.send(fd);
return false;
});
}
function initApp() {
initNetworkSettings();
initUpdateForm();
}
|