forked from evanhsu/MMM-TitanSchoolMealMenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
87 lines (76 loc) · 2.6 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
const NodeHelper = require("node_helper");
const TitanSchoolsClient = require("./TitanSchoolsClient");
module.exports = NodeHelper.create({
// Subclass start method.
start: function () {
var self = this;
console.log("Starting node helper for: " + self.name);
// There will be a separate API client for each instance of this module (in case we want to poll
// multiple school menus)
self.titanSchoolsClients = {};
},
fetchData: async function (instanceName) {
var self = this;
if (isObjectEmpty(self.titanSchoolsClients)) {
console.log(
`Skipping data fetch because there's no TitanSchools API clients configured`
);
return;
}
try {
// const menu = await this.titanSchoolsClient.fetchMockMenu();
const menu = await this.titanSchoolsClients[instanceName].fetchMenu();
this.sendSocketNotification(
`TITANSCHOOLS_FETCH_DATA_SUCCESS::${instanceName}`,
menu
);
} catch (error) {
this.sendSocketNotification(
`TITANSCHOOLS_FETCH_DATA_FAILED::${instanceName}`,
error.message
);
console.error(`Error response from the titanschools API: ${error}`);
}
return;
},
// Subclass socketNotificationReceived received.
socketNotificationReceived: function (notificationName, payload) {
var self = this;
console.log(
`TitanSchools node_helper received notification: ${notificationName}`
);
if (notificationName === "TITANSCHOOLS_SET_CONFIG") {
console.log(payload);
self.titanSchoolsClients[payload.instanceName] = new TitanSchoolsClient({
buildingId: payload.buildingId,
districtId: payload.districtId,
recipeCategoriesToInclude: payload.recipeCategoriesToInclude,
debug: payload.debug,
});
this.sendSocketNotification(
`TITANSCHOOLS_CLIENT_READY::${payload.instanceName}`
);
} else if (notificationName.includes(`TITANSCHOOLS_FETCH_DATA_REQUEST`)) {
self.fetchData(
extractInstanceNameSuffixFromNotification(notificationName)
);
}
}
});
/**
* Notifications are named with this pattern: ACTION_NAME::INSTANCE_NAME
* Where the ACTION_NAME will be something like "TITANSCHOOLS_FETCH_DATA_REQUEST"
* and the INSTANCE_NAME is the Titan Schools DistrictID concatenated with the BuildingID:
*/
function extractInstanceNameSuffixFromNotification(notificationName) {
return notificationName.split("::").pop();
}
function isObjectEmpty(obj) {
let propCount = 0;
for (const prop in obj) {
if (obj.hasOwnProperty(prop)) {
propCount++;
}
}
return propCount === 0;
}