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
34 lines (33 loc) • 1.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MysensorsMqtt = void 0;
const mysensors_msg_1 = require("../mysensors-msg");
const mysensors_decoder_1 = require("./mysensors-decoder");
class MysensorsMqtt extends mysensors_decoder_1.MysensorsDecoder {
async decode(msg) {
if (msg.topic) {
const split = msg.topic.toString().split('/');
if (split.length >= 6) {
const msgOut = {
...msg,
topicRoot: split.slice(0, split.length - 5).join('/'),
nodeId: parseInt(split[split.length - 5], 10),
childSensorId: parseInt(split[split.length - 4], 10),
messageType: parseInt(split[split.length - 3], 10),
ack: (split[split.length - 2] === '1') ? 1 : 0,
subType: parseInt(split[split.length - 1], 10),
origin: mysensors_msg_1.MsgOrigin.mqtt,
};
return this.enrich(msgOut);
}
}
}
encode(msg) {
return {
...msg,
topic: (msg.topicRoot ? `${msg.topicRoot}/` : '')
+ `${msg.nodeId}/${msg.childSensorId}/${msg.messageType}/${msg.ack}/${msg.subType}`,
};
}
}
exports.MysensorsMqtt = MysensorsMqtt;