lot of changes for first actual use

This commit is contained in:
Wolfgang Hottgenroth 2017-08-23 15:52:37 +02:00
parent 16313c868f
commit 3797d84b4d
14 changed files with 250 additions and 81 deletions

41
dist/callchain.js vendored
View File

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

26
dist/espthermtojson.js vendored Normal file
View File

@ -0,0 +1,26 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils = require("./utils");
class EspThermMessage {
constructor(client, temperature, voltage, timeConsumed) {
this._client = client;
this._temperature = temperature;
this._voltage = voltage;
this._timeConsumed = timeConsumed;
}
toString() {
return JSON.stringify(this);
}
toJSON() {
return utils.jsonPrepaper(this, []);
}
}
exports.EspThermMessage = EspThermMessage;
function espThermToJson(message) {
let messageStr = "" + message;
let parts = messageStr.split(' ');
let espThermMessage = new EspThermMessage(parts[0], parseFloat(parts[1]), parseFloat(parts[2]), parseInt(parts[3]));
return espThermMessage;
}
exports.espThermToJson = espThermToJson;
//# sourceMappingURL=espthermtojson.js.map

24
dist/main.js vendored
View File

@ -2,22 +2,14 @@
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const log = require("./log"); const log = require("./log");
const mqtt = require("./mqttdispatcher"); const mqtt = require("./mqttdispatcher");
const plugintest1 = require("./plugintest1"); const EspThermToJson = require("./espthermtojson");
const MongoSave = require("./mongosave");
log.info("Dispatcher starting"); log.info("Dispatcher starting");
exports.dispatcher = new mqtt.MqttDispatcher(); let dispatcher = new mqtt.MqttDispatcher("mqtts://broker.hottis.de:8883", "wn", "locutus", "/home/wn/server-ca.crt");
exports.dispatcher.register('IoT/test', 'print1', (message) => { dispatcher.register('IoT/espThermometer2/#', 'toJson', EspThermToJson.espThermToJson);
log.info("Callback for IoT/test"); let mongo = new MongoSave.MongoSave();
log.info(`message is ${message}`); dispatcher.register('IoT/espThermometer2/#', 'MongoSave', mongo);
return `<<${message}>>`; // plugintest1.pluginTest1Start(dispatcher)
}); dispatcher.exec();
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"); log.info("Dispatcher running");
//# sourceMappingURL=main.js.map //# sourceMappingURL=main.js.map

13
dist/mongosave.js vendored Normal file
View File

@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const CallChain = require("./callchain");
class MongoSave extends CallChain.ABaseChainItem {
constructor() {
super('MongoSave');
}
func(message) {
return "<<" + message + ">>";
}
}
exports.MongoSave = MongoSave;
//# sourceMappingURL=mongosave.js.map

View File

