-
Notifications
You must be signed in to change notification settings - Fork 2
/
MMM-TitanSchoolMealMenu.js
222 lines (188 loc) · 7.05 KB
/
MMM-TitanSchoolMealMenu.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
Module.register("MMM-TitanSchoolMealMenu", {
defaults: {
retryDelayMs: 20 * 1000, // milliseconds
updateIntervalMs: 60 * 60 * 1000, // milliseconds
numberOfDaysToDisplay: 3,
size: "medium",
recipeCategoriesToInclude: [
"Entrees",
"Grain"
// , "Fruit"
// , "Vegetable"
// , "Milk"
// , "Condiment"
// , "Extra"
],
debug: false
},
requiresVersion: "2.1.0", // Required version of MagicMirror
start: function () {
var self = this;
this.dataNotification = null; // This will contain the (formatted) data from the remote API after each request
this.dataError = false; // Toggle to true if an API request results in an error
this.loaded = false; // Toggle to true once this module has configured its API client and is ready to make API requests
// The instanceName is used to "namespace" the notifications when there are multiple instances of the TitanSchoolMealMenu module fetching different school menus
this.instanceName = `${this.config.buildingId}_${this.config.districtId}`;
// Send the module config to the node_helper
this.broadcastConfig({
retryDelayMs: this.config.retryDelayMs,
updateIntervalMs: this.config.updateIntervalMs,
buildingId: this.config.buildingId,
districtId: this.config.districtId,
recipeCategoriesToInclude: this.config.recipeCategoriesToInclude,
instanceName: this.instanceName,
debug: this.config.debug
});
// Schedule update timer
setInterval(function () {
self.getData();
// self.updateDom();
}, self.config.updateIntervalMs);
},
getNamespacedNotificationName: function (notificationName) {
return `TITANSCHOOLS_${notificationName}::${this.instanceName}`;
},
sendNamespacedSocketNotification: function (notificationName, config) {
return this.sendSocketNotification(
this.getNamespacedNotificationName(notificationName),
config
);
},
broadcastConfig: function (config) {
// This notification is intentionally *NOT NAMESPACED*
// This is the first notification that establishes a new instance of the TitaSchoolMealMenu module, so the
// namespaced notifications haven't been registered yet (the listeners don't know the name of this instance yet).
this.sendSocketNotification(`TITANSCHOOLS_SET_CONFIG`, config);
},
getData: function () {
this.sendNamespacedSocketNotification(`FETCH_DATA_REQUEST`, {});
},
scheduleUpdate: function (delay) {
var nextLoad = this.config.retryDelayMs;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setTimeout(function () {
self.getData();
}, nextLoad);
},
getDom: function () {
var self = this;
var wrapper = document.createElement("div");
if (!this.loaded) {
wrapper.innerHTML =
"<span class='small fa fa-refresh fa-spin fa-fw'></span>";
wrapper.className = "small dimmed";
return wrapper;
}
if (this.dataNotification && this.dataError) {
// The remote API responded with an error which is now stored in this.dataNotification
wrapper.innerHTML = `<div>${
this.dataNotification
}</div><div><span class='small fa fa-refresh fa-spin fa-fw'></span>Retry in ${
this.config.retryDelayMs / 1000
} seconds...</div>`;
wrapper.className = "error";
return wrapper;
}
// Data from helper
if (this.dataNotification && !this.dataError) {
console.log(this.dataNotification);
const wrapperDataNotification = document.createElement("div");
const meals = document.createElement("ul");
meals.className = `meal-list ${this.config.size || ""}`;
this.dataNotification.forEach((dayMenu, index) => {
if (index >= this.config.numberOfDaysToDisplay) {
return;
}
// Day list item.
const dayListItem = document.createElement("li");
const dayLabel = document.createElement("span");
dayLabel.className = "day-label";
dayLabel.innerHTML = dayMenu.label;
dayListItem.appendChild(dayLabel);
meals.appendChild(dayListItem);
// Breakfast.
const breakfastMenuList = document.createElement("ul");
const breakfastMenuItems = document.createElement("li");
const breakfastMenuTitle = document.createElement("span");
const breakfastMenuRecipes = document.createElement("span");
breakfastMenuTitle.innerHTML = "Breakfast: ";
breakfastMenuTitle.className = "meal-title";
breakfastMenuRecipes.innerHTML = dayMenu.breakfast ?? "none";
breakfastMenuRecipes.className = "meal-recipes";
breakfastMenuList.className = "meal-description";
breakfastMenuList.appendChild(breakfastMenuItems);
breakfastMenuItems.appendChild(breakfastMenuTitle);
breakfastMenuItems.appendChild(breakfastMenuRecipes);
dayListItem.appendChild(breakfastMenuList);
// Lunch.
const lunchMenuList = document.createElement("ul");
const lunchMenuItems = document.createElement("li");
const lunchMenuTitle = document.createElement("span");
const lunchMenuRecipes = document.createElement("span");
lunchMenuTitle.innerHTML = "Lunch: ";
lunchMenuTitle.className = "meal-title";
lunchMenuRecipes.innerHTML = dayMenu.lunch ?? "none";
lunchMenuRecipes.className = "meal-recipes";
lunchMenuList.className = "meal-description";
lunchMenuList.appendChild(lunchMenuItems);
lunchMenuItems.appendChild(lunchMenuTitle);
lunchMenuItems.appendChild(lunchMenuRecipes);
dayListItem.appendChild(lunchMenuList);
});
wrapperDataNotification.appendChild(meals);
wrapper.appendChild(wrapperDataNotification);
}
return wrapper;
},
getScripts: function () {
return [];
},
getStyles: function () {
return ["MMM-TitanSchoolMealMenu.css"];
},
getTranslations: function () {
return {
en: "translations/en.json"
// es: 'translations/es.json',
};
},
// socketNotificationReceived from helper
socketNotificationReceived: function (notificationName, payload) {
console.log(
`TitanSchools module (${this.instanceName}) received notification: ${notificationName}`
);
if (
notificationName ===
this.getNamespacedNotificationName("FETCH_DATA_SUCCESS")
) {
this.dataNotification = payload;
this.dataError = false;
this.loaded = true;
this.updateDom();
}
if (
notificationName ===
this.getNamespacedNotificationName("FETCH_DATA_FAILED")
) {
console.error(payload);
console.error(
`Retrying in ${this.config.retryDelayMs / 1000} seconds...`
);
this.scheduleUpdate();
this.dataNotification = payload;
this.dataError = true;
this.loaded = true;
this.updateDom();
}
if (
notificationName === this.getNamespacedNotificationName("CLIENT_READY")
) {
this.loaded = true;
this.getData();
this.updateDom();
}
}
});