Processor class introduced

This commit is contained in:
Wolfgang Hottgenroth
2017-08-09 11:37:22 +02:00
parent 09d7ea8ace
commit 16313c868f
7 changed files with 124 additions and 55 deletions

42
dist/main.js vendored
View File

@ -2,28 +2,22 @@
Object.defineProperty(exports, "__esModule", { value: true });
const log = require("./log");
const mqtt = require("./mqttdispatcher");
class Dispatcher {
constructor() {
this._mqttDispatcher = new mqtt.MqttDispatcher();
this._mqttDispatcher.register('IoT/test', 'print1', (message) => {
log.info("Callback for IoT/test");
log.info(`message is ${message}`);
return `<<${message}>>`;
});
this._mqttDispatcher.register('IoT/test', 'print2', (message) => {
log.info("Callback for IoT/test");
log.info(`message is ${message}`);
return `<<${message}>>`;
});
this._mqttDispatcher.register('IoT/test', 'null1', mqtt.passThrough);
this._mqttDispatcher.register('IoT/test', 'null2', mqtt.passThrough);
}
exec() {
log.info("Dispatcher starting");
this._mqttDispatcher.exec();
log.info("Dispatcher running");
}
}
const dispatcher = new Dispatcher();
dispatcher.exec();
const plugintest1 = require("./plugintest1");
log.info("Dispatcher starting");
exports.dispatcher = new mqtt.MqttDispatcher();
exports.dispatcher.register('IoT/test', 'print1', (message) => {
log.info("Callback for IoT/test");
log.info(`message is ${message}`);
return `<<${message}>>`;
});
exports.dispatcher.register('IoT/test', 'print2', (message) => {
log.info("Callback for IoT/test");
log.info(`message is ${message}`);
return `<<${message}>>`;
});
exports.dispatcher.register('IoT/test', 'null1', mqtt.passThrough);
exports.dispatcher.register('IoT/test', 'null2', mqtt.passThrough);
plugintest1.pluginTest1Start(exports.dispatcher);
exports.dispatcher.exec();
log.info("Dispatcher running");
//# sourceMappingURL=main.js.map

15
dist/plugintest1.js vendored Normal file
View File

@ -0,0 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const log = require("./log");
const processor = require("./processor");
var exRoute = new processor.ExProc1('label1');
function pluginTest1Start(dispatcher) {
log.info("starting plugintest1");
dispatcher.register('IoT/test', "plugintest1", (message) => {
log.info(`plugintest1 runs: ${message}`);
exRoute.in(message);
return message;
});
}
exports.pluginTest1Start = pluginTest1Start;
//# sourceMappingURL=plugintest1.js.map

24
dist/processor.js vendored Normal file
View File

@ -0,0 +1,24 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const log = require("./log");
const events = require("events");
class AProcessor extends events.EventEmitter {
constructor(label) {
super();
this._label = label;
this.addListener('input', this.process);
log.info(`Processor object instanciated: ${this.constructor.name}, ${this._label}`);
}
in(message) {
log.info(`Routing ${message} to Processor class ${this.constructor.name}, ${this._label}`);
this.emit('input', message);
}
}
exports.AProcessor = AProcessor;
class ExProc1 extends AProcessor {
process(message) {
log.info(`ExRoute1.process: ${this._label}, ${message}`);
}
}
exports.ExProc1 = ExProc1;
//# sourceMappingURL=processor.js.map

View File

@ -1,41 +1,28 @@
import * as log from './log'
import * as mqtt from './mqttdispatcher'
import * as callchain from './callchain'
import * as plugintest1 from './plugintest1'
class Dispatcher {
private _mqttDispatcher: mqtt.MqttDispatcher
log.info("Dispatcher starting")
export const dispatcher = new mqtt.MqttDispatcher()
dispatcher.register('IoT/test', 'print1', (message: any) : any => {
log.info("Callback for IoT/test")
log.info(`message is ${message}`)
return `<<${message}>>`
})
dispatcher.register('IoT/test', 'print2', (message: any) : any => {
log.info("Callback for IoT/test")
log.info(`message is ${message}`)
return `<<${message}>>`
})
dispatcher.register('IoT/test', 'null1', mqtt.passThrough)
dispatcher.register('IoT/test', 'null2', mqtt.passThrough)
constructor() {
this._mqttDispatcher = new mqtt.MqttDispatcher()
plugintest1.pluginTest1Start(dispatcher)
this._mqttDispatcher.register('IoT/test', 'print1', (message: any) : any => {
log.info("Callback for IoT/test")
log.info(`message is ${message}`)
return `<<${message}>>`
})
this._mqttDispatcher.register('IoT/test', 'print2', (message: any) : any => {
log.info("Callback for IoT/test")
log.info(`message is ${message}`)
return `<<${message}>>`
})
this._mqttDispatcher.register('IoT/test', 'null1', mqtt.passThrough)
this._mqttDispatcher.register('IoT/test', 'null2', mqtt.passThrough)
}
exec() : void {
log.info("Dispatcher starting")
this._mqttDispatcher.exec()
log.info("Dispatcher running")
}
}
const dispatcher = new Dispatcher()
dispatcher.exec()
log.info("Dispatcher running")

View File

@ -17,7 +17,12 @@ interface TopicHandler {
last: callchain.AChainItem | undefined
}
export class MqttDispatcher {
export interface IDispatcher {
register(topic: string, label: string,
newChainItemOrCallbackFunc: callchain.AChainItem | callchain.ChainItemFunc) : void
}
export class MqttDispatcher implements IDispatcher {
private _mqttClient: Mqtt.Client
private _mqttBrokerUrl: string
private _topicHandlers: TopicHandler[]

16
src/plugintest1.ts Normal file
View File

@ -0,0 +1,16 @@
import * as log from './log'
import { IDispatcher } from './mqttdispatcher'
import * as processor from './processor'
var exRoute : processor.AProcessor = new processor.ExProc1('label1')
export function pluginTest1Start(dispatcher : IDispatcher) : void {
log.info("starting plugintest1")
dispatcher.register('IoT/test', "plugintest1", (message : any) : any => {
log.info(`plugintest1 runs: ${message}`)
exRoute.in(message)
return message
})
}

28
src/processor.ts Normal file
View File

@ -0,0 +1,28 @@
import * as log from './log'
import * as events from 'events'
export abstract class AProcessor extends events.EventEmitter {
protected _label : string
constructor(label : string) {
super()
this._label = label
this.addListener('input', this.process)
log.info(`Processor object instanciated: ${this.constructor.name}, ${this._label}`)
}
public in(message : any) : void {
log.info(`Routing ${message} to Processor class ${this.constructor.name}, ${this._label}`)
this.emit('input', message)
}
protected abstract process(message : any) : void
}
export class ExProc1 extends AProcessor {
protected process(message : any) : void {
log.info(`ExRoute1.process: ${this._label}, ${message}`)
}
}