signalk-fixedstation 0.3.2 → 0.5.0

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2021 Inspired Technologies GmbH
3
+ Copyright (c) 2024 Inspired Technologies
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -0,0 +1,145 @@
1
+ const geoloc = require('./geoloc');
2
+
3
+ const navigationPosition = 'navigation.position';
4
+ const navigationElevation = 'navigation.gnss.antennaAltitude';
5
+ let deltahandler = null;
6
+ let log = null;
7
+
8
+ const latest = {
9
+ position: {
10
+ time: null,
11
+ lat: null,
12
+ lon: null
13
+ },
14
+ altitude: {
15
+ time: null,
16
+ elevation: null
17
+ }
18
+ }
19
+
20
+ function onPositionUpdated(lat, lon) {
21
+ latest.position.time = Date.now();
22
+ latest.position.lat = lat;
23
+ latest.position.lon = lon;
24
+
25
+ if (lat && typeof lat==="number" && lon && typeof lon==="number")
26
+ {
27
+ let update = []
28
+ update.push(buildDelta(navigationPosition, formatPosition()))
29
+ deltahandler(update);
30
+ }
31
+ }
32
+
33
+ function onAltitudeUpdated(elevation) {
34
+ latest.altitude.time = Date.now();
35
+ latest.altitude.elevation = elevation;
36
+
37
+ if (elevation && typeof elevation==="number")
38
+ {
39
+ let update = []
40
+ update.push(buildDelta(navigationElevation, formatAltitude()))
41
+ deltahandler(update);
42
+ }
43
+ }
44
+
45
+ function buildDelta(path, value) {
46
+ return {
47
+ path: path,
48
+ value: value
49
+ }
50
+ }
51
+
52
+ function prepareFixed() {
53
+ let update = []
54
+ let meta = []
55
+ if (latest.position.lat!==null && latest.position.lon!==null)
56
+ update.push(buildDelta(navigationPosition, formatPosition()))
57
+ if (latest.altitude.elevation!==null) {
58
+ update.push(buildDelta(navigationElevation, formatAltitude()))
59
+ meta.push(buildDelta(navigationElevation, { "units": "m", "description": "Altitude above sealevel", "pgn":129029 } ))
60
+ }
61
+ return { "update": update, "meta": meta }
62
+ }
63
+
64
+ function prepareMeta() {
65
+ let update = []
66
+ let meta = []
67
+ meta.push(buildDelta(navigationElevation, { "units": "m", "description": "Altitude above sealevel", "pgn":129029 } ))
68
+ return { "update": update, "meta": meta }
69
+ }
70
+
71
+ async function prepareDynamic (callback) {
72
+ await geoloc.getIP('v4')
73
+ .then (ip => {
74
+ let geo = geoloc.getGeo(ip)
75
+ if (ip && geo) {
76
+ log(`Fixed Position determined [${geo.ll[0]},${geo.ll[1]}]`)
77
+ onPositionUpdated(geo.ll[0], geo.ll[1])
78
+
79
+ } else {
80
+ log(`Failed to determine position via external IP [${ip}].`)
81
+ }
82
+ })
83
+ .finally(() => {
84
+ geoloc.callElevation(latest.position.lat, latest.position.lon, (e) => {
85
+ if (e && typeof e==='number')
86
+ {
87
+ log(`Elevation determined for [${latest.position.lat},${latest.position.lon}] to ${e}m`)
88
+ onAltitudeUpdated(e)
89
+ } else {
90
+ log(`Failed to determin elevation: ${e}`)
91
+ }
92
+
93
+ })
94
+ })
95
+ }
96
+
97
+ function formatPosition()
98
+ {
99
+ let value = {
100
+ "latitude": latest.position.lat,
101
+ "longitude": latest.position.lon,
102
+ }
103
+ if (latest.altitude.elevation!==null)
104
+ value.altitude = latest.altitude.elevation
105
+
106
+ return value;
107
+ }
108
+
109
+ function formatAltitude()
110
+ {
111
+ return latest.altitude.elevation;
112
+ }
113
+
114
+ module.exports = {
115
+
116
+ // initialize
117
+ init: function(config, loghandler, messagehandler) {
118
+ geoloc.init(config)
119
+ log = loghandler
120
+ deltahandler = messagehandler
121
+ },
122
+
123
+ // clear all data
124
+ clearAll: function () {
125
+ latest.position.lat = null;
126
+ latest.position.lon = null;
127
+ latest.altitude.elevation = null;
128
+ },
129
+
130
+ // @param {bool} value calculate from external IP.
131
+ // @returns {Array<[{path:path, value:value}]>} Delta JSON-array of updates
132
+ onLoad: function(lat, lon, elev, calc) {
133
+ if (calc) // throw new Error("Cannot add null value");
134
+ {
135
+ prepareDynamic();
136
+ return prepareMeta();
137
+ }
138
+ else
139
+ {
140
+ onPositionUpdated(lat, lon);
141
+ onAltitudeUpdated(elev);
142
+ return prepareFixed();
143
+ }
144
+ }
145
+ }
package/geoloc.js CHANGED
@@ -1,209 +1,90 @@
1
- var geoip = require('fast-geoip');
1
+ const geoip = require('geoip-lite')
2
2
 
