kahu-signalk 0.0.7 → 0.0.9

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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 KAHU Earth AS
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,13 @@
1
+ # KAHU Radar Hub protocol
2
+ *A radar crowdsourcing protocol*
3
+
4
+ Contribute AIS and ARPA targets from your vessel to crowdsourcing for marine safety!
5
+
6
+ This is the ptotocol specification used by our plugins and clients as well as server code (e.g. [radarhub-opencpn](https://github.com/KAHU-radar/radarhub-opencpn) and [radarhub-signalk](https://github.com/KAHU-radar/radarhub-signalk)) that lets you upload AIS and radar ARPA targets (or any NMEA) to an internet server.
7
+ The communication protocol is based on [Apache Avro](https://avro.apache.org/) and batches track points so that the overhead for each point above timestamp and lat/lon is low, meaning it is designed to be as bandwidth conservative as possible.
8
+
9
+ Note: This protocol does **not** use the Avro RPC mechanism, as it is not well supported in all languages, and adds extra requirements such as adding run-length framing to each message. Instead, it relies on a simple union of message types.
10
+
11
+ ## Database schema
12
+
13
+ The protocol includes a client side database schema used to cache tracks in an sqlite3 database. It is provided as a series of migration SQL files, to be applied in alphabetical order.
@@ -0,0 +1,24 @@
1
+ create table if not exists target (
2
+ target_id integer primary key autoincrement,
3
+ uuid varchar(36) unique
4
+ );
5
+
6
+ create table if not exists target_position (
7
+ id integer primary key autoincrement,
8
+ timestamp datetime default current_timestamp,
9
+ target_id integer references target(id),
10
+ target_distance float,
11
+ target_bearing float,
12
+ target_bearing_unit text,
13
+ target_speed float,
14
+ target_course float,
15
+ target_course_unit text,
16
+ target_distance_unit text,
17
+ target_name text,
18
+ target_status text,
19
+ latitude float,
20
+ longitude float,
21
+ target_latitude float,
22
+ target_longitude float,
23
+ sent integer default false
24
+ );
@@ -0,0 +1,3 @@
1
+ CREATE INDEX idx_sent ON target_position(sent);
2
+ CREATE INDEX idx_target_id ON target_position(target_id);
3
+ CREATE INDEX idx_sent_target_id_timestamp ON target_position(sent, target_id, timestamp);
@@ -0,0 +1,71 @@
1
+ {"name": "kahu.Proto", "type": "record",
2
+ "fields": [
3
+ {"name": "Message", "type": [
4
+ {"name": "kahu.Call", "type": "record",
5
+ "fields": [
6
+ {"name": "Call", "type": {
7
+ "name": "kahu.CallMessage", "type": "record",
8
+ "fields": [
9
+ {"name": "id", "type": "int"},
10
+ {"name": "Call", "type": [
11
+ {"name": "kahu.LoginMessage", "type": "record",
12
+ "fields": [{"name": "Login", "type": {
13
+ "name": "kahu.Login", "type": "record",
14
+ "fields": [
15
+ {"name": "apikey", "type": "string"}
16
+ ]
17
+ }}]},
18
+ {"name": "kahu.SubmitMessage", "type": "record",
19
+ "fields": [{"name": "Submit", "type": {
20
+ "name": "kahu.Submit", "type": "record",
21
+ "fields": [
22
+ {"name": "uuid", "type": ["null", "string"], "logicalType": "uuid"},
23
+ {"name": "route", "type": {
24
+ "type": "array", "items": {
25
+ "name": "kahu.LineString",
26
+ "type": "record",
27
+ "fields": [
28
+ {"name": "lat", "type": "float"},
29
+ {"name": "lon", "type": "float"},
30
+ {"name": "timestamp", "type": "float"}
31
+ ]
32
+ }}},
33
+ {"name": "nmea", "type": ["null", "string"]},
34
+ {"name": "start", "type": "long", "logicalType": "timestamp-millis"}
35
+ ]
36
+ }}]}
37
+ ]}
38
+ ]}}
39
+ ]},
40
+ {"name": "kahu.Response", "type": "record",
41
+ "fields": [
42
+ {"name": "Response", "type": {
43
+ "name": "kahu.ResponseMessage", "type": "record",
44
+ "fields": [
45
+ {"name": "id", "type": "int"},
46
+ {"name": "Response", "type": [
47
+ {"name": "kahu.ErrorResponseMessage", "type": "record",
48
+ "fields": [{"name": "Error", "type": {
49
+ "name": "kahu.ErrorResponse", "type": "record",
50
+ "fields": [
51
+ {"name": "exception", "type": "string"}
52
+ ]
53
+ }}]},
54
+ {"name": "kahu.LoginResponseMessage", "type": "record",
55
+ "fields": [{"name": "Login", "type": {
56
+ "name": "kahu.LoginResponse", "type": "record",
57
+ "fields": []
58
+ }}]},
59
+ {"name": "kahu.SubmitResponseMessage", "type": "record",
60
+ "fields": [{"name": "Submit", "type": {
61
+ "name": "kahu.SubmitResponse", "type": "record",
62
+ "fields": [
63
+ {"name": "uuid", "type": ["null", "string"], "logicalType": "uuid"}
64
+ ]
65
+ }}]}
66
+ ]}
67
+ ]}}
68
+ ]}
69
+ ]}
70
+ ]
71
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kahu-signalk",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "Contribute AIS and ARPA targets from your vessel to crowdsourcing for marine safety!",
5
5
  "keywords": [
6
6
  "signalk-node-server-plugin",
@@ -1,18 +1,18 @@
1
- const fs = require('fs').promises;
2
- const { delay } = require('./utils');
3
- const { AvroClient } = require('./avroclient');
4
- const path = require('path');
1
+ const fs = require("fs").promises;
2
+ const { delay } = require("./utils");
3
+ const { AvroClient } = require("./avroclient");
4
+ const path = require("path");
5
5
 
6
6
  // Connector acts as a cooperative thread: When created, it starts an
7
7
  // async timeout function immediately
8
8
 
9
9
  process.on("unhandledRejection", (reason, promise) => {
10
- console.error("Unhandled Rejection at:", promise);
11
- console.error("Reason:", reason.stack);
10
+ console.error("Unhandled Rejection at:", promise);
11
+ console.error("Reason:", reason.stack);
12
12
  });
13
13
 
14
14
  class Connector {
15
- constructor({status_function, routecache, plugin_dir, config}) {
15
+ constructor({ status_function, routecache, plugin_dir, config }) {
16
16
  this.last_stats = null;
17
17
  this.last_status = null;
18
18
  this.status_function = status_function;
@@ -28,7 +28,8 @@ class Connector {
28
28
  plugin_dir,
29
29
  "data",
30
30
  "protocol",
31
- "proto_avro.json");
31
+ "proto_avro.json",
32
+ );
32
33
  this.schema = null;
33
34
  console.error("Connector created");
34
35
  setTimeout(this.main.bind(this), 0);
@@ -45,7 +46,7 @@ class Connector {
45
46
  this.last_stats = await this.routecache.connectionStats();
46
47
  this.updateStatus();
47
48
  }
48
-
49
+
49
50
  setStatus(status) {
50
51
  this.last_status = status;
51
52
  this.updateStatus();
@@ -55,11 +56,13 @@ class Connector {
55
56
  const status = [];
56
57
  if (this.last_status !== null) status.push(this.last_status);
57
58
  if (this.last_stats !== null) {
58
- status.push(`${this.last_stats.unsent_tracks} unsent tracks totalling ${this.last_stats.unsent_datapoints} unsent datapoints`);
59
+ status.push(
60
+ `${this.last_stats.unsent_tracks} unsent tracks totalling ${this.last_stats.unsent_datapoints} unsent datapoints`,
61
+ );
59
62
  }
60
63
  this.status_function?.(status.join(", "));
61
64
  }
62
-
65
+
63
66
  async read(type) {
64
67
  console.error("Connector parsing response");
65
68
 
@@ -70,12 +73,16 @@ class Connector {
70
73
  throw "Received response with wrong callid";
71
74
  }
72
75
  const content = response.Response;
73
-
74
- if (content["kahu.ErrorResponseMessage"] !== undefined) {
76
+
77
+ if (content["kahu.ErrorResponseMessage"] !== undefined) {
75
78
  throw content.Error.exception;
76
- } else if ( (type !== undefined)
77
- && (content[type] === undefined)) {
78
- throw "Received response for wrong method: expected " + type + " but got " + Object.keys(content)[0];
79
+ } else if (type !== undefined && content[type] === undefined) {
80
+ throw (
81
+ "Received response for wrong method: expected " +
82
+ type +
83
+ " but got " +
84
+ Object.keys(content)[0]
85
+ );
79
86
  }
80
87
  console.error("Connector response parsed");
81
88
  return container;
@@ -83,7 +90,7 @@ class Connector {
83
90
 
84
91
  async login() {
85
92
  while (!this.config.api_key) await delay(500);
86
-
93
+
87
94
  console.error("Connector logging in");
88
95
 
89
96
  await this.client.send({
@@ -94,15 +101,15 @@ class Connector {
94
101
  Call: {
95
102
  "kahu.LoginMessage": {
96
103
  Login: {
97
- apikey: this.config.api_key
98
- }
99
- }
100
- }
101
- }
102
- }
103
- }
104
+ apikey: this.config.api_key,
105
+ },
106
+ },
107
+ },
108
+ },
109
+ },
110
+ },
104
111
  });
105
-
112
+
106
113
  console.error("Send done, gonna parse response\n");
107
114
  const response = await this.read("kahu.LoginResponseMessage");
108
115
  console.error("Response parsed");
@@ -110,9 +117,9 @@ class Connector {
110
117
  }
111
118
 
112
119
  async sendTracks() {
113
- console.error("Connector sending tracks");
114
120
  const submit = await this.routecache.retrieve();
115
121
  if (submit === null) return;
122
+ console.error("Connector sending tracks");
116
123
  await this.client.send({
117
124
  Message: {
118
125
  "kahu.Call": {
@@ -120,14 +127,14 @@ class Connector {
120
127
  id: ++this.callid,
121
128
  Call: {
122
129
  "kahu.SubmitMessage": {
123
- Submit: submit
124
- }
125
- }
126
- }
127
- }
128
- }
130
+ Submit: submit,
131
+ },
132
+ },
133
+ },
134
+ },
135
+ },
129
136
  });
