kahu-signalk 0.0.6 → 0.0.7
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/README.md +25 -2
- package/package.json +3 -4
- package/plugin/routecache.js +120 -91
- package/scripts/check-node-version.js +16 -0
package/README.md
CHANGED
|
@@ -185,8 +185,31 @@ wanting to build a more elaborate server-side setup.
|
|
|
185
185
|
## Runtime Requirements
|
|
186
186
|
|
|
187
187
|
- **Signal K Server:** v2.22.1+ (latest stable)
|
|
188
|
-
- **Node.js:**
|
|
189
|
-
- **Dependencies:** avro-js, promise-socket,
|
|
188
|
+
- **Node.js:** **22.5.0+** (built-in `node:sqlite`; no native `sqlite3` bindings).
|
|
189
|
+
- **Dependencies:** avro-js, promise-socket, uuid
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
## Node.js version (enforced)
|
|
194
|
+
|
|
195
|
+
This plugin uses **Node’s built-in SQLite** (`node:sqlite`, `DatabaseSync`) — **not** the `sqlite3` npm package. That avoids native addon / binding failures entirely.
|
|
196
|
+
|
|
197
|
+
| Requirement | Detail |
|
|
198
|
+
|---------------|--------|
|
|
199
|
+
| **Node.js** | **22.5.0 or later** (same major line Signal K recommends) |
|
|
200
|
+
| **Install check** | `npm install` runs a **preinstall** script; it **exits with an error** on older Node so you get a clear message instead of a runtime crash. |
|
|
201
|
+
|
|
202
|
+
If you see *“kahu-signalk requires Node.js 22.5.0 or later”*, switch the Node version used to run Signal K (e.g. [nvm](https://github.com/nvm-sh/nvm) or [Signal K’s Node guide](https://github.com/SignalK/signalk-server/wiki/Installing-and-Updating-Node.js)), then install the plugin again.
|
|
203
|
+
|
|
204
|
+
You may see an **ExperimentalWarning** about SQLite from Node; that is expected until the API is stabilized.
|
|
205
|
+
|
|
206
|
+
### Command typo
|
|
207
|
+
|
|
208
|
+
Use `--config-dir` (with an **n**), not `--confic-dir`:
|
|
209
|
+
```bash
|
|
210
|
+
npm start -- --config-dir /home/bs01743/Projects/KAHU/signalk-server/signalk-server/config
|
|
211
|
+
```
|
|
212
|
+
The `--` is needed so npm passes `--config-dir` to the start script.
|
|
190
213
|
|
|
191
214
|
---
|
|
192
215
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kahu-signalk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
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",
|
|
@@ -15,9 +15,10 @@
|
|
|
15
15
|
},
|
|
16
16
|
"main": "plugin/index.js",
|
|
17
17
|
"engines": {
|
|
18
|
-
"node": ">=
|
|
18
|
+
"node": ">=22.5.0"
|
|
19
19
|
},
|
|
20
20
|
"scripts": {
|
|
21
|
+
"preinstall": "node scripts/check-node-version.js",
|
|
21
22
|
"test": "echo \"Error: no test specified passing build\" && exit 0"
|
|
22
23
|
},
|
|
23
24
|
"author": "Kahu <info@kahu.earth>",
|
|
@@ -29,8 +30,6 @@
|
|
|
29
30
|
"dependencies": {
|
|
30
31
|
"avro-js": "^1.12.0",
|
|
31
32
|
"promise-socket": "^8.0.0",
|
|
32
|
-
"sqlite": "^5.1.1",
|
|
33
|
-
"sqlite3": "^6.0.1",
|
|
34
33
|
"uuid": "^8.1.0"
|
|
35
34
|
}
|
|
36
35
|
}
|
package/plugin/routecache.js
CHANGED
|
@@ -1,8 +1,23 @@
|
|
|
1
|
-
const sqlite3 = require('sqlite3').verbose();
|
|
2
|
-
const sqlite = require('sqlite');
|
|
3
1
|
const fs = require('fs').promises;
|
|
4
2
|
const path = require('path');
|
|
5
3
|
|
|
4
|
+
let DatabaseSync;
|
|
5
|
+
try {
|
|
6
|
+
({ DatabaseSync } = require('node:sqlite'));
|
|
7
|
+
} catch (e) {
|
|
8
|
+
DatabaseSync = null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function requireNodeSqlite() {
|
|
12
|
+
if (!DatabaseSync) {
|
|
13
|
+
throw new Error(
|
|
14
|
+
'kahu-signalk requires Node.js 22.5+ (built-in node:sqlite). ' +
|
|
15
|
+
'Install Node 22 LTS as recommended for Signal K: ' +
|
|
16
|
+
'https://github.com/SignalK/signalk-server/wiki/Installing-and-Updating-Node.js'
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
6
21
|
class Routecache {
|
|
7
22
|
constructor(migrations_dir, db_name) {
|
|
8
23
|
this.db = null;
|
|
@@ -11,8 +26,25 @@ class Routecache {
|
|
|
11
26
|
console.log("Routecache created for " + db_name + " with migrations " + migrations_dir);
|
|
12
27
|
}
|
|
13
28
|
|
|
29
|
+
_sanitizeParam(v) {
|
|
30
|
+
return v === undefined ? null : v;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
_all(sql, params = []) {
|
|
34
|
+
const stmt = this.db.prepare(sql);
|
|
35
|
+
const safe = params.map(this._sanitizeParam);
|
|
36
|
+
return safe.length ? stmt.all(...safe) : stmt.all();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
_run(sql, params = []) {
|
|
40
|
+
const stmt = this.db.prepare(sql);
|
|
41
|
+
const safe = params.map(this._sanitizeParam);
|
|
42
|
+
return safe.length ? stmt.run(...safe) : stmt.run();
|
|
43
|
+
}
|
|
44
|
+
|
|
14
45
|
async init() {
|
|
15
46
|
try {
|
|
47
|
+
requireNodeSqlite();
|
|
16
48
|
await this.openDB();
|
|
17
49
|
await this.createEmpty();
|
|
18
50
|
await this.migrate();
|
|
@@ -30,33 +62,30 @@ class Routecache {
|
|
|
30
62
|
}
|
|
31
63
|
}
|
|
32
64
|
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
"SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name=?"
|
|
36
|
-
|
|
37
|
-
return
|
|
65
|
+
doesTableExist(tableName) {
|
|
66
|
+
const row = this.db
|
|
67
|
+
.prepare("SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name=?")
|
|
68
|
+
.get(tableName);
|
|
69
|
+
return Number(row.count) > 0;
|
|
38
70
|
}
|
|
39
71
|
|
|
40
72
|
async destroy() {
|
|
41
|
-
if (this.db != null)
|
|
73
|
+
if (this.db != null) this.db.close();
|
|
42
74
|
this.db = null;
|
|
43
75
|
}
|
|
44
76
|
|
|
45
77
|
async openDB() {
|
|
46
|
-
this.db =
|
|
47
|
-
filename: this.db_name,
|
|
48
|
-
driver: sqlite3.Database
|
|
49
|
-
});
|
|
78
|
+
this.db = new DatabaseSync(this.db_name);
|
|
50
79
|
}
|
|
51
80
|
|
|
52
81
|
async closeDB() {
|
|
53
|
-
if (this.db != null)
|
|
82
|
+
if (this.db != null) this.db.close();
|
|
54
83
|
this.db = null;
|
|
55
84
|
}
|
|
56
85
|
|
|
57
86
|
async createEmpty() {
|
|
58
87
|
if (await this.doesTableExist("migrations")) return;
|
|
59
|
-
|
|
88
|
+
this.db.exec(`
|
|
60
89
|
CREATE TABLE IF NOT EXISTS migrations (
|
|
61
90
|
id integer,
|
|
62
91
|
name text,
|
|
@@ -66,58 +95,51 @@ class Routecache {
|
|
|
66
95
|
}
|
|
67
96
|
|
|
68
97
|
async migrate() {
|
|
69
|
-
const rows =
|
|
70
|
-
"SELECT max(id) as maxid FROM migrations");
|
|
98
|
+
const rows = this._all("SELECT max(id) as maxid FROM migrations");
|
|
71
99
|
const maxId = rows[0].maxid;
|
|
100
|
+
const baseline = maxId == null ? 0 : Number(maxId);
|
|
72
101
|
|
|
73
102
|
try {
|
|
74
103
|
const files = (
|
|
75
|
-
await fs.readdir(
|
|
76
|
-
|
|
77
|
-
)
|
|
78
|
-
|
|
79
|
-
(file) => file.isFile()
|
|
80
|
-
).map(
|
|
81
|
-
(file) => file.name);
|
|
104
|
+
await fs.readdir(this.migrations_dir, { withFileTypes: true })
|
|
105
|
+
)
|
|
106
|
+
.filter((file) => file.isFile())
|
|
107
|
+
.map((file) => file.name);
|
|
82
108
|
files.sort();
|
|
83
|
-
|
|
109
|
+
|
|
84
110
|
for (const filename of files) {
|
|
85
111
|
const migrationId = parseInt(filename, 10);
|
|
86
|
-
if (migrationId
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
112
|
+
if (!Number.isFinite(migrationId) || migrationId <= baseline) continue;
|
|
113
|
+
const migrationPath = path.join(this.migrations_dir, filename);
|
|
114
|
+
await this.runMigration(migrationId, migrationPath);
|
|
90
115
|
}
|
|
91
116
|
} catch (error) {
|
|
92
|
-
|
|
117
|
+
throw new Error(`Unable to process migrations directory: ${error.message}`);
|
|
93
118
|
}
|
|
94
119
|
}
|
|
95
120
|
|
|
96
121
|
async runMigration(i, name) {
|
|
97
122
|
console.error("Running migration ", i, ": ", name);
|
|
98
123
|
|
|
99
|
-
const sql = await fs.readFile(name, 'utf8');
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
"insert into migrations (id, name) values (?, ?)",
|
|
103
|
-
[i, name]);
|
|
124
|
+
const sql = await fs.readFile(name, 'utf8');
|
|
125
|
+
this.db.exec(sql);
|
|
126
|
+
this._run("insert into migrations (id, name) values (?, ?)", [i, name]);
|
|
104
127
|
}
|
|
105
128
|
|
|
106
|
-
async insert({...props}) {
|
|
107
|
-
const target_count =
|
|
108
|
-
select count(*) as count from target where uuid =
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
`, [props.target_id]);
|
|
129
|
+
async insert({ ...props }) {
|
|
130
|
+
const target_count = this._all(
|
|
131
|
+
`select count(*) as count from target where uuid = ?;`,
|
|
132
|
+
[props.target_id]
|
|
133
|
+
);
|
|
134
|
+
if (Number(target_count[0].count) === 0) {
|
|
135
|
+
this._run(`insert into target (uuid) values (?);`, [props.target_id]);
|
|
114
136
|
}
|
|
115
|
-
const target =
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
insert into target_position (
|
|
137
|
+
const target = this._all(`select target_id from target where uuid = ?;`, [
|
|
138
|
+
props.target_id,
|
|
139
|
+
]);
|
|
140
|
+
|
|
141
|
+
this._run(
|
|
142
|
+
`insert into target_position (
|
|
121
143
|
target_id,
|
|
122
144
|
target_distance,
|
|
123
145
|
target_bearing,
|
|
@@ -135,32 +157,35 @@ class Routecache {
|
|
|
135
157
|
) values (
|
|
136
158
|
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
137
159
|
`,
|
|
138
|
-
[
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
160
|
+
[
|
|
161
|
+
target[0].target_id,
|
|
162
|
+
props.position?.relative?.distance,
|
|
163
|
+
props.position?.relative?.bearing,
|
|
164
|
+
props.position?.relative?.bearing_unit,
|
|
165
|
+
props.speedOverGround,
|
|
166
|
+
props.courseOverGroundTrue,
|
|
167
|
+
'T',
|
|
168
|
+
props.position?.relative?.distance_unit,
|
|
169
|
+
props.name,
|
|
170
|
+
'T',
|
|
171
|
+
props.position?.relative?.position?.latitude,
|
|
172
|
+
props.position?.relative?.position?.longitude,
|
|
173
|
+
props.position?.latitude,
|
|
174
|
+
props.position?.longitude,
|
|
175
|
+
]
|
|
176
|
+
);
|
|
152
177
|
}
|
|
153
178
|
|
|
154
179
|
async connectionStats() {
|
|
155
|
-
const query1 =
|
|
180
|
+
const query1 = this._all(`
|
|
156
181
|
select
|
|
157
182
|
count(*) as unsent_datapoints
|
|
158
183
|
from
|
|
159
184
|
target_position
|
|
160
|
-
where
|
|
185
|
+
where
|
|
161
186
|
not sent;
|
|
162
187
|
`);
|
|
163
|
-
const query2 =
|
|
188
|
+
const query2 = this._all(`
|
|
164
189
|
select
|
|
165
190
|
count(*) as unsent_tracks
|
|
166
191
|
from
|
|
@@ -172,12 +197,14 @@ class Routecache {
|
|
|
172
197
|
not sent
|
|
173
198
|
)
|
|
174
199
|
`);
|
|
175
|
-
return {
|
|
176
|
-
|
|
200
|
+
return {
|
|
201
|
+
unsent_datapoints: Number(query1[0].unsent_datapoints),
|
|
202
|
+
unsent_tracks: Number(query2[0].unsent_tracks),
|
|
203
|
+
};
|
|
177
204
|
}
|
|
178
|
-
|
|
205
|
+
|
|
179
206
|
async retrieve() {
|
|
180
|
-
const query =
|
|
207
|
+
const query = this._all(`
|
|
181
208
|
select
|
|
182
209
|
target.uuid,
|
|
183
210
|
target_position.timestamp,
|
|
@@ -201,9 +228,9 @@ class Routecache {
|
|
|
201
228
|
target_id
|
|
202
229
|
from
|
|
203
230
|
target_position
|
|
204
|
-
group by
|
|
231
|
+
group by
|
|
205
232
|
target_id
|
|
206
|
-
having
|
|
233
|
+
having
|
|
207
234
|
count(*) > 1
|
|
208
235
|
)
|
|
209
236
|
order by
|
|
@@ -218,19 +245,17 @@ class Routecache {
|
|
|
218
245
|
if (!query.length) return null;
|
|
219
246
|
|
|
220
247
|
const res = {
|
|
221
|
-
uuid: {
|
|
248
|
+
uuid: { string: query[0].uuid },
|
|
222
249
|
route: [],
|
|
223
250
|
nmea: null,
|
|
224
|
-
start: query[0].timestamp_epoch
|
|
225
|
-
|
|
226
|
-
let isfirst = true;
|
|
227
|
-
let start;
|
|
251
|
+
start: Number(query[0].timestamp_epoch),
|
|
252
|
+
};
|
|
228
253
|
|
|
229
254
|
for (const row of query) {
|
|
230
255
|
res.route.push({
|
|
231
256
|
lat: row.target_latitude,
|
|
232
257
|
lon: row.target_longitude,
|
|
233
|
-
timestamp: row.timestamp_epoch - res.start
|
|
258
|
+
timestamp: Number(row.timestamp_epoch) - res.start,
|
|
234
259
|
});
|
|
235
260
|
}
|
|
236
261
|
|
|
@@ -238,28 +263,32 @@ class Routecache {
|
|
|
238
263
|
}
|
|
239
264
|
|
|
240
265
|
async markAsSent(route_message) {
|
|
241
|
-
const end =
|
|
266
|
+
const end =
|
|
267
|
+
route_message.route[route_message.route.length - 1].timestamp +
|
|
268
|
+
route_message.start;
|
|
242
269
|
|
|
243
270
|
const uuid = route_message.uuid.string;
|
|
244
|
-
|
|
245
|
-
const
|
|
246
|
-
update
|
|
271
|
+
|
|
272
|
+
const result = this._run(
|
|
273
|
+
`update
|
|
247
274
|
target_position
|
|
248
275
|
set
|
|
249
276
|
sent = 1
|
|
250
277
|
where
|
|
251
278
|
target_id = (select target_id from target where uuid = ?)
|
|
252
|
-
and timestamp <= datetime(? / 1000, 'unixepoch')
|
|
253
|
-
|
|
254
|
-
|
|
279
|
+
and timestamp <= datetime(? / 1000, 'unixepoch');`,
|
|
280
|
+
[uuid, end]
|
|
281
|
+
);
|
|
282
|
+
|
|
255
283
|
console.error(
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
+
|
|
260
|
-
|
|
261
|
-
+
|
|
262
|
-
|
|
284
|
+
'Updated ' +
|
|
285
|
+
result.changes +
|
|
286
|
+
' rows for ' +
|
|
287
|
+
uuid +
|
|
288
|
+
' @ ' +
|
|
289
|
+
end +
|
|
290
|
+
'.'
|
|
291
|
+
);
|
|
263
292
|
}
|
|
264
293
|
}
|
|
265
294
|
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* kahu-signalk uses Node's built-in node:sqlite (no native sqlite3 bindings).
|
|
4
|
+
* Requires Node 22.5+ where DatabaseSync is available.
|
|
5
|
+
*/
|
|
6
|
+
const [major, minor] = process.versions.node.split('.').map(Number);
|
|
7
|
+
const ok = major > 22 || (major === 22 && minor >= 5);
|
|
8
|
+
if (!ok) {
|
|
9
|
+
console.error(
|
|
10
|
+
'\nkahu-signalk requires Node.js 22.5.0 or later.\n' +
|
|
11
|
+
'This plugin uses the built-in SQLite module (node:sqlite) so it does not depend on native sqlite3 binaries.\n' +
|
|
12
|
+
'Signal K recommends Node 22: https://github.com/SignalK/signalk-server/wiki/Installing-and-Updating-Node.js\n' +
|
|
13
|
+
`Current Node version: ${process.version}\n`
|
|
14
|
+
);
|
|
15
|
+
process.exit(1);
|
|
16
|
+
}
|