diff options
author | Minteck <contact@minteck.org> | 2022-10-18 08:59:09 +0200 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2022-10-18 08:59:09 +0200 |
commit | 2c4ae43e688a9873e86211ea0e7aeb9ba770dd77 (patch) | |
tree | 17848d95522dab25d3cdeb9c4a6450e2a234861f /alarm/index.js | |
parent | 108525534c28013cfe1897c30e4565f9893f3766 (diff) | |
download | pluralconnect-2c4ae43e688a9873e86211ea0e7aeb9ba770dd77.tar.gz pluralconnect-2c4ae43e688a9873e86211ea0e7aeb9ba770dd77.tar.bz2 pluralconnect-2c4ae43e688a9873e86211ea0e7aeb9ba770dd77.zip |
Update
Diffstat (limited to 'alarm/index.js')
-rw-r--r-- | alarm/index.js | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/alarm/index.js b/alarm/index.js new file mode 100644 index 0000000..a601ab6 --- /dev/null +++ b/alarm/index.js @@ -0,0 +1,107 @@ +const axios = require('axios'); +const pronote = require('pronote-api'); +const config = require('../includes/app.json'); + +const url = config.pronote.url; +const username = config.pronote.user; +const password = config.pronote.password; + +async function getTimetable() { + const session = await pronote.login(url, username, password); + const timetable = await session.timetable(); + + let nextClass = timetable.filter(i => !i.isCancelled)[0]; + + return nextClass ? nextClass["from"] : null; +} + +async function notification(title, content, priority) { + await fetch("https://" + config.ntfy.server, { + method: "POST", + body: JSON.stringify({ + topic: config.ntfy.topic, + message: content, + title: title, + tags: [ "alarm" ], + priority: priority + }), + headers: { + "Authorization": "Basic " + Buffer.from(config.ntfy.user + ":" + config.ntfy.password).toString("base64"), + "Content-Type": "application/json" + } + }) +} + +global.wakeUpTime = null; +global.wakeUpTimeDiff = null; +global.sleepTime = null; +global.sleepTimeDiff = null; +global.sleepClockR = null; +global.sleepClockC = null; +global.wakeUpClockC = null; +global.wakeUpClockR = null; + +async function nightWarning() { + if (global.sleepTimeDiff) { + await notification("🌙 Good evening!", "You are going to sleep " + global.sleepTimeDiff + ", " + global.sleepClockC + " BST (" + global.sleepClockR + " CEST) so you can wake up " + global.wakeUpTimeDiff + ", " + global.wakeUpClockC + " BST (" + global.wakeUpClockR + " CEST). You will be reminded when it's time to go to sleep.", 3); + } else { + await notification("🌙 Good evening!", "You don't need to wake up at a specific time tomorrow, enjoy!", 3); + } +} + +async function nightReminder() { + if (global.sleepTimeDiff) { + await notification("🌙 Now let's sleep!", "You should get ready to sleep now so you can wake up " + global.wakeUpTimeDiff + ", " + global.wakeUpClockC + " BST (" + global.wakeUpClockR + " CEST).", 3); + } +} + +function digits(input) { + if (input < 10) { + return "0" + input; + } else { + return input; + } +} + +async function updateSleepTime() { + let classTime = await getTimetable(); + + if (classTime) { + global.wakeUpTime = new Date(classTime.getTime() - 5100000); + global.sleepTime = new Date(global.wakeUpTime.getTime() - 27000000); + + let diff = (wakeUpTime - new Date().getTime()) / 1000; + let diffHours = Math.floor(diff / 3600); + let diffMinutes = Math.floor((diff - diffHours * 3600) / 60); + + global.wakeUpTimeDiff = "in " + diffHours + " hour" + (diffHours > 1 ? "s" : "") + (diffMinutes > 0 ? " and " + diffMinutes + " minute" + (diffMinutes > 1 ? "s" : "") : ""); + + let diff2 = (sleepTime - new Date().getTime()) / 1000; + let diffHours2 = Math.floor(diff2 / 3600); + let diffMinutes2 = Math.floor((diff2 - diffHours2 * 3600) / 60); + + global.sleepTimeDiff = "in " + diffHours2 + " hour" + (diffHours2 > 1 ? "s" : "") + (diffMinutes2 > 0 ? " and " + diffMinutes2 + " minute" + (diffMinutes2 > 1 ? "s" : "") : ""); + + global.sleepClockR = (sleepTime.getUTCHours() + 2) + ":" + digits(sleepTime.getUTCMinutes()); + global.sleepClockC = (sleepTime.getUTCHours() + 1) + ":" + digits(sleepTime.getUTCMinutes()); + global.wakeUpClockR = (wakeUpTime.getUTCHours() + 2) + ":" + digits(wakeUpTime.getUTCMinutes()); + global.wakeUpClockC = (wakeUpTime.getUTCHours() + 1) + ":" + digits(wakeUpTime.getUTCMinutes()); + } else { + global.wakeUpTime = null; + global.wakeUpTimeDiff = null; + global.sleepTime = null; + global.sleepTimeDiff = null; + } + + console.log(global.wakeUpTime, "Waking up " + global.wakeUpTimeDiff + " (" + global.wakeUpClockR + "/" + global.wakeUpClockC + "), sleeping " + global.sleepTimeDiff + " (" + global.sleepClockR + "/" + global.sleepClockC + ")"); +} + +(async () => { + //await notification("Test notification", "Testing notification latency, you can safely ignore this notification.", 3); + setInterval(async () => { + await updateSleepTime(); + }, 3600000); + + await updateSleepTime(); + await nightReminder(); +})()
\ No newline at end of file |