UNPKG

homebridge-bold

Version:
58 lines (57 loc) 2.46 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Device = void 0; /// Class representing an activatable Bold device. /// This class is used by each accessory to interact with the Bold API. class Device { constructor(config, bold) { this.config = config; this.bold = bold; // Initialize with current Date, making it not activated. this.activatedUntil = new Date(); } // Getters /// Whether the device is currently activated get isActivated() { return this.activatedUntil > new Date(); } // Methods setState(activate) { return __awaiter(this, void 0, void 0, function* () { if (activate) { if (yield this.bold.activate(this.config.id)) { // Store activation end date let date = new Date(); date.setSeconds(date.getSeconds() + this.config.settings.activationTime); this.activatedUntil = date; // Call state change handler this.onStateChange(true); // Deactivate after activation time setTimeout(() => { this.setState(false); }, this.config.settings.activationTime * 1000); } else { // If failed to activate, set device state to deactivated. yield this.setState(false); } } else { // Set activation end date to now this.activatedUntil = new Date(); // Call state change handler this.onStateChange(false); } }); } } exports.Device = Device;