forked from ianperrin/MMM-Formula1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
98 lines (91 loc) · 3.29 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
/* MMM-Formula1
* Node Helper
*
* By cirdan http://github.com/73cirdan/MMM-Formula1
* Forked from: Ian Perrin http://github.com/ianperrin/MMM-Formula1
* MIT Licensed.
*/
const Log = require("logger");
const NodeHelper = require("node_helper");
const axios = require("axios").default;
module.exports = NodeHelper.create({
// Subclass start method.
start() {
Log.log(`Starting module: ${this.name}`);
this.config = {};
},
// Subclass socketNotificationReceived received.
socketNotificationReceived(notification, payload) {
Log.log(`${this.name} received a notification: ${notification}`);
if (notification === "CONFIG") {
this.config = payload;
// Clear existing timers
if (this.timerId) {
clearTimeout(this.timerId);
}
this.fetchApiData();
}
},
/**
* fetchStandings
* Request driver or constructor standings from the Ergast MRD API and broadcast it to the MagicMirror module if it's received.
* Request current race schedule from the Ergast MRD API and broadcast as an iCal
*/
fetchApiData() {
const season = this.config.season === "current" ? new Date().getFullYear() : this.config.season;
var f1Url = "http://ergast.com/api/f1/" + season;
if (this.config.loadDriver) this.invokeURL("DRIVER", f1Url + "/driverStandings.json");
if (this.config.loadConstructor)
this.invokeURL("CONSTRUCTOR", f1Url + "/constructorStandings.json");
if (this.config.showSchedule) this.invokeURL("SCHEDULE", f1Url + ".json");
const self = this;
this.timerId = setTimeout(function () {
self.fetchApiData();
}, this.config.reloadInterval);
},
// call the api at ergast
invokeURL(type, url) {
const self = this;
Log.log(`${self.name} is requesting the ${type} on url ` + url);
axios
.get(url)
.then(function (response) {
// handle success console.log(response);
var data = null;
if (type.includes("SCHEDULE")) {
data = response.data.MRData.RaceTable.Races;
} else {
data = response.data.MRData.StandingsTable.StandingsLists[0];
}
self.sendSocketNotification(type, data);
Log.log(`${self.name} is returning the ${type} for the season`);
//Log.log(data);
})
.catch(function (error) {
self.handleError(error, type);
});
},
// handle the error on an axios request
handleError(error, type) {
if (!error) {
console.log("Unknown Error: " + this.name);
} else if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log("Error: " + this.name + ": " + error.response.status);
console.log("Error: " + this.name + ": " + error.response.data);
console.log("Error: " + this.name + ": " + error.response.headers);
} else if (error.request) {
// The request was made but no response was received
console.log("Error: " + this.name + ": " + error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log("Error: " + this.name + ": " + error.message);
}
//console.log(error.config);
this.sendSocketNotification(
type + "_ERROR",
this.name + " No F1 connection ($(type}), will retry"
);
}
});