3
- const navigationPosition = 'navigation.position';
4
- const navigationElevation = 'navigation.gnss.antennaAltitude';
5
- let deltahandler = null;
6
- let log = null;
3
+ let maps_api_key
7
4
 
8
- const latest = {
9
- position: {
10
- time: null,
11
- lat: null,
12
- lon: null
13
- },
14
- altitude: {
15
- time: null,
16
- elevation: null
17
- }
18
- }
19
-
20
- function onPositionUpdated(lat, lon) {
21
- latest.position.time = Date.now();
22
- latest.position.lat = lat;
23
- latest.position.lon = lon;
24
- }
25
-
26
- function onAltitudeUpdated(elevation) {
27
- latest.altitude.time = Date.now();
28
- latest.altitude.elevation = elevation;
29
- }
30
-
31
- function buildDelta(path, value) {
32
- return {
33
- path: path,
34
- value: value
35
- }
36
- }
37
-
38
- function prepareFixed() {
39
- let update = []
40
- let meta = []
41
- if (latest.position.lat!==null && latest.position.lon!==null)
42
- update.push(buildDelta(navigationPosition, formatPosition()))
43
- if (latest.altitude.elevation!==null) {
44
- update.push(buildDelta(navigationElevation, formatAltitude()))
45
- meta.push(buildDelta(navigationElevation, { "units": "m", "description": "Altitude above sealevel", "pgn":129029 } ))
46
- }
47
- return { "update": update, "meta": meta }
48
- }
49
-
50
- function getpublicip (callback) {
51
-
52
- require('external-ip')()(function(err, ip) {
53
- if (!err) {
54
- log("Identified IP "+ip)
55
- callback(ip);
56
- } else {
57
- log("Cannot get IP: "+err)
58
- callback(err);
59
- }
60
- })
61
- }
62
-
63
- function getprettyip(ip) {
64
- // return geoip.pretty(ip);
65
- return ip;
66
- }
67
-
68
- async function getgeodata(ip) {
69
- return await geoip.lookup(ip);
70
- }
71
-
72
- async function preparePosition(ip) {
73
- let update = []
74
- if (ip!==null)
75
- {
76
- var geo = await getgeodata(ip)
77
- log("Geo data: "+geo.ll)
78
- }
79
- else
80
- {
81
- geo = null;
82
- log("No geo data")
83
- }
84
- if (extractPosition(geo))
85
- update.push(buildDelta(navigationPosition, formatPosition()))
86
- deltahandler(update);
87
- }
88
-
89
- function formatPosition()
5
+ function init(config)
90
6
  {
91
- let value = {
92
- "latitude":latest.position.lat,
93
- "longitude":latest.position.lon,
94
- }
95
- if (latest.altitude.elevation!==null)
96
- value.altitude = latest.altitude.elevation
97
-
98
- return value;
7
+ maps_api_key = config.apikey
99
8
  }
