forked from schinken/Eurolite-ESN-Ledboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.js
76 lines (60 loc) · 2.53 KB
/
default.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
'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) => {
const mqttClient = mqtt.connect('mqtt://' + config.mqtt.host);
mqttClient.subscribe('psa/alarm');
mqttClient.subscribe('psa/donation');
mqttClient.subscribe('psa/pizza');
mqttClient.subscribe('psa/newMember');
mqttClient.subscribe('psa/message');
mqttClient.subscribe('psa/nowPlaying');
mqttClient.subscribe('sensor/door/bell');
mqttClient.subscribe('sensor/space/member/present');
const ledBoard = new LedBoardClient(config.host);
let memberCount = 0;
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);
ledBoard.sendScreen(screens.idle(memberCount));
break;
case 'psa/pizza':
ledBoard.sendScreens([screens.pizzaTimer(), screens.idle(memberCount)]);
break;
case 'psa/donation':
ledBoard.sendScreens([screens.donation(), screens.idle(memberCount)]);
break;
case 'psa/alarm':
ledBoard.sendScreens([screens.alarm(message), screens.idle(memberCount)]);
break;
case 'psa/newMember':
ledBoard.sendScreens([screens.newMemberRegistration(payload), screens.idle(memberCount)]);
break;
case 'sensor/door/bell':
if (message === 'pressed') {
ledBoard.sendScreens([screens.doorBell(), screens.idle(memberCount)]);
}
break;
case 'psa/message':
if (message) {
ledBoard.sendScreens([screens.publicServiceAnnouncement(message), screens.idle(memberCount)]);
}
break;
case 'psa/nowPlaying':
if (message) {
ledBoard.sendScreens([screens.nowPlaying(message), screens.idle(memberCount)]);
}
break;
}
});
const aliveProbe = new PingProbe(config.host, config.ping);
aliveProbe.on('alive', () => {
ledBoard.setDate(new Date());
ledBoard.sendScreen(screens.idle(memberCount));
});
};