-
Notifications
You must be signed in to change notification settings - Fork 6
/
node_helper.js
50 lines (46 loc) · 1.25 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
/**
* MMM-TautulliActivity node helper
*/
const NodeHelper = require('node_helper');
module.exports = NodeHelper.create({
config: null,
data: null,
reloadTimer: null,
getData: function() {
console.info('MMM-TautulliActivity: fetching data');
fetch(`${this.config.host}/api/v2?apikey=${this.config.apiKey}&cmd=get_activity`)
.then((res) => {
if (res.ok) {
return res;
} else {
throw res.statusText;
}
})
.then((res) => res.json())
.then((data) => {
if (data.response.result != 'success') {
throw data.response.message || 'there was an error';
}
this.data = data.response.data;
this.sendSocketNotification('SET_DATA', this.data);
})
.catch((err) => {
console.error('MMM-TautulliActivity: ' + err)
this.sendSocketNotification('SET_DATA', err);
})
.finally(() => {
this.reloadTimer = setTimeout(() => { this.getData(this.config) }, this.config.updateFrequency);
});
},
socketNotificationReceived: function(notification, payload) {
if (notification == 'INIT') {
if (! this.reloadTimer) {
console.info('MMM-TautulliActivity: starting reload timer');
this.config = payload;
this.getData();
} else {
this.sendSocketNotification('SET_DATA', this.data);
}
}
},
});