UNPKG

homebridge-nest-cam-ft

Version:

Nest cam plugin for homebridge: https://homebridge.io/

161 lines 6.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UiServer = void 0; const plugin_ui_utils_1 = require("@homebridge/plugin-ui-utils"); const connection_1 = require("../nest/connection"); const structure_1 = require("../nest/structure"); const cam_1 = require("../nest/cam"); class UiServer extends plugin_ui_utils_1.HomebridgePluginUiServer { constructor() { super(); this.ft = false; this.onRequest('/logout', this.handleLogoutRequest.bind(this)); this.onRequest('/auth', this.handleAuthRequest.bind(this)); this.onRequest('/structures', this.handleStructureRequest.bind(this)); this.onRequest('/cameras', this.handleCamerasRequest.bind(this)); this.onRequest('/faces', this.handleFacesRequest.bind(this)); this.onRequest('/zones', this.handleZonesRequest.bind(this)); this.onRequest('/owner', this.handleOwnerRequest.bind(this)); this.onRequest('/generateToken', this.handleGenerateTokenRequest.bind(this)); this.onRequest('/getRefreshToken', this.handleGetRefreshTokenRequest.bind(this)); this.ready(); } generateConfig() { if (this.accessToken) { const config = { platform: 'Nest-cam', fieldTest: this.ft, access_token: this.accessToken, }; return config; } } async handleAuthRequest(payload) { this.accessToken = await (0, connection_1.auth)(payload.refreshToken, payload.ft); this.ft = payload.ft; return this.accessToken ? true : false; } async handleStructureRequest() { const config = this.generateConfig(); if (config) { const structures = []; const cameras = this.cameras || (await (0, connection_1.getCameras)(config)); this.cameras = cameras; cameras.forEach((cameraInfo) => { const exists = structures.find((x) => x.enum[0] === cameraInfo.nest_structure_id.replace('structure.', '')); if (!exists) { structures.push({ title: cameraInfo.nest_structure_name, enum: [cameraInfo.nest_structure_id.replace('structure.', '')], }); } }); return structures; } } async handleOwnerRequest() { const config = this.generateConfig(); if (config) { const cameras = this.cameras || (await (0, connection_1.getCameras)(config)); this.cameras = cameras; if (cameras && cameras.length > 0) { const structure = new structure_1.NestStructure(cameras[0], config); const members = await structure.getMembers(); const owner = members.find((m) => m.roles.includes('owner')); return owner; } } } async handleCamerasRequest() { const config = this.generateConfig(); if (config) { const cameras = this.cameras || (await (0, connection_1.getCameras)(config)); this.cameras = cameras; return cameras; } } async handleFacesRequest() { const config = this.generateConfig(); if (config) { const structures = []; let faces = []; const cameras = this.cameras || (await (0, connection_1.getCameras)(config)); for (const camera of cameras) { if (!structures.some((structure) => structure.id === camera.nest_structure_id.replace('structure.', ''))) { structures.push(new structure_1.NestStructure(camera, config)); } } for (const structure of structures) { const newFaces = await structure.getFaces(); if (newFaces) { faces = faces.concat(newFaces); } } return faces; } } async handleZonesRequest() { const config = this.generateConfig(); if (config) { let zones = []; const cameras = this.cameras || (await (0, connection_1.getCameras)(config)); for (const camera of cameras) { const cam = new cam_1.NestCam(config, camera); const newZones = await cam.getZones(); if (newZones) { zones = zones.concat(newZones); } } return zones; } } async handleLogoutRequest() { this.cameras = undefined; } async sendToParent(request) { const promise = new Promise((resolve) => { this.onRequest(`/${request.action}`, async (payload) => { return resolve(payload); }); }); this.pushEvent(request.action, request.payload); return promise; } async setCredentials(credentials) { await this.sendToParent({ action: 'credentials', payload: credentials }); } async showError(msg) { await this.sendToParent({ action: 'error', payload: msg, }); } async handleGenerateTokenRequest(payload) { return (0, connection_1.generateToken)(payload.ft); } async handleGetRefreshTokenRequest(payload) { var _a, _b, _c, _d; if (!payload.code) { return ''; } try { return await (0, connection_1.getRefreshToken)(payload.code, payload.ft); } catch (err) { let msg = err; if ((_b = (_a = err.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.error_description) { msg = (_d = (_c = err.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.error_description; } else if (err.message) { msg = err.message; } await this.showError(msg); } return ''; } } exports.UiServer = UiServer; (() => { return new UiServer(); })(); //# sourceMappingURL=server.js.map