some changes

This commit is contained in:
Wolfgang Hottgenroth
2017-07-27 23:38:17 +02:00
parent 8685bdde87
commit 7e236974c2
4 changed files with 29 additions and 17 deletions

12
dist/main.js vendored
View File

@ -1,12 +1,16 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const log = require("./log"); const log = require("./log");
const MqttClient = require("./mqttclient"); const mqtt = require("./mqttclient");
class Dispatcher { class Dispatcher {
constructor() { constructor() {
this._mqttClient = new MqttClient.MqttClient(); this._mqttClient = new mqtt.MqttClient();
this._mqttClient.register('IoT/test', null); this._mqttClient.register('IoT/test', (message) => {
this._mqttClient.register('IoT/Device/#', null); log.info("Callback for IoT/test");
log.info(`message is ${message}`);
return `<<${message}>>`;
});
this._mqttClient.register('IoT/Device/#', mqtt.passThrough);
} }
exec() { exec() {
log.info("Dispatcher starting"); log.info("Dispatcher starting");

6
dist/mqttclient.js vendored
View File

@ -3,6 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
const Mqtt = require("mqtt"); const Mqtt = require("mqtt");
const log = require("./log"); const log = require("./log");
const MQTT_BROKER_DEFAULT_URL = "mqtt://localhost"; const MQTT_BROKER_DEFAULT_URL = "mqtt://localhost";
function passThrough(message) {
return message;
}
exports.passThrough = passThrough;
class MqttClient { class MqttClient {
constructor(mqttBrokerUrl) { constructor(mqttBrokerUrl) {
this._mqttBrokerUrl = (mqttBrokerUrl) ? mqttBrokerUrl : MQTT_BROKER_DEFAULT_URL; this._mqttBrokerUrl = (mqttBrokerUrl) ? mqttBrokerUrl : MQTT_BROKER_DEFAULT_URL;
@ -26,11 +30,9 @@ class MqttClient {
for (let topicHandler of this._topicHandlers) { for (let topicHandler of this._topicHandlers) {
if (this.topicMatch(topicHandler.topic, topic)) { if (this.topicMatch(topicHandler.topic, topic)) {
log.info(`received topic ${topic} matches registered topic ${topicHandler.topic}`); log.info(`received topic ${topic} matches registered topic ${topicHandler.topic}`);
if (topicHandler.callback != null) {
topicHandler.callback(payload); topicHandler.callback(payload);
} }
} }
}
}); });
} }
topicMatch(registeredTopic, receivedTopic) { topicMatch(registeredTopic, receivedTopic) {

View File

@ -1,14 +1,18 @@
import * as log from './log' import * as log from './log'
import * as MqttClient from './mqttclient' import * as mqtt from './mqttclient'
class Dispatcher { class Dispatcher {
private _mqttClient: MqttClient.MqttClient private _mqttClient: mqtt.MqttClient
constructor() { constructor() {
this._mqttClient = new MqttClient.MqttClient() this._mqttClient = new mqtt.MqttClient()
this._mqttClient.register('IoT/test', null) this._mqttClient.register('IoT/test', (message: any) : any => {
this._mqttClient.register('IoT/Device/#', null) log.info("Callback for IoT/test")
log.info(`message is ${message}`)
return `<<${message}>>`
})
this._mqttClient.register('IoT/Device/#', mqtt.passThrough)
} }
exec() : void { exec() : void {

View File

@ -7,7 +7,11 @@ type TopicHandlerCallback = (message: any) => any
interface TopicHandler { interface TopicHandler {
topic: string, topic: string,
callback: TopicHandlerCallback|null callback: TopicHandlerCallback
}
export function passThrough(message: any) {
return message
} }
export class MqttClient { export class MqttClient {
@ -20,7 +24,7 @@ export class MqttClient {
this._topicHandlers = [] this._topicHandlers = []
} }
register(topic: string, callback: TopicHandlerCallback|null) : void { register(topic: string, callback: TopicHandlerCallback) : void {
this._topicHandlers.push({topic, callback}) this._topicHandlers.push({topic, callback})
log.info(`handler registered for topic ${topic}`) log.info(`handler registered for topic ${topic}`)
} }
@ -39,11 +43,9 @@ export class MqttClient {
for (let topicHandler of this._topicHandlers) { for (let topicHandler of this._topicHandlers) {
if (this.topicMatch(topicHandler.topic, topic)) { if (this.topicMatch(topicHandler.topic, topic)) {
log.info(`received topic ${topic} matches registered topic ${topicHandler.topic}`) log.info(`received topic ${topic} matches registered topic ${topicHandler.topic}`)
if (topicHandler.callback != null) {
topicHandler.callback(payload) topicHandler.callback(payload)
} }
} }
}
}) })
} }