const { Analysis, Device, Utils, Services } = require("@tago-io/sdk");
async function mqttPushExample(context, scope) {
if (!scope.length) {
return context.log("This analysis must be triggered by a dashboard.");
}
console.log("my scope:", scope);
const my_temp_max = scope.find((x) => x.variable === "temp_max") || scope[0];
const my_temp_min = scope.find((x) => x.variable === "temp_min") || scope[0];
console.log("temp max: ",my_temp_max.value);
console.log("temp min: ",my_temp_min.value);
if (!my_temp_max || !my_temp_min) {
return context.log("Couldn't find any variable in the scope.");
}
const env_vars = Utils.envToJson(context.environment);
if (!env_vars.device_token) {
return context.log("Device token not found on environment parameters");
}
const device_iSpindel = new Device({ token: env_vars.device_token });
const result = await device_iSpindel.getData({variable: "temperature"});
console.log("iSpindel: ", result[0].value);
if(result[0].value > my_temp_max.value){
console.log("\n Ligou a Geladeira");
}else if (result[0].value < my_temp_min.value){
console.log("\n Desligou a Geladeira");
}
}
module.exports = new Analysis(mqttPushExample);