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

145 lines 7.16 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_toggle'; /** * checkParameter * * @param {toggleType.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 {toggleType.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); } /** * setTapoTurnOff * * @param {toggleType.configBase} config * @returns {Promise< tplinkTapoConnectWrapperType.tapoConnectResults >} */ async function setTapoTurnOff(config) { return await tplink_tapo_connect_wrapper_1.tplinkTapoConnectWrapper.getInstance(). setTapoTurnOff(config.email, config.password, config.deviceIp); } /** * setTapoTurnOn * * @param {toggleType.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); } /** * getTapoDeviceInfo * * @param {toggleType.configBase} config * @returns {Promise< tplinkTapoConnectWrapperType.tapoDeviceInfoResults >} */ async function getTapoDeviceInfo(config) { return await tplink_tapo_connect_wrapper_1.tplinkTapoConnectWrapper.getInstance(). getTapoDeviceInfo(config.email, config.password, config.deviceIp); } 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 }; // 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: toggle ret = await getTapoDeviceInfo(config); if (ret.result) { // Keep the original deviceIp since deviceId is not an IP address node.status({ fill: "yellow", shape: "dot", text: "resources.message.processing" }); // デバイスのON・OFF状態をコンソールログに出力 const deviceStatus = ((_g = ret.tapoDeviceInfo) === null || _g === void 0 ? void 0 : _g.device_on) ? "ON" : "OFF"; console.log(`[${REGISTER_TYPE}] デバイス状態: ${deviceStatus} (IP: ${config.deviceIp})`); // device on/off? switch ((_h = ret.tapoDeviceInfo) === null || _h === void 0 ? void 0 : _h.device_on) { case true: console.log(`[${REGISTER_TYPE}] デバイスをOFFに切り替え中... (IP: ${config.deviceIp})`); ret = await setTapoTurnOff(config); break; case false: console.log(`[${REGISTER_TYPE}] デバイスをONに切り替え中... (IP: ${config.deviceIp})`); ret = await setTapoTurnOn(config); break; default: throw new Error("tapoDeviceInfo.device_on not found."); } // 操作結果をログに出力 if (ret.result) { console.log(`[${REGISTER_TYPE}] トグル操作完了 (IP: ${config.deviceIp})`); } else { console.log(`[${REGISTER_TYPE}] トグル操作失敗 (IP: ${config.deviceIp})`); } } else { if (ret === null || ret === void 0 ? void 0 : ret.errorInf) { throw new Error(ret.errorInf.message); } throw new Error("faild to get tapo device info."); } } 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: error }; } node.send(msg); }); } RED.nodes.registerType(REGISTER_TYPE, tplinkTapoConnectApiConstructor, { credentials: { email: { type: "text" }, password: { type: "password" } } }); }; module.exports = nodeInit; //# sourceMappingURL=tplink_toggle.js.map