-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_helper.js
44 lines (39 loc) · 1.12 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
/* TfL Bus Atrival Predictions */
/* Magic Mirror
* Module: MMM-TFL-Arrivals
* By Ricardo Gonzalez
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
const axios = require("axios");
module.exports = NodeHelper.create({
start: function () {
console.log("MMM-TFL-Arrivals helper started ...");
},
/* getTimetable()
* Requests new data from TfL API.
* Sends data back via socket on succesfull response.
*/
getTimetable: async function (url) {
var self = this;
const { data, status, statusText } = await axios.get(url);
if (status === 200) {
if (statusText === "error") {
self.sendSocketNotification("TFL_ARRIVALS_DATA", { data: null, url });
} else {
self.sendSocketNotification("TFL_ARRIVALS_DATA", {
data,
url,
});
}
} else {
self.sendSocketNotification("TFL_ARRIVALS_DATA", { data: null, url });
}
},
//Subclass socketNotificationReceived received.
socketNotificationReceived: function (notification, payload) {
if (notification === "GET_TFL_ARRIVALS_DATA") {
this.getTimetable(payload.url);
}
},
});