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
82 lines (81 loc) • 2.9 kB
JavaScript
"use strict";
const mysensors_types_1 = require("../lib/mysensors-types");
function getSensor(config) {
const sensor = {
_msgid: '',
ack: config.ack ? 1 : 0,
childSensorId: Number(config.childid),
messageType: Number(config.msgtype),
nodeId: Number(config.nodeid),
payload: '',
subType: Number(config.subtype),
};
return sensor;
}
module.exports = (RED) => {
RED.nodes.registerType('mysencap', function (props) {
RED.nodes.createNode(this, props);
this.sensor = getSensor(props);
this.presentation = props.presentation || false;
this.presentationtext = props.presentationtext || '';
this.presentationtype = props.presentationtype || 0;
this.fullpresentation = props.fullpresentation || false;
this.internal = props.internal || 0;
this.firmwarename = props.firmwarename || '';
this.firmwareversion = props.firmwareversion || '';
if (this.presentation) {
setTimeout(() => {
const msg = getSensor(props);
msg.ack = 0;
if (this.fullpresentation) {
msg.messageType = 3;
msg.childSensorId = 255;
msg.subType = 11;
msg.payload = this.firmwarename;
this.send(msg);
msg.subType = 12;
msg.payload = this.firmwareversion;
this.send(msg);
}
msg.messageType = 0;
msg.subType = this.presentationtype;
msg.payload = this.presentationtext;
this.send(msg);
}, 5000);
}
this.on('input', (msg, send, done) => {
const msgOut = this.sensor;
msgOut.payload = msg.payload;
if (this.sensor.messageType === 3) {
msgOut.childSensorId = 255;
msgOut.subType = this.internal;
}
send(msgOut);
done();
});
});
RED.httpAdmin.get('/mysensordefs/:id', RED.auth.needsPermission(''), (req, res) => {
const type = req.params.id;
let mysVal;
switch (type) {
case 'subtype':
mysVal = mysensors_types_1.mysensor_data;
break;
case 'presentation':
mysVal = mysensors_types_1.mysensor_sensor;
break;
case 'internal':
mysVal = mysensors_types_1.mysensor_internal;
break;
}
const kv = Object.keys(mysVal)
.filter((k) => typeof mysVal[k] === 'number')
.reduce((l, k) => {
if (typeof k !== 'number') {
l[k] = mysVal[k];
}
return l;
}, {});
res.json(JSON.stringify(kv));
});
};