UNPKG

homebridge-nest-cam-ft

Version:

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

93 lines 3.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NestSession = void 0; const endpoints_1 = require("./endpoints"); const KNOWN_BUCKET_TYPES = [ 'quartz', ]; const SUBSCRIBE_TIMEOUT = 850 + Math.round(250 * Math.random()); const RETRY_INTERVAL = 10000; const delay = function (time) { return new Promise((resolve) => setTimeout(resolve, time)); }; class NestSession { constructor(config, log) { var _a; this.subscribeFailures = 0; this.nestUserID = ''; this.endpoints = new endpoints_1.NestEndpoints((_a = config.options) === null || _a === void 0 ? void 0 : _a.fieldTest); this.config = config; this.log = log; } async getUserID() { try { const session = await this.endpoints.sendRequest(this.config.access_token, this.endpoints.CAMERA_API_HOSTNAME, '/api/v1/users.get_current', 'GET'); const userId = session.items[0].nest_user_id; this.nestUserID = userId; return userId; } catch (error) { (0, endpoints_1.handleError)(this.log, error, 'Error fetching session'); return ''; } } async getAppLaunch() { const userId = this.nestUserID || (await this.getUserID()); if (userId) { const data = { known_bucket_types: KNOWN_BUCKET_TYPES, known_bucket_versions: [], }; try { const appLaunch = await this.endpoints.sendRequest(this.config.nest_token || this.config.access_token, this.endpoints.NEST_API_HOSTNAME, `/api/0.1/user/${userId}/app_launch`, 'POST', 'json', 'application/json,text/json,text/javascript', true, data); return appLaunch; } catch (error) { (0, endpoints_1.handleError)(this.log, error, 'Error fetching app_launch'); } } } async subscribe(cameras) { var _a; const appLaunch = await this.getAppLaunch(); const userId = this.nestUserID || (await this.getUserID()); if (appLaunch) { const currDate = new Date(); const epoch = Math.round(currDate.getTime() / 1000); const data = { objects: appLaunch.updated_buckets, timeout: SUBSCRIBE_TIMEOUT, sessionID: `ios-${userId}.${String(Math.random()).substr(2, 5)}.${epoch}`, }; try { const response = await this.endpoints.sendRequest(this.config.nest_token || this.config.access_token, appLaunch.service_urls.urls.transport_url, '/v6/subscribe', 'POST', 'json', 'application/json,text/json,text/javascript', false, data); this.subscribeFailures = 0; const objects = response.objects; if (objects && Array.isArray(objects)) { for (const object of objects) { if (object.object_key) { const uuid = object.object_key.split('.')[1]; const camera = cameras.find((x) => x.info.uuid === uuid); if (camera) { (_a = this.log) === null || _a === void 0 ? void 0 : _a.debug(`Updating info for ${camera.info.name}`); camera.updateData(); } } } } } catch (error) { (0, endpoints_1.handleError)(this.log, error, 'Error subscribing', true); if (this.subscribeFailures < 10) { this.subscribeFailures++; } await delay(RETRY_INTERVAL * Math.pow(this.subscribeFailures, 2)); } finally { await this.subscribe(cameras); } } } } exports.NestSession = NestSession; //# sourceMappingURL=session.js.map