@ -3,14 +3,24 @@ Object.defineProperty(exports, "__esModule", { value: true });
const Mqtt = require("mqtt"); const Mqtt = require("mqtt");
const log = require("./log"); const log = require("./log");
const callchain = require("./callchain"); const callchain = require("./callchain");
const fs = require("fs");
const MQTT_BROKER_DEFAULT_URL = "mqtt://localhost"; const MQTT_BROKER_DEFAULT_URL = "mqtt://localhost";
function passThrough(message) { function passThrough(message) {
return message; return message;
} }
exports.passThrough = passThrough; exports.passThrough = passThrough;
class MqttDispatcher { class MqttDispatcher {
constructor(mqttBrokerUrl) { constructor(mqttBrokerUrl, mqttUser, mqttPass, mqttCAFile) {
this._mqttOptions = {};
this._mqttBrokerUrl = (mqttBrokerUrl) ? mqttBrokerUrl : MQTT_BROKER_DEFAULT_URL; this._mqttBrokerUrl = (mqttBrokerUrl) ? mqttBrokerUrl : MQTT_BROKER_DEFAULT_URL;
if (mqttUser && mqttPass) {
this._mqttOptions.username = mqttUser;
this._mqttOptions.password = mqttPass;
}
if (mqttCAFile) {
this._mqttOptions.ca = fs.readFileSync(mqttCAFile, 'ascii');
this._mqttOptions.rejectUnauthorized = true;
}
this._topicHandlers = []; this._topicHandlers = [];
} }
register(topic, label, newChainItemOrCallbackFunc) { register(topic, label, newChainItemOrCallbackFunc) {
@ -41,7 +51,8 @@ class MqttDispatcher {
for (let topicHandler of this._topicHandlers) { for (let topicHandler of this._topicHandlers) {
topicHandler.root.begin(); topicHandler.root.begin();
} }
this._mqttClient = Mqtt.connect(this._mqttBrokerUrl); log.info(`connecting to ${this._mqttBrokerUrl}`);
this._mqttClient = Mqtt.connect(this._mqttBrokerUrl, this._mqttOptions);
this._mqttClient.on('error', log.error); this._mqttClient.on('error', log.error);
this._mqttClient.on('connect', () => { this._mqttClient.on('connect', () => {
log.info("connected to mqtt broker"); log.info("connected to mqtt broker");

14
dist/utils.js vendored Normal file
View File

@ -0,0 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function jsonPrepaper(obj, hideKeys) {
let dup = {};
for (let key in obj) {
if ((hideKeys.indexOf(key) == -1) && !((key[0] == "_") && (key[1] == "_"))) {
let dkey = (key[0] == "_") ? key.slice(1) : key;
dup[dkey] = obj[key];
}
}
return dup;
}
exports.jsonPrepaper = jsonPrepaper;
//# sourceMappingURL=utils.js.map

View File

@ -1,7 +1,7 @@
0 info it worked if it ends with ok 0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/nodejs', '/usr/bin/npm', 'start' ] 1 verbose cli [ '/usr/bin/nodejs', '/usr/bin/npm', 'start' ]
2 info using npm@3.10.10 2 info using npm@3.10.10
3 info using node@v6.11.1 3 info using node@v6.11.2
4 verbose run-script [ 'prestart', 'start', 'poststart' ] 4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle dispatcher@1.0.0~prestart: dispatcher@1.0.0 5 info lifecycle dispatcher@1.0.0~prestart: dispatcher@1.0.0
6 silly lifecycle dispatcher@1.0.0~prestart: no script for prestart, continuing 6 silly lifecycle dispatcher@1.0.0~prestart: no script for prestart, continuing
@ -26,7 +26,7 @@
16 verbose cwd /home/wn/workspace-node/Dispatcher 16 verbose cwd /home/wn/workspace-node/Dispatcher
17 error Linux 4.9.0-3-amd64 17 error Linux 4.9.0-3-amd64
18 error argv "/usr/bin/nodejs" "/usr/bin/npm" "start" 18 error argv "/usr/bin/nodejs" "/usr/bin/npm" "start"
19 error node v6.11.1 19 error node v6.11.2
20 error npm v3.10.10 20 error npm v3.10.10
21 error code ELIFECYCLE 21 error code ELIFECYCLE
22 error dispatcher@1.0.0 start: `node dist/main.js` 22 error dispatcher@1.0.0 start: `node dist/main.js`

View File

@ -3,61 +3,81 @@ import * as events from 'events'
export type ChainItemFunc = (message: any) => any export type ChainItemFunc = (message: any) => any
export abstract class AChainItem extends events.EventEmitter { export interface Receivable {
begin() : void;
send(message : any) : void;
}
class LastChainItem implements Receivable {
public begin() : void {
}
public send(message: any) {
log.info(`Last chain item, final result ${message}`)
}
}
let lastChainItem : LastChainItem = new LastChainItem();
export abstract class AChainItem extends events.EventEmitter implements Receivable {
protected _label : string protected _label : string
protected _next : AChainItem | null protected _next : Receivable
constructor(label : string) { constructor(label : string) {
super() super()
this._label = label this._label = label
this._next = null this._next = lastChainItem
} }
public toString() : string { public toString() : string {
return `<${this._label}` return `<${this._label}>`
} }
registerNext(next : AChainItem) :void { public registerNext(next : AChainItem) :void {
this._next = next this._next = next
} }
send(message : any) : void { public send(message : any) : void {
this.emit('yourturn', message) this.emit('yourturn', message)
} }
abstract begin() : void public abstract begin() : void
} }
export class ChainItem extends AChainItem { export abstract class ABaseChainItem extends AChainItem {
private _chainItemFunc : ChainItemFunc
constructor(label : string) { constructor(label : string) {
super(label) super(label)
} }
protected abstract func(message : any) : any;
public begin() :void {
if (this._next != null) {
this._next.begin()
}
this.addListener('yourturn', (message : any) : void => {
log.info(`Calling ${this.toString()}`)
let result : any = this.func(message)
this._next.send(result)
})
}
}
export class ChainItem extends ABaseChainItem {
private _chainItemFunc : ChainItemFunc
public toString() : string { public toString() : string {
let funcName : string = (this._chainItemFunc.name === "") ? "lambda" : this._chainItemFunc.name let funcName : string = (this._chainItemFunc.name === "") ? "lambda" : this._chainItemFunc.name
return `<${funcName}, ${this._label}>` return `<${funcName}, ${this._label}>`
} }
registerFunc(func : ChainItemFunc) : void { public registerFunc(func : ChainItemFunc) : void {
this._chainItemFunc = func this._chainItemFunc = func
} }
begin() :void { protected func(message : any) : any {
if (this._next != null) { return this._chainItemFunc(message)
this._next.begin()
}
this.addListener('yourturn', (message : any) : void => {
log.info(`Calling ${this.toString()}`)
let result : any = this._chainItemFunc(message)
if (this._next == null) {
log.info(`Last chain item, final result ${result}`)
} else {
this._next.send(result)
}
})
} }
} }

33
src/espthermtojson.ts Normal file
View File

@ -0,0 +1,33 @@
import * as log from './log'
import * as utils from './utils'
export class EspThermMessage {
private _client : string
private _temperature : number
private _voltage : number
private _timeConsumed : number
constructor(client:string, temperature:number, voltage:number, timeConsumed:number) {
this._client = client
this._temperature = temperature
this._voltage = voltage
this._timeConsumed = timeConsumed
}
toString() :string {
return JSON.stringify(this)
}
toJSON() : any {
return utils.jsonPrepaper(this, [])
}
}
export function espThermToJson(message : any) : any {
let messageStr : string = "" + <string>message
let parts : string[] = messageStr.split(' ')
let espThermMessage : EspThermMessage = new EspThermMessage(parts[0],
parseFloat(parts[1]), parseFloat(parts[2]),
parseInt(parts[3]))
return espThermMessage
}

View File

@ -3,23 +3,20 @@ import * as mqtt from './mqttdispatcher'
import * as callchain from './callchain' import * as callchain from './callchain'
import * as plugintest1 from './plugintest1' import * as plugintest1 from './plugintest1'
import * as EspThermToJson from './espthermtojson'
import * as MongoSave from './mongosave'
log.info("Dispatcher starting") log.info("Dispatcher starting")
export const dispatcher = new mqtt.MqttDispatcher() let dispatcher = new mqtt.MqttDispatcher("mqtts://broker.hottis.de:8883",
dispatcher.register('IoT/test', 'print1', (message: any) : any => { "wn", "locutus", "/home/wn/server-ca.crt")
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)
plugintest1.pluginTest1Start(dispatcher) dispatcher.register('IoT/espThermometer2/#', 'toJson', EspThermToJson.espThermToJson)
let mongo : MongoSave.MongoSave = new MongoSave.MongoSave()
dispatcher.register('IoT/espThermometer2/#', 'MongoSave', mongo);
// plugintest1.pluginTest1Start(dispatcher)
dispatcher.exec() dispatcher.exec()
log.info("Dispatcher running") log.info("Dispatcher running")

30
src/mongosave.ts Normal file
View File

@ -0,0 +1,30 @@
import * as CallChain from './callchain'
import * as log from './log'
export class MongoSave extends CallChain.ABaseChainItem {
constructor() {
super('MongoSave')
}
protected func(message : any) : any {
return "<<" + message + ">>"
}
/*
public begin() :void {
if (this._next != null) {
this._next.begin()
}
this.addListener('yourturn', (message : any) : void => {
log.info(`Calling ${this.toString()}`)
let result : any = this.func(message)
if (this._next == null) {
log.info(`Last chain item, final result ${result}`)
} else {
this._next.send(result)
}
})
}
*/
}

View File

@ -1,10 +1,12 @@
import * as Mqtt from 'mqtt' import * as Mqtt from 'mqtt'
import * as log from './log' import * as log from './log'
import * as callchain from './callchain' import * as callchain from './callchain'
import * as fs from 'fs'
const MQTT_BROKER_DEFAULT_URL : string = "mqtt://localhost" const MQTT_BROKER_DEFAULT_URL : string = "mqtt://localhost"
export function passThrough(message: any) { export function passThrough(message: any) {
return message return message
} }
@ -24,11 +26,22 @@ export interface IDispatcher {
export class MqttDispatcher implements IDispatcher { export class MqttDispatcher implements IDispatcher {
private _mqttClient: Mqtt.Client private _mqttClient: Mqtt.Client
private _mqttOptions: Mqtt.IClientOptions = {}
private _mqttBrokerUrl: string private _mqttBrokerUrl: string
private _topicHandlers: TopicHandler[] private _topicHandlers: TopicHandler[]
constructor(mqttBrokerUrl? : string) { constructor(mqttBrokerUrl? : string, mqttUser? : string, mqttPass? : string, mqttCAFile? : string) {
this._mqttBrokerUrl = (mqttBrokerUrl) ? mqttBrokerUrl : MQTT_BROKER_DEFAULT_URL this._mqttBrokerUrl = (mqttBrokerUrl) ? mqttBrokerUrl : MQTT_BROKER_DEFAULT_URL
if (mqttUser && mqttPass) {
this._mqttOptions.username = mqttUser
this._mqttOptions.password = mqttPass
}
if (mqttCAFile) {
this._mqttOptions.ca = fs.readFileSync(mqttCAFile, 'ascii')
this._mqttOptions.rejectUnauthorized = true
}
this._topicHandlers = [] this._topicHandlers = []
} }
@ -62,7 +75,8 @@ export class MqttDispatcher implements IDispatcher {
(topicHandler.root as callchain.ChainItem).begin() (topicHandler.root as callchain.ChainItem).begin()
} }
this._mqttClient = Mqtt.connect(this._mqttBrokerUrl) log.info(`connecting to ${this._mqttBrokerUrl}`)
this._mqttClient = Mqtt.connect(this._mqttBrokerUrl, this._mqttOptions)
this._mqttClient.on('error', log.error) this._mqttClient.on('error', log.error)
this._mqttClient.on('connect', (): void => { this._mqttClient.on('connect', (): void => {
log.info("connected to mqtt broker") log.info("connected to mqtt broker")

10
src/utils.ts Normal file
View File

@ -0,0 +1,10 @@
export function jsonPrepaper(obj:any, hideKeys:string[]) : any {
let dup = {}
for (let key in obj) {
if ((hideKeys.indexOf(key) == -1) && ! ((key[0] == "_") && (key[1] == "_"))) {
let dkey = (key[0] == "_") ? key.slice(1) : key
dup[dkey] = obj[key]
}
}
return dup
}

View File

@ -1,12 +1,12 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "es2015", "target": "es6",
"module": "commonjs", "module": "commonjs",
"moduleResolution": "node", "moduleResolution": "node",
"sourceMap": true, "sourceMap": true,
"lib": ["es2015"], "lib": ["es6"],
"strictNullChecks": true, "strictNullChecks": true,
"noImplicitAny": true, //"noImplicitAny": true,
"noEmitOnError": true, "noEmitOnError": true,
"outDir": "dist", "outDir": "dist",
"typeRoots": [ "typeRoots": [