-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
151 lines (138 loc) · 4.72 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
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
/* Magic Mirror
* Module: MMM-covid19
*
* By 0m4r
*
*/
const NodeHelper = require('node_helper');
const { URL, URLSearchParams } = require('url');
const fetch = require('node-fetch');
const { version } = require('./package.json');
const Log = require('../../js/logger.js');
module.exports = NodeHelper.create({
interval: null,
defaults: {
countryCodes: ['DE', 'IT'],
updateInterval: 24 * 60 * 60 * 1000, // 24 hours (only used if useScheduler=false)
useScheduler: false,
schedulerConfig: '0 0 */12 * * */1', //00 and 12 of every dayOfWeek (only used if useScheduler=true)
yesterday: false,
locale: 'en-US'
},
baseURL: 'https://disease.sh/v3/covid-19',
scheduler: null,
config: {},
start: function () {
Log.log('Starting node helper for: ' + this.name);
},
stop: function () {
Log.log('Stopping node helper for: ' + this.name);
if (this.scheduler !== null) {
this.scheduler.cancel()
}
},
fetchTotal: async function (countryCodes = []) {
countryCodes.sort()
Log.debug(this.name, 'fetchTotal', countryCodes);
const url = new URL(`${this.baseURL}/countries/${countryCodes.join(',')}?yesterday=${this.config.yesterday}&strict=true&`);
Log.info(this.name, 'fetchTotal', url.href);
let results = []
const resp = await fetch(url);
if (resp.status === 200) {
const data = await resp.json();
if (data) {
results.push(data)
results = [].concat.apply([], results).map(cc => ({
body: [
{
country: cc.country,
active: null,
confirmed: cc.cases,
recovered: cc.recovered,
death: cc.deaths,
test: null,
date: new Date(cc.updated).toISOString()
}, {
country: cc.country,
active: cc.active,
confirmed: cc.todayCases,
recovered: cc.todayRecovered,
death: cc.todayDeaths,
test: cc.tests,
date: new Date(cc.updated).toISOString()
}]
}))
}
} else {
Log.error(this.name, 'fetchTotal', countryCodes.join(','), resp);
results = countryCodes.map(m => ({ countryCode: m, body: [] }))
}
this.sendSocketNotification(this.name + 'TOTAL_RESULTS', results)
},
fetchVersion: async function () {
let remote = 0;
const url = 'https://raw.githubusercontent.com/0m4r/MMM-covid19/main/package.json';
Log.debug(this.name, 'fetchVersion', url);
const resp = await fetch(url);
if (resp.status === 200) {
const data = await resp.json();
remote = data.version || 0;
}
this.sendSocketNotification(this.name + 'VERSION_RESULTS', {
local: version,
remote,
});
},
fetchAll: function (config = this.config) {
Log.info(this.name, 'fetchAll', JSON.stringify(config));
this.fetchTotal(config.countryCodes);
this.fetchVersion();
},
fetchAllWithInterval: function () {
try {
clearInterval(this.interval);
const fetch = () => {
this.fetchAll();
this.sendSocketNotification(this.name + 'NEXT_UPDATE', [new Date(), new Date(Date.now() + this.config.updateInterval)]);
Log.info(this.name, 'fetchAllWithInterval| next execution scheduled for', new Date(Date.now() + this.config.updateInterval).toLocaleString(this.config.locale));
}
this.interval = setInterval(fetch, this.config.updateInterval);
fetch();
} catch (e) {
Log.error(this.name, 'fetchAllWithInterval', e)
}
},
fetchAllWithSchedule() {
try {
const schedule = require('node-schedule');
if (this.scheduler !== null) {
this.scheduler.cancel()
}
const fetch = () => {
this.fetchAll();
this.sendSocketNotification(this.name + 'NEXT_UPDATE', [new Date(), new Date(this.scheduler.nextInvocation())]);
Log.info(this.name, 'fetchAllWithSchedule | next execution scheduled for:', new Date(this.scheduler.nextInvocation()).toLocaleString(this.config.locale));
}
const scheduleConfig = this.config.schedulerConfig;
this.scheduler = schedule.scheduleJob(scheduleConfig, fetch)
fetch();
} catch (e) {
Log.error(this.name, 'fetchAllWithSchedule', e)
}
},
socketNotificationReceived: function (notification, payload) {
Log.debug(this.name, notification, JSON.stringify(payload));
if (notification === this.name + 'CONFIG') {
this.config = {
...this.defaults,
...payload,
};
Log.debug(this.name, 'socketNotificationReceived', 'useScheduler:', payload.useScheduler);
if (payload.useScheduler) {
this.fetchAllWithSchedule(payload)
} else {
this.fetchAllWithInterval(payload);
}
}
},
});