UNPKG

@homenet/plugin-mqtt

Version:

Homenet plugin for buttons and sensors emitted over MQTT

142 lines 5.85 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); const button_1 = require("./button"); const sensor_1 = require("./sensor"); const mqtt = require("mqtt"); function create(annotate) { let MqttPluginLoader = class MqttPluginLoader { constructor(buttons, sensors, config, logger) { this._buttons = {}; this._subscriptions = {}; this._mqttConnected = false; this._logger = logger; this._config = config; this._init(); buttons.addType('mqtt', this._createButtonFactory()); sensors.addType('mqtt-trigger', this._createSensorsFactory('trigger')); sensors.addType('mqtt-value', this._createSensorsFactory('value')); } load() { } _init() { this._logger.info('Starting MQTT plugin'); const mqttConfig = this._config.mqtt || {}; const host = mqttConfig.host || 'localhost'; const mqttUri = 'mqtt://' + host; this._logger.info('Connecting to broker ' + mqttUri); const mqttClient = mqtt.connect(mqttUri); this._mqttConnected = false; mqttClient.on('connect', () => { this._logger.info('MQTT connected'); this._mqttConnected = true; this._onConnected(); }); mqttClient.on('close', () => { this._logger.info('MQTT closed'); this._mqttConnected = false; }); mqttClient.on('offline', () => { this._logger.info('MQTT offline'); this._mqttConnected = false; }); this._mqttClient = mqttClient; mqttClient.on('message', (topic, message) => { const msgObj = tryParse(message.toString()); const handler = this._subscriptions[topic]; if (handler) handler(msgObj); }); } _onConnected() { this._bindExistingSubscriptions(); } _addSubscription(topic, callback) { this._subscriptions[topic] = callback; if (!this._mqttConnected) return; this._subscribe(topic); } _subscribe(topic) { this._mqttClient.subscribe(topic); } _bindExistingSubscriptions() { for (const topic in this._subscriptions) { this._subscribe(topic); } } _createButtonFactory() { return (id, opts) => { this._logger.info(`Adding MQTT button: ${id}`); const button = new button_1.MqttButton(); this._buttons[id] = button; const baseTopic = `homenet/button/${id}/input`; this._addSubscription(`${baseTopic}/click`, (e) => { button.emit('click', e); }); this._addSubscription(`${baseTopic}/dblclick`, (e) => { button.emit('dblclick', e); }); this._addSubscription(`${baseTopic}/hold`, (e) => { button.emit('hold', e); }); return button; }; } _createSensorsFactory(type) { return (id, opts) => { const sensor = new sensor_1.MqttSensor(id, opts); const baseTopic = `homenet/sensor/${id}/input`; if (!type || type === 'trigger') { this._addSubscription(`${baseTopic}/trigger`, (e) => { sensor.emit('trigger'); }); sensor.isTrigger = true; sensor.isToggle = false; sensor.isValue = false; } else if (type && type === 'value') { this._addSubscription(`${baseTopic}/value`, (e) => { Object.keys(e).forEach(key => { sensor.emit('value', key, e[key]); }); }); sensor.isTrigger = false; sensor.isToggle = false; sensor.isValue = true; } return sensor; }; } }; MqttPluginLoader = __decorate([ annotate.plugin(), __param(0, annotate.service('IButtonManager')), __param(1, annotate.service('ISensorManager')), __param(2, annotate.service('IConfig')), __param(3, annotate.service('ILogger')), __metadata("design:paramtypes", [Object, Object, Object, Object]) ], MqttPluginLoader); return { MqttPluginLoader }; } exports.create = create; function tryParse(str) { try { return JSON.parse(str); } catch (err) { return str; } } //# sourceMappingURL=loader.js.map