-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
46 lines (40 loc) · 1.54 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
/* Magic Mirror
* Module: MMM-OLYMPIC-MEDALS
*
* By taylornoss https://github.com/taylornoss/MMM-OLYMPIC-MEDALS
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const Log = require('logger');
const ESPN = require('./espn');
module.exports = NodeHelper.create({
requiresVersion: '2.15.0',
async socketNotificationReceived(notification, payload) {
if (notification === 'GET-Olympic-Data') {
const config = payload.config;
const identifier = payload.id;
if((config.season === "summer" && config.year % 4 != 0) || (config.season === "winter" && config.year % 4 != 2)){
Log.error(this.name+": "+config.year + " is an invalid year for "+config.season+" olympics");
this.sendSocketNotification('OLYMPIC-CONFIG-ERROR', identifier);
return;
}
await this.getData(config.season, config.year, config.numOfRows, identifier);
}
},
async getData(season, year, limit, identifier) {
try {
const id = identifier;
var table = await ESPN.getMedalData(season, year,limit);
Log.debug(JSON.stringify(table));
var returnVal = {
identifier: id,
header: table.header,
rows: table.rows
}
Log.info("Sending Olympics Notification!");
this.sendSocketNotification('OLYMPIC-DATA', returnVal);
} catch (error) {
Log.error(`Error getting Olympic Data: ${error}`);
}
},
});