summaryrefslogtreecommitdiff
path: root/alarm/index.js
blob: a601ab6b20cc5a6767c4b6342b30ee52312e8d42 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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();
})()