-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (57 loc) · 1.94 KB
/
index.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
const gpio = require('./gpio');
const mqtt = require('./mqtt');
const { config } = require('./config');
const { log } = require('./logger');
function init(){
if (!config.loadError) {
if (config.username && config.password) {
log(`Connect MQTT server with credentials`);
mqtt.connect(config.server, config.username, config.password);
} else {
log(`Connect MQTT server`);
mqtt.connect(config.server);
}
mqtt.subscribe(config.subscriptionTopic, responseToMqttMessage);
setInterval(getStates, config.statusPollingPeriod * 1000);
}
}
function getStates() {
gpio.getInUse().map((gpioAddress) => {
state = gpio.read(gpioAddress);
publishState(gpioAddress, state);
});
}
function publishState(gpioAddress, state){
mqtt.publish(`${config.publishTopic}${gpioAddress}/STATE`,
JSON.stringify({
'GPIO': gpioAddress,
'State': state ? 1 : 0
}));
}
function responseToMqttMessage(topic, message) {
log(`Received topic: ${topic} message: ${message}`, true);
const gpioAddress = parseGpioFromTopic(topic);
const state = isSwitchOnState(message);
const newState = gpio.set(gpioAddress, state);
publishState(gpioAddress, newState);
}
function parseGpioFromTopic(topic){
const regex = new RegExp(config.topicToGpioRegex);
const results = regex.exec(topic);
if (results && results.length === 2) {
try {
return parseInt(results[1]);
} catch(e) {
console.error('Could not parse integer value for GPIO address');
}
} else {
console.error('Could not find integer value for GPIO address, check regex and/or topic');
}
}
function isSwitchOnState(input){
const test = input.toString().toLowerCase();
return test.indexOf('true') >= 0
|| test.indexOf('1') >= 0
|| test.indexOf('on') >= 0;
}
init();