-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.htm
287 lines (247 loc) · 8.25 KB
/
monitor.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
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>IoT HomeDing Monitor</title>
<link rel="icon" type="image/png" href="/favicon48.png" sizes="48x48">
<link Content-Type="text/css" href="/iotstyle.css" rel="stylesheet" />
<script defer src="/micro.js"></script>
<script defer src="/monitorTab.js"></script>
<style>
.dataTab {
border-collapse: collapse;
background-color: white;
border: solid 1px #203050;
padding: 0;
color: #111111;
}
.dataTab tr:nth-child(even) {
background: #ddd
}
.dataTab tr:nth-child(odd) {
background: #eee
}
.dataTab td,
.dataTab th {
border: solid 1px #203050;
padding: 0.25em 0.8em;
}
.dataTab thead th {
background-color: #203050;
color: white;
}
.dataTab thead th[sort] {
cursor: pointer;
}
.remote img {
width: 1em;
height: 1em;
}
</style>
</head>
<body class="sitelayout">
<header>
<svg class="icon">
<use href="./icons.svg#device" />
</svg>
<h1>IoT HomeDing Monitor</h1>
</header>
<main>
<table is="table-sortable" id="devices" class="dataTab">
<thead>
<tr>
<th>*</th>
<th sort>hostname</th>
<th sort>room</th>
<th sort>title</th>
<th>memory</th>
<th sort>core</th>
<th sort=date>date</th>
<th>uptime</th>
<th sort>flash</th>
<th>safemode</th>
<th>...</th>
</tr>
</thead>
<tbody></tbody>
</table>
</main>
<script>
window.onerror = function(msg, url, lineNo, columnNo, error) {
var string = msg.toLowerCase();
var substring = 'script error';
if (string.indexOf(substring) > -1) {
alert('Script Error: See Browser Console for Detail');
} else {
var message = [
'Message: ' + msg,
'URL: ' + url,
'Line: ' + lineNo,
'Column: ' + columnNo,
'Error object: ' + JSON.stringify(error)
].join(' - ');
alert(message);
}
return false;
};
</script>
<script>
const param = {
REFRESHSTATE: 12 * 1000, // refresh the state of all devices
REFRESHLIST: 5 * 60 * 1000, // refresh the list of devices
FETCHTIMEOUT: 3 * 1000
};
var devices;
var deviceInfo;
var needLoadDevices = true;
var remotes = []; // document.querySelectorAll('.remote');
var remoteIndex = 0;
var state = 0;
var tabObj = document.querySelector('#devices tbody');
tabObj.addEventListener('click', (evt) => {
/** @type HTMLElement */
let tar = evt.target;
if (tar.className == "delete") {
while (tar && (tar.tagName !== 'TR')) { tar = tar.parentElement; }
const host = tar.getAttribute('host');
if (deviceInfo[host])
delete deviceInfo[host];
tar.remove();
localStorage.setItem('devices', JSON.stringify(deviceInfo));
}
});
// duration in seconds to readable format
function fmtDuration(dur) {
var ret = '';
if (dur < 24 * 60 * 60) {
var d = new Date(dur * 1000);
ret = d.toISOString().split(/[T\.]/)[1];
} else {
var d = dur / (24 * 60 * 60);
ret = d.toFixed(1) + 'd';
}
return (ret);
}
function addDevice(name, data) {
// find existing row for device
var r = tabObj.querySelector('tr[host="' + name + '"]');
if (!r) {
var r = tabObj.insertRow(-1);
r.className = 'remote';
r.setAttribute('host', name);
var c;
createHTMLElement(r.insertCell(-1), 'img', { class: 'status', src: 'i/no.svg' });
createHTMLElement(r.insertCell(-1), 'a', { href: 'http://' + name }).innerText = name;
r.insertCell(-1).innerText = data.room || '';
c = r.insertCell(-1);
c.innerText = data.title;
c.title = JSON.stringify(data, null, 2);
r.insertCell(-1).className = 'memory';
r.insertCell(-1).className = 'core';
r.insertCell(-1).className = 'date';
c = r.insertCell(-1);
c.className = 'uptime';
c.style = "text-align:right";
r.insertCell(-1).className = 'flash';
r.insertCell(-1).className = 'safemode';
c = r.insertCell(-1);
c.className = 'delete';
createHTMLElement(c, 'img', { class: 'delete', src: 'i/remove.svg' });
} // if
} // addDevice()
function loadDevices() {
fetch('/api/discovery')
.then(function(result) {
return result.json();
})
.then(function(json) {
// all discovered devices are active
for (a in json) { json[a].active = true; }
// add old devices from localstorage.
var od = JSON.parse(localStorage.getItem('devices'));
for (e in od) {
if (!json[e]) {
// add known, inactive device
var o = od[e];
delete (o.ts);
delete (o.path);
o.active = false;
json[e] = o;
}
}
deviceInfo = json;
localStorage.setItem('devices', JSON.stringify(deviceInfo));
devices = Object.keys(json);
devices.sort();
for (var n = 0; n < devices.length; n++) {
if (devices[n]) addDevice(devices[n], json[devices[n]]);
}
window.setTimeout(updateNext, 10);
});
} // loadDevices()
function updateNext() {
if (remoteIndex === 0) {
if (needLoadDevices) {
needLoadDevices = false;
loadDevices();
return;
} // if
remotes = document.querySelectorAll('.remote');
} // if
if (remoteIndex >= remotes.length) {
remoteIndex = 0;
window.setTimeout(updateNext, param.REFRESHSTATE);
} else {
var obj = remotes[remoteIndex];
// var hostname = devices[remoteIndex];
var host = obj.cells[1].textContent;
var t = deviceInfo[host];
var statImg = obj.querySelector('img.status');
if (!t.active) {
statImg.src = 'i/stop.svg';
remoteIndex++;
window.setTimeout(updateNext, 50);
} else {
statImg.src = 'i/config.svg';
const controller = new AbortController();
window.setTimeout(function() { controller.abort(); }, param.FETCHTIMEOUT);
fetch('http://' + host + '/api/sysinfo', { signal: controller.signal })
.then(function(response) {
if (response.status === 200) {
return response.json();
}
})
.then(function(si) {
statImg.src = 'i/start.svg';
obj.querySelector('.memory').textContent = si.freeHeap;
obj.querySelector('.core').textContent = (si.coreVersion || '').replace('.ino', '') + ' ' + (si.coreBuild || '');
obj.querySelector('.date').textContent = new Date(si.build).toISOString().split(/[T\.]/)[0];
obj.querySelector('.uptime').textContent = fmtDuration(si.upTime);
obj.querySelector('.flash').textContent = si.flashSize;
obj.querySelector('.safemode').textContent = si.safemode;
})
.catch(function(err) {
statImg.src = 'i/stop.svg';
obj.querySelector('.memory').textContent = '-';
obj.querySelector('.uptime').textContent = '-';
obj.querySelector('.safemode').textContent = '-';
t.active = false;
})
.finally(function() {
remoteIndex++;
window.setTimeout(updateNext, 200);
});
} // if
} // if
} // updateNext()
window.addEventListener('load', function() {
state = 'l'; // load devices
window.setTimeout(updateNext, 400);
window.setInterval(function() {
needLoadDevices = true;
}, param.REFRESHLIST);
});
</script>
</body>
</html>