2017-07-22 23:39:14 +02:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
const Mqtt = require("mqtt");
|
|
|
|
const log = require("./log");
|
2017-08-09 00:03:57 +02:00
|
|
|
const callchain = require("./callchain");
|
2017-08-23 15:52:37 +02:00
|
|
|
const fs = require("fs");
|
2017-07-22 23:39:14 +02:00
|
|
|
const MQTT_BROKER_DEFAULT_URL = "mqtt://localhost";
|
2017-07-27 23:38:17 +02:00
|
|
|
function passThrough(message) {
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
exports.passThrough = passThrough;
|
2017-08-09 00:21:14 +02:00
|
|
|
class MqttDispatcher {
|
2017-08-23 15:52:37 +02:00
|
|
|
constructor(mqttBrokerUrl, mqttUser, mqttPass, mqttCAFile) {
|
|
|
|
this._mqttOptions = {};
|
2017-07-22 23:39:14 +02:00
|
|
|
this._mqttBrokerUrl = (mqttBrokerUrl) ? mqttBrokerUrl : MQTT_BROKER_DEFAULT_URL;
|
2017-08-23 15:52:37 +02:00
|
|
|
if (mqttUser && mqttPass) {
|
|
|
|
this._mqttOptions.username = mqttUser;
|
|
|
|
this._mqttOptions.password = mqttPass;
|
|
|
|
}
|
|
|
|
if (mqttCAFile) {
|
|
|
|
this._mqttOptions.ca = fs.readFileSync(mqttCAFile, 'ascii');
|
|
|
|
this._mqttOptions.rejectUnauthorized = true;
|
|
|
|
}
|
2017-07-22 23:39:14 +02:00
|
|
|
this._topicHandlers = [];
|
|
|
|
}
|
2017-08-09 00:13:31 +02:00
|
|
|
register(topic, label, newChainItemOrCallbackFunc) {
|
|
|
|
let newChainItem;
|
|
|
|
if (newChainItemOrCallbackFunc instanceof callchain.AChainItem) {
|
|
|
|
newChainItem = newChainItemOrCallbackFunc;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let myNewChainItem = new callchain.ChainItem(label);
|
|
|
|
myNewChainItem.registerFunc(newChainItemOrCallbackFunc);
|
|
|
|
newChainItem = myNewChainItem;
|
|
|
|
}
|
2017-07-27 23:53:23 +02:00
|
|
|
let done = false;
|
|
|
|
for (let topicHandler of this._topicHandlers) {
|
|
|
|
if (topicHandler.topic === topic) {
|
2017-08-09 00:03:57 +02:00
|
|
|
topicHandler.last.registerNext(newChainItem);
|
|
|
|
topicHandler.last = newChainItem;
|
2017-07-27 23:53:23 +02:00
|
|
|
done = true;
|
2017-08-09 00:03:57 +02:00
|
|
|
log.info(`additional callback ${newChainItem.toString()} added for topic ${topic}`);
|
2017-07-27 23:53:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!done) {
|
2017-08-09 00:03:57 +02:00
|
|
|
this._topicHandlers.push({ topic: topic, root: newChainItem, last: newChainItem });
|
|
|
|
log.info(`first callback ${newChainItem.toString()} added for topic ${topic}`);
|
2017-07-27 23:53:23 +02:00
|
|
|
}
|
2017-07-22 23:39:14 +02:00
|
|
|
}
|
|
|
|
exec() {
|
2017-08-09 00:03:57 +02:00
|
|
|
for (let topicHandler of this._topicHandlers) {
|
|
|
|
topicHandler.root.begin();
|
|
|
|
}
|
2017-08-23 15:52:37 +02:00
|
|
|
log.info(`connecting to ${this._mqttBrokerUrl}`);
|
|
|
|
this._mqttClient = Mqtt.connect(this._mqttBrokerUrl, this._mqttOptions);
|
2017-07-22 23:39:14 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this._mqttClient.on('message', (topic, payload) => {
|
|
|
|
log.info(`message received, topic ${topic}, payload ${payload}`);
|
|
|
|
for (let topicHandler of this._topicHandlers) {
|
|
|
|
if (this.topicMatch(topicHandler.topic, topic)) {
|
|
|
|
log.info(`received topic ${topic} matches registered topic ${topicHandler.topic}`);
|
2017-08-09 00:03:57 +02:00
|
|
|
topicHandler.root.send(payload);
|
2017-07-22 23:39:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
topicMatch(registeredTopic, receivedTopic) {
|
|
|
|
let registeredTopicFields = registeredTopic.split('/');
|
|
|
|
let receivedTopicFields = receivedTopic.split('/');
|
|
|
|
for (let field in registeredTopicFields) {
|
|
|
|
let regField = registeredTopicFields[field];
|
|
|
|
let recvField = receivedTopicFields[field];
|
|
|
|
log.info(`recv: ${recvField}, reg: ${regField}`);
|
|
|
|
if (regField === "#") {
|
|
|
|
log.info('true');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (regField != recvField && regField != "+") {
|
|
|
|
log.info('false');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (registeredTopicFields.length == receivedTopicFields.length) {
|
|
|
|
log.info('true');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log.info('false');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-09 00:21:14 +02:00
|
|
|
exports.MqttDispatcher = MqttDispatcher;
|
2017-08-09 00:24:22 +02:00
|
|
|
//# sourceMappingURL=mqttdispatcher.js.map
|