typescriptifying
This commit is contained in:
87
dist/MqttDispatcher.js
vendored
Normal file
87
dist/MqttDispatcher.js
vendored
Normal file
@ -0,0 +1,87 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const logger = require("./log");
|
||||
const Mqtt = require("mqtt");
|
||||
const fs = require("fs");
|
||||
const config = require("./config");
|
||||
class MqttHandler {
|
||||
constructor() {
|
||||
this.mqttOptions = {};
|
||||
this.mqttBrokerUrl = config.dict.brokerUrl;
|
||||
if (config.dict.brokerUser && config.dict.brokerPass) {
|
||||
this.mqttOptions.username = config.dict.brokerUser;
|
||||
this.mqttOptions.password = config.dict.brokerPass;
|
||||
}
|
||||
if (config.dict.brokerCa) {
|
||||
this.mqttOptions.ca = fs.readFileSync(config.dict.brokerCa, 'ascii');
|
||||
this.mqttOptions.rejectUnauthorized = true;
|
||||
}
|
||||
this.topicHandlers = [];
|
||||
}
|
||||
register(topics, cb) {
|
||||
topics.forEach((topic) => {
|
||||
this.topicHandlers.push({ topic: topic, callback: cb });
|
||||
logger.info(`additional callback registered for ${topic}`);
|
||||
});
|
||||
}
|
||||
exec() {
|
||||
logger.info(`connecting to ${this.mqttBrokerUrl}`);
|
||||
this.mqttClient = Mqtt.connect(this.mqttBrokerUrl, this.mqttOptions);
|
||||
this.mqttClient.on('error', (err) => {
|
||||
logger.error(`Error in mqttHandler: ${err}`);
|
||||
});
|
||||
this.mqttClient.on('connect', () => {
|
||||
this.mqttClient.publish('dispatcher_ng/status', 'dispatcher_ng running');
|
||||
this.mqttClient.subscribe('dispatcher_ng/cmd');
|
||||
this.topicHandlers.forEach((topicHandler) => {
|
||||
this.mqttClient.subscribe(topicHandler.topic);
|
||||
logger.info(`${topicHandler.topic} subscribed`);
|
||||
});
|
||||
logger.info('mqtt connection established');
|
||||
});
|
||||
this.mqttClient.on('message', (topic, payload, packet) => {
|
||||
if (!packet.retain) {
|
||||
let payloadStr = payload.toString('UTF-8');
|
||||
logger.info(`message received on topic ${topic}: ${payload}`);
|
||||
this.processMessage(topic, payloadStr);
|
||||
}
|
||||
});
|
||||
}
|
||||
processMessage(topic, payload) {
|
||||
let found = false;
|
||||
this.topicHandlers.forEach((topicHandler) => {
|
||||
// logger.warn(`Test: ${subscribedTopic}, ${topic}`);
|
||||
// console.log(`Test: ${subscribedTopic}, ${topic}`);
|
||||
if (topicHandler.topic == topic) {
|
||||
// logger.warn('1');
|
||||
topicHandler.callback(topic, payload);
|
||||
found = true;
|
||||
}
|
||||
else if (topicHandler.topic.endsWith('#') &&
|
||||
(topicHandler.topic.substring(0, topicHandler.topic.length - 1) ==
|
||||
topic.substring(0, topicHandler.topic.length - 1))) {
|
||||
// logger.warn('2');
|
||||
// console.log('2');
|
||||
topicHandler.callback(topic, payload);
|
||||
found = true;
|
||||
}
|
||||
});
|
||||
return found;
|
||||
}
|
||||
send(topic, payload, internalFirst = false) {
|
||||
let sent = false;
|
||||
if (internalFirst) {
|
||||
logger.info(`Try internal sending: ${topic}`);
|
||||
sent = this.processMessage(topic, payload);
|
||||
}
|
||||
if (!sent) {
|
||||
logger.info(`External sending required: ${topic}`);
|
||||
this.mqttClient.publish(topic, payload);
|
||||
}
|
||||
else {
|
||||
logger.info(`Internally delivered: ${topic}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.mqttHandler = new MqttHandler();
|
||||
//# sourceMappingURL=MqttDispatcher.js.map
|
16
dist/config.js
vendored
Normal file
16
dist/config.js
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const fs = require("fs");
|
||||
const cmdargs = require("command-line-args");
|
||||
const logger = require("./log");
|
||||
const OPTION_DEFINITIONS = [
|
||||
{ name: 'verbose', alias: 'v', type: Boolean },
|
||||
{ name: 'config', alias: 'c', type: String, defaultValue: '~/dispatcher_ng.conf' }
|
||||
];
|
||||
function readConfig() {
|
||||
let options = cmdargs(OPTION_DEFINITIONS);
|
||||
exports.dict = JSON.parse(fs.readFileSync(options.config, "utf8"));
|
||||
logger.info(JSON.stringify(exports.dict));
|
||||
}
|
||||
exports.readConfig = readConfig;
|
||||
//# sourceMappingURL=config.js.map
|
82
dist/log.js
vendored
Normal file
82
dist/log.js
vendored
Normal file
@ -0,0 +1,82 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const moment = require("moment");
|
||||
const config = require("./config");
|
||||
const nodemailer = require("nodemailer");
|
||||
var Level;
|
||||
(function (Level) {
|
||||
Level[Level["All"] = 0] = "All";
|
||||
Level[Level["NoDebug"] = 1] = "NoDebug";
|
||||
Level[Level["NoDebugNoInfo"] = 2] = "NoDebugNoInfo";
|
||||
Level[Level["NoDebugNoInfoNoWarning"] = 3] = "NoDebugNoInfoNoWarning";
|
||||
})(Level || (Level = {}));
|
||||
var level = Level.NoDebug;
|
||||
function timestamp() {
|
||||
return moment().format('HH:mm:ss.SSS');
|
||||
}
|
||||
function setLevel(value) {
|
||||
switch (value) {
|
||||
case 'info':
|
||||
level = Level.NoDebug;
|
||||
break;
|
||||
case 'warn':
|
||||
level = Level.NoDebugNoInfo;
|
||||
break;
|
||||
case 'error':
|
||||
level = Level.NoDebugNoInfoNoWarning;
|
||||
break;
|
||||
default: level = Level.All;
|
||||
}
|
||||
}
|
||||
exports.setLevel = setLevel;
|
||||
function sendAlarmMail(subject, message) {
|
||||
let transport = nodemailer.createTransport({
|
||||
host: config.dict.smtpHost,
|
||||
port: config.dict.smtpPort,
|
||||
secure: false,
|
||||
tls: {
|
||||
rejectUnauthorized: false
|
||||
}
|
||||
});
|
||||
let mail = {
|
||||
from: config.dict.smtpSender,
|
||||
to: config.dict.smtpReceiver,
|
||||
subject: subject,
|
||||
text: message
|
||||
};
|
||||
transport.sendMail(mail)
|
||||
.then((v) => {
|
||||
info(`Mail sent, ${subject}, ${message}, ${v.response}`);
|
||||
})
|
||||
.catch((reason) => {
|
||||
error(`Failure when sending alarm mail: ${message}, ${reason}`);
|
||||
});
|
||||
}
|
||||
exports.sendAlarmMail = sendAlarmMail;
|
||||
function info(message) {
|
||||
if (level < Level.NoDebugNoInfo) {
|
||||
console.log(`${timestamp()} [ II ] ${message}`);
|
||||
}
|
||||
}
|
||||
exports.info = info;
|
||||
function warn(message) {
|
||||
if (level < Level.NoDebugNoInfoNoWarning) {
|
||||
console.log(`${timestamp()} [ WW ] ${message}`);
|
||||
}
|
||||
}
|
||||
exports.warn = warn;
|
||||
function error(message) {
|
||||
console.log(`${timestamp()} [ EE ] ${message}`);
|
||||
}
|
||||
exports.error = error;
|
||||
function success(message) {
|
||||
console.log(`${timestamp()} [ OK ] ${message}`);
|
||||
}
|
||||
exports.success = success;
|
||||
function debug(message) {
|
||||
if (level < Level.NoDebug) {
|
||||
console.log(`${timestamp()} [ DB ] ${message}`);
|
||||
}
|
||||
}
|
||||
exports.debug = debug;
|
||||
//# sourceMappingURL=log.js.map
|
17
dist/main.js
vendored
17
dist/main.js
vendored
@ -1,11 +1,18 @@
|
||||
class Test {
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const logger = require("./log");
|
||||
const config = require("./config");
|
||||
// import { mqttHandler } from './MqttDispatcher'
|
||||
config.readConfig();
|
||||
class Dispatcher {
|
||||
constructor() {
|
||||
console.log("Test constructed");
|
||||
logger.info("Dispatcher starting");
|
||||
}
|
||||
exec() {
|
||||
console.log("Hello world");
|
||||
logger.info("Hello world");
|
||||
// mqttHandler.exec()
|
||||
}
|
||||
}
|
||||
const test = new Test();
|
||||
test.exec();
|
||||
const dispatcher = new Dispatcher();
|
||||
dispatcher.exec();
|
||||
//# sourceMappingURL=main.js.map
|
Reference in New Issue
Block a user