-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
447 lines (302 loc) · 13.5 KB
/
node_helper.js
File metadata and controls
447 lines (302 loc) · 13.5 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* global Module, MMM-ChartProvider-JSON */
/* Magic Mirror
* Module: node_helper
*
* By Neil Scott
* MIT Licensed.
*/
const moduleruntime = new Date();
//this loads and formats JSON feeds into NDTF items, depending on its config when called to from the main module
//to minimise activity, it will track what data has been already sent back to the module
//and only send the delta each time, using the timestamp of the JSON data.
//this is done by making a note of the last timestamp data of feeds sent to the module, tracked at the jsonfeed level
//and ignoring anything older than that
//as some feeds wont have a timestamp date, they will be allocated a pseudo timestamp date of the latest timestamp date in the current processed jsonfeeds
//if the module calls a RESET, then the date tracking is reset and all data will be sent (TODO)
var NodeHelper = require("node_helper");
var moment = require("moment");
//pseudo structures for commonality across all modules
//obtained from a helper file of modules
var LOG = require('../MMM-FeedUtilities/LOG');
var QUEUE = require('../MMM-FeedUtilities/queueidea');
var RSS = require('../MMM-FeedUtilities/RSS');
var commonutils = require('../MMM-FeedUtilities/utilities');
// get required structures and utilities
const structures = require("../MMM-ChartUtilities/structures");
const utilities = require("../MMM-ChartUtilities/common");
const JSONutils = new utilities.JSONutils();
const configutils = new utilities.configutils();
// local variables, held at provider level as this is a common module
var providerstorage = {};
var trackingfeeddates = []; //an array of last date of feed recevied, one for each feed in the feeds index, build from the config
var aFeed = { lastFeedDate: '', feedURL: '' };
var payloadformodule = []; //we send back an array of identified stuff
var payloadstuffitem = { stuffID: '', stuff: '' }
var latestfeedpublisheddate = new Date(0) // set the date so no feeds are filtered, it is stored in providerstorage
module.exports = NodeHelper.create({
start: function () {
this.debug = false;
console.log(this.name + ' node_helper is started!');
this.logger = {};
this.logger[null] = LOG.createLogger("logs/logfile_Startup" + ".log", this.name);
this.queue = new QUEUE.queue("single", false);
},
showElapsed: function () {
endTime = new Date();
var timeDiff = endTime - startTime; //in ms
// strip the ms
timeDiff /= 1000;
// get seconds
var seconds = Math.round(timeDiff);
return (" " + seconds + " seconds");
},
stop: function () {
console.log("Shutting down node_helper");
//this.connection.close();
},
setconfig: function (moduleinstance, config) {
if (this.debug) { this.logger[moduleinstance].info("In setconfig: " + moduleinstance + " " + config); }
if (config.input != null) {
config['useHTTP'] = false;
// work out if we need to use a HTTP processor
if (config.input.substring(0, 4).toLowerCase() == "http") { config.useHTTP = true; }
}
//store a local copy so we don't have keep moving it about
providerstorage[moduleinstance] = { config: config, trackingfeeddates: [] };
var self = this;
//process the jsonfeed details into the local tracker
providerstorage[moduleinstance].config.jsonfeeds.forEach(function (configfeed) {
var feed = { sourcetitle: '', lastFeedDate: '', latestfeedpublisheddate: new Date(0), feedconfig: configfeed };
//we add some additional config information for usage in processing the data
//var jsonfeed = Object.assign({}, paramdefaults, config.params[idx]);
configfeed["useruntime"] = false;
configfeed["usenumericoutput"] = false;
if (configfeed.type == 'numeric') { configfeed["usenumericoutput"] = true; }
if (typeof configfeed.timestamp == "number") { //wants an offset of the runtime, provided in seconds, or it was blank
configfeed["useruntime"] = true;
configfeed["runtime"] = new Date(moduleruntime.getTime() + (configfeed.timestamp * 1000));
}
//store the actual timestamp to start filtering, this will change as new feeds are pulled to the latest date of those feeds
//if no date is available on a feed, then the current latest date of a feed published is allocated to it
feed.lastFeedDate = commonutils.calcTimestamp(configfeed.oldestage);
feed.sourcetitle = configfeed.feedtitle;
feed.feedconfig = configfeed;
providerstorage[moduleinstance].trackingfeeddates.push(feed);
});
},
getconfig: function () { return config; },
socketNotificationReceived: function (notification, payload) {
var self = this;
if (this.logger[payload.moduleinstance] == null) {
this.logger[payload.moduleinstance] = LOG.createLogger("logfile_" + payload.moduleinstance + ".log", payload.moduleinstance);
};
if (this.debug) {
this.logger[payload.moduleinstance].info(this.name + " NODE HELPER notification: " + notification + " - Payload: ");
this.logger[payload.moduleinstance].info(JSON.stringify(payload));
}
//we can receive these messages:
//
//RESET: clear any date processing or other so that all available stuff is returned to the module
//CONFIG: we get our copy of the config to look after
//UPDATE: request for any MORE stuff that we have not already sent
//STATUS: show the stored local config for a provider
//
switch (notification) {
case "CONFIG":
this.setconfig(payload.moduleinstance, payload.config);
break;
case "RESET":
this.reset(payload);
break;
case "UPDATE":
//because we can get some of these in a browser refresh scenario, we check for the
//local storage before accepting the request
if (providerstorage[payload.moduleinstance] == null) { break; } //need to sort this out later !!
this.processfeeds(payload.moduleinstance, payload.providerid);
break;
case "STATUS":
this.showstatus(payload.moduleinstance);
break;
}
},
processfeeds: function (moduleinstance, providerid) {
var self = this;
var feedidx = -1;
if (this.debug) { this.logger[moduleinstance].info("In processfeeds: " + moduleinstance + " " + providerid); }
//because we only get one data feed in the chart providers, then we preload the data before letting the jsonfeed actually process it
//we need to initialise the storage area within the providerstorage.
this.outputarray = new Array(providerstorage[moduleinstance].config.jsonfeeds.length)// feeds and then items
for (var cidx = 0; cidx < providerstorage[moduleinstance].config.jsonfeeds.length; cidx++) {
this.outputarray[cidx] = [];
}
//using version 2.0 of the utils that use callbacks for all cases to process the data captured
var options = {};
if (providerstorage[moduleinstance].config.useHTTP) {
var options = new URL(providerstorage[moduleinstance].config.input);
}
var JSONconfig = {
options: options,
config: providerstorage[moduleinstance].config,
feed: {},
moduleinstance: moduleinstance,
providerid: providerid,
feedidx: feedidx,
};
JSONconfig['callback'] = function (JSONconfig, inputjson) {
providerstorage[JSONconfig.moduleinstance].trackingfeeddates.forEach(function (feed) {
var jsonarray = utilities.getkeyedJSON(inputjson, feed.feedconfig.rootkey);
//this should now be an array that we can process in the simplest case
//check it actually contains something, assuming if empty it is in error
if (jsonarray.length == 0) {
console.error("json array is empty");
return;
}
self.queue.addtoqueue(function () { self.processfeed(feed, JSONconfig.moduleinstance, JSONconfig.providerid, ++JSONconfig.feedidx, jsonarray); });
self.queue.startqueue(providerstorage[JSONconfig.moduleinstance].config.waitforqueuetime); //the start function ignores a start once started
});
}
JSONutils.getJSONnew(JSONconfig);
},
showstatus: function (moduleinstance) {
console.log('============================ start of status ========================================');
console.log('config for provider: ' + moduleinstance);
console.log(providerstorage[moduleinstance].config);
console.log('feeds for provider: ' + moduleinstance);
console.log(providerstorage[moduleinstance].trackingfeeddates);
console.log('============================= end of status =========================================');
},
sendNotificationToMasterModule: function (stuff, stuff2) {
this.sendSocketNotification(stuff, stuff2);
},
done: function (err) {
if (err) {
console.log(err, err.stack);
}
},
send: function (moduleinstance, providerid, source, feedidx) {
//wrap the output array in an object so the main module handles it in the same way as if it was a collection of feeds
//and add an id for tracking purposes and wrap that in an array
var payloadforprovider = {
providerid: providerid, source: source, payloadformodule: [{ setid: providerstorage[moduleinstance].trackingfeeddates[feedidx].feedconfig.setid, itemarray: this.outputarray[feedidx] }]
};
if (this.debug) {
this.logger[moduleinstance].info("In send, source, feeds // sending items this time: " + (this.outputarray[feedidx].length > 0));
this.logger[moduleinstance].info(JSON.stringify(source));
}
if (this.outputarray[feedidx].length > 0) {
this.sendNotificationToMasterModule("UPDATED_STUFF_" + moduleinstance, payloadforprovider);
}
// as we have sent it and the important date is stored we can clear the outputarray
this.outputarray[feedidx] = [];
this.queue.processended();
},
//now to the core of the system, where there are most different to the feedprovider modules
//we enter this for wach of the jsonfeeds we want to create to send back for later processing
processfeed: function (feed, moduleinstance, providerid, feedidx, jsonarray) {
//we process a feed at a time here
if (this.debug) {
this.logger[moduleinstance].info("In fetch feed: " + JSON.stringify(feed));
this.logger[moduleinstance].info("In fetch feed: " + moduleinstance);
this.logger[moduleinstance].info("In fetch feed: " + providerid);
this.logger[moduleinstance].info("In fetch feed: " + feedidx);
}
//use these in the output area
var sourcetitle = feed.sourcetitle;
var self = this;
var maxfeeddate = new Date(0);
if (new Date(0) < maxfeeddate) {
providerstorage[moduleinstance].trackingfeeddates[feedidx]['latestfeedpublisheddate'] = maxfeeddate;
}
for (var idx = 0; idx < jsonarray.length; idx++) {
//look for any key value pairs required and create an item
//ignore any items that are older than the max feed date
var processthisitem = false;
var tempitem = new structures.NDTFItem()
tempitem.object = feed.feedconfig.object;
if (jsonarray[idx][feed.feedconfig.subject] != null) {
tempitem.subject = jsonarray[idx][feed.feedconfig.subject];
if (jsonarray[idx][feed.feedconfig.value] != null) {
//check if numeric
if (feed.feedconfig.usenumericoutput) {
if (isNaN(parseFloat(jsonarray[idx][feed.feedconfig.value]))) {
console.error("Invalid numeric value: " + jsonarray[idx][feed.feedconfig.value]);
}
else {
tempitem.value = parseFloat(jsonarray[idx][feed.feedconfig.value]);
}
}
else {
tempitem.value = jsonarray[idx][feed.feedconfig.value];
}
//if the timestamp is requested do we have one of those as well
if (!feed.feedconfig.useruntime) {
//got a timestamp key, now validate it
var temptimestamp = jsonarray[idx][feed.feedconfig.timestamp];
if (temptimestamp != null) {
//need to try and convert it to a valid date before doing anything else
if (feed.feedconfig.timestampformat != null) {
if (moment(temptimestamp, feed.feedconfig.timestampformat).isValid()) {
tempitem.timestamp = moment(temptimestamp, feed.feedconfig.timestampformat).toDate();
if (feed.feedconfig.oldestage != null) {
if (tempitem.timestamp >= feed.lastFeedDate) {
processthisitem = true;
}
}
else {
processthisitem = true;
}
}
else {
console.error("Invalid date");
}
}
else {
if (moment(temptimestamp).isValid()) {
tempitem.timestamp = moment(temptimestamp).toDate();
processthisitem = true;
}
else {
console.error("Invalid date");
}
}
}
else { // use an offset timestamp
tempitem.timestamp = feed.feedconfig.adjustedruntime;
processthisitem = true;
}
self.maxfeeddate = new Date(Math.max(self.maxfeeddate, tempitem.timestamp));
if (tempitem.timestamp <= feed.latestfeedpublisheddate) {
processthisitem = false;
Console.info("Item too old")
}
}
}
}
if (processthisitem) {
//stringify the timestamp to preserve its format later
tempitem.timestamp =tempitem.timestamp.getTime();
this.outputarray[feedidx].push(tempitem);
}
else {
//console.info("Ignoring: ", JSON.stringify(tempitem));
}
delete tempitem;
} //end of process loop - input array
if (feed.feedconfig.filename == null) {
console.info(this.name,"output data length",this.outputarray[feedidx].length);
}
else {
// write out to a file
JSONutils.putJSON("./" + feed.feedconfig.filename, this.outputarray[feedidx]);
}
var rsssource = new RSS.RSSsource();
rsssource.sourceiconclass = '';
rsssource.sourcetitle = feed.sourcetitle;
rsssource.title = feed.sourcetitle;
if (new Date(0) < self.maxfeeddate) {
providerstorage[moduleinstance].trackingfeeddates[feedidx]['latestfeedpublisheddate'] = self.maxfeeddate;
}
self.send(moduleinstance, providerid, rsssource, feedidx);
self.done();
},
});