NubeIO / module-core-rql

0 stars 0 forks source link

example #2

Open NubeDev opened 11 months ago

NubeDev commented 11 months ago
/*
SCOPE:
get city weather data to dynamically control the indoor zone temperature based of the forecasted outside air temp

shutting down outside air vents when there is a dust storm or bush fire

the main goal is to reduce

1. call weather api and get current temp and forecasted temp
2. get current zone temp and shift the zone temp setpoint based on the forecast outside air temp (for example on a colder day lower the set-point)
3. call public holiday API

*/

function setpoint(outsideTemp) {
  let minSp = 18;
  let maxSp = 23;
  let sp = 22;
  if (outsideTemp < minSp) {
    return minSp;
  }

  if (outsideTemp > maxSp) {
    return maxSp;
  }

  if (outsideTemp > 18 && outsideTemp < 20) {
    return minSp + 0.5;
  }
  if (outsideTemp > 19 && outsideTemp < 21) {
    return minSp + 1;
  }
  if (outsideTemp > 21 && outsideTemp < 23) {
    return maxSp;
  }
  return sp;
}

let runAt = "30min";

let response = {};

let apiKey = "b8293d3cba3542838d622151232106";
let city = "Sydney";
let date = RQL.Date();

let fakeAlert = [
  {
    areas: "",
    category: "Other dangers",
    certainty: "",
    desc: "High forest fire danger until significant precipitation.\n\n    Vegetation is very easily ignited and very large areas may be affected.",
    effective: "2023-06-18T08:00:00Z",
    event: "High forest fire danger",
    expires: "2023-06-23T22:00:00Z",
    headline: "Meteorologisk Institutt",
    instruction: "",
    msgtype: "",
    note: "",
    severity: "",
    urgency: "",
  },
];

// date = "2023-12-25" // override the date for testing !!!!!!!!!!FOR TESTING!!!!!!!!!!

let isHolidayResp = RQL.IsPublicHoliday("2023", "AU", date);

let isHoliday = isHolidayResp.Result.IsPublicHoliday;
response.isHoliday = isHoliday;
let hostUUID = "hos_c4be792c63c74454";
if (isHoliday == true) {
  // is a holiday so trun off the A/C
  let pri = {
    P16: 0,
  };

  let pntEnable = "pnt_2bb8530d267140a1";
  RQL.WritePointValue(hostUUID, pntEnable, pri);
  response.turnOffAC = `did turn of AC as is holiday: ${isHolidayResp.Result.Name}`;
} else {
  // is not a holiday so trun on the A/C
  let pri = {
    P16: 1,
  };
  let pntEnable = "pnt_2bb8530d267140a1";
  RQL.WritePointValue(hostUUID, pntEnable, pri);
  response.turnOffAC = `did turn of AC as is holiday: ${isHolidayResp.Result.Name}`;

  // get weather data
  response.turnOffAC = `is not hoilday`;
  let getWeather = RQL.GetCurrentWeather(apiKey, city);
  let outSideAirTemp = getWeather.Result.Current.TempC;
  outSideAirTemp = 21; // override the temp for testing !!!!!!!!!!FOR TESTING!!!!!!!!!!
  response.outSideAirTemp = outSideAirTemp;
  response.weather = `current outside air temp:${outSideAirTemp}, humidity:${getWeather.Result.Current.Humidity},  condition:${getWeather.Result.Current.Condition.Text}`;

  // adjust SP
  pri = {
    P16: setpoint(outSideAirTemp),
  };
  let pntSp = "pnt_d2e2ced50da74b1a";
  let adjustSP = RQL.WritePointValue(hostUUID, pntSp, pri);
  response.adjustSP = `adjusted setpoint to: ${adjustSP.Result.PresentValue}`;

  let = getForecast = RQL.GetForecast(apiKey, city);
  let alerts = fakeAlert; // override the event for testing !!!!!!!!!!FOR TESTING!!!!!!!!!!
  let event = alerts[0].event;
  if (event == "High forest fire danger") {
    // adjust close damper
    pri = {
      P16: 1,
    };
    let pntDmp = "pnt_877a8285a91a43f5";
    let closeDamper = RQL.WritePointValue(hostUUID, pntDmp, pri);
    response.weatherEvent = true;
    response.weatherEventMessage = `closed dampers due to event:${event}`;
  } else {
    pri = {
      P16: 0,
    };
    let pntDmp = "pnt_877a8285a91a43f5";
    let closeDamper = RQL.WritePointValue(hostUUID, pntDmp, pri);
    response.weatherEvent = false;
  }
}

RQL.Result = response; 
NubeDev commented 9 months ago
let groups = RQL.GetAllHostsStatus();

let hostList = [];
for (let group of groups) {
  for (let host of group.Hosts) {
    let newHost = {};
    newHost["name"] = host.Name;
    newHost["uuid"] = host.UUID;
    newHost["failCount"] = host.PingFailCount;
    hostList.push(newHost);
  }
}

let alertsAdded = {
  count: 0,
};

let alertCount = 0;
for (let host of hostList) {
  if (host.failCount >= 0) {
    let body = {
      hostUUID: host.uuid,
      entityType: "gateway",
      type: "ping",
      status: "active",
      severity: "crucial",
      body: `host-name: ${host.name} fail count: ${host.failCount}`,
    };
    let newAlert = RQL.AddAlert(host.uuid, body);
    alertsAdded.count = alertCount = alertCount + 1;
    if (newAlert != null) {
      if (newAlert.UUID == null) {
        alertsAdded.body = body;
        alertsAdded.error = newAlert.toString();
      } else {
        alertsAdded.result = newAlert;
      }
    }
  }
}

if (alertsAdded.count >= 1) {
  //   RQL.Email();
}

RQL.Return = alertsAdded;