UNPKG

@di-zed/yandex-smart-home

Version:

The Yandex Smart Home skills for the different device types.

174 lines (173 loc) 6.26 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MqttProvider = void 0; /** * @author DiZed Team * @copyright Copyright (c) DiZed Team (https://github.com/di-zed/) */ const fs_1 = __importDefault(require("fs")); const mqtt_1 = __importDefault(require("mqtt")); const mqttRepository_1 = __importDefault(require("../repositories/mqttRepository")); const configProvider_1 = __importDefault(require("./configProvider")); /** * MQTT Provider. */ class MqttProvider { constructor() { /** * MQTT Client. * * @protected */ this.client = undefined; } /** * Get MQTT Client. * * @returns mqtt.MqttClient | undefined */ getClient() { return this.client; } /** * Get MQTT Client. * Async method with reconnect possibility. * * @returns Promise<mqtt.MqttClient> */ getClientAsync() { return __awaiter(this, void 0, void 0, function* () { if (this.client === undefined) { this.client = yield this.connect(); } return this.client; }); } /** * By default, client connects when constructor is called. * To prevent this you can set "manualConnect" option to "true" and call "client.connect()" manually. * * @returns Promise<mqtt.MqttClient> * @see https://github.com/mqttjs/MQTT.js?tab=readme-ov-file#connect-async */ connect() { return __awaiter(this, void 0, void 0, function* () { const options = { keepalive: 120, reconnectPeriod: 1000, clean: true, }; const host = process.env.MQTT_HOST.trim(); const port = parseInt(process.env.MQTT_CONTAINER_PORT, 10); const username = process.env.MQTT_USERNAME.trim(); const password = process.env.MQTT_PASSWORD.trim(); const clientId = process.env.MQTT_CLIENT_ID.trim(); const ca = process.env.MQTT_CA.trim(); const cert = process.env.MQTT_CERT.trim(); const key = process.env.MQTT_KEY.trim(); const rejectUnauthorized = process.env.MQTT_REJECT_UNAUTHORIZED.trim(); if (port) { options.port = port; } if (username) { options.username = username; } if (password) { options.password = password; } if (clientId) { options.clientId = clientId; } if (ca) { options.ca = fs_1.default.readFileSync(ca); } if (cert) { options.cert = fs_1.default.readFileSync(cert); } if (key) { options.key = fs_1.default.readFileSync(key); } if (rejectUnauthorized) { options.rejectUnauthorized = rejectUnauthorized === '1'; } this.client = yield mqtt_1.default.connectAsync(host, options); return this.client; }); } /** * Subscribe to a topic or topics. * * @param eventMessageCallback * @returns Promise<ISubscriptionGrant[]> * @see https://github.com/mqttjs/MQTT.js?tab=readme-ov-file#mqttclientsubscribeasynctopictopic-arraytopic-object-options */ subscribe(eventMessageCallback) { return __awaiter(this, void 0, void 0, function* () { const config = yield mqttRepository_1.default.getConfig(); const client = yield this.getClientAsync(); const result = client.subscribeAsync(config.subscribeTopic); client.on('message', (topic, message) => { eventMessageCallback(topic, String(message)); }); return result; }); } /** * Publish a message to a topic. * * @param topic * @param message * @param opts * @returns Promise<Packet | undefined> * @see https://github.com/mqttjs/MQTT.js?tab=readme-ov-file#mqttclientpublishasynctopic-message-options */ publish(topic, message, opts) { return __awaiter(this, void 0, void 0, function* () { const client = yield this.getClientAsync(); return yield client.publishAsync(topic, message, opts); }); } /** * Close the client. * * @returns Promise<void> * @see https://github.com/mqttjs/MQTT.js?tab=readme-ov-file#mqttclientendasyncforce-options */ end() { return __awaiter(this, void 0, void 0, function* () { const client = yield this.getClientAsync(); return yield client.endAsync(); }); } /** * Listen Topic. * * @param topic * @param oldMessage * @param newMessage * @returns void */ listenTopic(topic, oldMessage, newMessage) { if (process.env.NODE_ENV === 'development' && process.env.LOG_LISTEN_TOPIC === '1') { console.log('Listen Topic.', { topic, newMessage }); } const callbackListenTopic = configProvider_1.default.getConfigOption('callbackListenTopic'); if (typeof callbackListenTopic === 'function') { callbackListenTopic(topic, oldMessage, newMessage); } } } exports.MqttProvider = MqttProvider; exports.default = new MqttProvider();