45 lines
1.4 KiB
JavaScript

const got = require('got');
const moment = require('moment');
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
});
},
pause: async (time) => {
await new Promise(resolve => setTimeout(resolve, time));
},
nextEvent: async (time) => {
if (time === 'Thursday, January 1st, 1970 12:13:00 AM') {
return 'No Timer Set'
} 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);
}
},
time: async (nextEvent) => {
let currentDate = moment(new Date(new Date().toUTCString()))
nextEvent = moment(nextEvent, "dddd, MMMM Do, YYYY LTS")
return currentDate.isAfter(nextEvent);
}
}