Repository navigation
Expand file tree
/
Copy pathnode_helper.js
More file actions
102 lines (85 loc) · 3.29 KB
/
Copy pathnode_helper.js
File metadata and controls
102 lines (85 loc) · 3.29 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
const NodeHelper = require("node_helper");
const exec = require("child_process").exec;
const mqtt = require("mqtt");
module.exports = NodeHelper.create({
start: function () {
console.log("Starting node_helper for MMM-MQTTScreenOnOff");
},
socketNotificationReceived: function (notification, payload) {
if (notification === "CONFIG") {
console.log("Received config");
this.config = payload;
this.connectToMQTTServer();
}
},
connectToMQTTServer: function () {
const { mqttServer, mqttPort, mqttUsername, mqttPassword } = this.config;
const mqttOptions = {
username: mqttUsername,
password: mqttPassword,
};
console.log(`Connecting to MQTT server ${mqttServer}:${mqttPort}`);
this.client = mqtt.connect(mqttServer, mqttOptions);
this.client.on("connect", () => {
console.log("Connected to MQTT server");
this.subscribeToTopic();
});
this.client.on("message", (topic, message) => {
const payload = message.toString();
console.log(`Received message on topic ${topic}: ${payload}`);
this.handlePayload(payload);
});
if (this.config.defaultOffOnStartup) {
console.log("Turning screen off on startup");
this.turnScreenOff();
}
},
subscribeToTopic: function () {
const { mqttTopic } = this.config;
this.client.subscribe(mqttTopic, (err) => {
if (err) {
console.error(`Failed to subscribe to topic ${mqttTopic}: ${err}`);
} else {
console.log(`Subscribed to topic ${mqttTopic}`);
}
});
},
handlePayload: function (payload) {
const { mqttTopic, mqttPayloadOccupancyField } = this.config;
const payloadJson = JSON.parse(payload);
const occupancy = mqttPayloadOccupancyField ? payloadJson[mqttPayloadOccupancyField] : payloadJson;
if (occupancy === true) {
console.log(`Motion detected on topic ${mqttTopic}`);
this.turnScreenOn();
} else if (occupancy === false) {
console.log(`No motion detected on topic ${mqttTopic}`);
this.turnScreenOff();
} else {
console.warn(`Invalid payload received on topic ${mqttTopic}: ${payload} (occupancy field: ${mqttPayloadOccupancyField})`);
}
},
turnScreenOn: function () {
console.log("Turning screen on");
this.sendSocketNotification("SCREEN_ON");
const { screenOnCommand } = this.config;
exec(screenOnCommand, (error, stdout, stderr) => {
if (error) {
console.error(`Error turning screen on: ${error}`);
} else {
console.log(`Screen turned on: ${stdout}`);
}
});
},
turnScreenOff: function () {
console.log("Turning screen off");
this.sendSocketNotification("SCREEN_OFF");
const { screenOffCommand } = this.config;
exec(screenOffCommand, (error, stdout, stderr) => {
if (error) {
console.error(`Error turning screen off: ${error}`);
} else {
console.log(`Screen turned off: ${stdout}`);
}
});
},
});