forked from lucacalcaterra/risco-mqtt-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
165 lines (154 loc) · 7.84 KB
/
app.js
File metadata and controls
165 lines (154 loc) · 7.84 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* eslint linebreak-style: ["error", "windows"] */
const MQTT = require('async-mqtt');
// const express = require('express'); // TODO
const RiscoPoller = require('./panelPoller');
const Config = require('./config/config');
const riscoLogger = require('./logger');
// const app = express(); // TODO
const riscoPoller = new RiscoPoller(Config.Conn.POLLINGINTERVAL);
async function armdisarm(part, message) {
riscoLogger.log('info', 'arm/disarm command arrived');
switch (message.toString()) {
case (Config.States.armCommands.ARM):
riscoPoller.riscoConn.setArm(part, Config.States.armCommands.ARM);
break;
case (Config.States.armCommands.DISARM):
riscoPoller.riscoConn.setArm(part, Config.States.armCommands.DISARM);
break;
case (Config.States.armCommands.PARTARM):
riscoPoller.riscoConn.setArm(part, Config.States.armCommands.PARTARM);
break;
default:
riscoLogger.log('warn', 'arm command not recognized');
}
}
function publishDetector(mqttClient, topic, idx, element) {
// note - detector id is unique accross all partitions and can be non-continuous
// within particular partitions (eg. partition 0 detectors 0 2 3 4, partition 3 detector 1),
// for this reason now we use detectors array element index while previously it was element.id
const mqttMsg = JSON.stringify(element);
mqttClient.publish(`${topic}/${idx}`, mqttMsg, Config.Mqtt.msgOptions);
// publish sensor state
let sensState = 'active';
if (element.filter !== '') sensState = element.filter;
mqttClient.publish(`${topic}/${idx}/status`, sensState, Config.Mqtt.msgOptions);
}
async function main() {
/* TODO
// express server
app.get('/risco/armstatus', (req, res) => {
res.send(riscoPoller.riscoConn.riscoArmStatus);
});
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.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: ' + JSON.stringify(riscoPoller.riscoConn.riscoArmStatus));
// publish arm status
riscoPoller.riscoConn.riscoArmStatus.forEach((armStatus, partId) => {
mqttClient.publish(`${Config.Mqtt.channels.MAINCHAN}/${Config.Mqtt.channels.ARMSTATUS}/${partId}`, Config.Mqtt.transforms.states[armStatus], Config.Mqtt.msgOptions);
});
// publish for one partition backward compatibility
mqttClient.publish(`${Config.Mqtt.channels.MAINCHAN}/${Config.Mqtt.channels.ARMSTATUS}`, Config.Mqtt.transforms.states[riscoPoller.riscoConn.riscoArmStatus[0]], 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 parts = riscoPoller.riscoConn.riscoDetectors.parts;
const mqttDectsTopic = `${Config.Mqtt.channels.MAINCHAN}/${Config.Mqtt.channels.DETECTORS}`;
// publish total numbers of detectors
parts.forEach((part) => {
mqttClient.publish(`${mqttDectsTopic}/part${part.id}/count`, part.detectors.length.toString(), Config.Mqtt.msgOptions);
part.detectors.forEach((element, idx) => {
publishDetector(mqttClient, `${mqttDectsTopic}/part${part.id}`, idx, element);
});
});
// publish for one partition backward compatibility
mqttClient.publish(`${mqttDectsTopic}/count`, parts[0].detectors.length.toString(), Config.Mqtt.msgOptions);
parts[0].detectors.forEach((element, idx) => {
publishDetector(mqttClient, mqttDectsTopic, idx, element);
});
// 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 regexpart = new RegExp(`${Config.Mqtt.channels.MAINCHAN}/${Config.Mqtt.channels.ARMSTATUS}/[^/]+/SET`);
const regexdect = new RegExp(`${Config.Mqtt.channels.MAINCHAN}/${Config.Mqtt.channels.DETECTORS}/[^/]+/SET`);
riscoLogger.log('info', `message from mqtt arrived:${topic}/${message}`);
if (topic === `${Config.Mqtt.channels.MAINCHAN}/${Config.Mqtt.channels.ARMSTATUS}/SET`) {
// one partition backward compatibility
armdisarm(0, message);
}
else if (topic.match(regexpart)) {
armdisarm(parseInt(topic.split('/')[2]), message);
}
else if (topic.match(regexdect)) {
// Case of detector command enable/disable
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');
}
else {
riscoLogger.log('warn', '...command not recognized');
}
});
process.on('SIGINT', () => {
riscoLogger.log('info', 'Exiting ... ');
riscoPoller.stop();
mqttClient.end();
process.exit();
});
}
main();