signalk-navico-routes 1.0.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 (80) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +122 -0
  3. package/dist/discovery.d.ts +64 -0
  4. package/dist/discovery.js +188 -0
  5. package/dist/discovery.js.map +1 -0
  6. package/dist/gpx.d.ts +41 -0
  7. package/dist/gpx.js +121 -0
  8. package/dist/gpx.js.map +1 -0
  9. package/dist/id-map.d.ts +42 -0
  10. package/dist/id-map.js +133 -0
  11. package/dist/id-map.js.map +1 -0
  12. package/dist/index.d.ts +67 -0
  13. package/dist/index.js +198 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/mapper.d.ts +60 -0
  16. package/dist/mapper.js +388 -0
  17. package/dist/mapper.js.map +1 -0
  18. package/dist/mfd-client.d.ts +63 -0
  19. package/dist/mfd-client.js +143 -0
  20. package/dist/mfd-client.js.map +1 -0
  21. package/dist/resource-store.d.ts +16 -0
  22. package/dist/resource-store.js +40 -0
  23. package/dist/resource-store.js.map +1 -0
  24. package/dist/sync-engine.d.ts +147 -0
  25. package/dist/sync-engine.js +347 -0
  26. package/dist/sync-engine.js.map +1 -0
  27. package/dist/types.d.ts +95 -0
  28. package/dist/types.js +10 -0
  29. package/dist/types.js.map +1 -0
  30. package/dist/usr/binary.d.ts +39 -0
  31. package/dist/usr/binary.js +139 -0
  32. package/dist/usr/binary.js.map +1 -0
  33. package/dist/usr/codec.d.ts +23 -0
  34. package/dist/usr/codec.js +282 -0
  35. package/dist/usr/codec.js.map +1 -0
  36. package/dist/usr/mercator.d.ts +12 -0
  37. package/dist/usr/mercator.js +29 -0
  38. package/dist/usr/mercator.js.map +1 -0
  39. package/dist/usr/model.d.ts +105 -0
  40. package/dist/usr/model.js +27 -0
  41. package/dist/usr/model.js.map +1 -0
  42. package/dist/usr-archive.d.ts +15 -0
  43. package/dist/usr-archive.js +52 -0
  44. package/dist/usr-archive.js.map +1 -0
  45. package/dist/usr-cache.d.ts +14 -0
  46. package/dist/usr-cache.js +44 -0
  47. package/dist/usr-cache.js.map +1 -0
  48. package/dist/webapp-api.d.ts +52 -0
  49. package/dist/webapp-api.js +194 -0
  50. package/dist/webapp-api.js.map +1 -0
  51. package/docs/DEVELOPMENT.md +107 -0
  52. package/docs/plan.md +15 -0
  53. package/docs/screenshots/config.png +0 -0
  54. package/docs/screenshots/confirmation.png +0 -0
  55. package/docs/screenshots/export.png +0 -0
  56. package/docs/screenshots/webapp.png +0 -0
  57. package/docs/usr-v6-format.md +153 -0
  58. package/package.json +96 -0
  59. package/public/app.css +448 -0
  60. package/public/app.js +2 -0
  61. package/public/app.js.map +7 -0
  62. package/public/apple-touch-icon.png +0 -0
  63. package/public/configpanel.385.js +3 -0
  64. package/public/configpanel.385.js.LICENSE.txt +9 -0
  65. package/public/configpanel.385.js.map +1 -0
  66. package/public/configpanel.540.js +3 -0
  67. package/public/configpanel.540.js.LICENSE.txt +9 -0
  68. package/public/configpanel.540.js.map +1 -0
  69. package/public/configpanel.js +2 -0
  70. package/public/configpanel.js.map +1 -0
  71. package/public/favicon-16x16.png +0 -0
  72. package/public/favicon-32x32.png +0 -0
  73. package/public/favicon.ico +0 -0
  74. package/public/icon-192.png +0 -0
  75. package/public/icon-512.png +0 -0
  76. package/public/index.html +23 -0
  77. package/public/logo.png +0 -0
  78. package/public/manifest.webmanifest +25 -0
  79. package/public/remoteEntry.js +2 -0
  80. package/public/remoteEntry.js.map +1 -0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # Navico Route Sync (signalk-navico-routes)
