handle retained messages, introduc missing event detector

This commit is contained in:
Wolfgang Hottgenroth
2017-08-25 17:03:25 +02:00
parent 3967a6fb3a
commit 3309ba29e5
7 changed files with 173 additions and 26 deletions

20
dist/main.js vendored
View File

@ -5,21 +5,21 @@ const mqtt = require("./mqttdispatcher");
const fs = require("fs");
const cmdargs = require("command-line-args");
const EspThermToJson = require("./espthermtojson");
const MongoSave = require("./mongosave");
log.info("Dispatcher starting");
const optionDefinitions = [
const MissingEventDetector = require("./missingeventdetector");
const OPTION_DEFINITIONS = [
{ name: 'verbose', alias: 'v', type: Boolean },
{ name: 'config', alias: 'c', type: String, defaultValue: '~/mqttDispatcher.conf' }
];
const OPTIONS = cmdargs(optionDefinitions);
log.info(`OPTIONS.config`);
const OPTIONS = cmdargs(OPTION_DEFINITIONS);
const CONFIG = JSON.parse(fs.readFileSync(OPTIONS.config, "utf8"));
log.info("Dispatcher starting");
let dispatcher = new mqtt.MqttDispatcher(CONFIG.brokerUrl, CONFIG.brokerUser, CONFIG.brokerPass, CONFIG.brokerCa);
dispatcher.register('IoT/espThermometer2/#', 'toJson', EspThermToJson.espThermToJson);
let mongoUrl = "mongodb://localhost/hottis";
// let mongoUrl = "mongodb://receiver:esp8266.@cluster0-shard-00-00-7qduq.mongodb.net:27017,cluster0-shard-00-01-7qduq.mongodb.net:27017,cluster0-shard-00-02-7qduq.mongodb.net:27017/hottis?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin"
let mongo = new MongoSave.MongoSave(CONFIG.mongoDbUrl);
dispatcher.register('IoT/espThermometer2/#', 'MongoSave', mongo);
const ESP_THERM_TOPIC = 'IoT/espThermometer2/#';
dispatcher.register(ESP_THERM_TOPIC, 'toJson', EspThermToJson.espThermToJson);
let missingeventdetector = new MissingEventDetector.MissingEventDetector();
dispatcher.register(ESP_THERM_TOPIC, 'MissingEventDetector', missingeventdetector);
// let mongo : MongoSave.MongoSave = new MongoSave.MongoSave(CONFIG.mongoDbUrl)
// dispatcher.register(ESP_THERM_TOPIC, 'MongoSave', mongo);
dispatcher.exec();
log.info("Dispatcher running");
//# sourceMappingURL=main.js.map

60
dist/missingeventdetector.js vendored Normal file
View File

@ -0,0 +1,60 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const log = require("./log");
const CallChain = require("./callchain");
const Processor = require("./processor");
const CHECK_PERIOD = 10; // seconds
class ClientEntry {
}
class MissingEventProcessor extends Processor.AProcessor {
constructor() {
super("MissingEventProcessor");
this.clientMap = new Map();
this.timer = setInterval(() => {
this.clientMap.forEach((value, key) => {
let currentTime = new Date().getTime();
let elapsedTime = currentTime - value.lastEvent;
log.info(`Checking ${key}, elapsed: ${elapsedTime / 1000}, avg. delay: ${value.avgDelay / 1000}`);
});
}, CHECK_PERIOD * 1000);
}
process(message) {
let client = message.metadata.client;
log.info(`Event for client ${client}`);
let currentTime = new Date().getTime();
if (this.clientMap.has(client)) {
let clientEntry = this.clientMap.get(client);
clientEntry.client = client;
clientEntry.delay = currentTime - clientEntry.lastEvent;
clientEntry.lastEvent = currentTime;
clientEntry.count += 1;
clientEntry.delaySum += clientEntry.delay;
clientEntry.avgDelay = clientEntry.delaySum / clientEntry.count;
this.clientMap.set(client, clientEntry);
log.info(`Entry for ${client} updated`);
}
else {
let clientEntry = new ClientEntry();
clientEntry.client = client;
clientEntry.lastEvent = currentTime;
clientEntry.delay = 0;
clientEntry.count = 0;
clientEntry.delaySum = 0;
clientEntry.avgDelay = 0;
this.clientMap.set(client, clientEntry);
log.info(`Entry for ${client} inserted`);
}
}
}
class MissingEventDetector extends CallChain.ABaseChainItem {
constructor() {
super("MissingEventDetector");
this.missingEventProcessor = new MissingEventProcessor();
}
func(message) {
this.missingEventProcessor.in(message);
return message;
}
}
exports.MissingEventDetector = MissingEventDetector;
//# sourceMappingURL=missingeventdetector.js.map

View File

@ -60,8 +60,11 @@ class MqttDispatcher {
this.mqttClient.subscribe(topicHandler.topic);
}
});
this.mqttClient.on('message', (topic, payload) => {
this.mqttClient.on('message', (topic, payload, packet) => {
log.info(`message received, topic ${topic}, payload ${payload}`);
if (packet.retain) {
log.info("IS RETAINED");
}
for (let topicHandler of this.topicHandlers) {
if (this.topicMatch(topicHandler.topic, topic)) {
log.info(`received topic ${topic} matches registered topic ${topicHandler.topic}`);