kahu-signalk 0.0.8 → 0.0.10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kahu-signalk",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
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
@@ -10,7 +10,50 @@ while (packageDir !== path.dirname(packageDir)
10
10
  packageDir = path.dirname(packageDir);
11
11
  }
12
12
 
13
- const nmeaRattmRegex = /\$RATTM,(\d{2}),([\d\.\-]+),([\d\.\-]+),([^,]*),([\d\.\-]+),([\d\.\-]+),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),(..*)\*([A-Fa-f0-9]{2})\s*/;
13
+ const parseOptionalFloat = (value) => {
14
+ if (value === undefined || value === null || value === '') return undefined;
15
+ const parsed = parseFloat(value);
16
+ return Number.isNaN(parsed) ? undefined : parsed;
17
+ };
18
+
19
+ const parseRattmSentence = (sentence) => {
20
+ const [payload] = sentence.split('*');
21
+ if (!payload) return null;
22
+
23
+ const parts = payload.trim().split(',');
24
+ if (parts[0] !== '$RATTM') return null;
25
+ if (parts.length < 16) return null;
26
+
27
+ const target_id = parseInt(parts[1], 10);
28
+ const target_distance = parseOptionalFloat(parts[2]);
29
+ const target_bearing = parseOptionalFloat(parts[3]);
30
+ const target_bearing_unit = parts[4] || '';
31
+ const target_speed = parseOptionalFloat(parts[5]);
32
+ const target_course = parseOptionalFloat(parts[6]);
33
+ const target_course_unit = parts[7] || '';
34
+ const target_distance_unit = parts[10] || '';
35
+ const target_name = parts[12] || '';
36
+ const target_status = parts[13] || '';
37
+
38
+ if (!Number.isFinite(target_id) ||
39
+ !Number.isFinite(target_distance) ||
40
+ !Number.isFinite(target_bearing)) {
41
+ return null;
42
+ }
43
+
44
+ return {
45
+ target_id,
46
+ target_distance,
47
+ target_bearing,
48
+ target_bearing_unit,
49
+ target_speed,
50
+ target_course,
51
+ target_course_unit,
52
+ target_distance_unit,
53
+ target_name,
54
+ target_status,
55
+ };
56
+ };
14
57
 
15
58
  const deg2rad = (degrees) => degrees * (Math.PI / 180);
16
59
 
@@ -48,21 +91,24 @@ module.exports = (app) => {
48
91
  parser: ({ id, sentence, parts, tags }, session) => {
49
92
  if (sentence.startsWith("$RATTL")) {
50
93
  } else if (sentence.startsWith("$RATTM")) {
51
- const match = nmeaRattmRegex.exec(sentence);
52
-
53
- if (!match) {
94
+ const parsed = parseRattmSentence(sentence);
95
+ if (!parsed) {
54
96
  console.error("Failed to parse RATTM NMEA sentence: [", sentence, "]");
55
97
  return;
56
98
  }
57
- if (match.length - 1 != 14) {
58
- console.log("Only parsed ", (match.length - 1), " fields of RATTM NMEA sentence: [", sentence, "]");
59
- return;
60
- }
61
-
62
- const target_id = parseInt(match[1]);
63
- const target_distance = parseFloat(match[2]);
64
- const target_bearing = parseFloat(match[3]);
65
- const target_bearing_unit = match[4];
99
+
100
+ const {
101
+ target_id,
102
+ target_distance,
103
+ target_bearing,
104
+ target_bearing_unit,
105
+ target_speed,
106
+ target_course,
107
+ target_course_unit,
108
+ target_distance_unit,
109
+ target_name,
110
+ target_status,
111
+ } = parsed;
66
112
 
67
113
  if (target_bearing_unit === 'R') {
68
114
  console.warn("Relative bearings not yet supported, skipping RATTM sentence");
@@ -81,16 +127,11 @@ module.exports = (app) => {
81
127
  distance: target_distance,
82
128
  bearing: target_bearing,
83
129
  bearing_unit: target_bearing_unit,
84
- distance_unit: match[10],
130
+ distance_unit: target_distance_unit,
85
131
  }
86
132
 
87
- const target_speed = parseFloat(match[5]);
88
- const target_course = parseFloat(match[6]);
89
- const target_course_unit = match[7];
90
133
  // target_distance_closes_point_of_approac: parseInt(match[8]),
91
134
  // target_time_closes_point_of_approac: parseInt(match[9]),
92
- const target_name = match[11];
93
- const target_status = match[12];
94
135
 
95
136
  const now = new Date();
96
137
  if (now - plugin.route_updates[target_id] > 60000) {
@@ -130,7 +171,7 @@ module.exports = (app) => {
130
171
  },
131
172
  stop: async () => {
132
173
  await plugin.connector?.destroy?.();
133
- await plugin.routecache?.destroy?.();
174
+ await plugin.cache?.destroy?.();
134
175
  console.log("Stopped KAHU radar Hub")
135
176
  },
136
177
  schema: () => {