From c8e55b64b33c875982289d8ed73d981a0f1d117d Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Wed, 9 Aug 2017 00:13:31 +0200 Subject: [PATCH] overloaded register func in mqttclilent --- dist/main.js | 8 ++++---- dist/mqttclient.js | 16 ++++++++++------ src/main.ts | 8 ++++---- src/mqttclient.ts | 17 ++++++++++------- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/dist/main.js b/dist/main.js index 516798f..ee7a793 100644 --- a/dist/main.js +++ b/dist/main.js @@ -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"); diff --git a/dist/mqttclient.js b/dist/mqttclient.js index 911a30e..45a965f 100644 --- a/dist/mqttclient.js +++ b/dist/mqttclient.js @@ -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) { diff --git a/src/main.ts b/src/main.ts index 2f8a1eb..8aae304 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,18 +9,18 @@ class Dispatcher { constructor() { this._mqttClient = new mqtt.MqttClient() - this._mqttClient.registerCallbackFunc('IoT/test', 'print1', (message: any) : any => { + this._mqttClient.register('IoT/test', 'print1', (message: any) : any => { log.info("Callback for IoT/test") log.info(`message is ${message}`) return `<<${message}>>` }) - this._mqttClient.registerCallbackFunc('IoT/test', 'print2', (message: any) : any => { + this._mqttClient.register('IoT/test', 'print2', (message: any) : any => { 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() : void { diff --git a/src/mqttclient.ts b/src/mqttclient.ts index 3c7d5b7..c7076fc 100644 --- a/src/mqttclient.ts +++ b/src/mqttclient.ts @@ -27,13 +27,16 @@ export class MqttClient { this._topicHandlers = [] } - registerCallbackFunc(topic: string, label: string, callbackFunc: callchain.ChainItemFunc) : void { - let newChainItem = new callchain.ChainItem(label) - newChainItem.registerFunc(callbackFunc) - this.registerCallbackClass(topic, label, newChainItem) - } - - registerCallbackClass(topic: string, label: string, newChainItem: callchain.AChainItem) : void { + register(topic: string, label: string, + newChainItemOrCallbackFunc: callchain.AChainItem | callchain.ChainItemFunc) : void { + let newChainItem : callchain.AChainItem + if (newChainItemOrCallbackFunc instanceof callchain.AChainItem) { + newChainItem = newChainItemOrCallbackFunc + } else { + let myNewChainItem = new callchain.ChainItem(label) + myNewChainItem.registerFunc(newChainItemOrCallbackFunc) + newChainItem = myNewChainItem + } let done: boolean = false for (let topicHandler of this._topicHandlers) { if (topicHandler.topic === topic) {