callchains implemented

This commit is contained in:
Wolfgang Hottgenroth
2017-08-09 00:03:57 +02:00
parent 1aa7765079
commit ca1dea597c
7 changed files with 278 additions and 67 deletions

50
dist/callchain.js vendored Normal file
View File

@ -0,0 +1,50 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const log = require("./log");
const events = require("events");
class AChainItem extends events.EventEmitter {
constructor(label) {
super();
this._label = label;
this._next = null;
}
toString() {
return `<${this._label}`;
}
registerNext(next) {
this._next = next;
}
send(message) {
this.emit('yourturn', message);
}
}
exports.AChainItem = AChainItem;
class ChainItem extends AChainItem {
constructor(label) {
super(label);
}
toString() {
let funcName = (this._chainItemFunc.name === "") ? "lambda" : this._chainItemFunc.name;
return `<${funcName}, ${this._label}>`;
}
registerFunc(func) {
this._chainItemFunc = func;
}
begin() {
if (this._next != null) {
this._next.begin();
}
this.addListener('yourturn', (message) => {
log.info(`Calling ${this.toString()}`);
let result = this._chainItemFunc(message);
if (this._next == null) {
log.info(`Last chain item, final result ${result}`);
}
else {
this._next.send(result);
}
});
}
}
exports.ChainItem = ChainItem;
//# sourceMappingURL=callchain.js.map

33
dist/main.js vendored
View File

@ -5,20 +5,47 @@ const mqtt = require("./mqttclient");
class Dispatcher {
constructor() {
this._mqttClient = new mqtt.MqttClient();
this._mqttClient.register('IoT/test', 'print', (message) => {
this._mqttClient.registerCallbackFunc('IoT/test', 'print1', (message) => {
log.info("Callback for IoT/test");
log.info(`message is ${message}`);
return `<<${message}>>`;
});
this._mqttClient.register('IoT/Device/#', 'null1', mqtt.passThrough);
this._mqttClient.register('IoT/Device/#', 'null2', mqtt.passThrough);
this._mqttClient.registerCallbackFunc('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);
}
exec() {
log.info("Dispatcher starting");
this._mqttClient.exec();
log.info("Dispatcher running");
}
test() {
log.info("Sending test data");
this._mqttClient.test();
}
}
// callchain.registerChainItemFunc('first', (message : any) : any => {
// log.info(`first callback ${message}`)
// return `<${message}>`
// })
// callchain.registerChainItemFunc('second', (message : any) : any => {
// log.info(`second callback ${message}`)
// return `<${message}>`
// })
// callchain.registerChainItemFunc('third', (message : any) : any => {
// log.info(`third callback ${message}`)
// return `<${message}>`
// })
// callchain.begin()
// callchain.send('test1')
// callchain.send('test2')
// callchain.send('test3')
// callchain.send('test4')
const dispatcher = new Dispatcher();
dispatcher.exec();
dispatcher.test();
//# sourceMappingURL=main.js.map

45
dist/mqttclient.js vendored
View File

@ -2,20 +2,8 @@
Object.defineProperty(exports, "__esModule", { value: true });
const Mqtt = require("mqtt");
const log = require("./log");
const callchain = require("./callchain");
const MQTT_BROKER_DEFAULT_URL = "mqtt://localhost";
class TopicHandlerCallback {
constructor(label, func) {
this._label = label;
this._func = func;
}
get func() {
return this._func;
}
toString() {
let funcName = (this._func.name === "") ? "lambda" : this._func.name;
return `<${funcName}, ${this._label}>`;
}
}
function passThrough(message) {
return message;
}
@ -25,22 +13,33 @@ class MqttClient {
this._mqttBrokerUrl = (mqttBrokerUrl) ? mqttBrokerUrl : MQTT_BROKER_DEFAULT_URL;
this._topicHandlers = [];
}
register(topic, label, callbackFunc) {
registerCallbackFunc(topic, label, callbackFunc) {
let newChainItem = new callchain.ChainItem(label);
newChainItem.registerFunc(callbackFunc);
this.registerCallbackClass(topic, label, newChainItem);
}
registerCallbackClass(topic, label, newChainItem) {
let done = false;
let callback = new TopicHandlerCallback(label, callbackFunc);
for (let topicHandler of this._topicHandlers) {
if (topicHandler.topic === topic) {
topicHandler.callbacks.push(callback);
topicHandler.last.registerNext(newChainItem);
topicHandler.last = newChainItem;
done = true;
log.info(`additional callback ${callback.toString()} added for topic ${topic}`);
log.info(`additional callback ${newChainItem.toString()} added for topic ${topic}`);
}
}
if (!done) {
this._topicHandlers.push({ topic: topic, callbacks: [callback] });
log.info(`first callback ${callback.toString()} added for topic ${topic}`);
this._topicHandlers.push({ topic: topic, root: newChainItem, last: newChainItem });
log.info(`first callback ${newChainItem.toString()} added for topic ${topic}`);
}
}
test() {
this._mqttClient.emit("message", 'IoT/test', 'payload');
}
exec() {
for (let topicHandler of this._topicHandlers) {
topicHandler.root.begin();
}
this._mqttClient = Mqtt.connect(this._mqttBrokerUrl);
this._mqttClient.on('error', log.error);
this._mqttClient.on('connect', () => {
@ -54,13 +53,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}`);
// topicHandler.callback(payload)
let returnValue = payload;
for (let topicHandlerCallback of topicHandler.callbacks) {
log.info(`Calling ${topicHandlerCallback}`);
returnValue = topicHandlerCallback.func(returnValue);
}
log.info(`Final return value is ${returnValue}`);
topicHandler.root.send(payload);
}
}
});