signalk-to-noforeignland 0.1.17 → 0.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +57 -11
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -10,15 +10,14 @@ const fetch = require('node-fetch');
|
|
|
10
10
|
const isReachable = require('is-reachable');
|
|
11
11
|
const sendEmail = require('./sendEmail');
|
|
12
12
|
const createGPX = require('./createGPX');
|
|
13
|
-
const defaultApiUrl = 'https://www.noforeignland.com/home/api/v1/boat/tracking/track';
|
|
14
13
|
const pluginApiKey = '0ede6cb6-5213-45f5-8ab4-b4836b236f97';
|
|
15
14
|
// const msToKn = 1.944;
|
|
16
15
|
|
|
17
16
|
|
|
18
17
|
module.exports = function(app) {
|
|
19
18
|
var plugin = {};
|
|
20
|
-
plugin.id = 'signalk-to-noforeignland
|
|
21
|
-
plugin.name = 'SignalK to Noforeignland
|
|
19
|
+
plugin.id = 'signalk-to-noforeignland';
|
|
20
|
+
plugin.name = 'SignalK to Noforeignland';
|
|
22
21
|
plugin.description = 'SignalK track logger to noforeignland.com';
|
|
23
22
|
|
|
24
23
|
plugin.schema = {
|
|
@@ -90,11 +89,11 @@ module.exports = function(app) {
|
|
|
90
89
|
"description": "Keeps your boat active on NFL in your current location even if you do not move",
|
|
91
90
|
"default": true
|
|
92
91
|
},
|
|
93
|
-
"
|
|
92
|
+
"apiUrl": {
|
|
94
93
|
"type": "string",
|
|
95
94
|
"title": "NFL tracking API endpoint",
|
|
96
95
|
"description": "Change only if NFL gives you a different endpoint.",
|
|
97
|
-
"default":
|
|
96
|
+
"default": "https://www.noforeignland.com/home/api/v1/boat/tracking/track"
|
|
98
97
|
}
|
|
99
98
|
|
|
100
99
|
}
|
|
@@ -241,7 +240,7 @@ module.exports = function(app) {
|
|
|
241
240
|
initialSent = true;
|
|
242
241
|
app.debug('sending initial fix');
|
|
243
242
|
if (await testInternet()) {
|
|
244
|
-
await
|
|
243
|
+
await sendLatestPoint();
|
|
245
244
|
lastSentTime = Date.now();
|
|
246
245
|
}
|
|
247
246
|
}
|
|
@@ -335,12 +334,11 @@ module.exports = function(app) {
|
|
|
335
334
|
async function interval() {
|
|
336
335
|
const now = Date.now();
|
|
337
336
|
const twentyFourHrs = 24 * 3600 * 1000;
|
|
338
|
-
|
|
337
|
+
//const twentyFourHrs = 60 * 1000;
|
|
339
338
|
if (options.ping && (!lastSentTime || now - lastSentTime >= twentyFourHrs)) {
|
|
340
339
|
app.debug('24 hrs elapsed since last send, pushing periodic fix');
|
|
341
340
|
if (await testInternet()) {
|
|
342
|
-
await
|
|
343
|
-
lastSentTime = now;
|
|
341
|
+
await sendLatestPoint();
|
|
344
342
|
}
|
|
345
343
|
return;
|
|
346
344
|
}
|
|
@@ -386,6 +384,54 @@ module.exports = function(app) {
|
|
|
386
384
|
app.debug(`'${trackFile}'.size=${size} ${trackFile}'.exists=${exists}`);
|
|
387
385
|
return size > 0;
|
|
388
386
|
}
|
|
387
|
+
|
|
388
|
+
async function sendLatestPoint() {
|
|
389
|
+
if (!lastPosition) {
|
|
390
|
+
app.debug('No lastPosition cached, skipping 24hr ping');
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
const url = options.apiUrl || "https://www.noforeignland.com/home/api/v1/boat/tracking/track";
|
|
394
|
+
const timestamp = new Date(lastPosition.timestamp).getTime();
|
|
395
|
+
const lat = lastPosition.pos.latitude;
|
|
396
|
+
const lon = lastPosition.pos.longitude;
|
|
397
|
+
|
|
398
|
+
if (Math.abs(lat) <= 0.01 &&
|
|
399
|
+
Math.abs(lon) <= 0.01
|
|
400
|
+
) {
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
if (!isValidLatitude(lat) || !isValidLongitude(lon)) {
|
|
404
|
+
app.debug('Invalid lastPosition for 24hr ping, skipping');
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
const singlePointTrack = [[timestamp, lat, lon]];
|
|
408
|
+
const params = new URLSearchParams();
|
|
409
|
+
params.append('timestamp', timestamp);
|
|
410
|
+
params.append('track', JSON.stringify(singlePointTrack));
|
|
411
|
+
params.append('boatApiKey', options.boatApiKey);
|
|
412
|
+
|
|
413
|
+
const headers = {
|
|
414
|
+
'X-NFL-API-Key': pluginApiKey
|
|
415
|
+
};
|
|
416
|
+
app.debug('Sending latest position to API as single-point track', singlePointTrack);
|
|
417
|
+
try {
|
|
418
|
+
const response = await fetch(url, { method: 'POST', body: params, headers: new fetch.Headers(headers) });
|
|
419
|
+
if (response.ok) {
|
|
420
|
+
const responseBody = await response.json();
|
|
421
|
+
if (responseBody.status === 'ok') {
|
|
422
|
+
app.debug('Latest position successfully sent to API');
|
|
423
|
+
} else {
|
|
424
|
+
app.debug('API responded with error:', responseBody);
|
|
425
|
+
}
|
|
426
|
+
} else {
|
|
427
|
+
app.debug('API responded with HTTP error:', response.status, response.statusText);
|
|
428
|
+
}
|
|
429
|
+
} catch (err) {
|
|
430
|
+
app.debug('Failed to send latest position to API:', err);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
|
|
389
435
|
|
|
390
436
|
async function sendData() {
|
|
391
437
|
if (options.boatApiKey) {
|
|
@@ -396,8 +442,8 @@ module.exports = function(app) {
|
|
|
396
442
|
}
|
|
397
443
|
|
|
398
444
|
async function sendApiData() {
|
|
399
|
-
const url = options.apiUrl
|
|
400
|
-
|
|
445
|
+
const url = options.apiUrl;
|
|
446
|
+
app.debug('sending to ' + url)
|
|
401
447
|
app.debug('sending the data');
|
|
402
448
|
const trackData = await createTrack(path.join(options.trackDir, routeSaveName));
|
|
403
449
|
if (!trackData) {
|