UNPKG

homebridge-rituals-genie

Version:
214 lines 8.89 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RitualsGeniePlatform = void 0; const settings_1 = require("./settings"); const node_fetch_1 = __importDefault(require("node-fetch")); const node_localstorage_1 = require("node-localstorage"); const FanAccessory_1 = require("./FanAccessory"); class RitualsGeniePlatform { constructor(log, config, api) { this.log = log; this.config = config; this.api = api; this.Service = this.api.hap.Service; this.Characteristic = this.api.hap.Characteristic; this.accessories = []; this.name = this.config.name; this.email = this.config.email; this.password = this.config.password; this.storage = new node_localstorage_1.LocalStorage('./rituals_storage'); this.log.info('Finished initializing platform:', this.config.name); this.api.on("didFinishLaunching" /* DID_FINISH_LAUNCHING */, () => { log.info('Executed didFinishLaunching callback'); this.onDidFinishLaunching().then(() => this.log.info('Finished configuring')).catch(e => this.log.error(e)); }); } configureAccessory(accessory) { this.log.info('Loading accessory from cache:', accessory.displayName); this.accessories.push(accessory); } async onDidFinishLaunching() { try { const accountHash = this.storage.getItem('rituals_account_hash'); if (accountHash == null || accountHash === '') { this.log.info('Logging in'); await this.login(); } else { this.log.info('Account hash restored from storage'); this.accountHash = accountHash; } const hubHash = this.storage.getItem('rituals_hub_hash'); if (hubHash == null || hubHash === '') { this.log.info('Getting hub hash'); await this.getHub(); } else { this.log.info('Hub hash restored from storage'); this.hubHash = hubHash; } await this.getStateForHub(); this.addFanAccessory(); } catch (error) { const _error = error; this.log.error('Configuring failed!'); this.log.error(_error.message); } } addFanAccessory() { const uuid = this.api.hap.uuid.generate('fan'); const existingFan = this.accessories.find(accessory => accessory.UUID === uuid); if (existingFan) { this.log.info('Restoring existing accessory from cache:', existingFan.displayName); new FanAccessory_1.FanAccessory(this, existingFan); } else { this.log.info('Adding new accessory: Fan'); const accessory = new this.api.platformAccessory('Fan', uuid); accessory.context.displayName = this.config.name; new FanAccessory_1.FanAccessory(this, accessory); this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]); } } async login() { const body = { email: this.email, password: this.password, }; const formBody = Object.keys(body).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(body[key])).join('&'); try { const response = await (0, node_fetch_1.default)(`${settings_1.API_BASE_URL}/ocapi/login`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36', }, body: formBody, }); if (response.status === 200) { const loginResponse = await response.json(); if (loginResponse != null && loginResponse.account_hash != null) { this.storage.setItem('rituals_account_hash', loginResponse.account_hash); this.accountHash = loginResponse.account_hash; this.log.info('Logged in successfully!'); } else { this.log.error('Login failed!'); } } else { this.log.error('Login failed!'); } } catch (error) { const _error = error; this.log.error('Login Failed!'); this.log.error(_error.message); } } async getHub() { try { const response = await (0, node_fetch_1.default)(`${settings_1.API_BASE_URL}/api/account/hubs/${this.accountHash}`, { method: 'GET', headers: { 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36', }, }); if (response.status === 200) { const hubsResponse = await response.json(); if (hubsResponse != null && hubsResponse.length > 0) { const hubResponse = hubsResponse[0]; this.log.info('HubResponse found!'); this.storage.setItem('rituals_hub_hash', hubResponse.hub.hash); this.hubHash = hubResponse.hub.hash; } else { this.log.error('No hubs found!'); } } else { this.log.error('No hubs found!'); } } catch (error) { const _error = error; this.log.error('Get HubResponse Failed!'); this.log.error(_error.message); } } async getStateForHub() { try { const response = await (0, node_fetch_1.default)(`${settings_1.API_BASE_URL}/api/account/hub/${this.hubHash}`, { method: 'GET', headers: { 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36', }, }); if (response.status === 200) { const hubResponse = await response.json(); if (hubResponse != null && hubResponse.hub != null) { this.log.info('HubResponse found!'); this.hub = hubResponse.hub; this.lastHubState = new Date(); } else { this.log.error('No hubs found! Response is null'); } } else { this.log.error('No hubs found! Status is not 200'); } } catch (error) { const _error = error; this.log.error('Get State Failed!'); this.log.error(_error.message); } } async updateOnState(onState) { const body = { hub: this.hubHash, json: `{ "attr": { "fanc": "${onState ? 1 : 0}" } }`, }; const formBody = Object.keys(body).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(body[key])).join('&'); await this.updateHub(formBody); } async updateFanSpeed(speedValue) { const body = { hub: this.hubHash, json: `{ "attr": { "speedc": "${speedValue}" } }`, }; const formBody = Object.keys(body).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(body[key])).join('&'); await this.updateHub(formBody); } async updateHub(formBody) { try { const response = await (0, node_fetch_1.default)(`${settings_1.API_BASE_URL}/api/hub/update/attr`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36', }, body: formBody, }); if (response.status === 200) { this.log.info('Updated hub successfully!'); return this.getStateForHub(); } else { this.log.error('Update hub failed!'); } } catch (error) { const _error = error; this.log.error('Update hub Failed!'); this.log.error(_error.message); } } } exports.RitualsGeniePlatform = RitualsGeniePlatform; //# sourceMappingURL=platform.js.map