UNPKG

homebridge-switchbot-bluetooth-platform

Version:

A Homebridge platform Plugin for controlling SwitchBot bots using BLE (Bluetooth Low Energry)

98 lines 4.42 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SwitchBotClient = void 0; const node_switchbot_1 = __importDefault(require("node-switchbot")); const node_cache_1 = __importDefault(require("node-cache")); const settings_1 = require("../settings"); const errorLogger_1 = require("../utils/errorLogger"); class SwitchBotClient { constructor(log) { this.client = new node_switchbot_1.default(); this.deviceCache = new node_cache_1.default({ stdTTL: settings_1.CACHE_TTL, checkperiod: settings_1.CHECK_CACHE_TTL_PERIOD, // We want to store reference to the switchbot device // since deep clone kills the connection to it useClones: false, deleteOnExpire: true, }); this.getDevice = async (address, scanDuration, retries = settings_1.DEFAULT_SCAN_RETRIES, waitBetweenRetries = settings_1.DEFAULT_SCAN_RETRY_COOLDOWN) => { this.log.info(`Getting SwitchBot device (address ${address})`); // const deviceFromCache = this.getDeviceFromCache(address); // if (deviceFromCache) { // return deviceFromCache; // } try { const deviceFromScan = await this.attemptRun(async () => this.getDeviceFromScan(address, scanDuration), retries, waitBetweenRetries); return deviceFromScan; } catch (e) { (0, errorLogger_1.logSwitchbotClientError)(this.log, e); return null; } }; this.setDeviceState = async (device, targetState, retries = settings_1.DEFAULT_SCAN_RETRIES, waitBeteenRetries = settings_1.DEFAULT_SCAN_RETRY_COOLDOWN) => { const setState = async () => { this.log.info(`Updating SwitchBot device (id ${device.address}) power state to ${targetState ? 'ON' : 'OFF'}`); if (targetState) { return device.turnOn(); } return device.turnOff(); }; return this.attemptRun(setState, retries, waitBeteenRetries); }; this.getDeviceFromScan = async (address, scanDuration, shouldCache = true) => { this.log.info(`Scanning for SwitchBot device (address ${address})`); const scannedDevices = await this.client.discover({ duration: scanDuration, model: 'H', quick: true, id: address, }); const noDeviceFound = !scannedDevices || scannedDevices.length <= 0; if (noDeviceFound) { throw new Error(`No Device found for address ${address}`); } const targetDevice = scannedDevices[0]; this.log.info(`Found SwitchBot device (address ${address})`); if (shouldCache) { this.setDeviceOnCache(address, targetDevice); } return targetDevice; }; this.getDeviceFromCache = (address) => { const device = this.deviceCache.get(address); if (device) { this.log.info(`Found SwitchBot device (address ${address}) on cache.`); return device; } this.log.info(`No SwitchBot device (address ${address}) was found on cache.`); return null; }; this.setDeviceOnCache = (address, device) => { this.log.info(`Setting device (address ${address}) on cache.`); this.deviceCache.set(address, device); }; this.log = log; } async attemptRun(action, retries, waitBetweenRetries) { try { const actionResult = await action(); return actionResult; } catch (e) { if (retries <= 0) { throw e; } this.log.info(`Failed attempt, Retrying. retries left: ${retries - 1}`); this.log.info(`Waiting ${waitBetweenRetries}ms before next retry`); await this.client.wait(waitBetweenRetries); return this.attemptRun(action, retries - 1, waitBetweenRetries); } } } exports.SwitchBotClient = SwitchBotClient; //# sourceMappingURL=switchBotClient.js.map