forked from schinken/Eurolite-ESN-Ledboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlasercutter.js
118 lines (90 loc) · 3.91 KB
/
lasercutter.js
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
'use strict';
const mqtt = require('mqtt');
const LedBoardClient = require('./lib/LedBoard/Client');
const PingProbe = require('./lib/Utils/PingProbe');
const screens = require('./lib/Screens');
module.exports.run = (config) => {
let memberCount = 0;
let laserActive = false;
const defaultIdleScreen = _ => screens.idle(memberCount);
const laserActiveIdleScreen = _ => screens.laserOperation();
let idleScreen = defaultIdleScreen;
const mqttClient = mqtt.connect('mqtt://' + config.mqtt.host);
const ledBoard = new LedBoardClient(config.host);
// Set time initially
ledBoard.setDate(new Date());
mqttClient.subscribe('project/laser/operation');
mqttClient.subscribe('project/laser/finished');
mqttClient.subscribe('project/laser/duration');
mqttClient.subscribe('psa/alarm');
mqttClient.subscribe('psa/pizza');
mqttClient.subscribe('psa/message');
mqttClient.subscribe('sensor/door/bell');
mqttClient.subscribe('sensor/space/member/present');
mqttClient.on('message', (topic, payload) => {
const message = '' + payload;
console.log("Received mqtt topic " + topic + " with value '" + message + "'");
switch (topic) {
case 'sensor/space/member/present':
memberCount = parseInt(message, 10);
if (!laserActive) {
ledBoard.sendScreen(screens.idle(memberCount));
}
break;
case 'project/laser/operation':
if (message === 'active') {
laserActive = true;
idleScreen = laserActiveIdleScreen;
// Use the internal datetime to produce a counting screen!
const nullDate = new Date(2000, 1, 0, 0, 0, 2, 0);
ledBoard.setDate(nullDate);
ledBoard.sendScreen(screens.laserOperation());
} else {
laserActive = false;
idleScreen = defaultIdleScreen;
}
break;
// We need to correct the time of the LED Board every two minutes or so, because otherwise
// it get's a bit off
case 'project/laser/duration':
const duration = parseInt(message, 10);
const hours = Math.floor(duration / 3600);
const minutes = Math.floor(duration / 60);
const seconds = duration % 60;
if (minutes % 2 === 0 && seconds === 57) {
const correction = new Date(2000, 1, 0, hours, minutes + 1, 0, 0);
ledBoard.setDate(correction);
}
break;
case 'project/laser/finished':
if (message) {
const duration = parseInt(message, 10);
ledBoard.sendScreens([screens.laserFinished(duration), defaultIdleScreen()]);
// Reset datetime to something useful
ledBoard.setDate(new Date());
}
break;
case 'psa/pizza':
ledBoard.sendScreens([screens.pizzaTimer(), idleScreen()]);
break;
case 'psa/alarm':
ledBoard.sendScreens([screens.alarm(message), idleScreen()]);
break;
case 'sensor/door/bell':
if (message === 'pressed') {
ledBoard.sendScreens([screens.doorBell(), idleScreen()]);
}
break;
case 'psa/message':
if (message) {
ledBoard.sendScreens([screens.publicServiceAnnouncement(message), idleScreen()]);
}
break;
}
});
const aliveProbe = new PingProbe(config.host, config.ping);
aliveProbe.on('alive', () => {
ledBoard.setDate(new Date());
ledBoard.sendScreen(screens.idle(memberCount));
});
};