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

View File

@ -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");