-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.js
143 lines (132 loc) · 6.53 KB
/
app.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* eslint linebreak-style: ["error", "windows"] */
const MQTT = require('async-mqtt');
// only for healthcheck
const express = require('express');
const RiscoPoller = require('./panelPoller');
const Config = require('./config/config');
const riscoLogger = require('./logger');
const riscoPoller = new RiscoPoller(Config.Conn.POLLINGINTERVAL);
async function main() {
// express server only for healthcheck
const app = express();
app.get('/', (req, res) => {
res.sendStatus(200);
});
app.listen(3000, () => {
riscoLogger.log('info', 'Express server started...');
});
// init Mqtt Connection
const mqttClient = await MQTT.connect((`tcp://${Config.Mqtt.url.MQTT_SERVER}:${Config.Mqtt.url.MQTT_PORT}`), Config.Mqtt.options);
mqttClient.on('error', (err) => {
riscoLogger.log('error', `error! Cannot connect to MQTT Server: ${err}`);
riscoPoller.stop();
});
mqttClient.on('offline', () => {
riscoLogger.log('error', 'error! Cannot connect to MQTT Server... Server is offline... stopping poll...');
riscoPoller.stop();
});
// on Connection do stuff...
mqttClient.on('connect', async () => {
riscoLogger.log('info', 'Connected to MQTT Server');
// Init connection to Risco Cloud
await riscoPoller.init();
// start Poller
await riscoPoller.start();
// Subscribe for listening commands ( MQTT IN )
mqttClient.subscribe(`${Config.Mqtt.channels.MAINCHAN}/${Config.Mqtt.channels.ARMSTATUS}/SET`);
mqttClient.subscribe(`${Config.Mqtt.channels.MAINCHAN}/${Config.Mqtt.channels.DETECTORS}/+/SET`);
});
riscoPoller.on('polled', () => {
riscoLogger.log('debug', `Polled...counter: ${riscoPoller.counter}`);
});
riscoPoller.on('newpanelstatus', async () => {
riscoLogger.log('debug', 'newarmstatus emitted');
if (riscoPoller.riscoConn.riscoArmStatus !== null) {
riscoLogger.log('info', `Arming status: ${riscoPoller.riscoConn.riscoArmStatus}`);
// publish arm status
mqttClient.publish(`${Config.Mqtt.channels.MAINCHAN}/${Config.Mqtt.channels.ARMSTATUS}`, Config.Mqtt.transforms.states[riscoPoller.riscoConn.riscoArmStatus], Config.Mqtt.msgOptions);
// publish isonAlarm (in case of alarm...)
mqttClient.publish(`${Config.Mqtt.channels.MAINCHAN}/${Config.Mqtt.channels.ISONALARM}`, riscoPoller.riscoConn.riscoOngoingAlarm.toString(), Config.Mqtt.msgOptions);
// publish detectors
const detectorsArray = riscoPoller.riscoConn.riscoDetectors.parts[0].detectors;
const mqttDectsTopic = `${Config.Mqtt.channels.MAINCHAN}/${Config.Mqtt.channels.DETECTORS}`;
// publish total numbers of detectors
mqttClient.publish(`${mqttDectsTopic}/count`, detectorsArray.length.toString(), Config.Mqtt.msgOptions);
detectorsArray.forEach((element) => {
const mqttMsg = JSON.stringify(element);
mqttClient.publish(`${mqttDectsTopic}/${element.id}`, mqttMsg, Config.Mqtt.msgOptions);
// publish sensor state
let sensState = 'active';
if (element.filter !== '') sensState = element.filter;
mqttClient.publish(`${mqttDectsTopic}/${element.id}/status`, sensState, Config.Mqtt.msgOptions);
});
// publish Event history (json as getted from Risco Cloud)
// All
mqttClient.publish(`${Config.Mqtt.channels.MAINCHAN}/${Config.Mqtt.channels.EVENTHISTORY}`, JSON.stringify(riscoPoller.riscoConn.riscoEventHistory), Config.Mqtt.msgOptions);
// Today
// ..... sometimes is empty , check
if (riscoPoller.riscoConn.riscoEventHistory[0].LogRecords) {
const todayEventsArray = riscoPoller.riscoConn.riscoEventHistory[0].LogRecords;
// Today Not Errors Events
const todayNotErrEventsArray = todayEventsArray.filter((event) => event.Priority !== 'error');
// Today Errors
const todayErrorEventsArray = todayEventsArray.filter((event) => event.Priority === '');
mqttClient.publish(`${Config.Mqtt.channels.MAINCHAN}/${Config.Mqtt.channels.EVENTHISTORY}/today/errors`, JSON.stringify(todayErrorEventsArray), Config.Mqtt.msgOptions);
this.lastEventString = '';
// TODO - format Log Events in tabular , for now only last event
/*
lastEventObj.forEach((element) => {
this.lastEventString = `${element.YTimeToShow} - ${element.EventName}`;
});
*/
// Last Event (not error, useful for knows who arm/disarm)
this.lastEventString = (`${todayNotErrEventsArray[0].YTimeToShow} ${todayNotErrEventsArray[0].EventName}`).split(''').join('');
mqttClient.publish(`${Config.Mqtt.channels.MAINCHAN}/${Config.Mqtt.channels.EVENTHISTORY}/lastevent`, String(this.lastEventString), Config.Mqtt.msgOptions);
}
riscoLogger.log('info', 'publish messages on MQTT Server');
} else riscoLogger.log('debug', 'no new status');
});
// Check MQTT in ... translate message to commands
mqttClient.on('message', (topic, message) => {
const regexdect = new RegExp(`${Config.Mqtt.channels.MAINCHAN}/${Config.Mqtt.channels.DETECTORS}/[^/]+/SET`);
riscoLogger.log('info', `message from mqtt arrived:${topic}/${message}`);
/**
* CASES
*/
switch (topic) {
// Case of Arm Command
case (`${Config.Mqtt.channels.MAINCHAN}/${Config.Mqtt.channels.ARMSTATUS}/SET`):
riscoLogger.log('info', 'arm/disarm command arrived');
switch (message.toString()) {
case (Config.States.armCommands.ARM):
riscoPoller.riscoConn.setArm(Config.States.armCommands.ARM);
break;
case (Config.States.armCommands.DISARM):
riscoPoller.riscoConn.setArm(Config.States.armCommands.DISARM);
break;
case (Config.States.armCommands.PARTARM):
riscoPoller.riscoConn.setArm(Config.States.armCommands.PARTARM);
break;
default:
riscoLogger.log('warn', 'arm command not recognized');
}
break;
// Case of detector command enable/disable
case (topic.match(regexdect)[0]):
if ((message.toString() === 'bypass') || (message.toString() === 'unbypass')) {
riscoLogger.log('info', 'enable/disable detector command arrived...sending command to panel');
riscoPoller.riscoConn.setDetectorBypass(topic.split('/')[2], message.toString());
} else riscoLogger.log('warn', 'command enable/disable detector malformed');
break;
default:
riscoLogger.log('warn', '...command not recognized');
}
});
process.on('SIGINT', () => {
riscoLogger.log('info', 'Exiting ... ');
riscoPoller.stop();
mqttClient.end();
process.exit();
});
}
main();