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
168 lines • 8.33 kB
JavaScript
;
// tplinkTapoConnectWrapper
const tplink_tapo_connect_wrapper_1 = require("./tplink-tapo-connect/wrapper/tplink-tapo-connect-wrapper");
const nodeInit = (RED) => {
const REGISTER_TYPE = 'tplink_command';
/**
* 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 {commandType.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.command = (_f = config === null || config === void 0 ? void 0 : config.command) !== null && _f !== void 0 ? _f : "";
// if(checkParameter(node)){
// switch(node.command){
// case 0:
// case 1:
// node.status({fill:"blue", shape:"dot", text:RED._("resources.message.ready") + `(${node.command})` });
// break;
// default:
// node.status({fill:"red", shape:"ring", text:"resources.message.configError"});
// break;
// }
// } else {
// node.status({fill:"red", shape:"ring", text:"resources.message.configError"});
// }
}
catch (error) {
node.status({ fill: "red", shape: "ring", text: "resources.message.error" });
node.error(error);
}
/**
* setTapoTurnOff
*
* @param {commandType.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 {commandType.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 {commandType.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, _j, _k, _l, _m;
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,
command: (_h = (_g = msg.payload) === null || _g === void 0 ? void 0 : _g.command) !== null && _h !== void 0 ? _h : node.command,
option: (_k = (_j = msg.payload) === null || _j === void 0 ? void 0 : _j.option) !== null && _k !== void 0 ? _k : node.option
};
// debug
console.log(`config[${REGISTER_TYPE}]:`, {
deviceIp: config.deviceIp,
command: config.command,
option: config.option,
email: config.email ? '[REDACTED]' : '',
password: config.password ? '[REDACTED]' : ''
});
// debug
let ret = {
result: false
};
if (checkParameter(config)) {
node.status({ fill: "yellow", shape: "dot", text: "resources.message.processing" });
switch (config.command) {
case "power":
switch ((_l = config === null || config === void 0 ? void 0 : config.option) === null || _l === void 0 ? void 0 : _l.power) {
case 0:
ret = await setTapoTurnOff(config);
break;
case 1:
ret = await setTapoTurnOn(config);
break;
default: throw new Error("command(option.power) not found.");
}
break;
case "status":
ret = await getTapoDeviceInfo(config);
break;
case "toggle":
ret = await getTapoDeviceInfo(config);
if (ret.result) {
switch ((_m = ret.tapoDeviceInfo) === null || _m === void 0 ? void 0 : _m.device_on) {
case true:
ret = await setTapoTurnOff(config);
break;
case false:
ret = await setTapoTurnOn(config);
break;
default: throw new Error("tapoDeviceInfo.device_on not found.");
}
}
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.");
}
break;
default:
throw new Error("command not found.");
}
}
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_command.js.map