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
41 lines (40 loc) • 1.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MysensorsDecoder = void 0;
const mysensors_types_1 = require("../mysensors-types");
const nullcheck_1 = require("../nullcheck");
class MysensorsDecoder {
constructor(enrich, database) {
this.database = database;
this.enrichWithDb = enrich && !!database || false;
}
async enrich(msg) {
const newMsg = {
...msg
};
newMsg.messageTypeStr = mysensors_types_1.mysensor_command[msg.messageType];
switch (msg.messageType) {
case mysensors_types_1.mysensor_command.C_INTERNAL:
newMsg.subTypeStr = mysensors_types_1.mysensor_internal[msg.subType];
break;
case mysensors_types_1.mysensor_command.C_PRESENTATION:
newMsg.subTypeStr = mysensors_types_1.mysensor_sensor[msg.subType];
break;
case mysensors_types_1.mysensor_command.C_REQ:
case mysensors_types_1.mysensor_command.C_SET:
newMsg.subTypeStr = mysensors_types_1.mysensor_data[msg.subType];
break;
case mysensors_types_1.mysensor_command.C_STREAM:
newMsg.subTypeStr = mysensors_types_1.mysensor_stream[msg.subType];
break;
}
if (this.enrichWithDb && this.database) {
const res = await this.database.getChild(msg.nodeId, msg.childSensorId);
if (nullcheck_1.NullCheck.isDefinedOrNonNull(res)) {
newMsg.sensorTypeStr = mysensors_types_1.mysensor_sensor[res.sType];
}
}
return newMsg;
}
}
exports.MysensorsDecoder = MysensorsDecoder;