signalk-to-noforeignland 0.1.17 → 0.1.19

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.
Files changed (2) hide show
  1. package/index.js +61 -12
  2. 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-beta';
21
- plugin.name = 'SignalK to Noforeignland-beta';
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
- "defaultApiUrl": {
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": defaultApiUrl
96
+ "default": "https://www.noforeignland.com/home/api/v1/boat/tracking/track"
98
97
  }
99
98
 
100
99
  }
@@ -221,6 +220,8 @@ module.exports = function(app) {
221
220
  // SK sometimes messes up timestamps, when that happens we throw the update
222
221
  return;
223
222
  }
223
+ lastPosition = { pos: value.value, timestamp, currentTime: new Date().getTime() };
224
+
224
225
  const distance = equirectangularDistance(lastPosition.pos, value.value)
225
226
  if (options.minMove && distance < options.minMove) {
226
227
  return;
@@ -229,8 +230,9 @@ module.exports = function(app) {
229
230
  // app.debug('got error position', value.value, 'ignoring...');
230
231
  // return;
231
232
  // }
233
+ } else{
234
+ lastPosition = { pos: value.value, timestamp, currentTime: new Date().getTime() };
232
235
  }
233
- lastPosition = { pos: value.value, timestamp, currentTime: new Date().getTime() };
234
236
 
235
237
  await savePoint(lastPosition);
236
238
  if (options.minSpeed) {
@@ -241,7 +243,7 @@ module.exports = function(app) {
241
243
  initialSent = true;
242
244
  app.debug('sending initial fix');
243
245
  if (await testInternet()) {
244
- await sendData();
246
+ await sendLatestPoint();
245
247
  lastSentTime = Date.now();
246
248
  }
247
249
  }
@@ -335,12 +337,11 @@ module.exports = function(app) {
335
337
  async function interval() {
336
338
  const now = Date.now();
337
339
  const twentyFourHrs = 24 * 3600 * 1000;
338
-
340
+ //const twentyFourHrs = 60 * 1000;
339
341
  if (options.ping && (!lastSentTime || now - lastSentTime >= twentyFourHrs)) {
340
342
  app.debug('24 hrs elapsed since last send, pushing periodic fix');
341
343
  if (await testInternet()) {
342
- await sendData();
343
- lastSentTime = now;
344
+ await sendLatestPoint();
344
345
  }
345
346
  return;
346
347
  }
@@ -386,6 +387,54 @@ module.exports = function(app) {
386
387
  app.debug(`'${trackFile}'.size=${size} ${trackFile}'.exists=${exists}`);
387
388
  return size > 0;
388
389
  }
390
+
391
+ async function sendLatestPoint() {
392
+ if (!lastPosition) {
393
+ app.debug('No lastPosition cached, skipping 24hr ping');
394
+ return;
395
+ }
396
+ const url = options.apiUrl || "https://www.noforeignland.com/home/api/v1/boat/tracking/track";
397
+ const timestamp = new Date(lastPosition.timestamp).getTime();
398
+ const lat = lastPosition.pos.latitude;
399
+ const lon = lastPosition.pos.longitude;
400
+
401
+ if (Math.abs(lat) <= 0.01 &&
402
+ Math.abs(lon) <= 0.01
403
+ ) {
404
+ return;
405
+ }
406
+ if (!isValidLatitude(lat) || !isValidLongitude(lon)) {
407
+ app.debug('Invalid lastPosition for 24hr ping, skipping');
408
+ return;
409
+ }
410
+ const singlePointTrack = [[timestamp, lat, lon]];
411
+ const params = new URLSearchParams();
412
+ params.append('timestamp', timestamp);
413
+ params.append('track', JSON.stringify(singlePointTrack));
414
+ params.append('boatApiKey', options.boatApiKey);
415
+
416
+ const headers = {
417
+ 'X-NFL-API-Key': pluginApiKey
418
+ };
419
+ app.debug('Sending latest position to API as single-point track', singlePointTrack);
420
+ try {
421
+ const response = await fetch(url, { method: 'POST', body: params, headers: new fetch.Headers(headers) });
422
+ if (response.ok) {
423
+ const responseBody = await response.json();
424
+ if (responseBody.status === 'ok') {
425
+ app.debug('Latest position successfully sent to API');
426
+ } else {
427
+ app.debug('API responded with error:', responseBody);
428
+ }
429
+ } else {
430
+ app.debug('API responded with HTTP error:', response.status, response.statusText);
431
+ }
432
+ } catch (err) {
433
+ app.debug('Failed to send latest position to API:', err);
434
+ }
435
+ }
436
+
437
+
389
438
 
390
439
  async function sendData() {
391
440
  if (options.boatApiKey) {
@@ -396,8 +445,8 @@ module.exports = function(app) {
396
445
  }
397
446
 
398
447
  async function sendApiData() {
399
- const url = options.apiUrl || defaultApiUrl;
400
-
448
+ const url = options.apiUrl;
449
+ app.debug('sending to ' + url)
401
450
  app.debug('sending the data');
402
451
  const trackData = await createTrack(path.join(options.trackDir, routeSaveName));
403
452
  if (!trackData) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "signalk-to-noforeignland",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
4
4
  "description": "SignalK track logger to noforeignland.com ",
5
5
  "main": "index.js",
6
6
  "keywords": [