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

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}`)
}
}