2018-04-18 09:25:40 -07:00
|
|
|
const mqtt = require('mqtt');
|
|
|
|
const logger = require('./util/logger');
|
|
|
|
const settings = require('./util/settings');
|
|
|
|
|
|
|
|
class MQTT {
|
|
|
|
constructor() {
|
|
|
|
this.handleConnect = this.handleConnect.bind(this);
|
|
|
|
this.handleMessage = this.handleMessage.bind(this);
|
|
|
|
}
|
|
|
|
|
2018-04-25 11:54:41 -07:00
|
|
|
connect(onMessage, subscriptions, callback) {
|
2018-04-18 09:25:40 -07:00
|
|
|
const mqttSettings = settings.get().mqtt;
|
|
|
|
logger.info(`Connecting to MQTT server at ${mqttSettings.server}`);
|
|
|
|
|
|
|
|
const options = {};
|
|
|
|
if (mqttSettings.user && mqttSettings.password) {
|
|
|
|
options.username = mqttSettings.user;
|
|
|
|
options.password = mqttSettings.password;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.client = mqtt.connect(mqttSettings.server, options);
|
|
|
|
|
|
|
|
// Register callbacks.
|
2018-04-23 09:17:47 -07:00
|
|
|
this.client.on('connect', () => {
|
|
|
|
this.handleConnect();
|
|
|
|
callback();
|
|
|
|
});
|
2018-04-25 11:54:41 -07:00
|
|
|
|
2018-04-18 09:25:40 -07:00
|
|
|
this.client.on('message', this.handleMessage);
|
|
|
|
|
|
|
|
// Set timer at interval to check if connected to MQTT server.
|
|
|
|
const interval = 10 * 1000; // seconds * 1000.
|
|
|
|
this.connectionTimer = setInterval(() => {
|
|
|
|
if (this.client.reconnecting) {
|
|
|
|
logger.error('Not connected to MQTT server!');
|
|
|
|
}
|
|
|
|
}, interval);
|
|
|
|
|
|
|
|
this.onMessage = onMessage;
|
2018-04-25 11:54:41 -07:00
|
|
|
this.subscriptions = subscriptions;
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
clearTimeout(this.connectionTimer);
|
|
|
|
this.connectionTimer = null;
|
|
|
|
|
2018-05-15 09:42:26 -07:00
|
|
|
this.publish('bridge/state', 'offline', {retain: true, qos: 0}, () => {
|
2018-04-18 11:53:22 -07:00
|
|
|
logger.info('Disconnecting from MQTT server');
|
|
|
|
this.client.end();
|
|
|
|
});
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
handleConnect() {
|
|
|
|
logger.info('Connected to MQTT server');
|
2018-05-15 09:42:26 -07:00
|
|
|
this.publish('bridge/state', 'online', {retain: true, qos: 0});
|
2018-04-25 11:54:41 -07:00
|
|
|
this.subscriptions.forEach((topic) => this.client.subscribe(topic));
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
handleMessage(topic, message) {
|
|
|
|
if (this.onMessage) {
|
|
|
|
this.onMessage(topic, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 08:20:46 -07:00
|
|
|
publish(topic, payload, options, callback, baseTopic) {
|
|
|
|
baseTopic = baseTopic ? baseTopic : settings.get().mqtt.base_topic;
|
|
|
|
topic = `${baseTopic}/${topic}`;
|
2018-04-18 09:25:40 -07:00
|
|
|
|
|
|
|
if (!this.client || this.client.reconnecting) {
|
|
|
|
logger.error(`Not connected to MQTT server!`);
|
|
|
|
logger.error(`Cannot send message: topic: '${topic}', payload: '${payload}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info(`MQTT publish, topic: '${topic}', payload: '${payload}'`);
|
2018-05-15 09:42:26 -07:00
|
|
|
this.client.publish(topic, payload, options, callback);
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MQTT;
|