-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
112 lines (94 loc) · 3.34 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
'use strict';
/* Magic Mirror
* Module: CanadianPublicWeatherAlerts
*
* By Alex Souchereau http://github.com/aSouchereau
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const async = require('async');
const xml2js = require('xml2js');
const https = require('https');
module.exports = NodeHelper.create({
start() {
this.config = {};
console.log("Starting node helper for: " + this.name);
},
startUpdate() {
this.entries = []; // clear previously fetched entries
let urls = this.generatePaths(this.config.regions); // Generate new urls
// Foreach generated url, call getData()
async.each(urls, this.getData.bind(this), (err) => {
if (err) {
console.log(err);
} else {
if (this.config.showNoAlertsMsg) {
this.sendSocketNotification("CPWA_UPDATE", this.entries);
} else {
this.sendSocketNotification("CPWA_UPDATE", this.filterEntries(this.entries));
}
}
});
},
// Filter out unimportant alert entries
filterEntries(entries) {
let noAlertsEn = "No alerts in effect";
let noAlertsFr = "Aucune alerte en vigueur";
return entries.filter( e =>
!e.summary[0]._.includes(noAlertsEn) &&
!e.summary[0]._.includes(noAlertsFr)
);
},
// Generates an array of urls using configured region codes
generatePaths(regions) {
let urls = [];
for (let i = 0; i < regions.length; i++) {
let url = "/rss/battleboard/" + regions[i].code + "_" + this.config.lang.slice(0,1) + ".xml";
urls.push(url);
}
return urls;
},
getData(url, callback) {
let options = {
hostname: this.config.apiBase,
path: url
}
let data = "";
https.get(options, (response) => {
if (response.statusCode < 200 || response.statusCode > 299) {
response.on('data', () => {
callback("["+ this.name + "] Could not get alert data from " + url + " - Error " + response.statusCode)
});
} else {
response.on('data', (chunk) => { data += chunk; });
response.on('end', () => { this.parseData(data, callback); });
}
response.on('error', (err) => {
callback("["+ this.name + "] Failed making http request - " + err);
});
});
},
parseData(data, callback) {
let parser = new xml2js.Parser();
parser.parseString(data, (err, result) => {
let entries = result['feed']['entry'];
if (!err) {
for (let i = 0; i < entries.length; i++) {
this.entries.push(entries[i]);
}
} else {
console.log("[" + this.name + "] " + "Error parsing XML data: " + err);
callback(err);
}
callback(null);
});
},
socketNotificationReceived(notification, payload) {
if (notification === 'CPWA_CONFIG') {
this.config = payload;
this.sendSocketNotification("CPWA_STARTED", true);
} else if (notification === 'CPWA_REQUEST_UPDATE') {
this.startUpdate();
}
}
});