signalk-chiplog 2.7.0 → 2.9.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.
Files changed (50) hide show
  1. package/CHANGELOG.md +74 -1
  2. package/README.md +152 -45
  3. package/docs/screenshots/05-stats.png +0 -0
  4. package/docs/screenshots/06-3d-animation.png +0 -0
  5. package/index.js +241 -4
  6. package/lib/api.js +123 -2
  7. package/lib/cloud-pairing.js +172 -0
  8. package/lib/cloud-restore.js +416 -0
  9. package/lib/cloud-sync.js +455 -0
  10. package/lib/database.js +50 -1
  11. package/lib/detection.js +4 -0
  12. package/lib/events.js +84 -20
  13. package/lib/export.js +1 -1
  14. package/lib/mail.js +162 -0
  15. package/lib/passage-map.js +342 -0
  16. package/lib/passage-summary.js +169 -0
  17. package/lib/places.js +6 -0
  18. package/lib/png.js +295 -0
  19. package/lib/qr.js +266 -0
  20. package/lib/smtp.js +242 -0
  21. package/lib/summary-mailer.js +286 -0
  22. package/package.json +2 -1
  23. package/public/app.css +94 -0
  24. package/public/entry/entry.css +94 -1
  25. package/public/entry/icons/apple-touch-icon.png +0 -0
  26. package/public/entry/icons/icon-192.png +0 -0
  27. package/public/entry/icons/icon-512.png +0 -0
  28. package/public/entry/index.html +2 -2
  29. package/public/entry/js/components/Dialogs.mjs +82 -16
  30. package/public/entry/js/components/Icons.mjs +33 -0
  31. package/public/entry/js/components/NotePanel.mjs +21 -3
  32. package/public/entry/js/components/RecentList.mjs +2 -3
  33. package/public/entry/js/journal.mjs +24 -7
  34. package/public/entry/js/main.mjs +27 -18
  35. package/public/entry/manifest.webmanifest +1 -1
  36. package/public/entry/sw.js +2 -0
  37. package/public/icon-small.svg +37 -0
  38. package/public/icon.svg +82 -6
  39. package/public/index.html +1 -1
  40. package/public/js/components/CloudBackup.mjs +298 -0
  41. package/public/js/components/ExportView.mjs +3 -0
  42. package/public/js/components/LogView.mjs +63 -6
  43. package/public/js/components/PassageView.mjs +4 -3
  44. package/public/js/components/Timeline.mjs +40 -14
  45. package/public/js/context.mjs +22 -3
  46. package/public/js/format.mjs +34 -0
  47. package/public/js/i18n.mjs +107 -2
  48. package/public/js/log-lines.mjs +5 -0
  49. package/public/js/main.mjs +1 -1
  50. package/public/js/view-memory.mjs +39 -0
