overloaded register func in mqttclilent

This commit is contained in:
Wolfgang Hottgenroth
2017-08-09 00:13:31 +02:00
parent ca1dea597c
commit c8e55b64b3
4 changed files with 28 additions and 21 deletions

8
dist/main.js vendored
View File

@ -5,18 +5,18 @@ const mqtt = require("./mqttclient");
class Dispatcher {
constructor() {
this._mqttClient = new mqtt.MqttClient();
this._mqttClient.registerCallbackFunc('IoT/test', 'print1', (message) => {
this._mqttClient.register('IoT/test', 'print1', (message) => {
log.info("Callback for IoT/test");
log.info(`message is ${message}`);
return `<<${message}>>`;
});
this._mqttClient.registerCallbackFunc('IoT/test', 'print2', (message) => {
this._mqttClient.register('IoT/test', 'print2', (message) => {
log.info("Callback for IoT/test");
log.info(`message is ${message}`);
return `<<${message}>>`;
});
this._mqttClient.registerCallbackFunc('IoT/test', 'null1', mqtt.passThrough);
this._mqttClient.registerCallbackFunc('IoT/test', 'null2', mqtt.passThrough);
this._mqttClient.register('IoT/test', 'null1', mqtt.passThrough);
this._mqttClient.register('IoT/test', 'null2', mqtt.passThrough);
}
exec() {
log.info("Dispatcher starting");

16
dist/mqttclient.js vendored
View File

@ -13,12 +13,16 @@ class MqttClient {
this._mqttBrokerUrl = (mqttBrokerUrl) ? mqttBrokerUrl : MQTT_BROKER_DEFAULT_URL;
this._topicHandlers = [];
}
registerCallbackFunc(topic, label, callbackFunc) {
let newChainItem = new callchain.ChainItem(label);
newChainItem.registerFunc(callbackFunc);
this.registerCallbackClass(topic, label, newChainItem);
}
registerCallbackClass(topic, label, newChainItem) {
register(topic, label, newChainItemOrCallbackFunc) {
let newChainItem;
if (newChainItemOrCallbackFunc instanceof callchain.AChainItem) {
newChainItem = newChainItemOrCallbackFunc;
}
else {
let myNewChainItem = new callchain.ChainItem(label);
myNewChainItem.registerFunc(newChainItemOrCallbackFunc);
newChainItem = myNewChainItem;
}
let done = false;
for (let topicHandler of this._topicHandlers) {
if (topicHandler.topic === topic) {