100
9
 
101
- // not implemented due to require("@googlemaps/google-maps-services-js") not free any more
102
- async function getelevation(lat, lon, fixed, callback) {
103
-
104
- if (true)
105
- callback(fixed);
106
- else
10
+ async function getIP(version)
11
+ {
12
+ let result
13
+ if (!version || version==='v4')
107
14
  {
108
- const {Client} = require("@googlemaps/google-maps-services-js");
109
- const client = new Client({});
110
-
111
- client
112
- .elevation({
113
- params: {
114
- locations: [{ lat: lat, lng: lon }],
115
- key: "provide you own key",
116
- },
117
- timeout: 1000, // milliseconds
15
+ const { publicIpv4 } = await import('public-ip')
16
+ await publicIpv4()
17
+ .then((res) => {
18
+ result = res
118
19
  })
119
- .then((r) => {
120
- callback(r.data.results[0].elevation);
121
- })
122
- .catch((e) => {
123
- callback(e.response.data.error_message);
124
- });
125
20
  }
126
- }
127
-
128
- async function prepareAltitude(mapdata) {
129
- let update = []
130
- let meta = []
131
- if (extractAltitude(mapdata))
21
+ else if (version==='v6')
132
22
  {
133
- update.push(buildDelta(navigationElevation, formatAltitude()))
134
- if (latest.position.lat!==null && latest.position.lon!==null)
135
- update.push(buildDelta(navigationPosition, formatPosition()))
23
+ const { publicIpv6 } = await import('public-ip')
24
+ await publicIpv6({
25
+ fallbackUrls: [
26
+ 'https://ifconfig.co/ip'
27
+ ]
28
+ }).then((res) => {
29
+ result = res
30
+ })
136
31
  }
137
- deltahandler(update);
32
+ else
33
+ return undefined
34
+ return result
138
35
  }
139
36
 
140
- function formatAltitude()
37
+ function getGeo(ip)
141
38
  {
142
- return latest.altitude.elevation;
39
+ return geoip.lookup(ip)
143
40
  }
144
41
 
145
- function extractPosition(geodata)
146
- {
147
- if (geodata!==null)
148
- {
149
- onPositionUpdated(geodata.ll[0],geodata.ll[1]);
150
- return true;
151
- }
152
- else if (latest.position.lat!==null && latest.position.lon!==null)
153
- {
154
- // use static data
155
- return true;
156
- }
157
- return false;
158
- }
42
+ function getElevation (lat, lon) {
159
43
 
160
- function extractAltitude(elevation)
161
- {
162
- if (elevation!==null)
163
- {
164
- onAltitudeUpdated(elevation);
165
- return true;
166
- }
167
- else if (latest.altitude.elevation!==null)
168
- {
169
- // use static data
170
- return true;
171
- }
172
- return false;
44
+ const { Client } = require("@googlemaps/google-maps-services-js");
45
+ const client = new Client({});
46
+
47
+ client
48
+ .elevation({
49
+ params: {
50
+ locations: [{ lat: lat, lng: lon }],
51
+ key: config.google.maps.key,
52
+ },
53
+ timeout: 1000, // milliseconds
54
+ })
55
+ .then((r) => {
56
+ return r.data.results[0].elevation;
57
+ })
58
+ .catch((e) => {
59
+ return e.response.data.error_message;
60
+ });
173
61
  }
174
62
 
175
- module.exports = {
63
+ async function callElevation (lat, lon, callback) {
176
64
 
177
- getpublicip,
178
- getprettyip,
179
- getgeodata,
180
- getelevation,
65
+ const { Client } = require("@googlemaps/google-maps-services-js");
66
+ const client = new Client({});
181
67
 
182
- init: function(loghandler, messagehandler) {
183
- log = loghandler
184
- deltahandler = messagehandler
185
- },
68
+ client
69
+ .elevation({
70
+ params: {
71
+ locations: [{ lat: lat, lng: lon }],
72
+ key: maps_api_key,
73
+ },
74
+ timeout: 1000, // milliseconds
75
+ })
76
+ .then((r) => {
77
+ callback(r.data.results[0].elevation);
78
+ })
79
+ .catch((e) => {
80
+ callback(e.response.data.error_message);
81
+ });
82
+ }
186
83
 
187
- // clear all data
188
- clearAll: function () {
189
- latest.position.lat = null;
190
- latest.position.lon = null;
191
- latest.altitude.elevation = null;
192
- },
193
-
194
- // @param {bool} value calculate from external IP.
195
- // @returns {Array<[{path:path, value:value}]>} Delta JSON-array of updates
196
- onLoad: function(lat, lon, elev, calc) {
197
- if (calc) // throw new Error("Cannot add null value");
198
- {
199
- getpublicip(preparePosition);
200
- getelevation(lat, lon, elev, prepareAltitude);
201
- }
202
- else
203
- {
204
- onPositionUpdated(lat, lon);
205
- onAltitudeUpdated(elev);
206
- return prepareFixed();
207
- }
208
- }
209
- }
84
+ module.exports = {
85
+ init,
86
+ getIP,
87
+ getGeo,
88
+ getElevation,
89
+ callElevation
90
+ };
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  'use strict'
2
- var geoloc = require('./geoloc')
2
+ const fs = require('./fixedstation')
3
3
 
4
4
  module.exports = function (app) {
5
5
  var plugin = {};
@@ -8,15 +8,16 @@ module.exports = function (app) {
8
8
  plugin.name = 'Fixed Station';
9
9
  plugin.description = 'Provide fixed navigation data (position, altitude)';
10
10
 
11
- var unsubscribes = [ geoloc.clearAll ];
11
+ var unsubscribes = [ fs.clearAll ];
12
12
 
13
13
  plugin.start = function (options, restartPlugin) {
14
14
  app.debug('Plugin starting ...');
15
- geoloc.init(log, sendDelta);
16
- let delta = geoloc.onLoad(options["latitude"], options["longitude"], options["elevation"], options["dynamic"]);
15
+ fs.init(options, log, sendDelta);
16
+ let delta = fs.onLoad(options["latitude"], options["longitude"], options["elevation"], options["dynamic"]);
17
17
  app.debug('Plugin started.');
18
- if (delta!==undefined && delta.update.length>0) {
19
- sendDelta(delta.update);
18
+ if (delta) {
19
+ if (delta.update && delta.update.length>0)
20
+ sendDelta(delta.update);
20
21
  if (delta.meta && delta.meta.length>0)
21
22
  sendMeta(delta.meta);
22
23
  }
@@ -53,6 +54,10 @@ module.exports = function (app) {
53
54
  elevation: {
54
55
  type: 'number',
55
56
  title: 'Elevation above sea level. Used to export navigation.gnss.antennaAltitude'
57
+ },
58
+ apikey: {
59
+ type: 'string',
60
+ title: 'Google Maps API Key. Elevation data for any point in the world.'
56
61
  }
57
62
  }
58
63
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "signalk-fixedstation",
3
- "version": "0.3.2",
3
+ "version": "0.5.0",
4
4
  "description": "SignalK plugin to provide fixed navigation data",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -13,7 +13,8 @@
13
13
  },
14
14
  "keywords": [
15
15
  "signalk",
16
- "signalk-node-server-plugin"
16
+ "signalk-node-server-plugin",
17
+ "signalk-category-utility"
17
18
  ],
18
19
  "author": "Inspired Technologies",
19
20
  "license": "MIT",
@@ -22,9 +23,9 @@
22
23
  },
23
24
  "homepage": "https://github.com/inspired-technologies/signalk-fixedstation-plugin#readme",
24
25
  "dependencies": {
25
- "debug": "^4.3.1",
26
- "external-ip": "^2.3.1",
27
- "fast-geoip": "^1.1.3",
28
- "geoip-lite": "^1.4.2"
26
+ "@googlemaps/google-maps-services-js": "^3.3.42",
27
+ "debug": "^4.3.4",
28
+ "geoip-lite": "^1.4.10",
29
+ "public-ip": "^6.0.2"
29
30
  }
30
31
  }