forked from gberg927/MMM-DisneyWaitTimes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
149 lines (131 loc) · 4.45 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const NodeHelper = require("node_helper");
const axios = require("axios");
const { DateTime } = require("luxon");
module.exports = NodeHelper.create({
start: function () {
console.log(`Starting module helper: ${this.name}`);
},
getWaitTimes: function (park) {
const sendError = (msg) => {
console.log(`Error Processing Wait Times: ${msg}`);
var payload = {
errorMessage: msg,
};
this.sendSocketNotification(
`ERROR_${park.entity.replace(/ /g, "_")}`,
payload
);
};
const getPark = () => {
return {
entity: park.entity,
rides: park.rides ?? [],
};
};
const processWaitTimes = async (selectedPark) => {
console.log(`${selectedPark.entity}: Processing Wait Times...`);
const waitTimes = await axios.get(
`https://api.themeparks.wiki/v1/entity/${selectedPark.entity}/live`
);
//console.log(waitTimes.data.liveData);
const results = [];
for (const ride of waitTimes.data.liveData) {
let showRide = false;
if (
Array.isArray(selectedPark.rides) &&
selectedPark.rides.length > 0
) {
//if rides are specified then limit to those
showRide = selectedPark.rides.includes(ride.id);
} else {
showRide = ride.entityType === "ATTRACTION" && "queue" in ride;
}
if (showRide) {
const result = {
name: ride.name,
status: ride?.status || null,
waitTime: ride?.queue?.STANDBY?.waitTime ?? null,
};
// console.log(result);
results.push(result);
}
}
results.sort((a, b) =>
a.name.toLowerCase().localeCompare(b.name.toLowerCase())
);
const payload = { waitTimes: results };
console.log(`${waitTimes.data.name}: Processed Wait Times...`);
this.sendSocketNotification(
`POPULATE_WAIT_TIMES_${selectedPark.entity}`,
payload
);
};
const processOpeningTimes = async (selectedPark) => {
console.log(`${selectedPark.entity}: Processing Opening Times...`);
const openingTimes = await axios.get(
`https://api.themeparks.wiki/v1/entity/${selectedPark.entity}/schedule`
);
if (!openingTimes.data || !openingTimes.data.schedule.length) {
return;
}
const today = DateTime.now()
.setZone(selectedPark.timezone)
.startOf("day");
const todayHours = openingTimes.data.schedule.find(
(looking) =>
looking.type === "OPERATING" &&
looking.date === today.toFormat("yyyy-MM-dd")
);
const futureHours = openingTimes.data.schedule
.filter(
(looking) =>
looking.type === "OPERATING" &&
looking.date !== today.toFormat("yyyy-MM-dd")
)
.map((day) => {
return (
DateTime.fromISO(day.openingTime).toLocaleString({
month: "numeric",
day: "numeric",
hour: "numeric",
minute: "2-digit",
}) +
"-" +
DateTime.fromISO(day.closingTime).toLocaleString({
hour: "numeric",
minute: "2-digit",
})
)
.replaceAll(":00", "")
.replace(", ", " ")
.replaceAll(" AM", "AM")
.replaceAll(" PM", "PM");
});
const openingTime = DateTime.fromISO(todayHours.openingTime)
.setZone(selectedPark.timezone)
.toFormat("hh:mm a");
const closingTime = DateTime.fromISO(todayHours.closingTime)
.setZone(selectedPark.timezone)
.toFormat("hh:mm a");
const payload = { openingTime, closingTime, futureHours };
console.log(`${openingTimes.data.name}: Processed Opening Times...`);
this.sendSocketNotification(
`POPULATE_OPENING_TIMES_${selectedPark.entity}`,
payload
);
};
const selectedPark = getPark();
if (!selectedPark) {
sendError("Selected park not found");
return;
}
processWaitTimes(selectedPark);
processOpeningTimes(selectedPark);
},
socketNotificationReceived: function (notification, payload) {
//console.log(notification, "tesT", payload);
if (notification === "GET_WAIT_TIMES") {
this.getWaitTimes(payload);
}
},
});