UNPKG

@uboness/homebridge-unifi-access

Version:
131 lines 4.99 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Mqtt = void 0; const mqtt_1 = __importDefault(require("mqtt")); const node_events_1 = __importDefault(require("node:events")); class Mqtt { constructor(platform, unifi, config, logger) { this.emitter = new node_events_1.default(); this._state = 'init'; this._online = false; this.platform = platform; this.unifi = unifi; this.config = config; this.logger = logger.getLogger('mqtt'); } async start() { var _a; var _b, _c; const config = await (_a = this.platform.api.user.configPath(), Promise.resolve().then(() => __importStar(require(_a)))); this._state = 'starting'; this.client = await mqtt_1.default.connectAsync({ clientId: `homebridge-${config.bridge.name}-${this.platform.api.hap.uuid.generate(this.platform.api.user.configPath())}`, host: this.config.host, port: this.config.port, username: (_b = this.config.auth) === null || _b === void 0 ? void 0 : _b.username, password: (_c = this.config.auth) === null || _c === void 0 ? void 0 : _c.password, rejectUnauthorized: true, reconnectOnConnackError: true }); this._online = this.client.connected; this.client.on('offline', () => { this._online = false; this.logger.warn('offline'); this.emitter.emit('offline'); }); this.client.on('connect', () => { this._online = true; this.logger.info('(re)connected'); this.emitter.emit('online'); }); this.client.on('reconnect', () => { this.logger.info('attempting to reconnect...'); }); this.client.on('message', (fullTopic, msg) => { const topic = this.stripTopic(fullTopic); if (!topic) { return; } this.logger.debug(`message: topic [${topic}] value [${msg.toString('utf-8')}]`); }); this.unifi.on('message', msg => { if (!this.config.events || this.config.events.includes(msg.type)) { this.onMessage(msg); } }); this._state = 'started'; } async close() { var _a; this._state = 'closing'; await ((_a = this.client) === null || _a === void 0 ? void 0 : _a.endAsync()); this.client = undefined; this._state = 'closed'; } get state() { return this._state; } get online() { return this._online; } onMessage(msg) { const { type, ...rest } = msg; switch (msg.type) { case 'door-access': this.publish('access-event', JSON.stringify(rest)); return; case 'door-update': const state = rest; this.publish(`door/${state.id}`, JSON.stringify(state)); this.publish(`door/${state.name}`, JSON.stringify(state)); } } publish(topic, value) { var _a; topic = topic.startsWith('/') ? topic.substring(1) : topic; (_a = this.client) === null || _a === void 0 ? void 0 : _a.publishAsync(this.fullTopic(topic), `${value}`, { qos: 0, retain: true }); } stripTopic(fullTopic) { if (!this.config.baseTopic) { return fullTopic; } if (!fullTopic.startsWith(this.config.baseTopic)) { return; } return fullTopic.substring(this.config.baseTopic.length + 1); } fullTopic(topic) { return this.config.baseTopic ? `${this.config.baseTopic}/${topic}` : topic; } } exports.Mqtt = Mqtt; //# sourceMappingURL=mqtt.js.map