forked from boghammar/MMM-CrisisInformationSweden
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_helper.js
91 lines (84 loc) · 3.42 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
/* node_helper.js
*
* MagicMirror² module - News feed from the Swedish Government Crisis Information (Krisinformation.se).
*
* Module: MMM-CrisisInformationSweden
*
* MagicMirror² by Michael Teeuw https://michaelteeuw.nl
* MIT Licensed.
*
* Module MMM-CrisisInformationSweden by Anders Boghammar
*/
const Log = require("logger");
const NodeHelper = require("node_helper");
module.exports = NodeHelper.create({
// --------------------------------------- Start the helper
start () {
Log.log(`Starting helper: ${this.name}`);
this.started = false;
},
// --------------------------------------- Schedule a feed update
scheduleUpdate () {
const self = this;
this.updatetimer = setInterval(() => { // This timer is saved in uitimer so that we can cancel it
self.getFeed();
}, self.config.updateInterval);
},
// --------------------------------------- Retrive new feed
async getFeed () {
const self = this;
Log.log(`${new Date(Date.now()).toLocaleTimeString()}: Getting feed for module ${this.name}`);
const opt = {
uri: "https://api.krisinformation.se/v3/news/?includeTest=0&allCounties=True",
qs: {
},
json: true
};
Log.log(`Calling ${opt.uri}`);
try {
const response = await fetch(opt.uri, {method: "GET"});
const resp = await response.json();
Log.debug(resp);
const feeds = self.filterFeed(resp);
Log.log(`${self.name} - Sending NEW_FEED count: ${feeds.length} Org: ${resp.length}`);
self.sendSocketNotification("NEW_FEED", feeds); // Send feed to module
} catch (err) {
Log.log(`Problems with ${self.name}: ${err}`);
self.sendSocketNotification("SERVICE_FAILURE", {resp: {StatusCode: 600, Message: err}});
}
},
// --------------------------------------- Filter feeds according to config
filterFeed (resp) {
if (this.config.areas === undefined || this.config.areas.length < 1) return resp;
const feeds = [];
for (let ix = 0; ix < resp.length; ix++) {
Log.debug("MSB: " + ix);
let inc = false;
const feed = resp[ix];
const areas = feed.Area;
Log.debug("Looking at "+ feed.Identifier);
if (areas === undefined || areas === null) inc = true; // Always include iof there's no area defined
else {
for (let ia = 0; ia < areas.length; ia++) {
Log.debug("filter: " + JSON.stringify(areas[ia]));
for (let iad = 0; iad < this.config.areas.length; iad++) {
if (areas[ia].Type == "County" && areas[ia].Description == this.config.areas[iad]) inc = true;
}
if (this.config.alwaysNational && areas[ia].Type === "Country" && areas[ia].Description === "Sverige") inc = true;
}
}
if (inc) feeds.push(feed);
}
return feeds;
},
// --------------------------------------- Handle notifications
socketNotificationReceived (notification, payload) {
const self = this;
if (notification === "CONFIG" && this.started === false) {
this.config = payload;
this.started = true;
self.scheduleUpdate();
self.getFeed(); // Get it first time
}
}
});