remove leading underscore, fix next call in mongosave

This commit is contained in:
Wolfgang Hottgenroth
2017-08-24 15:51:34 +02:00
parent a0a922f851
commit 25116aae89
14 changed files with 230 additions and 170 deletions

39
dist/callchain.js vendored
View File

@ -6,53 +6,68 @@ class LastChainItem {
begin() {
}
send(message) {
log.info(`Last chain item, final result ${message}`);
log.info(`Last chain item, final result ${JSON.stringify(message)}`);
}
}
let lastChainItem = new LastChainItem();
class AChainItem extends events.EventEmitter {
constructor(label) {
super();
this._label = label;
this._next = lastChainItem;
this.label = label;
this.next = lastChainItem;
}
toString() {
return `<${this._label}>`;
return `<${this.label}>`;
}
registerNext(next) {
this._next = next;
this.next = next;
}
send(message) {
this.emit('yourturn', message);
}
}
exports.AChainItem = AChainItem;
class AAsyncBaseChainItem extends AChainItem {
constructor(label) {
super(label);
}
begin() {
if (this.next != null) {
this.next.begin();
}
this.addListener('yourturn', (message) => {
log.info(`Calling ${this.toString()}`);
this.func(message, this.next.send);
});
}
}
exports.AAsyncBaseChainItem = AAsyncBaseChainItem;
class ABaseChainItem extends AChainItem {
constructor(label) {
super(label);
}
begin() {
if (this._next != null) {
this._next.begin();
if (this.next != null) {
this.next.begin();
}
this.addListener('yourturn', (message) => {
log.info(`Calling ${this.toString()}`);
let result = this.func(message);
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}>`;
let funcName = (this.chainItemFunc.name === "") ? "lambda" : this.chainItemFunc.name;
return `<${funcName}, ${this.label}>`;
}
registerFunc(func) {
this._chainItemFunc = func;
this.chainItemFunc = func;
}
func(message) {
return this._chainItemFunc(message);
return this.chainItemFunc(message);
}
}
exports.ChainItem = ChainItem;

View File

@ -1,12 +1,37 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils = require("./utils");
class EspThermData {
constructor(temperature, voltage, timeConsumed) {
this.temperature = temperature;
this.voltage = voltage;
this.timeConsumed = timeConsumed;
}
toString() {
return JSON.stringify(this);
}
toJSON() {
return utils.jsonPrepaper(this, []);
}
}
exports.EspThermData = EspThermData;
class EspThermMetadata {
constructor(client, timestamp) {
this.client = client;
this.timestamp = (timestamp) ? timestamp : new Date();
}
toString() {
return JSON.stringify(this);
}
toJSON() {
return utils.jsonPrepaper(this, []);
}
}
exports.EspThermMetadata = EspThermMetadata;
class EspThermMessage {
constructor(client, temperature, voltage, timeConsumed) {
this._client = client;
this._temperature = temperature;
this._voltage = voltage;
this._timeConsumed = timeConsumed;
this.data = new EspThermData(temperature, voltage, timeConsumed);
this.metadata = new EspThermMetadata(client);
}
toString() {
return JSON.stringify(this);

32
dist/mongosave.js vendored
View File

@ -3,47 +3,51 @@ Object.defineProperty(exports, "__esModule", { value: true });
const CallChain = require("./callchain");
const log = require("./log");
const MongoDB = require("mongodb");
class MongoSave extends CallChain.ABaseChainItem {
class MongoSave extends CallChain.AAsyncBaseChainItem {
constructor(url) {
super('MongoSave');
this._url = url;
this._mongoClient = new MongoDB.MongoClient();
this._connectPending = false;
this.url = url;
this.mongoClient = new MongoDB.MongoClient();
this.connectPending = false;
}
func(message) {
if (!this._dbh) {
func(message, finished) {
if (!this.dbh) {
log.info("Not database connection yet");
if (!this._connectPending) {
this._connectPending = true;
this._mongoClient.connect(this._url)
if (!this.connectPending) {
this.connectPending = true;
this.mongoClient.connect(this.url)
.then((db) => {
log.info("Successfully opened MongoDB connect");
this._dbh = db;
this.dbh = db;
})
.catch((err) => {
log.error(`Failure when opening MongoDB connect: ${err}`);
this._dbh = undefined;
this.dbh = undefined;
});
}
else {
log.info("Connecting to database is pending");
}
}
if (this._dbh) {
if (this.dbh) {
log.info("Database handle is available");
let coll = this._dbh.collection("iot");
let coll = this.dbh.collection("iot");
coll.insertOne(message)
.then((res) => {
log.info(`Successfully wrote one item in database: ${res.insertedId}`);
let nextValue = { id: res.insertedId, payload: message };
finished(nextValue);
})
.catch((err) => {
log.error(`Failure when trying to write one item in database: ${err}`);
log.error("Chain interrupted");
});
}
else {
log.error(`No database connection yet, drop message ${message}`);
log.error("Chain interrupted");
}
return "<<" + message + ">>";
log.info(`Returning from ${this.label}`);
}
}
exports.MongoSave = MongoSave;

View File

@ -11,17 +11,17 @@ function passThrough(message) {
exports.passThrough = passThrough;
class MqttDispatcher {
constructor(mqttBrokerUrl, mqttUser, mqttPass, mqttCAFile) {
this._mqttOptions = {};
this._mqttBrokerUrl = (mqttBrokerUrl) ? mqttBrokerUrl : MQTT_BROKER_DEFAULT_URL;
this.mqttOptions = {};
this.mqttBrokerUrl = (mqttBrokerUrl) ? mqttBrokerUrl : MQTT_BROKER_DEFAULT_URL;
if (mqttUser && mqttPass) {
this._mqttOptions.username = mqttUser;
this._mqttOptions.password = mqttPass;
this.mqttOptions.username = mqttUser;
this.mqttOptions.password = mqttPass;
}
if (mqttCAFile) {
this._mqttOptions.ca = fs.readFileSync(mqttCAFile, 'ascii');
this._mqttOptions.rejectUnauthorized = true;
this.mqttOptions.ca = fs.readFileSync(mqttCAFile, 'ascii');
this.mqttOptions.rejectUnauthorized = true;
}
this._topicHandlers = [];
this.topicHandlers = [];
}
register(topic, label, newChainItemOrCallbackFunc) {
let newChainItem;
@ -34,7 +34,7 @@ class MqttDispatcher {
newChainItem = myNewChainItem;
}
let done = false;
for (let topicHandler of this._topicHandlers) {
for (let topicHandler of this.topicHandlers) {
if (topicHandler.topic === topic) {
topicHandler.last.registerNext(newChainItem);
topicHandler.last = newChainItem;
@ -43,26 +43,26 @@ class MqttDispatcher {
}
}
if (!done) {
this._topicHandlers.push({ topic: topic, root: newChainItem, last: newChainItem });
this.topicHandlers.push({ topic: topic, root: newChainItem, last: newChainItem });
log.info(`first callback ${newChainItem.toString()} added for topic ${topic}`);
}
}
exec() {
for (let topicHandler of this._topicHandlers) {
for (let topicHandler of this.topicHandlers) {
topicHandler.root.begin();
}
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(`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");
for (let topicHandler of this._topicHandlers) {
this._mqttClient.subscribe(topicHandler.topic);
for (let topicHandler of this.topicHandlers) {
this.mqttClient.subscribe(topicHandler.topic);
}
});
this._mqttClient.on('message', (topic, payload) => {
this.mqttClient.on('message', (topic, payload) => {
log.info(`message received, topic ${topic}, payload ${payload}`);
for (let topicHandler of this._topicHandlers) {
for (let topicHandler of this.topicHandlers) {
if (this.topicMatch(topicHandler.topic, topic)) {
log.info(`received topic ${topic} matches registered topic ${topicHandler.topic}`);
topicHandler.root.send(payload);

8
dist/processor.js vendored
View File

@ -5,19 +5,19 @@ const events = require("events");
class AProcessor extends events.EventEmitter {
constructor(label) {
super();
this._label = label;
this.label = label;
this.addListener('input', this.process);
log.info(`Processor object instanciated: ${this.constructor.name}, ${this._label}`);
log.info(`Processor object instanciated: ${this.constructor.name}, ${this.label}`);
}
in(message) {
log.info(`Routing ${message} to Processor class ${this.constructor.name}, ${this._label}`);
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}`);
log.info(`ExRoute1.process: ${this.label}, ${message}`);
}
}
exports.ExProc1 = ExProc1;

5
dist/utils.js vendored
View File

@ -3,9 +3,8 @@ 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];
if ((hideKeys.indexOf(key) == -1) && !(key[0] == "_")) {
dup[key] = obj[key];
}
}
return dup;