UNPKG

homebridge-hatch-baby-rest-volume

Version:
111 lines (110 loc) 4.91 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.HatchBabyApi = void 0; const rest_client_1 = require("./rest-client"); const aws_iot_device_sdk_1 = require("aws-iot-device-sdk"); const util_1 = require("./util"); const hatch_baby_rest_plus_1 = require("./hatch-baby-rest-plus"); class HatchBabyApi { constructor(config) { this.config = config; this.restClient = new rest_client_1.RestClient(this.config); } getAccount() { return this.restClient.getAccount(); } getMember() { return this.restClient.request({ url: rest_client_1.apiPath('service/app/v2/member'), }); } getRestPlusLightsInfo() { return __awaiter(this, void 0, void 0, function* () { const hbrps = yield this.restClient.request({ url: rest_client_1.apiPath('service/app/restPlus/v1/fetch'), }); return hbrps; }); } createAwsIotClient() { return __awaiter(this, void 0, void 0, function* () { const iotResponse = yield this.restClient.request({ url: rest_client_1.apiPath('service/app/restPlus/token/v1/fetch'), }), { Credentials: credentials } = yield rest_client_1.requestWithRetry({ url: `https://cognito-identity.${iotResponse.region}.amazonaws.com`, method: 'POST', headers: { 'content-type': 'application/x-amz-json-1.1', 'X-Amz-Target': 'AWSCognitoIdentityService.GetCredentialsForIdentity', }, json: { IdentityId: iotResponse.identityId, Logins: { 'cognito-identity.amazonaws.com': iotResponse.token, }, }, }), mqttClient = new aws_iot_device_sdk_1.thingShadow({ protocol: 'wss', host: iotResponse.endpoint.replace('https://', ''), accessKeyId: credentials.AccessKeyId, secretKey: credentials.SecretKey, sessionToken: credentials.SessionToken, }); return mqttClient; }); } getRestPlusLights() { return __awaiter(this, void 0, void 0, function* () { const lightsInfo = yield this.getRestPlusLightsInfo(), lights = lightsInfo.map((info) => { return new hatch_baby_rest_plus_1.HatchBabyRestPlus(info); }); let bindingNewIotClient = false, previousMqttClient = null; const createNewIotClient = () => __awaiter(this, void 0, void 0, function* () { try { if (bindingNewIotClient) { return; } bindingNewIotClient = true; if (previousMqttClient) { previousMqttClient.end(); previousMqttClient = null; } util_1.logDebug('Creating new MQTT Client'); const mqttClient = yield this.createAwsIotClient(); previousMqttClient = mqttClient; mqttClient.on('error', (error) => { if (error.message.includes('(403)')) { util_1.logError('MQTT Client No Longer Authorized'); } else { util_1.logError('MQTT Error:'); util_1.logError(error); } createNewIotClient(); }); lights.forEach((light) => light.registerMqttClient(mqttClient)); util_1.logDebug('Created new MQTT Client'); } catch (e) { util_1.logError('Failed to Create an MQTT Client'); util_1.logError(e); } finally { bindingNewIotClient = false; } }); createNewIotClient().catch(); return lights; }); } } exports.HatchBabyApi = HatchBabyApi;