const { Analysis, Device, Utils } = require("@tago-io/sdk");
async function hdaEncantada(context) {
try {
const env_vars = Utils.envToJson(context.environment);
const strega7_master = new Device({ token: env_vars.strega7_master });
const vivid_master = new Device({ token: env_vars.vivid_master });
const vivid_data = new Device({ token: env_vars.vivid_data });
const result_master = { variable: "state", qty: 4 };
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_prueba", 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_prueba", 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);
let undefined_OpenId;
let sendUndefinedOpen = true;
if (state_master[0] === "Open") {
if (state_master[1] === "Close") {
return;
} else {
undefined_OpenId = setTimeout(() => {
const newDate = {
variable: "state",
value: "Undefined_Open",
};
vivid_master.sendData(newDate);
}, 60 * 1000);
}
} else if (state_master[0] === "Undefined_Open") {
if (state_master[1] === "Close") {
return;
} else {
// APAGAR AIRE MASTER
const newMaster = {
variable: "switch_simulacion",
value: 0,
};
await strega7_master.sendData(newMaster);
console.log("MASTER --> APAGADO");
}
} else if (state_master[0] === "Close") {
if (state_master[2] === "Close") {
return;
} else {
setTimeout(() => {
const newDate = {
variable: "state",
value: "Undefined_Close",
};
vivid_master.sendData(newDate);
}, 15 * 1000);
}
} else if (state_master[0] === "Undefined_Close") {
if (state_master[2] === "Close") {
return;
} else if (state_master[2]=== "Open"){
clearTimeout(undefined_OpenId);
context.terminate()
}else {
// ENCENDER AIRE MASTER
const newMaster = {
variable: "switch_simulacion",
value: 1,
};
await strega7_master.sendData(newMaster);
console.log("MASTER --> ENCENDER");
}
}
} catch (error) {
console.log(error);
}
}
module.exports = new Analysis(hdaEncantada);