node-red-contrib-zigbee2mqtt-devices
Version:
Nodes to interact with zigbee2mqtt for Node-RED
36 lines (35 loc) • 1.32 kB
JavaScript
;
// import { log } from "node-red";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MqttSubscription = void 0;
class MqttSubscription {
constructor(topic, jsonPayload, callback) {
this.topic = topic;
// create regex for matching mqtt subscriptions containing whitespace chars # and +
let tmp = topic.split("+").join("[^\/]*");
tmp = tmp.split("#").join(".+");
this.regex = new RegExp(tmp + "$");
this.callback = callback;
this.jsonPayload = jsonPayload;
}
invokeIfMatch(topic, payload) {
if (this.comapreTopic(topic)) {
if (this.jsonPayload) {
try {
payload = JSON.parse(payload);
}
catch (err) {
// log.error("################################################################");
// log.error(" node-red-contrib-zigbee2mqtt-devices error:");
// log.error(err);
// log.error("################################################################");
}
}
this.callback(payload, topic);
}
}
comapreTopic(topic) {
return this.regex.test(topic);
}
}
exports.MqttSubscription = MqttSubscription;