diff --git a/dist/main.js b/dist/main.js index f323f1e..ef16fd5 100644 --- a/dist/main.js +++ b/dist/main.js @@ -1,12 +1,16 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const log = require("./log"); -const MqttClient = require("./mqttclient"); +const mqtt = require("./mqttclient"); class Dispatcher { constructor() { - this._mqttClient = new MqttClient.MqttClient(); - this._mqttClient.register('IoT/test', null); - this._mqttClient.register('IoT/Device/#', null); + this._mqttClient = new mqtt.MqttClient(); + this._mqttClient.register('IoT/test', (message) => { + log.info("Callback for IoT/test"); + log.info(`message is ${message}`); + return `<<${message}>>`; + }); + this._mqttClient.register('IoT/Device/#', mqtt.passThrough); } exec() { log.info("Dispatcher starting"); diff --git a/dist/mqttclient.js b/dist/mqttclient.js index 197a165..a20a4d4 100644 --- a/dist/mqttclient.js +++ b/dist/mqttclient.js @@ -3,6 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true }); const Mqtt = require("mqtt"); const log = require("./log"); const MQTT_BROKER_DEFAULT_URL = "mqtt://localhost"; +function passThrough(message) { + return message; +} +exports.passThrough = passThrough; class MqttClient { constructor(mqttBrokerUrl) { this._mqttBrokerUrl = (mqttBrokerUrl) ? mqttBrokerUrl : MQTT_BROKER_DEFAULT_URL; @@ -26,9 +30,7 @@ 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); - } + topicHandler.callback(payload); } } }); diff --git a/src/main.ts b/src/main.ts index 737e8cb..e836060 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,14 +1,18 @@ import * as log from './log' -import * as MqttClient from './mqttclient' +import * as mqtt from './mqttclient' class Dispatcher { - private _mqttClient: MqttClient.MqttClient + private _mqttClient: mqtt.MqttClient constructor() { - this._mqttClient = new MqttClient.MqttClient() + this._mqttClient = new mqtt.MqttClient() - this._mqttClient.register('IoT/test', null) - this._mqttClient.register('IoT/Device/#', null) + this._mqttClient.register('IoT/test', (message: any) : any => { + log.info("Callback for IoT/test") + log.info(`message is ${message}`) + return `<<${message}>>` + }) + this._mqttClient.register('IoT/Device/#', mqtt.passThrough) } exec() : void { diff --git a/src/mqttclient.ts b/src/mqttclient.ts index 54b9f4e..27a68d6 100644 --- a/src/mqttclient.ts +++ b/src/mqttclient.ts @@ -7,7 +7,11 @@ type TopicHandlerCallback = (message: any) => any interface TopicHandler { topic: string, - callback: TopicHandlerCallback|null + callback: TopicHandlerCallback +} + +export function passThrough(message: any) { + return message } export class MqttClient { @@ -20,7 +24,7 @@ export class MqttClient { this._topicHandlers = [] } - register(topic: string, callback: TopicHandlerCallback|null) : void { + register(topic: string, callback: TopicHandlerCallback) : void { this._topicHandlers.push({topic, callback}) log.info(`handler registered for topic ${topic}`) } @@ -39,9 +43,7 @@ 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) - } + topicHandler.callback(payload) } } })