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
45 lines (44 loc) • 1.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MysensorsSerial = void 0;
const mysensors_msg_1 = require("../mysensors-msg");
const mysensors_decoder_1 = require("./mysensors-decoder");
class MysensorsSerial extends mysensors_decoder_1.MysensorsDecoder {
constructor(enrich, database, addNewline = false) {
super(enrich, database);
this.addNewline = addNewline;
}
async decode(msg) {
let message = msg.payload.toString();
message = message.replace(/(\r\n|\n|\r)/gm, '');
const tokens = message.split(';');
if (tokens.length === 6) {
const msgOut = {
...msg,
nodeId: parseInt(tokens[0], 10),
childSensorId: parseInt(tokens[1], 10),
messageType: parseInt(tokens[2], 10),
ack: tokens[3] === '1' ? 1 : 0,
subType: parseInt(tokens[4], 10),
payload: tokens[5],
origin: mysensors_msg_1.MsgOrigin.serial
};
return this.enrich(msgOut);
}
}
encode(msg) {
const payload = [
msg.nodeId,
msg.childSensorId,
msg.messageType,
msg.ack,
msg.subType,
msg.payload
].join(';');
return {
...msg,
payload: `${payload}${this.addNewline ? '\n' : ''}`
};
}
}
exports.MysensorsSerial = MysensorsSerial;