node-red-contrib-mysensors
Version:
Mysensors related nodes, for decoding / encoding mysensors serial protocol and MQTT topic, and wrapping arbitrary messages into mysensors compatible messages
32 lines (31 loc) • 1.29 kB
JavaScript
;
const mysensors_controller_1 = require("../lib/mysensors-controller");
module.exports = (RED) => {
RED.nodes.registerType('myscontroller', function (props) {
RED.nodes.createNode(this, props);
if (!props.database) {
return;
}
this.database = RED.nodes.getNode(props.database);
this.controller = new mysensors_controller_1.MysensorsController(this.database.database, props.handleid ?? false, props.timeresponse ?? true, props.timezone ?? 'UTC', props.measurementsystem ?? 'M', props.mqttroot ?? 'mys-out', props.addSerialNewline ?? false);
this.on('input', async (msg, send, done) => {
try {
const msgOut = await this.controller.messageHandler(msg);
if (msgOut) {
send(msgOut);
}
done();
}
catch (err) {
done(err);
}
});
});
RED.httpAdmin.get('/mysensornodes/:database', RED.auth.needsPermission(''), async (req, res) => {
const dbNode = RED.nodes.getNode(req.params.database);
if (dbNode.database) {
const x = await dbNode.database.getNodeList();
res.json(JSON.stringify({ data: x }));
}
});
};