UNPKG

homebridge-tuya-laundry

Version:

Allows washer/dryer cycle completion notifications using Tuya smart plugs with power meter, now using local control.

82 lines 3.31 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (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.TelegramGateway = void 0; const node_telegram_bot_api_1 = __importDefault(require("node-telegram-bot-api")); const path = __importStar(require("path")); const fs = __importStar(require("fs")); class TelegramGateway { constructor(token, log, api) { this.log = log; this.api = api; this.subscribers = []; this.loadSubscribers(); this.bot = new node_telegram_bot_api_1.default(token, { polling: true }); this.bot.onText(/\/subscribe/, (msg, match) => { this.subscribers.push(msg.chat.id); this.saveSubscribers(); this.bot.sendMessage(msg.chat.id, 'Subscribe successful!'); }); this.bot.onText(/\/unsubscribe/, (msg, match) => { const index = this.subscribers.indexOf(msg.chat.id); if (index > -1) { this.subscribers.splice(index, 1); this.saveSubscribers(); this.bot.sendMessage(msg.chat.id, 'Unsubscribed...'); } else { this.bot.sendMessage(msg.chat.id, 'You are not subscribed!'); } }); } checkPersistPath() { if (!fs.existsSync(this.api.user.persistPath())) { fs.mkdirSync(this.api.user.persistPath()); } } loadSubscribers() { const file = path.join(this.api.user.persistPath(), 'LaundryTelegramSubscribers.json'); this.checkPersistPath(); try { const subscribersRaw = fs.readFileSync(file).toString(); this.subscribers = JSON.parse(subscribersRaw); this.log.info(`Loaded ${this.subscribers.length} Telegram subscribers from cache`); } catch (error) { this.subscribers = []; } } saveSubscribers() { const file = path.join(this.api.user.persistPath(), 'LaundryTelegramSubscribers.json'); this.checkPersistPath(); fs.writeFileSync(file, JSON.stringify(this.subscribers)); } async send(message) { for (const subscriber of this.subscribers) { await this.bot.sendMessage(subscriber, message); } } } exports.TelegramGateway = TelegramGateway; //# sourceMappingURL=telegramGateway.js.map