From the following code, how can I manage to delete the setTimeout so that Analysis does not consume me since I have a waiting time of 1 minute if it is "Door Open" . If a "Door Close" data arrives, the Analysis should cut the SetTimeOut of "Door Open" but it doesn't keep running.
const { Analysis, Device, Utils } = require("@tago-io/sdk");
async function hdaEncantada(context) {
try {
const env_vars = Utils.envToJson(context.environment);
const vivid_master = new Device({ token: env_vars.vivid_master });
const vivid_data = new Device({ token: env_vars.vivid_data });
const result_master = { variable: "hall_effect_state", qty: 7 };
const array_master = await vivid_master.getData(result_master);
const state_master = array_master.map((data) => data.value);
console.log("STATE MASTER -->", state_master);
const result_data = { variable: "master_simulacion", qty: 1 };
const array_data = await vivid_data.getData(result_data);
const state_data = array_data.map((data) => data.value);
console.log("master -->", state_data);
const result_ahorro = { variable: "ahorro_simulacion", qty: 1 };
const array_ahorro = await vivid_data.getData(result_ahorro);
const state_ahorro = array_ahorro.map((data) => data.value);
console.log("ahorro -->", state_ahorro);
const strega_master = new Device({ token: env_vars.strega_master });
const now = new Date();
const hour = now.getHours();
const minute = now.getMinutes();
const second = now.getSeconds();
if (state_data[0] === 1) {
if (state_ahorro[0] === '1') {
const newVariable = `Open_${hour}_${minute}_${second}`;
const newVariableClose = `Close_${hour}_${minute}_${second}`;
// -------- DOOR OPEN --------
if (state_master[0] === "Door Open") {
const newData = {
variable: "states",
value: newVariable,
};
await vivid_master.sendData(newData);
// SET TIME OUT
setTimeout(async () => {
const latestData = await vivid_master.getData({ variable: "states", qty: 1 }); // Obtener el último dato creado
const latestValue = latestData[0]?.value || ""; // Obtener el valor de la última variable o establecer una cadena vacía si no hay datos
if (latestValue === newVariable) {
const newDate = {
variable: "hall_effect_state",
value: "Undefined_Open",
};
vivid_master.sendData(newDate);
// --------------------------- APAGAR AIRE MASTER -------------------------------------
const newMaster = {
variable: "switch",
value: 0
}
await strega_master.sendData(newMaster);
console.log("MASTER --> APAGAR");
console.log(`La última variable creada coincide con ${newVariable}`);
// ------------------------ FIN APAGADO --------------------------
} else {
console.log(`La última variable creada no coincide con ${newVariable}`);
}
}, 60 * 1000);
}
// ------------------------------ DOOR CLOSE --------------------------
else if (state_master[0] === "Door Close") {
// -------------- NEW VARIABLE -------------------------
const newData = {
variable: "states",
value: newVariableClose,
};
await vivid_master.sendData(newData);
//---------- SET TIME OUT -----------
setTimeout(async () => {
const latestData = await vivid_master.getData({ variable: "states", qty: 1 }); // Obtener el último dato creado
const latestValue = latestData[0]?.value || ""; // Obtener el valor de la última variable o establecer una cadena vacía si no hay datos
if (latestValue === newVariableClose) {
const newDate = {
variable: "hall_effect_state",
value: "Undefined_Close",
};
vivid_master.sendData(newDate);
// -------------- ENCENDER AIRE MASTER -----------------------------
const newMaster = {
variable: "switch",
value: 1
}
await strega_master.sendData(newMaster);
console.log("MASTER --> ENCENDER");
console.log(`La última variable creada coincide con ${newVariableClose}`);
// ------------------------------ FIN ENCENDIDO --------------------
} else {
console.log(`La última variable creada no coincide con ${newVariableClose}`);
}
}, 15 * 1000);
}
} else {
if (state_master[0] === "Door Close") {
newData = {
variable: "hall_effect_state",
value: "Undefined_Close"
}
await vivid_master.sendData(newData);
} else if (state_master[0] === "Door Open") {
newData = {
variable: "hall_effect_state",
value: "Undefined_Open"
}
await vivid_master.sendData(newData);
return;
}
}
} else if (state_data[0] === 0) {
if (state_master[0] === "Door Close") {
const newData = {
variable: "hall_effect_state",
value: "Undefined_Close"
}
await vivid_master.sendData(newData);
} else if (state_master[0] === "Door Open") {
const newData = {
variable: "hall_effect_state",
value: "Undefined_Open"
}
await vivid_master.sendData(newData);
}
return;
}
} catch (error) {
console.log(error);
}
}
module.exports = new Analysis(hdaEncantada);