UNPKG

homebridge-blynk-plugin

Version:

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

151 lines 6.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BlynkWidgetDimmer = exports.BlynkWidgetButton = exports.BlynkWidgetBase = void 0; const accessories_1 = require("./accessories"); class BlynkWidgetBase { constructor(log, baseUrl, widget) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; this.got = require('got'); this.log = log; this.baseUrl = baseUrl; this.id = (_a = widget['id']) !== null && _a !== void 0 ? _a : 0; this.typeOf = accessories_1.HOMEKIT_TYPES[(_b = widget['typeOf']) === null || _b === void 0 ? void 0 : _b.toString().toUpperCase()]; this.name = (_c = widget['name']) !== null && _c !== void 0 ? _c : "Wojstead Button"; this.manufacturer = (_d = widget['manufacturer']) !== null && _d !== void 0 ? _d : "WojStead"; this.widgetType = (_e = widget['type']) !== null && _e !== void 0 ? _e : "BUTTON"; this.pinType = (_f = widget['pintype']) !== null && _f !== void 0 ? _f : "VIRUTAL"; this.pinNumber = (_g = widget['pinnumber']) !== null && _g !== void 0 ? _g : 0; this.pinLabel = (_h = widget['label']) !== null && _h !== void 0 ? _h : `${this.name}`; this.model = (_j = widget['model']) !== null && _j !== void 0 ? _j : this.pinLabel; this.token = (_k = widget['token']) !== null && _k !== void 0 ? _k : ""; this.pinUrlLabel = (this.pinType.toLowerCase() === 'virtual') ? `V${this.pinNumber}` : `D${this.pinNumber}`; } getId() { return this.id; } getTypeOf() { return this.typeOf; } getName() { return this.name; } getManufacturer() { return this.manufacturer; } getModel() { return this.model; } getWidgetType() { return this.widgetType; } getPinType() { return this.pinType; } getPinNumber() { return this.pinNumber; } getPinLabel() { return this.pinLabel; } getPin() { return `${this.baseUrl}/external/api/get?token=${this.token}&${this.pinUrlLabel}`; } async requestUrl(url) { const options = { dnsCache: true, retry: { limit: 5 } }; try { const response = await this.got(url, options); return response.body; } catch (error) { this.log.error(`acc requestUrl: ${error}`); return "[0]"; } } toString() { return `${this.manufacturer} made a ${this.widgetType}(${this.id}) named ${this.pinLabel} on pin ${this.pinUrlLabel}`; } } exports.BlynkWidgetBase = BlynkWidgetBase; class BlynkWidgetButton extends BlynkWidgetBase { constructor(log, baseUrl, widget) { var _a, _b, _c; super(log, baseUrl, widget); this.SWITCH_ON = '["1"]'; this.SWITCH_OFF = '["0"]'; this.minValue = (_a = widget['min']) !== null && _a !== void 0 ? _a : 0.0; this.maxValue = (_b = widget['max']) !== null && _b !== void 0 ? _b : 1.0; this.curValue = (_c = widget['value']) !== null && _c !== void 0 ? _c : 0; } setPin() { return `${this.baseUrl}/external/api/update?token=${this.token}&${this.pinUrlLabel}=${this.curValue}`; } setValue(value) { this.curValue = (value === 'true') ? 1 : 0; super.requestUrl(this.setPin()); this.log.debug(`setValue() --> value: ${value} | curValue: ${this.curValue}`); } getValue() { try { super.requestUrl(this.getPin()) .then((body) => { const valueJson = JSON.parse(body); this.curValue = valueJson[0]; }); } catch (error) { this.log.error(`failed on: ${error}`); } return this.curValue; } getMin() { return this.minValue; } getMax() { return this.maxValue; } toString() { return super.toString(); } } exports.BlynkWidgetButton = BlynkWidgetButton; class BlynkWidgetDimmer extends BlynkWidgetBase { constructor(log, baseUrl, widget) { var _a, _b, _c; super(log, baseUrl, widget); this.dimmerLow = 0; this.dimmerHigh = 100; this.dimmerCur = 0; this.dimmerLow = (_a = widget['min']) !== null && _a !== void 0 ? _a : 0.0; this.dimmerHigh = (_b = widget['max']) !== null && _b !== void 0 ? _b : 100.0; this.dimmerCur = (_c = widget['value']) !== null && _c !== void 0 ? _c : 0; if ((this.typeOf === accessories_1.HOMEKIT_TYPES.LIGHTBULB) && (this.dimmerHigh > 100)) { this.dimmerHigh = 100; } } getValue() { try { super.requestUrl(this.getPin()) .then((body) => { const valueJson = JSON.parse(body); this.dimmerCur = valueJson[0]; }); } catch (error) { this.log.error(`Dimmer get failed: ${error}`); } return this.dimmerCur; } getMin() { return this.dimmerLow; } getMax() { return this.dimmerHigh; } setPin() { return `${this.baseUrl}/external/api/update?token=${this.token}&${this.pinUrlLabel}=${+this.dimmerCur}`; } setValue(value) { const tempJsonInput = (value === "true" || value === "false") ? `["${(value === "true") ? this.dimmerHigh : this.dimmerLow}"]` : value; const valueJson = JSON.parse(tempJsonInput); const tempValue = +valueJson[0]; if (tempValue > this.dimmerHigh) { this.dimmerCur = this.dimmerHigh; } else if (tempValue < this.dimmerLow) { this.dimmerCur = this.dimmerLow; } else { this.dimmerCur = tempValue; } super.requestUrl(this.setPin()); this.log.debug(`Dimmer(${this.name}) value: ${tempValue} from ${value} --> ${this.dimmerCur}`); } setDimmerLow(value) { this.dimmerLow = value; } setDimmerHigh(value) { this.dimmerHigh = value; } toString() { return super.toString(); } } exports.BlynkWidgetDimmer = BlynkWidgetDimmer; //# sourceMappingURL=widget.js.map