-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_helper.js
36 lines (31 loc) · 1.15 KB
/
node_helper.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
const fs = require('fs');
const NodeHelper = require('node_helper');
module.exports = NodeHelper.create({
alarmFile: '', // will be set when module is started
start() {
console.log(`Starting module helper: ${this.name}`);
},
socketNotificationReceived(notification, payload) {
console.log('nodeHelper, socketNotificationReceived', notification, payload);
if (notification === `${this.name}-STARTED`) {
this.alarmFile = payload.alarmFile;
this.readSavedAlarm();
} else if (notification === `${this.name}-ALARM-CHANGED`) {
this.updateSavedAlarm(payload);
}
},
readSavedAlarm() {
fs.stat(this.alarmFile, (error, stats) => {
if (!error && stats.isFile()) {
const data = fs.readFileSync(this.alarmFile);
if (data) {
const alarmData = JSON.parse(data);
this.sendSocketNotification(`${this.name}-UPDATE-ALARM`, alarmData);
}
}
});
},
updateSavedAlarm(alarmData) {
fs.writeFileSync(this.alarmFile, JSON.stringify(alarmData));
}
});