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
69 lines (68 loc) • 2.77 kB
JavaScript
;
const auto_decode_1 = require("../lib/decoder/auto-decode");
const mysensors_debug_1 = require("../lib/mysensors-debug");
const mysensors_types_1 = require("../lib/mysensors-types");
module.exports = (RED) => {
RED.nodes.registerType('mysdebug', function (config) {
RED.nodes.createNode(this, config);
this.mysDbg = new mysensors_debug_1.MysensorsDebugDecode();
this.on('input', async (incommingMsg, send, done) => {
const msg = await (0, auto_decode_1.AutoDecode)(incommingMsg);
if (!msg) {
done();
return;
}
let msgHeader = '';
let msgSubType;
switch (msg.messageType) {
case mysensors_types_1.mysensor_command.C_PRESENTATION:
msgHeader = 'PRESENTATION';
msgSubType = mysensors_types_1.mysensor_sensor[msg.subType];
break;
case mysensors_types_1.mysensor_command.C_SET:
msgHeader = 'SET';
msgSubType = mysensors_types_1.mysensor_data[msg.subType];
break;
case mysensors_types_1.mysensor_command.C_REQ:
msgHeader = 'REQ';
msgSubType = mysensors_types_1.mysensor_data[msg.subType];
break;
case mysensors_types_1.mysensor_command.C_INTERNAL:
if (msg.subType === 9) {
msg.payload = this.mysDbg.decode(msg.payload);
}
else {
msgHeader = 'INTERNAL';
msgSubType = mysensors_types_1.mysensor_internal[msg.subType];
}
break;
case mysensors_types_1.mysensor_command.C_STREAM:
msgHeader = 'STREAM';
msgSubType = mysensors_types_1.mysensor_stream[msg.subType];
break;
default:
send({
payload: 'unsupported msgType ' + msg.messageType
});
done();
return;
}
if (msgSubType !== null) {
msg.payload =
msgHeader +
'nodeId:' +
msg.nodeId +
'childId:' +
msg.childSensorId +
'SubType:' +
msgSubType +
'ACK:' +
msg.ack +
'Payload:' +
msg.payload;
}
send(msg);
done();
});
});
};