UNPKG

homebridge-wideq

Version:
282 lines (281 loc) 15.4 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 __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WideQ = void 0; var wideq_1 = require("wideq"); var AccessoryUtil_1 = __importDefault(require("./lib/AccessoryUtil")); var ConfigUtil_1 = __importDefault(require("./lib/ConfigUtil")); var DeviceUtil_1 = __importDefault(require("./lib/DeviceUtil")); var LogUtil_1 = __importDefault(require("./lib/LogUtil")); var ParseUtil_1 = __importDefault(require("./lib/ParseUtil")); var packageFile = require('../package.json'); var delay = function (ms) { return new Promise(function (resolve) { return setTimeout(resolve, ms); }); }; var platformAccessory, accessory, service, characteristic, UUIDGen; function default_1(homebridge) { platformAccessory = homebridge.platformAccessory; accessory = homebridge.hap.Accessory; service = homebridge.hap.Service; characteristic = homebridge.hap.Characteristic; UUIDGen = homebridge.hap.uuid; homebridge.registerPlatform('homebridge-wideq', 'WideQ', WideQ, true); } exports.default = default_1; var WideQ = /** @class */ (function () { function WideQ(log, config, api) { this.log = log; this.config = config; this.api = api; this.Accessory = accessory; this.PlatformAccessory = platformAccessory; this.Service = service; this.Characteristic = characteristic; this.UUIDGen = UUIDGen; this.logger = new LogUtil_1.default(null, this.log); this.ConfigUtil = new ConfigUtil_1.default(this.config); this.DeviceUtil = new DeviceUtil_1.default(); this.AccessoryUtil = new AccessoryUtil_1.default(); this.ParseUtil = new ParseUtil_1.default(this); if (!this.api || !this.config) return; if (this.api.version < 2.2) { throw new Error('Unexpected API version. Please update your homebridge!'); } this.log('**************************************************************'); this.log('WideQ v' + packageFile.version + ' by NorDroN'); this.log('GitHub: https://github.com/NorDroN/homebridge-wideq'); this.log('Email: nordron@live.ru'); this.log('**************************************************************'); if (!this.ConfigUtil.refreshToken) { this.logger.error('Please add refresh_token!'); return; } this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this)); } WideQ.prototype.didFinishLaunching = function () { return __awaiter(this, void 0, void 0, function () { var preexistingAccessories, accessoriesToRemove, _a, promises; var _this = this; return __generator(this, function (_b) { switch (_b.label) { case 0: preexistingAccessories = Object.keys(this.AccessoryUtil.getAll()); accessoriesToRemove = preexistingAccessories; if (this.ConfigUtil.debug) this.logger.info('Create client from token and get devices'); _a = this; return [4 /*yield*/, wideq_1.Client.loadFromToken(this.ConfigUtil.refreshToken, this.ConfigUtil.country, this.ConfigUtil.language)]; case 1: _a.client = _b.sent(); return [4 /*yield*/, this.client.updateDevices()]; case 2: _b.sent(); promises = this.client.devices.map(function (d) { return __awaiter(_this, void 0, void 0, function () { var device, e_1, thisDeviceAccessoryUuid, createAccessories; return __generator(this, function (_a) { switch (_a.label) { case 0: if (this.ConfigUtil.debug) this.logger.info("Load device and start monitoring: " + d); return [4 /*yield*/, this.client.getDevice(d.id)]; case 1: device = _a.sent(); return [4 /*yield*/, device.load()]; case 2: _a.sent(); _a.label = 3; case 3: _a.trys.push([3, 5, , 6]); return [4 /*yield*/, device.startMonitor()]; case 4: _a.sent(); return [3 /*break*/, 6]; case 5: e_1 = _a.sent(); if (e_1 instanceof wideq_1.NotConnectedError) { this.logger.info("Device is not connected: " + d); } return [3 /*break*/, 6]; case 6: this.DeviceUtil.addOrUpdate(d.id, device); thisDeviceAccessoryUuid = this.ParseUtil.getByModel(device.device.type).getAccessoryUUID(d.id); createAccessories = this.ParseUtil.getCreateAccessories(device); if (!preexistingAccessories.includes(thisDeviceAccessoryUuid)) { this.logger.info("Accessory " + thisDeviceAccessoryUuid + " is new – adding"); this.registerPlatformAccessories([createAccessories]); this.ParseUtil.parserAccessories(device, {}); } else { this.logger.info("Accessory " + thisDeviceAccessoryUuid + " is already created/cached"); // remove accessoriesToRemove.splice(accessoriesToRemove.indexOf(thisDeviceAccessoryUuid), 1); } return [2 /*return*/]; } }); }); }); return [4 /*yield*/, Promise.all(promises)]; case 3: _b.sent(); this.unregisterPlatformAccessories(accessoriesToRemove.map(this.AccessoryUtil.getByUUID.bind(this.AccessoryUtil))); return [4 /*yield*/, this.runMonitoring()]; case 4: _b.sent(); return [2 /*return*/]; } }); }); }; WideQ.prototype.configureAccessory = function (accessory) { var _this = this; accessory.reachable = true; accessory.on('identify', function () { _this.logger.debug(accessory.displayName + ' Identify!!!'); }); if (this.AccessoryUtil) { this.AccessoryUtil.add(accessory); } }; WideQ.prototype.registerPlatformAccessories = function (accessories) { var _this = this; this.api.registerPlatformAccessories('homebridge-wideq', 'WideQ', accessories); accessories.forEach(function (accessory) { _this.logger.info('Create accessory - UUID: ' + accessory.UUID); _this.AccessoryUtil.add(accessory); }, this); }; WideQ.prototype.unregisterPlatformAccessories = function (accessories) { var _this = this; this.api.unregisterPlatformAccessories('homebridge-wideq', 'WideQ', accessories); accessories.forEach(function (accessory) { _this.logger.info('Delete accessory - UUID: ' + accessory.UUID); _this.AccessoryUtil.remove(accessory.UUID); }, this); }; WideQ.prototype.runMonitoring = function () { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { setInterval(function () { return __awaiter(_this, void 0, void 0, function () { var devices, promises; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: devices = Object.values(this.DeviceUtil.getAll()); promises = devices.map(function (device) { return __awaiter(_this, void 0, void 0, function () { var status; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getStatus(device)]; case 1: status = _a.sent(); if (status) this.ParseUtil.parserAccessories(device, status); return [2 /*return*/]; } }); }); }); return [4 /*yield*/, Promise.all(promises)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }, this.ConfigUtil.interval * 1000); return [2 /*return*/]; }); }); }; WideQ.prototype.getStatus = function (device) { return __awaiter(this, void 0, void 0, function () { var status_1, keys, _i, keys_1, key, e_2; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 7]); if (this.ConfigUtil.debug) this.logger.info("Polling status: " + device.device); return [4 /*yield*/, device.poll()]; case 1: status_1 = _a.sent(); if (!status_1) { this.logger.info("No status: " + device.device); return [2 /*return*/]; } if (this.ConfigUtil.debug) { this.logger.info("Status: " + device.device); keys = Reflect.ownKeys(status_1.constructor.prototype); for (_i = 0, keys_1 = keys; _i < keys_1.length; _i++) { key = keys_1[_i]; if (typeof key === 'string' && !['constructor'].includes(key)) { this.logger.info("- " + key + ": " + String(Reflect.get(status_1, key))); } } } return [2 /*return*/, status_1]; case 2: e_2 = _a.sent(); if (e_2 instanceof wideq_1.NotConnectedError) { this.logger.info("Device is not connected: " + device.device); return [2 /*return*/]; } this.logger.error(e_2); if (!(e_2 instanceof wideq_1.NotLoggedInError)) return [3 /*break*/, 6]; this.logger.info('Refresh token'); return [4 /*yield*/, device.stopMonitor()]; case 3: _a.sent(); return [4 /*yield*/, this.client.refresh()]; case 4: _a.sent(); return [4 /*yield*/, device.startMonitor()]; case 5: _a.sent(); _a.label = 6; case 6: return [3 /*break*/, 7]; case 7: return [2 /*return*/]; } }); }); }; return WideQ; }()); exports.WideQ = WideQ;