-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-script.js
58 lines (51 loc) · 1.49 KB
/
run-script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const { Axios } = require("axios");
const axios = new Axios({ baseURL: "http://127.0.0.1:5000" });
const cron = require("node-cron");
const temperatureThreshold = 3;
let previousTemperature = 0;
let previousPowerStatus = 0;
const mainTask = cron.schedule(
"* * * * *",
async () => {
try {
const { data: temperatureData } = await axios.get("/current-temp");
const { data: powerData } = await axios.get("/power");
const { data: targetTemperatureData } = await axios.get(
"/get-target-temp"
);
previousTemperature = temperatureData;
previousPowerStatus = powerData;
if (typeof temperatureData === "string" && powerData === "False") {
const temp = parseInt(temperatureData);
// Uses the set temperature of the stove to calcuate when it should turn it on
const tempThreshold =
typeof targetTemperatureData === "string"
? parseInt(targetTemperatureData) - temperatureThreshold
: 65;
if (temp < tempThreshold) {
await axios.get("/power-on");
console.log("Turning on stove, too cold");
}
}
} catch (err) {
console.log(err);
}
},
{ scheduled: false }
);
const loggingTask = cron.schedule(
"*/1 * * * *",
() => {
console.log(
new Date().toISOString(),
":\n Temp: ",
previousTemperature,
"\n Power: ",
previousPowerStatus,
"\n"
);
},
{ scheduled: false }
);
mainTask.start();
loggingTask.start();