-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
212 lines (196 loc) · 6.33 KB
/
node_helper.js
File metadata and controls
212 lines (196 loc) · 6.33 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Based on Newsfeed by Michael Teeuw https://michaelteeuw.nl
* MIT Licensed.
*/
const NodeHelper = require("node_helper");
const Log = require("logger");
const NewsfeedFetcher = require("./newsfeedfetcher");
const xml2js = require("xml2js");
const PiP = require("point-in-polygon");
const lodash = require("lodash");
module.exports = NodeHelper.create({
// Override start method.
start: function () {
Log.log(`Starting node helper for: ${this.name}`);
this.fetchers = [];
this.fetchFunction = fetch;
this.fetchCache = null;
this.alertDetail = {}; // key: url; value: parsed CAP object
this.location = null; // will be {lat: Y, lon: X} if configured
},
// Override socketNotificationReceived received.
socketNotificationReceived: function (notification, payload) {
if (notification === "ADD_FEED") {
this.createFetcher(payload.feed, payload.config);
}
},
ensureFetchCache: function () {
if (!this.fetchCache) {
let builder = require('node-fetch-cache').fetchBuilder;
let fscache = require('node-fetch-cache').FileSystemCache;
const options = {
// cacheDirectory: '/some/path', // defaults to .cache
ttl: 86400000, // ms
};
this.fetchCache = builder.withCache(new fscache(options));
this.fetchFunction = this.fetchCache;
}
},
/**
* Creates a fetcher for a new feed if it doesn't exist yet.
* Otherwise it reuses the existing one.
* @param {object} feed The feed object
* @param {object} config The configuration object
*/
createFetcher: function (feed, config) {
const url = feed.url || "";
const encoding = feed.encoding || "UTF-8";
const reloadInterval = feed.reloadInterval || config.reloadInterval || 5 * 60 * 1000;
let useCorsProxy = feed.useCorsProxy;
if (useCorsProxy === undefined) useCorsProxy = true;
if (config.cacheFeed) this.ensureFetchCache();
if (config.lat && config.lon) {
this.location = { lat: config.lat, lon: config.lon };
}
try {
new URL(url);
} catch (error) {
Log.error("CAP feed Error. Malformed feed url: ", url, error);
this.sendSocketNotification("FEED_ERROR", { error_type: "MODULE_ERROR_MALFORMED_URL" });
return;
}
let fetcher;
if (typeof this.fetchers[url] === "undefined") {
Log.log(`Create new newsfetcher for url: ${url} - Interval: ${reloadInterval}`);
fetcher = new NewsfeedFetcher(url, reloadInterval, encoding, config.logFeedWarnings, useCorsProxy, this.fetchCache);
fetcher.onReceive(() => {
this.processAlerts();
});
fetcher.onError((fetcher, error) => {
Log.error("CAP feed Error. Could not fetch feed: ", url, error);
let error_type = NodeHelper.checkFetchError(error);
this.sendSocketNotification("FEED_ERROR", {
error_type
});
});
this.fetchers[url] = fetcher;
} else {
Log.log(`Use existing newsfetcher for url: ${url}`);
fetcher = this.fetchers[url];
fetcher.setReloadInterval(reloadInterval);
fetcher.broadcastItems();
}
fetcher.startFetch();
},
/**
* Ensure we've got the data for all active items in all feeds, then broadcast them
*/
processAlerts: function () {
let asyncs = [];
// Grab all alert detail
for (let f in this.fetchers) {
let items = this.fetchers[f].items();
for (let i in items) {
let alert = items[i];
if (alert.url !== undefined && alert.detail === undefined) {
Log.log(`Retrieving alert detail for ${alert.url}`);
alert.detail = [];
asyncs.push(
this.fetchFunction(alert.url)
.then(NodeHelper.checkFetchStatus)
.then((response) => response.text())
.then((text) => xml2js.parseStringPromise(text))
.then((result) => {
let detail = result?.alert?.info || [];
alert.detail.push(...detail);
return alert;
})
.catch((error) => {
Log.error("CAP feed Error. Could not fetch detail: ", alert.url, error);
let error_type = NodeHelper.checkFetchError(error);
this.sendSocketNotification("FEED_ERROR", {
error_type
});
})
);
}
}
}
Promise.all(asyncs)
.finally(() => { this.filterItems(); })
.then(() => { this.broadcastFeeds(); });
},
/** Applies geo-filtering to the alert items, if configured */
filterItems: function () {
if (!this.location) return;
let latlon = [this.location.lat || 0.0, this.location.lon || 0.0];
for (let f in this.fetchers) {
let items = this.fetchers[f].items();
lodash.remove(items, function (item) {
// If there are any polygons in the alert detail, then we must be inside at least one of them to pass.
var seenAny = false;
for (let detail in item.detail) {
for (let area in item.detail[detail].area) {
var poly = item.detail[detail].area[area].polygon;
if (poly) {
const parsedPolys = parsePolygons(poly);
seenAny = true;
for (let p in parsedPolys) {
if (PiP(latlon, parsedPolys[p])) {
return false;
}
}
}
}
}
// But if there are no area detail polygons, let it pass.
return seenAny;
});
}
},
/**
* Creates an object with all feed items of the different registered feeds,
* and broadcasts these using sendSocketNotification.
*/
broadcastFeeds: function () {
const feeds = {};
for (let f in this.fetchers) {
feeds[f] = this.fetchers[f].items();
}
Log.debug(`CAP: Sending alerts for ${Object.keys(feeds).length} feeds`);
this.sendSocketNotification("FEED_ITEMS", feeds);
}
});
/** Converts area polygon strings into a form that point-in-polygon can handle
* Input: array of strings
* Output: array of polygons (as returned by parsePolygon; each is an array of arrays)
*/
function parsePolygons(data) {
if (data instanceof String) {
return [parsePolygon(data)];
}
return lodash.map(data, parsePolygon);
}
/** Converts an area polygon from string into an array of arrays
* Input example: "-40.866,174.111 -40.863,174.125 -40.911,174.128 -40.938,174.108"
* Output: [ [-40.866,174.111], [-40.863,174.125], [-40.911,174.128], [-40.938,174.108] ]
*/
function parsePolygon(data) {
var str = String(data);
var points = str.split(" ");
var output = [];
for (let i in points) {
var coords = points[i].split(",");
var thisPoint = [];
for (let c in coords) {
let n = Number(coords[c]);
if (n !== n) {
// parsing failed; Number conversion returned NaN
return null;
}
thisPoint.push(n);
}
output.push(thisPoint);
}
return output;
}