-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (37 loc) · 1.3 KB
/
index.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
const request = require('sync-request');
const EdimaxClient = require('./edimaxClient');
const edimaxClient = new EdimaxClient();
const sunsetISO8601 = () => {
const lat = process.env.EDIMAX_LAT;
const lng = process.env.EDIMAX_LNG;
const url = `https://api.sunrise-sunset.org/json?lat=${lat}&lng=${lng}&formatted=0`;
const res = request('GET', url).getBody();
return JSON.parse(res.toString()).results.sunset;
};
const withinThirtyMinsOfSunset = () => {
const now = new Date().valueOf();
const sunset = new Date(sunsetISO8601()).valueOf();
return (now >= (sunset - 1800000) && now <= sunset);
};
const generateErrorMessage = (timeToSwitchOn, plugIsOff) => {
let errors = [];
if (!timeToSwitchOn) {
errors.push("it's way before or after sunset")
}
if (!plugIsOff) {
errors.push("the plug is already on")
}
return `No need to switch on: ${errors.join(" and ")}.`
};
const main = async () => {
const timeToSwitchOn = withinThirtyMinsOfSunset()
const plugIsOff = await edimaxClient.plugIsOff()
if (timeToSwitchOn && plugIsOff) {
console.log("Switching on!");
edimaxClient.switchOn();
} else {
const errorMessage = generateErrorMessage(timeToSwitchOn, plugIsOff)
console.log(errorMessage)
}
};
main()