2
+
3
+ Sync the **routes** and **waypoints** from your Navico chartplotter — B&G
4
+ Zeus, Simrad NSS/NSX, Lowrance HDS, etc to [SignalK](https://signalk.org/),
5
+ and send routes from SignalK back to the chartplotter when you choose.
6
+
7
+ The plugin talks to the chartplotter over your boat's regular Ethernet/Wi-Fi
8
+ network using the MFD's built-in file service. No NMEA 2000 gateway or extra
9
+ hardware is needed — just the MFD and the SignalK server on the same network.
10
+
11
+ ## Features
12
+
13
+ - **Automatic MFD → SignalK mirror.** All routes and waypoints on the
14
+ chartplotter appear in SignalK's standard resources API
15
+ (`/signalk/v2/api/resources/routes` and `/waypoints`), refreshed on a
16
+ configurable schedule, so any SignalK app (Freeboard, etc.) can display them.
17
+ - **Multi-MFD friendly.** Point the plugin at the master chartplotter; Navico's
18
+ own sync spreads uploads to the rest of the network.
19
+ - **A webapp for sending routes the other way.** Pick routes from SignalK
20
+ and push them to the chartplotter, or download backups and GPX exports —
21
+ all from the SignalK admin UI. With [signalk-navico-embedder](http://npmjs.com/signalk-navico-embedder) you can manage the sync from the MFD itself.
22
+
23
+ ## Installation
24
+
25
+ Install **Navico Route Sync** from the SignalK Appstore (or `npm install
26
+ signalk-navico-routes` in your server directory), then enable it under
27
+ **Server → Plugin Config**. Chartplotters on the network are discovered
28
+ automatically; you can also pin one by IP address.
29
+
30
+ ## Configuration
31
+
32
+ | Setting | Default | Description |
33
+ | ----------------------- | ------- | ------------------------------------------------------- |
34
+ | `mfdAddress` | — | IP address or hostname of the chartplotter. Leave empty to auto-discover (see below) |
35
+ | `syncFromMfd` | `true` | Enable the MFD → SignalK mirror |
36
+ | `syncRoutes` | `true` | Mirror MFD routes into SignalK |
37
+ | `syncVisibleRoutesOnly` | `true` | Skip routes that are hidden on the MFD |
38
+ | `syncWaypoints` | `true` | Mirror free-standing MFD waypoints |
39
+ | `pollIntervalSeconds` | `600` | How often to refresh from the MFD, in seconds (min 30; 0 turns polling off) |
40
+
41
+ ### Auto-discovery
42
+
43
+ Navico chartplotters announce themselves on the network (GoFree multicast)
44
+ about once a second, and the plugin listens the whole time it runs. The
45
+ config panel lists every discovered chartplotter with its role
46
+ (master/slave) — click one to fill in the address field, or leave the field
47
+ empty and the plugin syncs with whatever it finds: the master first, falling
48
+ back to the others if it doesn't answer. Chartplotters that turn on or off
49
+ while the plugin is running appear and disappear from the list on their own.
50
+
51
+ With an empty address there is nothing to reconfigure when your boat
52
+ network hands out new DHCP leases. If you do pin an address, give the
53
+ chartplotter a static IP (or a DHCP reservation in your router) so it
54
+ stays valid.
55
+
56
+ **No chartplotters showing up?** Run `node scripts/gofree-watch.mjs` on the
57
+ SignalK machine — it listens the same way the plugin does and prints every
58
+ announcement it hears, so you can tell "nothing on the wire" apart from a
59
+ receive problem. One known receive problem: the Raspberry Pi 4's onboard
60
+ Ethernet can silently drop multicast even though everything is configured
61
+ correctly. If the watch script hears nothing while
62
+ `sudo tcpdump -i eth0 host 239.2.1.1` shows traffic, that's it — running
63
+ tcpdump itself (or `sudo ifconfig eth0 allmulti`) resets the network
64
+ interface's filter and discovery starts working.
65
+
66
+ ## How syncing works
67
+
68
+ **The chartplotter is the source of truth.** The plugin periodically
69
+ downloads the MFD's user database and mirrors it into SignalK — including
70
+ deletions, so removing a route on the MFD removes it from SignalK too. The
71
+ routes and waypoints it mirrors are read-only in SignalK; you can't edit
72
+ them through the resources API.
73
+
74
+ Going the other direction is deliberately manual: Navico chartplotters only
75
+ ever **add** routes from an upload — they never overwrite or delete existing
76
+ ones — so a fully automatic two-way sync isn't possible. Instead, you choose
77
+ which routes to send using the webapp.
78
+
79
+ ## The webapp
80
+
81
+ Open **Navico Routes** from the _Webapps_ screen in the SignalK admin UI. It
82
+ lists every SignalK route that didn't come from the chartplotter (e.g.
83
+ routes you planned in another app) in a sortable, searchable table, and lets
84
+ you:
85
+
86
+ - **Sync MFD → SignalK** — refresh from the chartplotter right now instead
87
+ of waiting for the next scheduled poll.
88
+ - **Download MFD routes** — save the chartplotter's complete user database
89
+ as a `.usr` backup, or its routes and waypoints as a `.gpx` file.
90
+ - **Download selected** — export the routes you've selected as a `.usr`
91
+ file (to import on another Navico chartplotter via SD card) or as GPX.
92
+ - **Send selected to MFD** — upload the selected routes straight to the
93
+ chartplotter.
94
+
95
+ A few things worth knowing:
96
+
97
+ - **Uploads are additive.** Sending an edited route again creates a second
98
+ copy on the chartplotter rather than replacing the first — delete the
99
+ outdated copy on the MFD itself.
100
+ - **Back up first.** Uploads can't be undone, so grab a `.usr` backup
101
+ before sending anything you're unsure about.
102
+ - **Route names** are editable in the table and capped at 16 characters
103
+ (the MFD's on-screen keyboard limit). SignalK keeps the full name, and
104
+ GPX exports use it unless you renamed the route in the table.
105
+ - The page has a day/night toggle and also follows your OS theme (or a
106
+ `?mode=day` / `?mode=night` URL parameter).
107
+
108
+ ## Known limitations
109
+
110
+ 1. Route and waypoint names longer than 32 characters are shortened when
111
+ sent to the chartplotter; SignalK keeps the full name.
112
+
113
+ ## For developers
114
+
115
+ Build instructions, the plugin's HTTP API, protocol research notes, and the
116
+ reverse-engineered USR v6 file format are documented in
117
+ [docs/DEVELOPMENT.md](docs/DEVELOPMENT.md) and
118
+ [docs/usr-v6-format.md](docs/usr-v6-format.md).
119
+
120
+ ## License
121
+
122
+ See [LICENSE](LICENSE).
@@ -0,0 +1,64 @@
1
+ /**
2
+ * GoFree multicast auto-discovery.
3
+ *
4
+ * Navico MFDs announce themselves about once a second as a JSON datagram on
5
+ * multicast 239.2.1.1:2052 (`IP`, `Model`, `Name`, `UDBMaster`,
6
+ * `NetworkMaster`, `Services[]`, …). This listener runs for the whole plugin
7
+ * lifetime — MFDs come and go while the plugin is running — and keeps a map
8
+ * of the devices currently announcing, expiring entries that have gone
9
+ * quiet.
10
+ *
11
+ * Only devices advertising an `http` service are kept: that is the GoFree
12
+ * file server (download.cgi / upload.cgi) the plugin talks to. Other Navico
13
+ * gear announces on the same group without it (e.g. an H5000 CPU) and is of
14
+ * no use here.
15
+ */
16
+ import type { Logger } from './types';
17
+ export declare const GOFREE_GROUP = "239.2.1.1";
18
+ export declare const GOFREE_PORT = 2052;
19
+ /** An MFD currently announcing itself on the GoFree multicast group. */
20
+ export interface DiscoveredMfd {
21
+ /** Source address of the announcement — known to be reachable from here. */
22
+ address: string;
23
+ model: string;
24
+ name: string;
25
+ /** Owns the Unified DataBase (routes/waypoints) — the preferred sync peer. */
26
+ udbMaster: boolean;
27
+ networkMaster: boolean;
28
+ lastSeen: Date;
29
+ }
30
+ export interface MfdDiscoveryOptions {
31
+ /** Listen port; 0 picks an ephemeral port (tests). Default 2052. */
32
+ port?: number;
33
+ staleAfterMs?: number;
34
+ now?: () => Date;
35
+ }
36
+ export declare class MfdDiscovery {
37
+ private readonly log;
38
+ private readonly listenPort;
39
+ private readonly staleAfterMs;
40
+ private readonly now;
41
+ private readonly mfds;
42
+ private socket?;
43
+ private retryTimer?;
44
+ private stopped;
45
+ constructor(log: Logger, options?: MfdDiscoveryOptions);
46
+ start(): void;
47
+ stop(): void;
48
+ /** Bound UDP port, once listening (tests bind port 0). */
49
+ get port(): number | undefined;
50
+ /** MFDs announcing right now, UDB master first. */
51
+ list(): DiscoveredMfd[];
52
+ /** Addresses to try for MFD I/O, in preference order (master first). */
53
+ candidates(): string[];
54
+ /**
55
+ * Digest one announcement datagram. Public so tests can feed messages
56
+ * without a socket. Malformed or irrelevant datagrams are ignored.
57
+ */
58
+ handleAnnouncement(msg: Buffer, fromAddress: string): void;
59
+ private openSocket;
60
+ private joinGroup;
61
+ private closeSocket;
62
+ private scheduleRetry;
63
+ private prune;
64
+ }
@@ -0,0 +1,188 @@
1
+ "use strict";
2
+ /**
3
+ * GoFree multicast auto-discovery.
4
+ *
5
+ * Navico MFDs announce themselves about once a second as a JSON datagram on
6
+ * multicast 239.2.1.1:2052 (`IP`, `Model`, `Name`, `UDBMaster`,
7
+ * `NetworkMaster`, `Services[]`, …). This listener runs for the whole plugin
8
+ * lifetime — MFDs come and go while the plugin is running — and keeps a map
9
+ * of the devices currently announcing, expiring entries that have gone
10
+ * quiet.
11
+ *
12
+ * Only devices advertising an `http` service are kept: that is the GoFree
13
+ * file server (download.cgi / upload.cgi) the plugin talks to. Other Navico
14
+ * gear announces on the same group without it (e.g. an H5000 CPU) and is of
15
+ * no use here.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.MfdDiscovery = exports.GOFREE_PORT = exports.GOFREE_GROUP = void 0;
19
+ const node_dgram_1 = require("node:dgram");
20
+ const node_os_1 = require("node:os");
21
+ exports.GOFREE_GROUP = '239.2.1.1';
22
+ exports.GOFREE_PORT = 2052;
23
+ /** How long an MFD may stay silent before it is considered gone. */
24
+ const DEFAULT_STALE_AFTER_MS = 60_000;
25
+ /** Delay before re-opening the socket after an error. */
26
+ const RETRY_AFTER_MS = 30_000;
27
+ class MfdDiscovery {
28
+ log;
29
+ listenPort;
30
+ staleAfterMs;
31
+ now;
32
+ mfds = new Map();
33
+ socket;
34
+ retryTimer;
35
+ stopped = false;
36
+ constructor(log, options = {}) {
37
+ this.log = log;
38
+ this.listenPort = options.port ?? exports.GOFREE_PORT;
39
+ this.staleAfterMs = options.staleAfterMs ?? DEFAULT_STALE_AFTER_MS;
40
+ this.now = options.now ?? (() => new Date());
41
+ }
42
+ start() {
43
+ this.stopped = false;
44
+ this.openSocket();
45
+ }
46
+ stop() {
47
+ this.stopped = true;
48
+ if (this.retryTimer) {
49
+ clearTimeout(this.retryTimer);
50
+ this.retryTimer = undefined;
51
+ }
52
+ this.closeSocket();
53
+ }
54
+ /** Bound UDP port, once listening (tests bind port 0). */
55
+ get port() {
56
+ try {
57
+ return this.socket?.address().port;
58
+ }
59
+ catch {
60
+ return undefined; // not bound yet
61
+ }
62
+ }
63
+ /** MFDs announcing right now, UDB master first. */
64
+ list() {
65
+ this.prune();
66
+ return [...this.mfds.values()].sort((a, b) => Number(b.udbMaster) - Number(a.udbMaster) ||
67
+ Number(b.networkMaster) - Number(a.networkMaster) ||
68
+ a.address.localeCompare(b.address, undefined, { numeric: true }));
69
+ }
70
+ /** Addresses to try for MFD I/O, in preference order (master first). */
71
+ candidates() {
72
+ return this.list().map((m) => m.address);
73
+ }
74
+ /**
75
+ * Digest one announcement datagram. Public so tests can feed messages
76
+ * without a socket. Malformed or irrelevant datagrams are ignored.
77
+ */
78
+ handleAnnouncement(msg, fromAddress) {
79
+ let parsed;
80
+ try {
81
+ parsed = JSON.parse(msg.toString('utf8'));
82
+ }
83
+ catch {
84
+ return;
85
+ }
86
+ if (typeof parsed !== 'object' || parsed === null) {
87
+ return;
88
+ }
89
+ const ann = parsed;
90
+ // No http service → no GoFree file server → nothing to sync with.
91
+ const services = Array.isArray(ann.Services) ? ann.Services : [];
92
+ const hasHttp = services.some((s) => typeof s === 'object' && s !== null && s.Service === 'http');
93
+ if (!hasHttp) {
94
+ return;
95
+ }
96
+ const model = typeof ann.Model === 'string' ? ann.Model : '';
97
+ const mfd = {
98
+ address: fromAddress,
99
+ model,
100
+ name: typeof ann.Name === 'string' ? ann.Name : model,
101
+ udbMaster: ann.UDBMaster === true,
102
+ networkMaster: ann.NetworkMaster === true,
103
+ lastSeen: this.now(),
104
+ };
105
+ const known = this.mfds.get(fromAddress);
106
+ if (!known) {
107
+ this.log.debug(`discovered MFD ${mfd.name || fromAddress} at ${fromAddress}` +
108
+ (mfd.udbMaster ? ' (UDB master)' : ''));
109
+ }
110
+ else if (known.udbMaster !== mfd.udbMaster) {
111
+ this.log.debug(`MFD at ${fromAddress} is ${mfd.udbMaster ? 'now' : 'no longer'} the UDB master`);
112
+ }
113
+ this.mfds.set(fromAddress, mfd);
114
+ }
115
+ // ── Socket plumbing ──────────────────────────────────────────────────────
116
+ openSocket() {
117
+ // reuseAddr so we can share port 2052 with other GoFree-aware software.
118
+ const socket = (0, node_dgram_1.createSocket)({ type: 'udp4', reuseAddr: true });
119
+ this.socket = socket;
120
+ socket.on('error', (err) => {
121
+ this.log.error(`GoFree discovery socket error: ${err.message}`);
122
+ this.closeSocket();
123
+ this.scheduleRetry();
124
+ });
125
+ socket.on('message', (msg, rinfo) => this.handleAnnouncement(msg, rinfo.address));
126
+ socket.bind(this.listenPort, () => this.joinGroup(socket));
127
+ }
128
+ joinGroup(socket) {
129
+ // Join on the default interface and on every external IPv4 interface:
130
+ // the boat network is often not the default route. Duplicate or
131
+ // unsupported joins are fine to ignore.
132
+ const interfaces = new Set([undefined]);
133
+ for (const addrs of Object.values((0, node_os_1.networkInterfaces)())) {
134
+ for (const a of addrs ?? []) {
135
+ if (a.family === 'IPv4' && !a.internal) {
136
+ interfaces.add(a.address);
137
+ }
138
+ }
139
+ }
140
+ let joined = 0;
141
+ for (const iface of interfaces) {
142
+ try {
143
+ socket.addMembership(exports.GOFREE_GROUP, iface);
144
+ joined++;
145
+ }
146
+ catch (err) {
147
+ this.log.debug(`could not join ${exports.GOFREE_GROUP} on ${iface ?? 'default interface'}: ${String(err)}`);
148
+ }
149
+ }
150
+ if (joined === 0) {
151
+ this.log.error(`could not join GoFree multicast group ${exports.GOFREE_GROUP} on any interface`);
152
+ }
153
+ }
154
+ closeSocket() {
155
+ const socket = this.socket;
156
+ this.socket = undefined;
157
+ if (socket) {
158
+ try {
159
+ socket.close();
160
+ }
161
+ catch {
162
+ // already closed
163
+ }
164
+ }
165
+ }
166
+ scheduleRetry() {
167
+ if (this.stopped || this.retryTimer) {
168
+ return;
169
+ }
170
+ this.retryTimer = setTimeout(() => {
171
+ this.retryTimer = undefined;
172
+ if (!this.stopped) {
173
+ this.openSocket();
174
+ }
175
+ }, RETRY_AFTER_MS);
176
+ }
177
+ prune() {
178
+ const cutoff = this.now().getTime() - this.staleAfterMs;
179
+ for (const [address, mfd] of this.mfds) {
180
+ if (mfd.lastSeen.getTime() < cutoff) {
181
+ this.mfds.delete(address);
182
+ this.log.debug(`MFD at ${address} stopped announcing; dropped from discovery`);
183
+ }
184
+ }
185
+ }
186
+ }
187
+ exports.MfdDiscovery = MfdDiscovery;
188
+ //# sourceMappingURL=discovery.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"discovery.js","sourceRoot":"","sources":["../src/discovery.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,2CAA0C;AAC1C,qCAA4C;AAI/B,QAAA,YAAY,GAAG,WA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;QACxD,KAAK,MAAM,CAAC,OAAO,EAAE,GAAG,CAAC,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;YACvC,IAAI,GAAG,CAAC,QAAQ,CAAC,OAAO,EAAE,GAAG,MAAM,EAAE,CAAC;gBACpC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;gBAC1B,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,UAAU,OAAO,6CAA6C,CAAC,CAAC;YACjF,CAAC;QACH,CAAC;IACH,CAAC;CACF;AA7LD,oCA6LC"}
package/dist/gpx.d.ts ADDED
@@ -0,0 +1,41 @@
1
+ /**
2
+ * GPX 1.1 export: an alternative download format next to the native USR
3
+ * file. Two sources feed it — the parsed MFD database (backup download)
4
+ * and selected SignalK route resources (selection download). Output is
5
+ * plain XML built by hand; the document is small and flat enough that a
6
+ * dependency would be overkill.
7
+ */
8
+ import type { UsrDatabase } from './usr/model';
9
+ import type { RouteResource } from './types';
10
+ export declare const GPX_CONTENT_TYPE = "application/gpx+xml";
11
+ export interface GpxPoint {
12
+ /** Decimal degrees. */
13
+ lon: number;
14
+ lat: number;
15
+ name?: string;
16
+ description?: string;
17
+ /** ISO-8601 creation time. */
18
+ time?: string;
19
+ }
20
+ export interface GpxRoute {
21
+ name?: string;
22
+ description?: string;
23
+ points: GpxPoint[];
24
+ }
25
+ export interface GpxDocument {
26
+ waypoints: GpxPoint[];
27
+ routes: GpxRoute[];
28
+ /** ISO-8601 metadata time (file creation). */
29
+ time: string;
30
+ }
31
+ /**
32
+ * GPX for the MFD's user database: routes become <rte> with named <rtept>
33
+ * legs, and only free-standing waypoints become <wpt> — waypoints serving
34
+ * as route legs already appear inside their route, mirroring how the sync
35
+ * publishes resources. Legs referencing a missing waypoint record are
36
+ * skipped rather than failing the whole export.
37
+ */
38
+ export declare function gpxFromUsrDatabase(db: UsrDatabase, now: Date): string;
39
+ /** GPX for selected SignalK routes: one <rte> per route, unnamed <rtept>s. */
40
+ export declare function gpxFromRouteResources(routes: Map<string, RouteResource>, now: Date): string;
41
+ export declare function buildGpx(doc: GpxDocument): string;