UNPKG

homebridge-blynk-plugin

Version:

Based on Peter J Wojciechowski but updated to use the new API

199 lines 8.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BlynkDeviceConfig = exports.BlynkConfig = void 0; const accessories_1 = require("./accessories"); const widget_1 = require("./widget"); class BlynkConfig { constructor(hap, log, config) { var _a; this.DEFAULT_PLATFORM_NAME = "BlynkPlatform"; this.DEFAULT_BLYNK_POLLER_SECONDS = 10; this.NEED_CONFIG = ['serverurl', 'devices']; this.baseUrl = ""; this.hap = hap; this.log = log; for (const confKey of this.NEED_CONFIG) { const confValue = (_a = config[confKey]) !== null && _a !== void 0 ? _a : function () { throw new Error(`Missing configuration for ${confKey}`); }; switch (confKey) { case "serverurl": this.baseUrl = confValue; this.baseUrl = (this.baseUrl[this.baseUrl.length - 1] === '/') ? this.baseUrl.slice(0, -1) : this.baseUrl; break; case "devices": break; default: log.info(`Unknown configuration found: ${confKey}`); } } this.platform = String(config['platform']) || this.DEFAULT_PLATFORM_NAME; this.pollerSeconds = Number(config['pollerseconds']) || this.DEFAULT_BLYNK_POLLER_SECONDS; this.devices = new Array(); const confDevices = config['devices']; if (confDevices != undefined) { confDevices.forEach((device) => { const deviceConfig = new BlynkDeviceConfig(this.hap, this.log, this.baseUrl, device); this.devices.push(deviceConfig); }); } else { log.error("Devices are missing from your configuration"); } } } exports.BlynkConfig = BlynkConfig; class BlynkDeviceConfig { constructor(hap, log, baseUrl, config) { var _a, _b, _c; this.NEED_CONFIG = ['name', 'token']; this.token = ""; this.deviceId = 0; this.name = ""; this.hap = hap; this.log = log; for (const confKey of this.NEED_CONFIG) { const confValue = (_a = config[confKey]) !== null && _a !== void 0 ? _a : function () { throw new Error(`Device Config missing configuration ${confKey}`); }; switch (confKey) { case 'name': this.name = confValue; break; case 'token': this.token = confValue; break; default: log.info(`Unknown device configuration found ${confKey} -> ${confValue}`); } } this.serverUrl = `${baseUrl}`; this.manufacturer = (_b = config['manufacturer']) !== null && _b !== void 0 ? _b : "Wojstead"; this.discover = (_c = config['discover']) !== null && _c !== void 0 ? _c : false; this.widgets = new Array(); if (this.discover === false) { if (config['accessories'] === undefined) { this.log.error(`Discovery is set to false and accessories were not defined for ${this.name}.`); return; } const accList = config['accessories']; if (accList.length > 0) { accList.forEach((acc) => { var _a, _b; const widget = { 'id': acc['id'], 'deviceId': (_a = acc['deviceId']) !== null && _a !== void 0 ? _a : 0, 'label': acc['name'], 'pin': acc['pinnumber'], 'type': acc['type'], 'pinType': acc['pintype'], 'max': acc['max'], 'min': acc['min'], 'value': acc['value'], 'model': acc['model'], 'typeOf': (_b = acc['typeOf']) !== null && _b !== void 0 ? _b : accessories_1.HOMEKIT_TYPES.OUTLET, 'token': this.token }; this.log.info(`Adding accessory: ${widget.label}`); this.addWidget(widget); }); } else { this.log.warn(`Accessories were not defined and discover is set to false for "${this.name}".`); } } else { if (config['deviceId'] === undefined) { this.log.error(`Discovery is set but missing deviceId to link with token for ${this.name}, will attempt with deviceId 0.`); } else { this.deviceId = config['deviceId']; } } } addWidget(widget) { if (widget.deviceId === this.deviceId) { switch (widget.type) { case "NUMBER_INPUT": case "TIME_INPUT": case "GAUGE": case "SEGMENTED_CONTROL": case "TABS": case "LCD": this.log.debug(`addWidget skip: ${widget.label}[${widget.id}] - ${widget.type}`); break; case "SLIDER": this.widgets.push(new widget_1.BlynkWidgetDimmer(this.log, this.serverUrl, { "id": widget.id, "name": widget.label, "label": widget.label, "type": widget.type, "pintype": widget.pinType, "pinnumber": widget.pin, "min": widget.min, "max": widget.max, "typeOf": widget.typeOf, "model": widget.model, "manufacturer": this.manufacturer, "token": widget.token })); this.log.info(`addWidget found: ${this.widgets.slice(-1)[0].toString()}`); break; case "BUTTON": case "STYLED_BUTTON": this.widgets.push(new widget_1.BlynkWidgetButton(this.log, this.serverUrl, { "id": widget.id, "name": widget.label, "label": widget.label, "type": widget.type, "pintype": widget.pinType, "pinnumber": widget.pin, "min": widget.min, "max": widget.max, "typeOf": widget.typeOf, "model": widget.model, "manufacturer": this.manufacturer, "token": widget.token })); this.log.info(`addWidget found: ${this.widgets.slice(-1)[0].toString()}`); break; default: this.log.debug(`addWidget skipped item: ${widget.label} is a ${widget.type}`); } } } async readProject() { const project = await this.getProjectJSON(); this.name = project.name; project.widgets.forEach((widget) => { if (widget.type === "SLIDER") { widget.typeOf = accessories_1.HOMEKIT_TYPES.LIGHTBULB; } else { widget.typeOf = accessories_1.HOMEKIT_TYPES.OUTLET; } this.addWidget(widget); }); } async getProjectJSON() { const got = require('got'); const options = { dnsCache: true, retry: { limit: 10 } }; try { const response = await got(`${this.serverUrl}/project`, options); return JSON.parse(response.body); } catch (error) { if (error instanceof Error) { throw new Error(error.message); } else { throw new Error("Unknown nonsense thrown..."); } } } } exports.BlynkDeviceConfig = BlynkDeviceConfig; //# sourceMappingURL=config.js.map