130
-
137
+
131
138
  await this.read("kahu.SubmitResponseMessage");
132
139
  await this.routecache.markAsSent(submit);
133
140
  this.last_track_sent = new Date();
@@ -137,9 +144,9 @@ class Connector {
137
144
 
138
145
  async main() {
139
146
  try {
140
- console.error("Connector running client is null: ", (this.client === null));
147
+ console.error("Connector running client is null: ", this.client === null);
141
148
 
142
- this.schema = await fs.readFile(this.schema_file, 'utf8');
149
+ this.schema = await fs.readFile(this.schema_file, "utf8");
143
150
 
144
151
  this.client = new AvroClient({
145
152
  schema: this.schema,
@@ -148,12 +155,11 @@ class Connector {
148
155
  min_reconnect_time: this.config.min_reconnect_time || 100.0,
149
156
  max_reconnect_time: this.config.max_reconnect_time || 6000.0,
150
157
  connect_function: this.login.bind(this),
151
- status_function: this.setStatus.bind(this)
158
+ status_function: this.setStatus.bind(this),
152
159
  });
153
160
 
154
161
  while (!this.cancelled) {
155
162
  try {
156
- console.error("Connector connecting...");
157
163
  await this.client.ensureConnection();
158
164
  if (this.cancelled) break;
159
165
  await this.sendTracks();
@@ -170,6 +176,6 @@ class Connector {
170
176
  console.error(e.stack);
171
177
  }
172
178
  }
173
- };
179
+ }
174
180
 
175
181
  module.exports = { Connector };
package/plugin/index.js CHANGED
@@ -130,7 +130,7 @@ module.exports = (app) => {
130
130
  },
131
131
  stop: async () => {
132
132
  await plugin.connector?.destroy?.();
133
- await plugin.routecache?.destroy?.();
133
+ await plugin.cache?.destroy?.();
134
134
  console.log("Stopped KAHU radar Hub")
135
135
  },
136
136
  schema: () => {