forked from smart-facility/cognicity-reports-floodgauge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FloodgaugeDataSource.js
347 lines (297 loc) · 10.3 KB
/
FloodgaugeDataSource.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
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
'use strict';
var rootCas = require('ssl-root-cas/latest').create();
rootCas.addFile(__dirname + '/intermediate.pem');
/**
* The Floodgauge data source.
* Poll the specified Floodgauge feed for new data and send it to the reports application.
* @constructor
* @param {Reports} reports An instance of the reports object.
* @param {object} config Floodgauge specific configuration.
*/
var FloodgaugeDataSource = function FloodgaugeDataSource(
reports,
config
){
// Store references to reports and logger
this.reports = reports;
this.logger = reports.logger;
// Copy reports config into our own config
this.config = reports.config;
for (var prop in config) {
if (config.hasOwnProperty(prop)) {
this.config[prop] = config[prop];
}
}
this.https = require('https');
this.https.globalAgent.options.ca = rootCas;
// Set constructor reference (used to print the name of this data source)
this.constructor = FloodgaugeDataSource;
};
FloodgaugeDataSource.prototype = {
/**
* Data source configuration.
* This contains the reports configuration and the data source specific configuration.
* @type {object}
*/
config: {},
/**
* Instance of the reports module that the data source uses to interact with Cognicity Server.
* @type {Reports}
*/
reports: null,
/**
* Instance of the Winston logger.
*/
logger: null,
/**
* Instance of node https.
*/
https: null,
/**
* Flag signifying if we are currently able to process incoming data immediately.
* Turned on if the database is temporarily offline so we can cache for a short time.
* @type {boolean}
*/
_cacheMode: false,
/**
* Store data if we cannot process immediately, for later processing.
* @type {Array}
*/
_cachedData: [],
/**
* Last contribution timestamp that was processed, for each floodgauge.
* Used to ensure we don't process the same result twice.
* @type {number}
*/
_lastContributionTime: {0:0},
/**
* Reference to the polling interval.
* @type {number}
*/
_interval: null,
/**
* Polling worker function.
* Poll the Floodgauge web service and process the results.
* This method is called repeatedly on a timer.
*/
_poll: function(){
var self = this;
// Begin processing results
self._fetchResults();
},
/**
* Fetch and process the results.
*/
_fetchResults: function() {
var self = this;
self.logger.verbose( 'FloodgaugeDataSource > poll > fetchResults: Loading data');
var requestURL = self.config.floodgauge.serviceURL;
var response = "";
var req = self.https.request( requestURL , function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
response += chunk;
});
res.on('end', function() {
var responseObject;
var responseCleaned;
try {
responseCleaned = response.replace(/("[^"]+")|([a-zA-Z0-9_-]+):/g, '"$2":$1 ').replace(/: "":/g, ':');
responseObject = JSON.parse(responseCleaned);
} catch (e) {
self.logger.error( "FloodgaugeDataSource > poll > fetchResults: Error parsing JSON: " + response );
return;
}
self.logger.debug("FloodgaugeDataSource > poll > fetchResults: fetched data, " + response.length + " bytes");
if ( !responseObject || responseObject.length === 0 ) {
// If page has a problem or 0 objects, end
self.logger.error( "FloodgaugeDataSource > poll > fetchResults: No results found ");
return;
} else {
// Run data processing callback on the result objects
self.logger.debug("FloodgaugeDataSource > poll > fetchResults: parsed incoming JSON.");
self._filterResults( responseObject );
}
});
});
req.on('error', function(error) {
self.logger.error( "FloodgaugeDataSource > poll > fetchResults: Error fetching data, " + error.message + ", " + error.stack );
});
req.end();
},
/**
* Process the passed result objects.
* Stop processing if we've seen a result before, or if the result is too old.
* @param {Array} results Array of result objects from the Floodgauge data to process
*/
_filterResults: function( results ) {
var self = this;
// For each result:
var result = results.reports.shift();
while( result ) {
if ( Date.parse(result.measureDateTime+'+0700') / 1000 <= self._lastContributionTime[result.gaugeId] ) {
// We've seen this result before, check the next gauge
self.logger.debug( "FloodgaugeDataSource > poll > processResults: Found already processed result with contribution ID " + result.gaugeId +': ' + result.measureDateTime );
} else if ( Date.parse(result.measureDateTime+'+0700') < new Date().getTime() - self.config.floodgauge.historicalLoadPeriod ) {
// This result is older than our cutoff, check the next gauge
self.logger.debug( "FloodgaugeDataSource > poll > processResults: Result " + result.gaugeId +', ' + result.measureDateTime + " older than maximum configured age of " + self.config.floodgauge.historicalLoadPeriod / 1000 + " seconds" );
} else {
// Process this result
self.logger.verbose( "FloodgaugeDataSource > poll > processResults: Processing result " + result.gaugeId +', ' + result.measureDateTime );
self._lastContributionTime[result.gaugeId] = Date.parse(result.measureDateTime+'+0700')/1000;
self._processResult( result );
}
result = results.reports.shift();
}
},
/**
* Process a result.
* This method is called for each new result we fetch from the web service.
* @param {object} result The result object from the web service
*/
_processResult: function( result ) {
var self = this;
if ( self._cacheMode ) {
// Store result for later processing
self._cachedData.push( result );
} else {
// Process result now
self._saveResult(result);
}
},
/**
* Save a result to cognicity server.
* @param {object} result The result object from the web service
*/
_saveResult: function( result ) {
var self = this;
// Don't allow users from the Gulf of Guinea (indicates no geo available)
if (result.longitude !== 0 && result.latitude !== 0){
self._insertGaugeReading(result);
}
},
/**
* Insert a confirmed report - i.e. has geo coordinates.
* Store both the floodgauge report and the user hash.
* @param {floodgaugeReport} floodgaugeReport Floodgauge report object
*/
_insertGaugeReading: function( floodgaugeReport ) {
var self = this;
// Insert report
self.reports.dbQuery(
{
text: "INSERT INTO " + self.config.floodgauge.pg.table_floodgauge + " " +
"(gaugeid, measuredatetime, depth, deviceid, reporttype, level, notificationflag, gaugenameid, gaugenameen, gaugenamejp, warninglevel, warningnameid, warningnameen, warningnamejp, observation_comment, the_geom) " +
"VALUES (" +
"$1, " +
"to_timestamp($2), " +
"$3, " +
"$4, " +
"$5, " +
"$6, " +
"$7, " +
"$8, " +
"$9, " +
"$10, " +
"$11, " +
"$12, " +
"$13, " +
"$14, " +
"$15, " +
"ST_GeomFromText('POINT(' || $16 || ')',4326)" +
");",
values : [
floodgaugeReport.gaugeId,
Date.parse(floodgaugeReport.measureDateTime+'+0700')/1000,
floodgaugeReport.depth,
floodgaugeReport.deviceId,
floodgaugeReport.reportType,
floodgaugeReport.level,
floodgaugeReport.notificationFlag,
floodgaugeReport.gaugeNameId,
floodgaugeReport.gaugeNameEn,
floodgaugeReport.gaugeNameJp,
floodgaugeReport.warningLevel,
floodgaugeReport.warningNameId,
floodgaugeReport.warningNameEn,
floodgaugeReport.warningNameJp,
floodgaugeReport.comment,
floodgaugeReport.longitude + " " + floodgaugeReport.latitude
]
},
function ( result ) {
self.logger.info('Logged confirmed floodgauge report');
}
);
},
/**
* Get the last contribution ID as stored in the database.
* Update _lastContributionTime
*/
_updateLastContributionIdFromDatabase: function() {
var self = this;
self.reports.dbQuery(
{
text: "SELECT gaugeid, date_part('epoch', max(measuredatetime)) epoch FROM " + self.config.floodgauge.pg.table_floodgauge + " " +
"GROUP BY gaugeid;"
},
function ( result ) {
if (result && result.rows && result.rows.length > 0){
for (var i = 0; i < result.rows.length; i++) {
self._lastContributionTime[result.rows[i].gaugeid] = result.rows[i].epoch;
}
self.logger.info('Set last observation times from database');
}
else {
self.logger.info('Error setting last observation time from database (is the reports table empty?)');
}
}
);
},
/**
* Connect to the data stream.
* Retrieve data now and start polling for more data.
*/
start: function(){
var self = this;
// Initiate by getting last report ID from database
self._updateLastContributionIdFromDatabase();
// Called on interval to poll data source
var poll = function(){
self.logger.debug( "FloodgaugeDataSource > start: Polling " + self.config.floodgauge.serviceURL + " every " + self.config.floodgauge.pollInterval / 1000 + " seconds" );
self._poll();
};
// Poll now, immediately
poll();
// Setup interval to poll repeatedly in future
self._interval = setInterval(
poll,
self.config.floodgauge.pollInterval
);
},
/**
* Stop realtime processing of results and start caching results until caching mode is disabled.
*/
enableCacheMode: function() {
var self = this;
self.logger.verbose( 'FloodgaugeDataSource > enableCacheMode: Enabling caching mode' );
self._cacheMode = true;
},
/**
* Resume realtime processing of results.
* Also immediately process any results cached while caching mode was enabled.
*/
disableCacheMode: function() {
var self = this;
self.logger.verbose( 'FloodgaugeDataSource > disableCacheMode: Disabling caching mode' );
self._cacheMode = false;
self.logger.verbose( 'FloodgaugeDataSource > disableCacheMode: Processing ' + self._cachedData.length + ' cached results' );
self._cachedData.forEach( function(data) {
self._processResult(data);
});
self.logger.verbose( 'FloodgaugeDataSource > disableCacheMode: Cached results processed' );
self._cachedData = [];
}
};
// Export the PowertrackDataSource constructor
module.exports = FloodgaugeDataSource;