forked from k-0/MMM-Canteen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
71 lines (62 loc) · 2.04 KB
/
node_helper.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
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
const NodeHelper = require("node_helper");
const Log = require("logger");
module.exports = NodeHelper.create({
start () {
Log.log(`Starting module helper: ${this.name}`);
},
socketNotificationReceived (notification, payload) {
if (notification === "CONFIG") {
this.config = payload.config;
this.collectData(payload.identifier);
const that = this;
setInterval(
() => {
that.collectData(payload.identifier);
},
this.config.updateInterval
);
}
},
async collectData (identifier) {
let done = false;
let extraDays = 0;
const data = {};
const date = new Date();
const [switchTimeHour, switchTimeMinute] = this.config.switchTime.split(":");
const switchTime = new Date().setHours(switchTimeHour, switchTimeMinute, 0, 0);
const isAfterSwitchTime = date > switchTime;
if (isAfterSwitchTime) {
date.setDate(date.getDate() + 1);
}
data.date = new Date(date)
.toISOString()
.slice(0, 10);
data.identifier = identifier;
while (extraDays < 7 && !done) {
const requestURL = `https://openmensa.org/api/v2/canteens/${this.config.canteen}/days/${data.date}/meals`;
Log.debug(`[MMM-Canteen] requestURL: ${requestURL}`);
const that = this;
try {
const response = await fetch(requestURL);
if (response.status === 404) {
Log.info(`[MMM-Canteen] Mensa closed on ${data.date} trying next day…`);
data.extraDays = extraDays;
that.sendSocketNotification("CLOSED", data);
date.setDate(date.getDate() + 1);
data.date = new Date(date)
.toISOString()
.slice(0, 10);
extraDays += 1;
} else {
Log.info(`[MMM-Canteen] Received menu for ${data.date}.`);
data.meals = await response.json();
Log.debug("MEALS", data);
that.sendSocketNotification("MEALS", data);
done = true;
}
} catch (error) {
Log.error(`[MMM-Canteen] ${error}`);
}
}
}
});