-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacklight.html
More file actions
452 lines (352 loc) · 14.6 KB
/
stacklight.html
File metadata and controls
452 lines (352 loc) · 14.6 KB
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
450
451
452
<html>
<head>
<title>APRS World Stacklights</title>
<link rel="stylesheet" href="/stacklight/res/pure-min.css">
<script src="/stacklight/res/mqttws31.js" type="text/javascript"></script>
<script src="/stacklight/res/howler.js"></script>
<script src="/stacklight/res/jquery-3.7.1.min.js"></script>
<script type = "text/javascript" language = "javascript">
/* parameters */
var host="192.168.10.188";
var port=9001;
var reconnectTimeout = 2000;
var topicPrefix='6300/';
var topicSuffix='/stacklight';
var machines=[
'LASERSHED',
'SHOP',
'MECHROOM',
'ROUTERROOM',
'VC500',
// 'QT250',
'QT15',
// 'SF6020T',
// 'STX3015',
'ENGRAVER',
'REFARM',
'OPTIMAT'
];
var periodicTalkSeconds=60;
const fZeroDecimal = new Intl.NumberFormat("en-US", {
style: "decimal",
minimumFractionDigits: 0, // Optional: Set minimum decimal places
maximumFractionDigits: 0, // Optional: Set maximum decimal places
});
const fOneDecimal = new Intl.NumberFormat("en-US", {
style: "decimal",
minimumFractionDigits: 0, // Optional: Set minimum decimal places
maximumFractionDigits: 1, // Optional: Set maximum decimal places
});
const fTwoDecimal = new Intl.NumberFormat("en-US", {
style: "decimal",
minimumFractionDigits: 0, // Optional: Set minimum decimal places
maximumFractionDigits: 2, // Optional: Set maximum decimal places
});
console.log('Machines: ' + machines);
var mqtt;
var sound = new Howl({src: ['/stacklight/res/sounds/BoomBoomPow_sample.mp3'], volume: 1.0, preload: true});
var last_power=new Array(machines.length).fill(-2);
var last_red=new Array(machines.length).fill(-2);
var last_yellow=new Array(machines.length).fill(-2);
var last_green=new Array(machines.length).fill(-2);
var last_blue=new Array(machines.length).fill(-2);
function lowercaseKeys(obj) {
return Object.fromEntries( Object.entries(obj).map(([key, value]) => [key.toLowerCase(), value]) );
}
function periodicTalk() {
if ( $("#periodicTalk").prop("checked") ) {
let utterance = new SpeechSynthesisUtterance("STACKLIGHT");
speechSynthesis.speak(utterance);
if ( 'vibrate' in navigator ) {
//navigator.vibrate([300,50,300]);
navigator.vibrate(1000);
console.log("vibrating");
} else {
console.log("device doesn't support vibrate");
}
} else {
console.log("checkbox not selected");
}
setTimeout(periodicTalk,periodicTalkSeconds*1000);
}
function onFailure(message) {
console.log("Connection Attempt to Host "+host+"Failed");
setTimeout(MQTTconnect, reconnectTimeout);
}
function onMessageArrived(msg){
var dataMixed = $.parseJSON(msg.payloadString);
var data=lowercaseKeys(dataMixed);
// console.log("MQTT message received: " + msg.payloadString);
/* dump data object */
// Object.entries(data).forEach(([key, value]) => {
// console.log(`${key}: ${value}`);
// });
var split = msg.destinationName.split("/");
console.log("split: " + split);
var machine = split[1];
var machineIndex = machines.indexOf(machine);
if ( split.length > 3 && "0" == split[3] ) {
console.log("speakerbox: non-status 0 sub message");
/* unhide the row */
// $("#" + machine + "_0").removeAttr('hidden');
$("#" + machine + "_0_table").removeAttr('hidden');
/* add / update time stamp */
$("#" + machine + "_0_date").html(new Date().toISOString().replace('T',' ').substring(0,19));
var html='';
html = "<b>Building:</b> " + fOneDecimal.format(data['t_a']*1.8 + 32.0) + "°F / ";
html += fZeroDecimal.format(data['r_a']) + "% RH";
$("#" + machine + "_0_left").html(html);
html = "<b>Nitrogen:</b> " + fZeroDecimal.format(data['p_n']) + " PSI" ;
$("#" + machine + "_0_middle").html(html);
html = "<b>Air:</b> " + fZeroDecimal.format(data['p_a']) + " PSI" ;
$("#" + machine + "_0_right").html(html);
/* add data dump */
// $("#" + machine + "_0" + " td:eq(0)").html(msg.payloadString);
return;
} else if ( 6 == split.length && "1" == split[3] && "status" == split[4] && "temperature:0" == split[5] ) {
console.log("MQTT message received from shelly of type temperature:0: " + msg.payloadString);
$("#" + machine + "_1_table").removeAttr('hidden');
/* add / update time stamp */
$("#" + machine + "_1_date").html(new Date().toISOString().replace('T',' ').substring(0,19));
if ( undefined === data['tf'] )
return;
var html = "<b>Temperature:</b> " + fOneDecimal.format(data['tf']) + "°F ";
$("#" + machine + "_1_left").html(html);
return;
} else if ( 6 == split.length && "1" == split[3] && "status" == split[4] && "humidity:0" == split[5] ) {
console.log("MQTT message received from shelly of type humidity:0: " + msg.payloadString);
$("#" + machine + "_1_table").removeAttr('hidden');
/* add / update time stamp */
$("#" + machine + "_1_date").html(new Date().toISOString().replace('T',' ').substring(0,19));
if ( undefined === data['rh'] )
return;
var html = "<b>Humidity:</b> " + fZeroDecimal.format(data['rh']) + "%";
$("#" + machine + "_1_middle").html(html);
return;
} else if ( 6 == split.length && "1" == split[3] && "status" == split[4] && "devicepower:0" == split[5] ) {
Object.entries(data).forEach(([key, value]) => {
console.log(`${key}: ${value}`);
});
// 6300/LASERSHED/stacklight/1/status/devicepower:0 {"id": 0,"battery":{"V":5.73, "percent":86},"external":{"present":true}}
console.log("MQTT message received from shelly of type devicepower:0: " + msg.payloadString);
$("#" + machine + "_1_table").removeAttr('hidden');
/* add / update time stamp */
$("#" + machine + "_1_date").html(new Date().toISOString().replace('T',' ').substring(0,19));
if ( undefined === data['battery'] )
return;
var html = "<b>Battery:</b> " + data['battery']['percent'] + "%";
var title = fTwoDecimal.format(data['battery']['V']) + " volts";
$("#" + machine + "_1_right").html(html);
$("#" + machine + "_1_right").attr('title',title);
return;
} else if ( 5 == split.length && "venstar" == split[4] ) {
console.log("MQTT message received from venstar gateway: " + msg.payloadString);
Object.entries(data).forEach(([key, value]) => {
console.log(`${key}: ${value}`);
});
$("#" + machine + "_" + split[3] + "_table").removeAttr('hidden');
/* add / update time stamp */
$("#" + machine + "_" + split[3] + "_date").html(new Date().toISOString().replace('T',' ').substring(0,19));
if ( undefined === data['temperaturef'] )
return;
var html = "<b>" + data['name'] + " Temperature:</b> " + fZeroDecimal.format(data['temperaturef']) + "°F";
$("#" + machine + "_" + split[3] + "_left").html(html);
var html = "<b>State / Fan:</b> " + data['state'] + " / " + data['fanstate'];
$("#" + machine + "_" + split[3] + "_middle").html(html);
var html = "<b>Heat / Cool:</b> " + fZeroDecimal.format(data['heattemperaturef']) + "°F / " + fZeroDecimal.format(data['cooltemperaturef']) + "°F";
$("#" + machine + "_" + split[3] + "_right").html(html);
return;
} else if ( 5 == split.length && "wn_" == split[4].toLowerCase().substring(0,3) ) {
console.log("MQTT message received from WattNode gateway: " + msg.payloadString);
Object.entries(data).forEach(([key, value]) => {
console.log(`${key}: ${value}`);
});
/* */
/* {"ENERGY":91732.0,"POWER":2785.2,"VLN":122.1,"VLL":244.3,"F":60.0} */
$("#" + machine + "_" + split[3] + "_table").removeAttr('hidden');
/* add / update time stamp */
$("#" + machine + "_" + split[3] + "_date").html(new Date().toISOString().replace('T',' ').substring(0,19));
if ( undefined === data['energy'] )
return;
var html = "<b>Energy:</b> " + fOneDecimal.format(data['energy']) + "KWh";
$("#" + machine + "_" + split[3] + "_left").html(html);
var html = "<bPower:</b> " + fZeroDecimal.format(data['power']) + " watts";
$("#" + machine + "_" + split[3] + "_middle").html(html);
var html = fOneDecimal.format(data['vln']) + " volts L-N<br/>" + fOneDecimal.format(data['vll']) + " volts L-L<br />" + fOneDecimal.format(data['f']) + " Hz";
$("#" + machine + "_" + split[3] + "_right").html(html);
return;
}
// console.log("Machine name from MQQT destinationName: '" + machine + "'");
// console.log("Machines index from MQQT destinationName: '" + machineIndex + "'");
if ( undefined !== data['red'] ) {
if ( -1 == last_red[machineIndex] && data['red'] >= 0 ) {
console.log('red state change');
let utterance = new SpeechSynthesisUtterance("THERE IS A FAULT ON " + machine + "!");
utterance.volume=1.0;
speechSynthesis.speak(utterance);
}
last_red[machineIndex]=data['red'];
}
if ( undefined !== data['yellow'] ) {
if ( -1 == last_yellow[machineIndex] && data['yellow'] >= 0 ) {
console.log('yellow state change');
if ( $("#soundFinished").prop("checked") && ! sound.playing() ) {
sound.play();
}
let utterance = new SpeechSynthesisUtterance("CYCLE DONE ON " + machine + "!");
utterance.volume=1.0;
speechSynthesis.speak(utterance);
}
last_yellow[machineIndex]=data['yellow'];
}
if ( undefined !== data['green'] ) {
if ( -1 == last_green[machineIndex] && data['green'] >= 0 ) {
console.log('green state change');
let utterance = new SpeechSynthesisUtterance("CYCLE STARTED ON " + machine + "!");
utterance.volume=1.0;
speechSynthesis.speak(utterance);
}
last_green[machineIndex]=data['green'];
}
if ( undefined !== data['power'] ) {
$("#" + machine + " td:eq(1)").html(data['power'].toLocaleString("en-US") + ' s');
}
if ( undefined !== data['red'] ) {
if ( -1 == data['red'] ) {
$("#" + machine +" td:eq(2)").css('background-color','#e0e0e0');
$("#" + machine +" td:eq(2)").html('');
} else {
$("#" + machine +" td:eq(2)").html(data['red'].toLocaleString("en-US") + ' s');
$("#" + machine +" td:eq(2)").css('background-color','red');
}
}
if ( undefined !== data['yellow'] ) {
if ( -1 == data['yellow'] ) {
$("#" + machine +" td:eq(3)").css('background-color','#e0e0e0');
$("#" + machine +" td:eq(3)").html('');
} else {
$("#" + machine +" td:eq(3)").html(data['yellow'].toLocaleString("en-US") + ' s');
$("#" + machine +" td:eq(3)").css('background-color','orange');
}
}
if ( undefined !== data['green'] ) {
if ( -1 == data['green'] ) {
$("#" + machine +" td:eq(4)").css('background-color','#e0e0e0');
$("#" + machine +" td:eq(4)").html('');
} else {
$("#" + machine +" td:eq(4)").css('background-color','green');
$("#" + machine +" td:eq(4)").html(data['green'].toLocaleString("en-US") + ' s');
}
}
if ( undefined !== data['blue'] ) {
if ( -1 == data['blue'] ) {
$("#" + machine +" td:eq(5)").css('background-color','#e0e0e0');
$("#" + machine +" td:eq(5)").html('');
} else {
$("#" + machine +" td:eq(5)").css('background-color','blue');
$("#" + machine +" td:eq(5)").html(data['blue'].toLocaleString("en-US") + ' s');
}
}
/* add time stamp */
$("#" + machine +" td:eq(6)").html(new Date().toISOString().replace('T',' ').substring(0,19));
}
function onConnect() {
console.log("Connected ");
/* subscribe to everything in machines array variable */
for ( let i=0 ; i<machines.length ; i++ ) {
mqtt.subscribe(topicPrefix + machines[i] + topicSuffix + "/#");
}
}
function MQTTconnect() {
console.log("connecting to "+ host +" "+ port);
var x=Math.floor(Math.random() * 10000);
var cname="orderform-"+x;
mqtt = new Paho.MQTT.Client(host,port,cname);
//document.write("connecting to "+ host);
var options = {
timeout: 3,
onSuccess: onConnect,
onFailure: onFailure,
};
mqtt.onMessageArrived = onMessageArrived
mqtt.connect(options); //connect
}
function onLoad() {
// $("#tm").width($(window).width());
/* add rows to the table */
for ( let i=0 ; i<machines.length ; i++ ) {
console.log("adding tr id=\"" + machines[i] + "\"");
var row = "<tr id=\"" + machines[i] + "\"><td>" + machines[i] + "</td><td></td><td></td><td></td><td></td><td></td><td>No Data</td></tr>";
$('#tm').append(row);
/* put a few sub rows */
for ( let j=0 ; j<=4 ; j++ ) {
var row = "<tr hidden id=\"" + machines[i] + "_" + j + "_table\"><td colspan=\"6\">";
row += "<div style=\"width: 100%; display: flex;\">";
row += "<span id=\"" + machines[i] + "_" + j + "_left\"style=\"color: white; background-color: blue; padding: 10px; display: inline-block; width: 33%;\">";
row += "</span>";
row += "<span id=\"" + machines[i] + "_" + j + "_middle\" style=\"color: black; background-color: pink; padding: 10px; display: inline-block; width: 33%;\">";
row += "</span>";
row += "<span id=\"" + machines[i] + "_" + j +"_right\" style=\"color: white; background-color: teal; padding: 10px; display: inline-block; width: 33%;\">";
row += "</span>";
row += "</div>";
row += "</td><td id=\"" + machines[i] + "_" + j + "_date\">No Data</td></tr>";
$('#tm').append(row);
}
}
MQTTconnect();
periodicTalk();
}
</script>
<!-- <meta name="viewport" content="width=device-width, initial-scale=1"> -->
</head>
<body onload="onLoad()">
<h1>Stacklight</h1>
<table id="tm" class="pure-table pure-table-bordered" style="width: 95vw; margin-left: auto; margin-right: auto;">
<thead>
<tr>
<th>Machine Name</th>
<th>Power</th>
<th title="RED">Attention!</th>
<th title="YELLOW / ORANGE">Complete</th>
<th title="GREEN">In Cycle</th>
<th title="BLUE">Custom</th>
<th>Last Received</th>
</tr>
</thead>
<tbody>
<!--
<tr id="STATIC">
<td>STATIC</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>No Data</td>
</tr>
-->
</tbody>
</table>
<h2>Settings</h2>
<h3>Click somewhere to enable audio.</h3>
<form class="pure-form" id="formSettings">
<fieldset>
<!--<legend>A compact inline form</legend>-->
<label for="periodicTalk">
<input type="checkbox" id="periodicTalk" /> Enable Periodic Sound (to keep Bluetooth alive)
</label>
<label for="soundFinished">
<input type="checkbox" id="soundFinished" /> Play song when cycle done
</label>
<!--
<input type="email" placeholder="Email" />
<input type="password" placeholder="Password" />
<label for="default-remember">
<input type="checkbox" id="default-remember" /> Remember me
</label>
<button type="submit" class="pure-button pure-button-primary">Sign in</button>
-->
</fieldset>
</form>
</body>
</html>