@@ -0,0 +1,416 @@
1
+ const { withTransaction } = require('./database');
2
+ const { getLogbookId, isRestoring } = require('./cloud-sync');
3
+ const { TRACK_READINGS, OBSERVATION_READINGS } = require('./passage-import');
4
+
5
+ const REQUEST_TIMEOUT_MS = 2 * 60 * 1000;
6
+
7
+ class CloudRestoreError extends Error {
8
+ constructor(message, { status = null, code = null } = {}) {
9
+ super(message);
10
+ this.status = status;
11
+ this.code = code;
12
+ }
13
+ }
14
+
15
+ const json = (value) => (value === null || value === undefined ? null : JSON.stringify(value));
16
+
17
+ // Rows keep the id they had: the passage then hashes as it did when it was sent, and the
18
+ // backup that follows the restore has nothing to send again. An id taken since -- by something
19
+ // logged while the restore was under way -- gives way to a new one.
20
+ function insertKeepingId(db, table, id, values) {
21
+ const taken =
22
+ id !== null &&
23
+ id !== undefined &&
24
+ db.prepare(`SELECT 1 FROM ${table} WHERE id = ?`).get(id) !== undefined;
25
+ const row = taken || id === null || id === undefined ? values : { id, ...values };
26
+ const columns = Object.keys(row);
27
+ const { lastInsertRowid } = db
28
+ .prepare(
29
+ `INSERT INTO ${table} (${columns.join(', ')}) VALUES (${columns.map(() => '?').join(', ')})`
30
+ )
31
+ .run(...columns.map((column) => row[column]));
32
+ return Number(lastInsertRowid);
33
+ }
34
+
35
+ // The next row a table numbers by itself comes after `id`: what is logged live during a
36
+ // restore must not take the number of a passage still to come.
37
+ function reserveIds(db, table, id) {
38
+ const row = db.prepare('SELECT seq FROM sqlite_sequence WHERE name = ?').get(table);
39
+ if (!row) {
40
+ db.prepare('INSERT INTO sqlite_sequence (name, seq) VALUES (?, ?)').run(table, id);
41
+ } else if (row.seq < id) {
42
+ db.prepare('UPDATE sqlite_sequence SET seq = ? WHERE name = ?').run(id, table);
43
+ }
44
+ }
45
+
46
+ // Places keep their ids too: passages point at them. Only what the service holds comes back --
47
+ // name, position, country -- so the list differs from the one sent, and is sent again once.
48
+ function restorePlaces(db, places, now) {
49
+ const insert = db.prepare(
50
+ `INSERT INTO places (id, name, lat, lon, source, country_code, country_checked, created_at, updated_at)
51
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
52
+ ON CONFLICT (id) DO NOTHING`
53
+ );
54
+ for (const place of places) {
55
+ insert.run(
56
+ place.id,
57
+ place.name,
58
+ place.position.lat,
59
+ place.position.lon,
60
+ place.source === 'geocoding' ? 'geocoding' : 'manual',
61
+ place.countryCode ?? null,
62
+ place.countryCode ? 1 : 0,
63
+ now,
64
+ now
65
+ );
66
+ }
67
+ }
68
+
69
+ // Writes one passage as the service holds it, with everything that hangs from it, under the
70
+ // id it had. All or nothing. A passage still under way when it was last sent is closed where
71
+ // its track ends: whatever the boat is doing now, it is not on that passage any more.
72
+ function restorePassage(db, { passage, points }, now) {
73
+ const { entry } = passage;
74
+ return withTransaction(db, () => {
75
+ const placeId = (id) =>
76
+ id !== null &&
77
+ id !== undefined &&
78
+ db.prepare('SELECT 1 FROM places WHERE id = ?').get(id) !== undefined
79
+ ? id
80
+ : null;
81
+ const closed = entry.state === 'closed' && entry.endTime;
82
+ const endTime = closed ? entry.endTime : (points.at(-1)?.time ?? entry.startTime);
83
+
84
+ db.prepare(
85
+ `INSERT INTO log_entries (
86
+ id, state, start_time, end_time, stopped_since, start_lat, start_lon, end_lat, end_lon,
87
+ start_place_id, end_place_id, start_place_name, end_place_name,
88
+ start_place_pending, end_place_pending, distance, engine_duration, sail_duration,
89
+ start_tanks, start_batteries, created_at, updated_at
90
+ ) VALUES (?, 'closed', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
91
+ ).run(
92
+ entry.id,
93
+ entry.startTime,
94
+ endTime < entry.startTime ? entry.startTime : endTime,
95
+ closed ? (entry.stoppedSince ?? null) : null,
96
+ entry.startPosition?.lat ?? null,
97
+ entry.startPosition?.lon ?? null,
98
+ entry.endPosition?.lat ?? null,
99
+ entry.endPosition?.lon ?? null,
100
+ placeId(entry.startPlaceId),
101
+ placeId(entry.endPlaceId),
102
+ entry.startPlaceName ?? null,
103
+ entry.endPlaceName ?? null,
104
+ entry.startPlacePending ? 1 : 0,
105
+ entry.endPlacePending ? 1 : 0,
106
+ entry.distance ?? 0,
107
+ entry.engineDuration ?? 0,
108
+ entry.sailDuration ?? 0,
109
+ json(entry.startTanks),
110
+ json(entry.startBatteries),
111
+ entry.createdAt ?? now,
112
+ entry.updatedAt ?? now
113
+ );
114
+
115
+ const insertPoint = db.prepare(
116
+ `INSERT INTO track_points (entry_id, time, lat, lon, ${TRACK_READINGS.join(', ')})
117
+ VALUES (?, ?, ?, ?, ${TRACK_READINGS.map(() => '?').join(', ')})`
118
+ );
119
+ for (const point of points) {
120
+ insertPoint.run(
121
+ entry.id,
122
+ point.time,
123
+ point.lat,
124
+ point.lon,
125
+ ...TRACK_READINGS.map((reading) => point[reading] ?? null)
126
+ );
127
+ }
128
+
129
+ const eventIds = new Map();
130
+ for (const event of passage.events ?? []) {
131
+ const id = insertKeepingId(db, 'events', event.id, {
132
+ entry_id: entry.id,
133
+ time: event.time,
134
+ type: event.type,
135
+ subtype: event.subtype ?? null,
136
+ lat: event.position?.lat ?? null,
137
+ lon: event.position?.lon ?? null,
138
+ comment: event.comment ?? null,
139
+ payload: json(event.payload),
140
+ source: event.source === 'auto' ? 'auto' : 'manual',
141
+ created_at: event.createdAt ?? now,
142
+ client_ref: event.clientRef ?? null
143
+ });
144
+ eventIds.set(event.id, id);
145
+ }
146
+ if (eventIds.has(entry.openedByEventId)) {
147
+ db.prepare('UPDATE log_entries SET opened_by_event_id = ? WHERE id = ?').run(
148
+ eventIds.get(entry.openedByEventId),
149
+ entry.id
150
+ );
151
+ }
152
+
153
+ for (const observation of passage.observations ?? []) {
154
+ insertKeepingId(db, 'observations', observation.id, {
155
+ entry_id: entry.id,
156
+ time: observation.time,
157
+ reason: observation.reason,
158
+ lat: observation.position?.lat ?? null,
159
+ lon: observation.position?.lon ?? null,
160
+ ...Object.fromEntries(
161
+ OBSERVATION_READINGS.map(([column, field]) => [column, observation[field] ?? null])
162
+ ),
163
+ engine_runtimes: json(observation.engineRuntimes)
164
+ });
165
+ }
166
+
167
+ for (const segment of passage.propulsion ?? []) {
168
+ insertKeepingId(db, 'propulsion_segments', segment.id, {
169
+ entry_id: entry.id,
170
+ type: segment.type,
171
+ start_time: segment.startTime,
172
+ end_time: segment.endTime ?? endTime,
173
+ source: segment.source === 'manual' ? 'manual' : 'auto',
174
+ average_rpm: segment.averageRpm ?? null
175
+ });
176
+ }
177
+
178
+ for (const member of passage.crew ?? []) {
179
+ // The crew list itself is not backed up, only who sailed each passage: the people are
180
+ // made again from the passages they were on, under the number they had.
181
+ let memberId = member.crewMemberId ?? null;
182
+ if (memberId !== null) {
183
+ const known = db.prepare('SELECT name FROM crew_members WHERE id = ?').get(memberId);
184
+ if (!known) {
185
+ db.prepare(
186
+ `INSERT INTO crew_members (id, name, role, created_at, updated_at)
187
+ VALUES (?, ?, ?, ?, ?)`
188
+ ).run(memberId, member.name, member.role ?? null, now, now);
189
+ } else if (known.name !== member.name) {
190
+ memberId = null;
191
+ }
192
+ }
193
+ insertKeepingId(db, 'log_entry_crew', member.id, {
194
+ entry_id: entry.id,
195
+ crew_member_id: memberId,
196
+ name: member.name,
197
+ role: member.role ?? null,
198
+ created_at: now
199
+ });
200
+ }
201
+
202
+ for (const [table, forecast] of [
203
+ ['tide_forecasts', passage.tide],
204
+ ['weather_forecasts', passage.weather]
205
+ ]) {
206
+ if (forecast?.position && Array.isArray(forecast.points)) {
207
+ db.prepare(
208
+ `INSERT INTO ${table} (entry_id, lat, lon, fetched_at, points) VALUES (?, ?, ?, ?, ?)`
209
+ ).run(
210
+ entry.id,
211
+ forecast.position.lat,
212
+ forecast.position.lon,
213
+ forecast.fetchedAt ?? now,
214
+ JSON.stringify(forecast.points)
215
+ );
216
+ }
217
+ }
218
+
219
+ // A passage restored is not one just sailed: nobody is to be emailed its summary.
220
+ db.prepare(
221
+ `INSERT INTO passage_summary_mails (entry_id, error, created_at)
222
+ VALUES (?, 'restored from the online backup', ?)`
223
+ ).run(entry.id, now);
224
+ });
225
+ }
226
+
227
+ // Brings an earlier logbook of this boat back from the online service into this one, empty
228
+ // (SPEC §4.17) -- after a lost SD card, a new computer. This logbook takes the identity of the
229
+ // one restored and its passages their numbers, so the backup carries on where it stopped
230
+ // instead of sending everything again as a second logbook. The boat asks; the service never
231
+ // calls it. Interrupted, it picks up where it stopped: the identity is taken first, and the
232
+ // passages already written are skipped.
233
+ function createCloudRestore({
234
+ db,
235
+ settings,
236
+ userAgent,
237
+ log,
238
+ onDone = () => {},
239
+ fetch = globalThis.fetch,
240
+ clock = Date.now
241
+ }) {
242
+ let running = null;
243
+ let stopped = false;
244
+ let progress = null;
245
+ let lastError = null;
246
+ let lastResult = null;
247
+
248
+ const iso = (ms) => new Date(ms).toISOString();
249
+ const configured = () => Boolean(settings.cloudSyncUrl && settings.cloudSyncToken);
250
+ const isEmpty = () => db.prepare('SELECT count(*) AS n FROM log_entries').get().n === 0;
251
+
252
+ async function call(path) {
253
+ const response = await fetch(`${settings.cloudSyncUrl.replace(/\/+$/, '')}/v1${path}`, {
254
+ method: 'GET',
255
+ headers: {
256
+ authorization: `Bearer ${settings.cloudSyncToken}`,
257
+ 'user-agent': userAgent,
258
+ 'x-chiplog-version': userAgent.split('/')[1] ?? ''
259
+ },
260
+ signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS)
261
+ });
262
+ if (!response.ok) {
263
+ let detail = {};
264
+ try {
265
+ detail = await response.json();
266
+ } catch {
267
+ // Not every failure comes from the service itself: a proxy, a captive portal.
268
+ }
269
+ throw new CloudRestoreError(
270
+ `GET ${path} answered ${response.status}${detail.message ? `: ${detail.message}` : ''}`,
271
+ { status: response.status, code: detail.error ?? null }
272
+ );
273
+ }
274
+ return response.json();
275
+ }
276
+
277
+ async function restore(logbookId) {
278
+ const base = `/logbooks/${logbookId}`;
279
+ const index = await call(`${base}/restore`);
280
+ const now = iso(clock());
281
+ withTransaction(db, () => {
282
+ db.prepare('UPDATE logbook_identity SET id = ?, restoring = 1').run(logbookId);
283
+ reserveIds(db, 'log_entries', Math.max(0, ...index.passages.map((item) => item.originId)));
284
+ restorePlaces(db, index.places ?? [], now);
285
+ });
286
+
287
+ const onBoard = new Set(
288
+ db
289
+ .prepare('SELECT id FROM log_entries')
290
+ .all()
291
+ .map((row) => row.id)
292
+ );
293
+ const due = index.passages.filter((item) => !onBoard.has(item.originId));
294
+ progress = { total: index.passages.length, done: index.passages.length - due.length };
295
+ let restored = 0;
296
+ let failed = 0;
297
+ for (const item of due) {
298
+ if (stopped) {
299
+ return null;
300
+ }
301
+ const { passage } = await call(`${base}/passages/${item.originId}`);
302
+ const points = [];
303
+ for (let chunk = 0; chunk < item.trackChunks; chunk += 1) {
304
+ if (stopped) {
305
+ return null;
306
+ }
307
+ try {
308
+ points.push(...(await call(`${base}/passages/${item.originId}/track/${chunk}`)).points);
309
+ } catch (error) {
310
+ // A chunk the service never received: the passage comes back with the track it has.
311
+ if (error.code !== 'chunk_not_found') {
312
+ throw error;
313
+ }
314
+ }
315
+ }
316
+ try {
317
+ restorePassage(db, { passage, points }, iso(clock()));
318
+ restored += 1;
319
+ } catch (error) {
320
+ // One passage this logbook cannot take must not keep all the others out.
321
+ failed += 1;
322
+ log('error', `Passage ${item.originId} could not be restored: ${error.message}`);
323
+ }
324
+ progress.done += 1;
325
+ }
326
+ db.prepare('UPDATE logbook_identity SET restoring = 0').run();
327
+ return { passages: restored, failed, total: index.passages.length };
328
+ }
329
+
330
+ function start(logbookId) {
331
+ if (running) {
332
+ throw new CloudRestoreError('A restore is already under way', { code: 'restore_running' });
333
+ }
334
+ if (!configured()) {
335
+ throw new CloudRestoreError('Pair this boat with the service first', {
336
+ code: 'cloud_sync_not_configured'
337
+ });
338
+ }
339
+ const resuming = isRestoring(db) && getLogbookId(db) === logbookId;
340
+ if (!resuming && !isEmpty()) {
341
+ throw new CloudRestoreError(
342
+ 'Only an empty logbook can take a restored one: this one already holds passages',
343
+ { code: 'logbook_not_empty' }
344
+ );
345
+ }
346
+ stopped = false;
347
+ lastError = null;
348
+ lastResult = null;
349
+ progress = { total: 0, done: 0 };
350
+ running = restore(logbookId)
351
+ .then((result) => {
352
+ if (result) {
353
+ lastResult = { at: iso(clock()), logbookId, ...result };
354
+ log(
355
+ 'info',
356
+ `Logbook restored from the online backup: ${result.passages} passage(s)${result.failed ? `, ${result.failed} refused` : ''}`
357
+ );
358
+ onDone();
359
+ }
360
+ })
361
+ .catch((error) => {
362
+ lastError = { at: iso(clock()), message: error.message, code: error.code ?? null };
363
+ log('error', `Restoring from the online backup failed: ${error.message}`);
364
+ })
365
+ .finally(() => {
366
+ running = null;
367
+ progress = null;
368
+ });
369
+ return running;
370
+ }
371
+
372
+ return {
373
+ // The logbooks of this boat the service can give back; this one, still empty, is not one.
374
+ async candidates() {
375
+ if (!configured()) {
376
+ return [];
377
+ }
378
+ const own = getLogbookId(db);
379
+ const { logbooks } = await call('/restore/logbooks');
380
+ return logbooks.filter((logbook) => logbook.logbookId !== own || isRestoring(db));
381
+ },
382
+
383
+ // Starts in the background; `finished` settles when the restore ends, well or not.
384
+ start(logbookId) {
385
+ const finished = start(logbookId);
386
+ return { finished };
387
+ },
388
+
389
+ // Carries on with a restore the last run left unfinished, if any.
390
+ resume() {
391
+ if (!running && configured() && isRestoring(db)) {
392
+ return this.start(getLogbookId(db));
393
+ }
394
+ return null;
395
+ },
396
+
397
+ status() {
398
+ const restoring = isRestoring(db);
399
+ return {
400
+ // Unfinished: under way, or waiting to be carried on with.
401
+ restoring,
402
+ running: running !== null,
403
+ possible: configured() && !restoring && running === null && isEmpty(),
404
+ inProgress: progress,
405
+ lastResult,
406
+ lastError
407
+ };
408
+ },
409
+
410
+ stop() {
411
+ stopped = true;
412
+ }
413
+ };
414
+ }
415
+
416
+ module.exports = { createCloudRestore, restorePassage, CloudRestoreError };