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.41 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_turn_on'; /** * checkParameter * * @param {turnOnType.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 {turnOnType.appNodeDef} config */ function tplinkTapoConnectApiConstructor(config) { var _a, _b, _c, _d, _e; 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 : ""; } catch (error) { node.status({ fill: "red", shape: "ring", text: "resources.message.error" }); node.error(error); } /** * setTapoTurnOn * * @param {turnOnType.configBase} config * @returns {Promise< tplinkTapoConnectWrapperType.tapoConnectResults >} */ async function setTapoTurnOn(config) { return await tplink_tapo_connect_wrapper_1.tplinkTapoConnectWrapper.getInstance(). setTapoTurnOn(config.email, config.password, config.deviceIp); } node.on('input', async (msg) => { var _a, _b, _c, _d, _e, _f; 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 }; // debug console.log(`config[${REGISTER_TYPE}]:`, { deviceIp: config.deviceIp, email: config.email ? '[REDACTED]' : '', password: config.password ? '[REDACTED]' : '' }); // debug let ret = { result: false }; if (checkParameter(config)) { // node: turn_on node.status({ fill: "yellow", shape: "dot", text: "resources.message.processing" }); ret = await setTapoTurnOn(config); } else { if (ret === null || ret === void 0 ? void 0 : ret.errorInf) { throw new Error(ret.errorInf.message); } 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_turn_on.js.map