From 8685bdde87670903ff84a61eed46606e3c0185e0 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Sun, 23 Jul 2017 22:55:05 +0200 Subject: [PATCH] interface instead of type --- dist/main.js | 1 + dist/mqttclient.js | 3 +++ src/main.ts | 2 ++ src/mqttclient.ts | 8 ++++++-- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/dist/main.js b/dist/main.js index be4af7d..f323f1e 100644 --- a/dist/main.js +++ b/dist/main.js @@ -11,6 +11,7 @@ class Dispatcher { exec() { log.info("Dispatcher starting"); this._mqttClient.exec(); + log.info("Dispatcher running"); } } const dispatcher = new Dispatcher(); diff --git a/dist/mqttclient.js b/dist/mqttclient.js index 5cdd46c..197a165 100644 --- a/dist/mqttclient.js +++ b/dist/mqttclient.js @@ -26,6 +26,9 @@ class MqttClient { for (let topicHandler of this._topicHandlers) { if (this.topicMatch(topicHandler.topic, topic)) { log.info(`received topic ${topic} matches registered topic ${topicHandler.topic}`); + if (topicHandler.callback != null) { + topicHandler.callback(payload); + } } } }); diff --git a/src/main.ts b/src/main.ts index 9e6c882..737e8cb 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,6 +15,8 @@ class Dispatcher { log.info("Dispatcher starting") this._mqttClient.exec() + + log.info("Dispatcher running") } } diff --git a/src/mqttclient.ts b/src/mqttclient.ts index 4a2b006..54b9f4e 100644 --- a/src/mqttclient.ts +++ b/src/mqttclient.ts @@ -3,12 +3,13 @@ import * as log from './log' const MQTT_BROKER_DEFAULT_URL : string = "mqtt://localhost" -type TopicHandlerCallback = (message: Buffer) => void +type TopicHandlerCallback = (message: any) => any -type TopicHandler = { +interface TopicHandler { topic: string, callback: TopicHandlerCallback|null } + export class MqttClient { private _mqttClient: Mqtt.Client private _mqttBrokerUrl: string @@ -38,6 +39,9 @@ export class MqttClient { for (let topicHandler of this._topicHandlers) { if (this.topicMatch(topicHandler.topic, topic)) { log.info(`received topic ${topic} matches registered topic ${topicHandler.topic}`) + if (topicHandler.callback != null) { + topicHandler.callback(payload) + } } } })