58 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-09-12 11:42:02 +01:00
const got = require('got');
2020-09-21 21:38:46 +01:00
const moment = require('moment');
2020-09-29 10:23:41 +01:00
const globalVars = require('./globalVars')
2020-09-12 11:42:02 +01:00
module.exports = {
request: async (input) => {
var options = {
json: {
"switch": JSON.stringify(input)
}
};
got.post(`http://192.168.4.5:2020/water/switch`, options)
.then((resp) => {
return resp
});
},
2020-09-14 12:11:37 +01:00
pause: async (time) => {
await new Promise(resolve => setTimeout(resolve, time));
},
2020-09-14 13:47:23 +01:00
nextEvent: async (time) => {
2020-09-21 21:38:46 +01:00
if (time === 'Thursday, January 1st, 1970 12:13:00 AM') {
2020-09-22 11:40:06 +01:00
return 'Not Set'
2020-09-21 21:38:46 +01:00
} else {
time = time.match(/.{11}$/gm)
time = time[0].toLowerCase()
var hours = parseInt(time.substr(0, 2));
if (time.indexOf('am') != -1 && hours == 12) {
time = time.replace('12', '0');
}
if (time.indexOf('pm') != -1 && hours < 12) {
time = time.replace(hours, (hours + 12));
}
time = time.replace(/(am|pm)/, '');
time = time.replace(/ /g, '')
return time.substring(0, time.length - 3);
2020-09-14 13:47:23 +01:00
}
2020-09-21 21:38:46 +01:00
},
time: async (nextEvent) => {
let currentDate = moment(new Date(new Date().toUTCString()))
nextEvent = moment(nextEvent, "dddd, MMMM Do, YYYY LTS")
return currentDate.isAfter(nextEvent);
2020-09-29 10:23:41 +01:00
},
clearPolling: async () => {
await clearInterval(globalVars.waterPolling);
await clearInterval(globalVars.heatingPolling)
await clearInterval(globalVars.piHolePolling)
await clearInterval(globalVars.weatherPolling)
},
clearGlobalVars: async () => {
globalVars.lastHeatingRequest = ""
globalVars.lastWaterRequest = ""
globalVars.lastWeatherRequest = ""
2020-09-14 13:47:23 +01:00
}
2020-09-12 11:42:02 +01:00
}