lot of changes for first actual use
This commit is contained in:
43
dist/callchain.js
vendored
43
dist/callchain.js
vendored
@ -2,14 +2,22 @@
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const log = require("./log");
|
||||
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 {
|
||||
constructor(label) {
|
||||
super();
|
||||
this._label = label;
|
||||
this._next = null;
|
||||
this._next = lastChainItem;
|
||||
}
|
||||
toString() {
|
||||
return `<${this._label}`;
|
||||
return `<${this._label}>`;
|
||||
}
|
||||
registerNext(next) {
|
||||
this._next = next;
|
||||
@ -19,32 +27,33 @@ class AChainItem extends events.EventEmitter {
|
||||
}
|
||||
}
|
||||
exports.AChainItem = AChainItem;
|
||||
class ChainItem extends AChainItem {
|
||||
class ABaseChainItem 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);
|
||||
}
|
||||
let result = this.func(message);
|
||||
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;
|
||||
//# sourceMappingURL=callchain.js.map
|
26
dist/espthermtojson.js
vendored
Normal file
26
dist/espthermtojson.js
vendored
Normal 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
24
dist/main.js
vendored
@ -2,22 +2,14 @@
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const log = require("./log");
|
||||
const mqtt = require("./mqttdispatcher");
|
||||
const plugintest1 = require("./plugintest1");
|
||||
const EspThermToJson = require("./espthermtojson");
|
||||
const MongoSave = require("./mongosave");
|
||||
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();
|
||||
let dispatcher = new mqtt.MqttDispatcher("mqtts://broker.hottis.de:8883", "wn", "locutus", "/home/wn/server-ca.crt");
|
||||
dispatcher.register('IoT/espThermometer2/#', 'toJson', EspThermToJson.espThermToJson);
|
||||
let mongo = new MongoSave.MongoSave();
|
||||
dispatcher.register('IoT/espThermometer2/#', 'MongoSave', mongo);
|
||||
// plugintest1.pluginTest1Start(dispatcher)
|
||||
dispatcher.exec();
|
||||
log.info("Dispatcher running");
|
||||
//# sourceMappingURL=main.js.map
|
13
dist/mongosave.js
vendored
Normal file
13
dist/mongosave.js
vendored
Normal 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
|
15
dist/mqttdispatcher.js
vendored
15
dist/mqttdispatcher.js
vendored
@ -3,14 +3,24 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const Mqtt = require("mqtt");
|
||||
const log = require("./log");
|
||||
const callchain = require("./callchain");
|
||||
const fs = require("fs");
|
||||
const MQTT_BROKER_DEFAULT_URL = "mqtt://localhost";
|
||||
function passThrough(message) {
|
||||
return message;
|
||||
}
|
||||
exports.passThrough = passThrough;
|
||||
class MqttDispatcher {
|
||||
constructor(mqttBrokerUrl) {
|
||||
constructor(mqttBrokerUrl, mqttUser, mqttPass, mqttCAFile) {
|
||||
this._mqttOptions = {};
|
||||
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 = [];
|
||||
}
|
||||
register(topic, label, newChainItemOrCallbackFunc) {
|
||||
@ -41,7 +51,8 @@ class MqttDispatcher {
|
||||
for (let topicHandler of this._topicHandlers) {
|
||||
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('connect', () => {
|
||||
log.info("connected to mqtt broker");
|
||||
|
14
dist/utils.js
vendored
Normal file
14
dist/utils.js
vendored
Normal 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
|
Reference in New Issue
Block a user