UNPKG

node-red-contrib-tplink-tapo-connect-api

Version:

This unofficial node-RED node allows connection to TP-Link Tapo devices. This project has been enhanced with AI support to enable new features. Starting with v0.50, we have added support for the KLAP protocol. To prioritize the operation of this node, we

97 lines 4.64 kB
"use strict"; // tplinkTapoConnectWrapper const tplink_tapo_connect_wrapper_1 = require("./tplink-tapo-connect/wrapper/tplink-tapo-connect-wrapper"); const nodeInit = (RED) => { const REGISTER_TYPE = 'tplink_brightness'; /** * checkParameter * * @param {brightnessType.configBase} config * @returns {boolean} */ function checkParameter(config) { let _result = false; if ((config === null || config === void 0 ? void 0 : config.email.length) > 0 && (config === null || config === void 0 ? void 0 : config.password.length) > 0 && (config === null || config === void 0 ? void 0 : config.deviceIp.length) > 0) { _result = true; } return _result; } /** * tplinkTapoConnectApiConstructor * * @param {any} this * @param {brightnessType.appNodeDef} config */ function tplinkTapoConnectApiConstructor(config) { var _a, _b, _c, _d, _e, _f; RED.nodes.createNode(this, config); let node = this; try { node.email = (_b = (_a = this === null || this === void 0 ? void 0 : this.credentials) === null || _a === void 0 ? void 0 : _a.email) !== null && _b !== void 0 ? _b : ""; node.password = (_d = (_c = this === null || this === void 0 ? void 0 : this.credentials) === null || _c === void 0 ? void 0 : _c.password) !== null && _d !== void 0 ? _d : ""; node.deviceIp = (_e = config === null || config === void 0 ? void 0 : config.deviceIp) !== null && _e !== void 0 ? _e : ""; node.brightness = (_f = config === null || config === void 0 ? void 0 : config.brightness) !== null && _f !== void 0 ? _f : 0; } catch (error) { node.status({ fill: "red", shape: "ring", text: "resources.message.error" }); node.error(error); } /** * * * @param {brightnessType.configBase} config * @returns {Promise< tplinkTapoConnectWrapperType.tapoConnectResults >} */ async function setTapoBrightness(config) { return await tplink_tapo_connect_wrapper_1.tplinkTapoConnectWrapper.getInstance(). setTapoBrightness(config.email, config.password, config.deviceIp, config.brightness); } node.on('input', async (msg) => { var _a, _b, _c, _d, _e, _f, _g, _h; try { // config let config = { email: (_b = (_a = msg.payload) === null || _a === void 0 ? void 0 : _a.email) !== null && _b !== void 0 ? _b : node.email, password: (_d = (_c = msg.payload) === null || _c === void 0 ? void 0 : _c.password) !== null && _d !== void 0 ? _d : node.password, deviceIp: (_f = (_e = msg.payload) === null || _e === void 0 ? void 0 : _e.deviceIp) !== null && _f !== void 0 ? _f : node.deviceIp, brightness: (_h = (_g = msg.payload) === null || _g === void 0 ? void 0 : _g.brightness) !== null && _h !== void 0 ? _h : node.brightness }; // debug console.log(`config[${REGISTER_TYPE}]:`, { deviceIp: config.deviceIp, brightness: config.brightness, email: config.email ? '[REDACTED]' : '', password: config.password ? '[REDACTED]' : '' }); // debug let ret = { result: false }; if (checkParameter(config)) { // node: brightness node.status({ fill: "yellow", shape: "dot", text: "resources.message.processing" }); ret = await setTapoBrightness(config); } else { throw new Error("faild to get config."); } msg.payload = ret; node.status({ fill: "green", shape: "dot", text: "resources.message.complete" }); } catch (error) { node.status({ fill: "red", shape: "ring", text: "resources.message.communicationError" }); node.error(error); msg.payload = { result: false, errorInf: /*{ name: "Error", message: error}*/ error }; } node.send(msg); }); } RED.nodes.registerType(REGISTER_TYPE, tplinkTapoConnectApiConstructor, { credentials: { email: { type: "text" }, password: { type: "password" } } }); }; module.exports = nodeInit; //# sourceMappingURL=tplink_brightness.js.map