signalk-multihelm 0.5.0 → 0.7.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.
package/README.md CHANGED
@@ -41,11 +41,36 @@ and Multihelm versions and why it cannot be reached. Every server also lists
41
41
  the servers paired with it, so the master shows the others; a server on an
42
42
  older Multihelm version is not listed there until it is updated.
43
43
 
44
+ Servers with SSL on are paired the same way, by their HTTPS address or their
45
+ HTTP one, which then leads to HTTPS. When no CA that Node knows has signed a
46
+ server's certificate, as when Signal K made it or
47
+ [signalk-ssl](https://github.com/dirkwa/signalk-ssl) issued it, Multihelm
48
+ trusts the certificate the server presents when it is paired: the CA's, with
49
+ signalk-ssl, so the server's renewed certificates are trusted as well. Should
50
+ the certificate change for good, or a server paired over HTTP turn SSL on,
51
+ its settings ask to pair it again.
52
+
44
53
  Needs Signal K server 2.31 or later: peers read the replication routes with a
45
54
  readonly device token.
46
55
 
47
56
  Multihelm is enabled when it is installed.
48
57
 
58
+ ## Routes and waypoints
59
+
60
+ Routes, waypoints, regions and notes are the same on every server of the
61
+ boat: one made or changed at one station appears at the others within a
62
+ minute or so, and one deleted there is deleted at the others. There is no
63
+ master for them; when two stations change the same route before they hear
64
+ of each other, the change of the server with the higher server id wins.
65
+ Multihelm reads and writes them through each server's Resources API, so
66
+ they are kept by whichever resources provider the server uses, and charts
67
+ and tracks are left to their own plugins. A deletion counts once the server
68
+ announces it, as it does for every deletion through the Resources API while
69
+ Multihelm runs; a resource missing from a listing, as when its provider
70
+ fails, is not taken for deleted. When a failure leaves it unclear, a
71
+ deleted route can come back, but none is lost. Turn **Keep routes and waypoints in step** off on a server to keep
72
+ its own.
73
+
49
74
  ## The master
50
75
 
51
76
  Set **This server is the boat's master** in the plugin settings of one
@@ -57,6 +82,27 @@ from the transfers, and a master that has not answered for two and a half
57
82
  minutes no longer counts.
58
83
  When several servers are set, the one with the smallest server id is the
59
84
  master, and the plugin status says to set one only.
85
+ A server that is set to be the master, or no longer is, says so when it
86
+ next asks its peers, which it does right after the setting changes, so the
87
+ role moves within seconds.
88
+
89
+ ## Plugins that run on the master only
90
+
91
+ Some plugins must run on one server only: an autopilot provider, for
92
+ example, so that only one server sends commands to the autopilot. Tick them
93
+ under **Plugins that run on the master only** in Multihelm's settings. The
94
+ list is the same on every server: it is kept in step like the routes, so it
95
+ can be changed on any of them, and a server that becomes the master already
96
+ knows it.
97
+
98
+ Each server starts those plugins while it is the master and stops them
99
+ while another server is, through its own plugin settings, as the admin UI
100
+ does: it shows them as disabled on the other servers, and they stay so
101
+ across a restart. Their own settings are left as they are, so set them up
102
+ on every server that may become the master. While no master is found,
103
+ nothing is switched. While another server is the master, a plugin on the
104
+ list turned on by hand here is turned off again within a minute. Switching uses the same admin access as
105
+ taking the vessel settings over (see below), on the master too.
60
106
 
61
107
  ## Vessel data, metadata and source priorities from the master
62
108
 
@@ -99,6 +145,72 @@ credentials, and the route follows this server's own security: whoever may
99
145
  not read here cannot read through it either. While no other server is the
100
146
  master, it answers 503.
101
147
 
148
+ ## The autopilot from every station
149
+
150
+ Only one server may send commands to the autopilot, but it should be
151
+ operated from the chartplotter at any station. On the servers that are not
152
+ the master, Multihelm serves the master's autopilot in their own Autopilot
153
+ API: it registers as their autopilot provider for the master's autopilot
154
+ devices and sends every command (engage, standby, mode, course changes,
155
+ tack, gybe, dodge) on to the master's Autopilot API. It reads the master's
156
+ autopilot state every second and publishes it on its own server, so a
157
+ chartplotter there shows it as on the master. Autopilot alarms are not
158
+ passed on yet.
159
+
160
+ Run the autopilot provider plugin on the master only (see _Plugins that run
161
+ on the master only_), so the other servers do not drive the autopilot
162
+ themselves. Commands need write access on the master: when a server first
163
+ finds an autopilot there, Multihelm asks the master for it, and the request
164
+ waits under **Security -> Access Requests** on the master until it is
165
+ approved. Its settings on the other servers show which request to approve.
166
+ On the master itself Multihelm steps aside, so its own provider serves.
167
+ Turn it off per server with **Operate the master's autopilot from this
168
+ server**.
169
+
170
+ ## The course from every station
171
+
172
+ The active destination or route is the same on every server: set it at any
173
+ station, and all the others take it over. A server that is not the master
174
+ reads its own course and the master's each second. When only its own
175
+ changed, it sends it to the master, and the other servers take it over from
176
+ there; otherwise it takes the master's over. Two stations changing the course
177
+ within the same second is the one case where the master's course holds.
178
+
179
+ What is kept in step is the course as set: the destination, or the route,
180
+ its direction and the point it is on, the arrival circle and the target
181
+ arrival time. So when the master and another station both move on to the
182
+ next point at an arrival, they end on the same point, not one further. Each
183
+ server works out bearings and cross-track error from its own position, and
184
+ a leg that starts at the vessel's position starts where that server was when
185
+ it took the course over.
186
+
187
+ Sending the course to the master uses the same write access on the master as
188
+ the autopilot (see above), and taking it over the same admin access on this
189
+ server as the vessel settings. Turn it off per server with **Keep the course
190
+ the same as on the master**.
191
+
192
+ ## Alarms from every station
193
+
194
+ Every active alarm on the master sounds on every station, and silencing or
195
+ acknowledging it at any station does it at all of them. A server that is not
196
+ the master reads the master's alarms each second and raises each one through
197
+ its own Notifications API, marked as the master's, so its chartplotters show
198
+ and sound it like any other. Silencing or acknowledging one here is sent to
199
+ the master, and whatever the master has silenced or acknowledged is done
200
+ here too. When the master clears an alarm, or another server becomes the
201
+ master, the alarm goes from this server as well.
202
+
203
+ Stations on the same NMEA 2000 bus see the same alarm themselves. A server
204
+ that already has an alarm of its own on a path does not show the master's
205
+ on top of it: it keeps its own and only keeps it in step, so silencing it
206
+ here silences it on the master and the other way round. Alarms are matched
207
+ by path, as each server gives its alarms ids of its own. An alarm the master
208
+ does not have stays this server's own.
209
+
210
+ This uses the same write access on the master as the autopilot (see above).
211
+ It needs the server's Notifications API with notification management on,
212
+ and does nothing otherwise. Turn it off per server with **Show the master's alarms here**.
213
+
102
214
  ## History from the master
103
215
 
104
216
  While the master has a History API provider (signalk-questdb, for example),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "signalk-multihelm",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "Keeps several Signal K servers on one boat in step, one per nav station, without depending on a single one",
5
5
  "author": "Dirk Wahrheit",
6
6
  "license": "Apache-2.0",
@@ -1 +1 @@
1
- var e=window.__SK_REACT__;if(!e)throw Error(`signalk-multihelm: the admin UI does not expose React (Signal K 2.27 or later)`);var{Children:t,Component:n,Fragment:r,Profiler:i,PureComponent:a,StrictMode:o,Suspense:s,cloneElement:c,createContext:l,createElement:u,createRef:d,forwardRef:f,isValidElement:p,lazy:m,memo:h,startTransition:g,use:_,useActionState:v,useCallback:y,useContext:ee,useDebugValue:b,useDeferredValue:te,useEffect:x,useId:ne,useImperativeHandle:S,useInsertionEffect:C,useLayoutEffect:w,useMemo:re,useOptimistic:ie,useReducer:ae,useRef:T,useState:E,useSyncExternalStore:oe,useTransition:se,version:ce}=e;function D(e){return e===1?`1 server`:`${String(e)} servers`}function O(e,t){if(!e.running)return{tone:`danger`,text:`Multihelm is not running. Enable it above.`};if(e.masterConflict)return{tone:`danger`,text:`More than one server is set to be the boat's master. Set one only.`};if(e.role===`master`){let t=e.pairedBy.length,n=e.pairedBy.filter(e=>e.online).length;return{tone:`info`,text:t===0?`This server is the boat's master. No other server is paired with it yet.`:`This server is the boat's master. ${String(n)} of the ${D(t)} paired with it ${n===1?`is`:`are`} online.`}}if(e.master)return{tone:`success`,text:`The boat's master is ${e.master.name}; it answered ${F(e.master.lastSeen,t)}.`};let n=e.peers.filter(e=>e.state===`paired`),r=n.filter(e=>e.online===!1);return r.length>0?{tone:`danger`,text:`No master found: ${r.map(e=>e.name).join(`, `)} ${r.length===1?`is`:`are`} not reachable.`}:n.length===0?{tone:`warning`,text:`No master found: this server is not paired yet. Pair it with the server set to be the boat's master below.`}:{tone:`warning`,text:`No master found: none of the paired servers is set to be the boat's master.`}}function k(e,t){return e.url===t.url||e.name.toLowerCase()===t.name.toLowerCase()}function A(e){return e.discovered.filter(t=>!e.peers.some(e=>k(t,e)))}function j(e,t){return e.id===``?t.find(t=>t.url===e.url)?.name??e.name:e.name}function M(e){return`/skServer/security/access/requests/${encodeURIComponent(e)}/approved`}function N(e){return[e.signalk&&`Signal K ${e.signalk}`,e.multihelm&&`Multihelm ${e.multihelm}`].filter(Boolean).join(` · `)}function P(e){return`${e}/admin/#/security/access/requests`}function F(e,t){if(e===void 0)return`never`;let n=Math.max(0,Math.round((t-e)/1e3));if(n<10)return`just now`;if(n<90)return`${String(n)} s ago`;let r=Math.round(n/60);if(r<90)return`${String(r)} min ago`;let i=Math.round(r/60);return i<36?`${String(i)} h ago`:`${String(Math.round(i/24))} days ago`}var I=window.__SK_REACT_JSX_RUNTIME__;if(!I)throw Error(`signalk-multihelm: the admin UI does not expose React (Signal K 2.27 or later)`);var{Fragment:L,jsx:R,jsxs:z}=I,B=`/plugins/signalk-multihelm`,V=3e3,H=`own-access`;async function U(e,t){let n=await fetch(e,{credentials:`same-origin`,...t}),r=await n.json().catch(()=>({}));if(!n.ok)throw Error(typeof r.error==`string`?r.error:`HTTP ${String(n.status)}`);return r}function W(e){return e instanceof Error?e.message:String(e)}function G(e){return{master:e?.master===!0,followVessel:e?.followVessel!==!1,followMetadata:e?.followMetadata!==!1,followSourcePriorities:e?.followSourcePriorities!==!1,historyFromMaster:e?.historyFromMaster!==!1}}function K({configuration:e,save:t}){let[n,r]=E(null),[i,a]=E(null),[o,s]=E(null),[c,l]=E(null),[u,d]=E(``),f=y(async()=>{try{r(await U(`${B}/status`)),a(null)}catch(e){a(W(e))}},[]);x(()=>{f();let e=setInterval(()=>void f(),V);return()=>{clearInterval(e)}},[f]);let p=async(e,t)=>{s(e),l(null);try{return await t(),!0}catch(t){return l({key:e,text:W(t)}),!1}finally{s(null),await f()}},m=e=>p(e,()=>U(`${B}/peers`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({url:e})})),h=(e,t)=>{window.confirm(`Stop keeping this server in step with ${t}?`)&&p(e.url,()=>U(`${B}/peers?url=${encodeURIComponent(e.url)}`,{method:`DELETE`}))},g=e=>{window.confirm(`Take ${e.name} off this list? It comes back once it asks this server again.`)&&p(e.id,()=>U(`${B}/paired-by?id=${encodeURIComponent(e.id)}`,{method:`DELETE`}))},_=e=>{p(H,()=>U(M(e),{method:`PUT`,headers:{"Content-Type":`application/json`},body:JSON.stringify({permissions:`admin`})}))},v=async e=>{e.preventDefault(),u.trim()!==``&&await m(u.trim())&&d(``)};return z(`div`,{children:[i&&z(`div`,{className:`alert alert-danger`,children:[`Cannot read Multihelm's status: `,i,n&&`. What is shown below may be out of date.`]}),n&&R(q,{status:n,allowing:o===H,allowError:c?.key===H?c.text:null,onAllow:_}),n?.running&&z(L,{children:[n.role===`master`&&R(Z,{status:n,busy:o,onForget:g}),R(J,{status:n,busy:o,onPair:m,onUnpair:h}),n.role!==`master`&&n.pairedBy.length>0&&R(Z,{status:n,busy:o,onForget:g}),z(`div`,{className:`card mb-3`,children:[R(`div`,{className:`card-header`,children:`Pair with another server`}),z(`div`,{className:`card-body`,children:[R(`p`,{className:`small text-body-secondary`,children:n.role===`master`?`The other servers pair with this one in their own Multihelm settings. Pair this one with another only to replicate charts both ways.`:`Each server takes over what the servers it is paired with share. Pair this one with the boat's master; to replicate charts both ways, pair the other server with this one too.`}),R(Q,{status:n,busy:o,onPair:m}),z(`form`,{className:`row g-2 mt-1`,onSubmit:e=>void v(e),children:[R(`div`,{className:`col-sm`,children:R(`input`,{className:`form-control`,value:u,onChange:e=>{d(e.target.value)},placeholder:`Or enter its address, e.g. 192.168.1.10 or helm.local:3000`,"aria-label":`Address of the other server`})}),R(`div`,{className:`col-sm-auto`,children:R(`button`,{type:`submit`,className:`btn btn-primary`,disabled:o!==null||u.trim()===``,children:o===u.trim()?`Pairing…`:`Pair`})})]}),c&&c.key!==H&&R(`div`,{className:`alert alert-danger mt-3 mb-0`,children:c.text})]})]})]}),R($,{configuration:e,save:t})]})}function q({status:e,allowing:t,allowError:n,onAllow:r}){let i=O(e,Date.now()),a=e.ownAccess;return z(L,{children:[R(`div`,{className:`alert alert-${i.tone}`,children:i.text}),!e.available&&R(`div`,{className:`alert alert-danger`,children:`This Signal K server is older than 2.31, so the other servers cannot read from it. Update it.`}),a?.state===`waiting-approval`&&z(`div`,{className:`alert alert-warning`,children:[z(`p`,{children:[R(`strong`,{children:`One more step:`}),` Multihelm needs admin access to this server to write the master's vessel data, path metadata and source priorities here.`]}),R(`button`,{type:`button`,className:`btn btn-primary`,disabled:t,onClick:()=>{r(a.request)},children:t?`Allowing…`:`Allow`}),z(`div`,{className:`small mt-2`,children:[`This approves the request “Multihelm: take the vessel settings over from the master” under `,R(`a`,{href:`#/security/access/requests`,children:`Security → Access Requests`}),`.`]}),n&&R(`div`,{className:`text-danger mt-2`,children:n})]}),a?.state===`denied`&&R(`div`,{className:`alert alert-danger`,children:`The access request on this server was denied, so the vessel settings are not taken over from the master. Save the settings below to ask again.`})]})}function J({status:e,busy:t,onPair:n,onUnpair:r}){let i=Date.now();return z(`div`,{className:`card mb-3`,children:[R(`div`,{className:`card-header`,children:`Paired servers`}),R(`div`,{className:`card-body`,children:e.peers.length===0?R(`p`,{className:`text-body-secondary mb-0`,children:e.role===`master`?`This server takes nothing over from another.`:`This server is not paired with another yet.`}):R(`table`,{className:`table table-sm align-middle mb-0`,children:R(`tbody`,{children:e.peers.map(a=>{let o=j(a,e.discovered);return z(`tr`,{children:[z(`td`,{children:[R(`strong`,{children:o}),a.master&&R(`span`,{className:`badge text-bg-primary ms-2`,children:`master`}),R(`div`,{className:`small text-body-secondary`,children:a.url}),N(a)&&R(`div`,{className:`small text-body-secondary`,children:N(a)})]}),R(`td`,{children:R(Y,{peer:a,name:o,status:e,now:i})}),z(`td`,{className:`text-end text-nowrap`,children:[a.state===`needs-pairing`&&R(`button`,{type:`button`,className:`btn btn-sm btn-primary me-2`,disabled:t!==null,onClick:()=>void n(a.url),children:`Pair again`}),R(`button`,{type:`button`,className:`btn btn-sm btn-outline-danger`,disabled:t!==null,onClick:()=>{r(a,o)},children:`Remove`})]})]},a.url)})})})})]})}function Y({peer:e,name:t,status:n,now:r}){return z(L,{children:[e.state===`paired`&&z(L,{children:[R(X,{online:e.online===!0}),z(`span`,{className:`small text-body-secondary ms-2`,children:[`seen `,F(e.lastSeen,r)]})]}),e.state===`waiting-approval`&&z(L,{children:[R(`span`,{className:`badge text-bg-warning`,children:`Waiting for approval`}),z(`div`,{className:`small mt-1`,children:[`On `,t,`, approve “Multihelm on `,n.name,`” under`,` `,R(`a`,{href:P(e.url),target:`_blank`,rel:`noreferrer`,children:`Security → Access Requests`}),`.`]})]}),e.state===`needs-pairing`&&R(`span`,{className:`badge text-bg-danger`,children:`Not paired`}),e.lastError&&R(`div`,{className:`small text-danger mt-1`,children:e.lastError})]})}function X({online:e}){return e?R(`span`,{className:`badge text-bg-success`,children:`Online`}):R(`span`,{className:`badge text-bg-secondary`,children:`Offline`})}function Z({status:e,busy:t,onForget:n}){let r=Date.now();return z(`div`,{className:`card mb-3`,children:[R(`div`,{className:`card-header`,children:`Servers paired with this one`}),R(`div`,{className:`card-body`,children:e.pairedBy.length===0?R(`p`,{className:`text-body-secondary mb-0`,children:`No other server is paired with this one yet. Pair the others with it in their Multihelm settings.`}):R(`table`,{className:`table table-sm align-middle mb-0`,children:R(`tbody`,{children:e.pairedBy.map(e=>z(`tr`,{children:[z(`td`,{children:[R(`strong`,{children:e.name}),N(e)&&R(`div`,{className:`small text-body-secondary`,children:N(e)})]}),z(`td`,{children:[R(X,{online:e.online}),z(`span`,{className:`small text-body-secondary ms-2`,children:[`seen `,F(e.lastSeen,r)]})]}),R(`td`,{className:`text-end`,children:!e.online&&R(`button`,{type:`button`,className:`btn btn-sm btn-outline-secondary`,disabled:t!==null,onClick:()=>{n(e)},children:`Forget`})})]},e.id))})})})]})}function Q({status:e,busy:t,onPair:n}){let r=A(e);return r.length===0?R(`p`,{className:`text-body-secondary`,children:`No other Signal K server that is not paired yet was found on the network.`}):R(`table`,{className:`table table-sm align-middle`,children:R(`tbody`,{children:r.map(e=>z(`tr`,{children:[z(`td`,{children:[R(`strong`,{children:e.name}),e.vessel&&z(`span`,{className:`text-body-secondary ms-2`,children:[`vessel `,e.vessel]}),R(`div`,{className:`small text-body-secondary`,children:e.url})]}),R(`td`,{className:`text-end`,children:R(`button`,{type:`button`,className:`btn btn-sm btn-primary`,disabled:t!==null,onClick:()=>void n(e.url),children:t===e.url?`Pairing…`:`Pair`})})]},e.url))})})}function $({configuration:e,save:t}){let[n,r]=E(()=>G(e)),i=(e,t)=>{r(n=>({...n,[e]:t}))},a=(e,t,r,a=!1)=>z(`div`,{className:`form-check mb-3`,children:[R(`input`,{className:`form-check-input`,type:`checkbox`,id:`multihelm-${e}`,checked:n[e],disabled:a,onChange:t=>{i(e,t.target.checked)}}),R(`label`,{className:`form-check-label`,htmlFor:`multihelm-${e}`,children:t}),R(`div`,{className:`form-text`,children:r})]}),o=n.master;return z(`div`,{className:`card mb-3`,children:[R(`div`,{className:`card-header`,children:`Settings`}),z(`div`,{className:`card-body`,children:[a(`master`,`This server is the boat's master`,`Set this on one server only. Plugins that run on the master alone run here, and the other servers use them through this one.`),o&&R(`p`,{className:`small text-body-secondary`,children:`The options below apply to the servers that are not the master.`}),a(`followVessel`,`Take the vessel data over from the master`,`Name, MMSI, call sign, dimensions and ship type from Server → Settings. Changes made here are replaced by the master's.`,o),a(`followMetadata`,`Take the path metadata over from the master`,`Display names, units, zones and alarm limits set for paths. Changes made here are replaced by the master's.`,o),a(`followSourcePriorities`,`Take the source priorities over from the master`,`Which source wins for a path, from Data → Source Priorities. They name sources by connection, so give the NMEA 2000 bus the same connection name on every server. Changes made here are replaced by the master's.`,o),a(`historyFromMaster`,`Answer this server's History API from the master`,`While the master has a history provider (signalk-questdb, for example), history graphs on this server show the master's history, so it is kept once on the boat. Needs Signal K 2.32.0 or later.`,o),R(`button`,{type:`button`,className:`btn btn-primary`,onClick:()=>{t({...e,...n})},children:`Save settings`})]})]})}export{K as default};
1
+ var e=window.__SK_REACT__;if(!e)throw Error(`signalk-multihelm: the admin UI does not expose React (Signal K 2.27 or later)`);var{Children:t,Component:n,Fragment:r,Profiler:i,PureComponent:a,StrictMode:o,Suspense:s,cloneElement:c,createContext:l,createElement:u,createRef:d,forwardRef:f,isValidElement:p,lazy:m,memo:h,startTransition:g,use:_,useActionState:v,useCallback:y,useContext:ee,useDebugValue:te,useDeferredValue:ne,useEffect:b,useId:re,useImperativeHandle:ie,useInsertionEffect:ae,useLayoutEffect:oe,useMemo:x,useOptimistic:se,useReducer:S,useRef:ce,useState:C,useSyncExternalStore:w,useTransition:le,version:ue}=e;function T(e){return e===1?`1 server`:`${String(e)} servers`}function E(e,t){if(!e.running)return{tone:`danger`,text:`Multihelm is not running. Enable it above.`};if(e.masterConflict)return{tone:`danger`,text:`More than one server is set to be the boat's master. Set one only.`};if(e.role===`master`){let t=e.pairedBy.length,n=e.pairedBy.filter(e=>e.online).length;return{tone:`info`,text:t===0?`This server is the boat's master. No other server is paired with it yet.`:`This server is the boat's master. ${String(n)} of the ${T(t)} paired with it ${n===1?`is`:`are`} online.`}}if(e.master)return{tone:`success`,text:`The boat's master is ${e.master.name}; it answered ${N(e.master.lastSeen,t)}.`};let n=e.peers.filter(e=>e.state===`paired`),r=n.filter(e=>e.online===!1);return r.length>0?{tone:`danger`,text:`No master found: ${r.map(e=>e.name).join(`, `)} ${r.length===1?`is`:`are`} not reachable.`}:n.length===0?{tone:`warning`,text:`No master found: this server is not paired yet. Pair it with the server set to be the boat's master below.`}:{tone:`warning`,text:`No master found: none of the paired servers is set to be the boat's master.`}}function D(e,t){return e.url===t.url||e.name.toLowerCase()===t.name.toLowerCase()}function O(e){return e.discovered.filter(t=>!e.peers.some(e=>D(t,e)))}function k(e,t){return e.id===``?t.find(t=>t.url===e.url)?.name??e.name:e.name}function A(e){return`/skServer/security/access/requests/${encodeURIComponent(e)}/approved`}function j(e){return[e.signalk&&`Signal K ${e.signalk}`,e.multihelm&&`Multihelm ${e.multihelm}`].filter(Boolean).join(` · `)}function M(e){return`${e}/admin/#/security/access/requests`}function N(e,t){if(e===void 0)return`never`;let n=Math.max(0,Math.round((t-e)/1e3));if(n<10)return`just now`;if(n<90)return`${String(n)} s ago`;let r=Math.round(n/60);if(r<90)return`${String(r)} min ago`;let i=Math.round(r/60);return i<36?`${String(i)} h ago`:`${String(Math.round(i/24))} days ago`}var P=window.__SK_REACT_JSX_RUNTIME__;if(!P)throw Error(`signalk-multihelm: the admin UI does not expose React (Signal K 2.27 or later)`);var{Fragment:F,jsx:I,jsxs:L}=P,R=`/plugins/signalk-multihelm`,z=3e3,B=`own-access`;async function V(e,t){let n=await fetch(e,{credentials:`same-origin`,...t}),r=await n.json().catch(()=>({}));if(!n.ok)throw Error(typeof r.error==`string`?r.error:`HTTP ${String(n.status)}`);return r}function H(e){return e instanceof Error?e.message:String(e)}function U(e){return{master:e?.master===!0,syncResources:e?.syncResources!==!1,followVessel:e?.followVessel!==!1,followMetadata:e?.followMetadata!==!1,followSourcePriorities:e?.followSourcePriorities!==!1,historyFromMaster:e?.historyFromMaster!==!1,autopilotFromMaster:e?.autopilotFromMaster!==!1,courseFromMaster:e?.courseFromMaster!==!1,alarmsFromMaster:e?.alarmsFromMaster!==!1}}function W({configuration:e,save:t}){let[n,r]=C(null),[i,a]=C(null),[o,s]=C(null),[c,l]=C(null),[u,d]=C(``),f=y(async()=>{try{r(await V(`${R}/status`)),a(null)}catch(e){a(H(e))}},[]);b(()=>{f();let e=setInterval(()=>void f(),z);return()=>{clearInterval(e)}},[f]);let p=async(e,t)=>{s(e),l(null);try{return await t(),!0}catch(t){return l({key:e,text:H(t)}),!1}finally{s(null),await f()}},m=e=>p(e,()=>V(`${R}/peers`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({url:e})})),h=(e,t)=>{window.confirm(`Stop keeping this server in step with ${t}?`)&&p(e.url,()=>V(`${R}/peers?url=${encodeURIComponent(e.url)}`,{method:`DELETE`}))},g=e=>{window.confirm(`Take ${e.name} off this list? It comes back once it asks this server again.`)&&p(e.id,()=>V(`${R}/paired-by?id=${encodeURIComponent(e.id)}`,{method:`DELETE`}))},_=e=>{p(B,()=>V(A(e),{method:`PUT`,headers:{"Content-Type":`application/json`},body:JSON.stringify({permissions:`admin`})}))},v=async e=>{e.preventDefault(),u.trim()!==``&&await m(u.trim())&&d(``)};return L(`div`,{children:[i&&L(`div`,{className:`alert alert-danger`,children:[`Cannot read Multihelm's status: `,i,n&&`. What is shown below may be out of date.`]}),n&&I(G,{status:n,allowing:o===B,allowError:c?.key===B?c.text:null,onAllow:_}),n?.running&&L(F,{children:[n.role===`master`&&I(Y,{status:n,busy:o,onForget:g}),I(K,{status:n,busy:o,onPair:m,onUnpair:h}),n.role!==`master`&&n.pairedBy.length>0&&I(Y,{status:n,busy:o,onForget:g}),I(Q,{status:n,onSaved:f}),L(`div`,{className:`card mb-3`,children:[I(`div`,{className:`card-header`,children:`Pair with another server`}),L(`div`,{className:`card-body`,children:[I(`p`,{className:`small text-body-secondary`,children:n.role===`master`?`The other servers pair with this one in their own Multihelm settings. Pair this one with another only to replicate charts both ways.`:`Each server takes over what the servers it is paired with share. Pair this one with the boat's master; to replicate charts both ways, pair the other server with this one too.`}),I(X,{status:n,busy:o,onPair:m}),L(`form`,{className:`row g-2 mt-1`,onSubmit:e=>void v(e),children:[I(`div`,{className:`col-sm`,children:I(`input`,{className:`form-control`,value:u,onChange:e=>{d(e.target.value)},placeholder:`Or enter its address, e.g. 192.168.1.10 or helm.local:3000`,"aria-label":`Address of the other server`})}),I(`div`,{className:`col-sm-auto`,children:I(`button`,{type:`submit`,className:`btn btn-primary`,disabled:o!==null||u.trim()===``,children:o===u.trim()?`Pairing…`:`Pair`})})]}),c&&c.key!==B&&I(`div`,{className:`alert alert-danger mt-3 mb-0`,children:c.text})]})]})]}),I($,{configuration:e,save:t})]})}function G({status:e,allowing:t,allowError:n,onAllow:r}){let i=E(e,Date.now()),a=e.ownAccess;return L(F,{children:[I(`div`,{className:`alert alert-${i.tone}`,children:i.text}),!e.available&&I(`div`,{className:`alert alert-danger`,children:`This Signal K server is older than 2.31, so the other servers cannot read from it. Update it.`}),a?.state===`waiting-approval`&&L(`div`,{className:`alert alert-warning`,children:[L(`p`,{children:[I(`strong`,{children:`One more step:`}),` Multihelm needs admin access to this server to write the master's vessel data, path metadata and source priorities here, and to start and stop the plugins that run on the master only.`]}),I(`button`,{type:`button`,className:`btn btn-primary`,disabled:t,onClick:()=>{r(a.request)},children:t?`Allowing…`:`Allow`}),L(`div`,{className:`small mt-2`,children:[`This approves Multihelm's own request under`,` `,I(`a`,{href:`#/security/access/requests`,children:`Security → Access Requests`}),`.`]}),n&&I(`div`,{className:`text-danger mt-2`,children:n})]}),e.controlAccess&&L(`div`,{className:`alert alert-warning`,children:[`To operate the autopilot, set the course and acknowledge alarms from this station, approve Multihelm's request `,I(`code`,{children:e.controlAccess.request}),` under Security → Access Requests on the master, `,e.master?.name??`the master`,`.`]}),a?.state===`denied`&&I(`div`,{className:`alert alert-danger`,children:`The access request on this server was denied, so the vessel settings are not taken over from the master and no plugin is switched for the master. Save the settings below to ask again.`})]})}function K({status:e,busy:t,onPair:n,onUnpair:r}){let i=Date.now();return L(`div`,{className:`card mb-3`,children:[I(`div`,{className:`card-header`,children:`Paired servers`}),I(`div`,{className:`card-body`,children:e.peers.length===0?I(`p`,{className:`text-body-secondary mb-0`,children:e.role===`master`?`This server takes nothing over from another.`:`This server is not paired with another yet.`}):I(`table`,{className:`table table-sm align-middle mb-0`,children:I(`tbody`,{children:e.peers.map(a=>{let o=k(a,e.discovered);return L(`tr`,{children:[L(`td`,{children:[I(`strong`,{children:o}),a.master&&I(`span`,{className:`badge text-bg-primary ms-2`,children:`master`}),I(`div`,{className:`small text-body-secondary`,children:a.url}),j(a)&&I(`div`,{className:`small text-body-secondary`,children:j(a)})]}),I(`td`,{children:I(q,{peer:a,name:o,status:e,now:i})}),L(`td`,{className:`text-end text-nowrap`,children:[a.state===`needs-pairing`&&I(`button`,{type:`button`,className:`btn btn-sm btn-primary me-2`,disabled:t!==null,onClick:()=>void n(a.url),children:`Pair again`}),I(`button`,{type:`button`,className:`btn btn-sm btn-outline-danger`,disabled:t!==null,onClick:()=>{r(a,o)},children:`Remove`})]})]},a.url)})})})})]})}function q({peer:e,name:t,status:n,now:r}){return L(F,{children:[e.state===`paired`&&L(F,{children:[I(J,{online:e.online===!0}),L(`span`,{className:`small text-body-secondary ms-2`,children:[`seen `,N(e.lastSeen,r)]})]}),e.state===`waiting-approval`&&L(F,{children:[I(`span`,{className:`badge text-bg-warning`,children:`Waiting for approval`}),L(`div`,{className:`small mt-1`,children:[`On `,t,`, approve “Multihelm on `,n.name,`” under`,` `,I(`a`,{href:M(e.url),target:`_blank`,rel:`noreferrer`,children:`Security → Access Requests`}),`.`]})]}),e.state===`needs-pairing`&&I(`span`,{className:`badge text-bg-danger`,children:`Not paired`}),e.lastError&&I(`div`,{className:`small text-danger mt-1`,children:e.lastError})]})}function J({online:e}){return e?I(`span`,{className:`badge text-bg-success`,children:`Online`}):I(`span`,{className:`badge text-bg-secondary`,children:`Offline`})}function Y({status:e,busy:t,onForget:n}){let r=Date.now();return L(`div`,{className:`card mb-3`,children:[I(`div`,{className:`card-header`,children:`Servers paired with this one`}),I(`div`,{className:`card-body`,children:e.pairedBy.length===0?I(`p`,{className:`text-body-secondary mb-0`,children:`No other server is paired with this one yet. Pair the others with it in their Multihelm settings.`}):I(`table`,{className:`table table-sm align-middle mb-0`,children:I(`tbody`,{children:e.pairedBy.map(e=>L(`tr`,{children:[L(`td`,{children:[I(`strong`,{children:e.name}),j(e)&&I(`div`,{className:`small text-body-secondary`,children:j(e)})]}),L(`td`,{children:[I(J,{online:e.online}),L(`span`,{className:`small text-body-secondary ms-2`,children:[`seen `,N(e.lastSeen,r)]})]}),I(`td`,{className:`text-end`,children:!e.online&&I(`button`,{type:`button`,className:`btn btn-sm btn-outline-secondary`,disabled:t!==null,onClick:()=>{n(e)},children:`Forget`})})]},e.id))})})})]})}function X({status:e,busy:t,onPair:n}){let r=O(e);return r.length===0?I(`p`,{className:`text-body-secondary`,children:`No other Signal K server that is not paired yet was found on the network.`}):I(`table`,{className:`table table-sm align-middle`,children:I(`tbody`,{children:r.map(e=>L(`tr`,{children:[L(`td`,{children:[I(`strong`,{children:e.name}),e.vessel&&L(`span`,{className:`text-body-secondary ms-2`,children:[`vessel `,e.vessel]}),I(`div`,{className:`small text-body-secondary`,children:e.url})]}),I(`td`,{className:`text-end`,children:I(`button`,{type:`button`,className:`btn btn-sm btn-primary`,disabled:t!==null,onClick:()=>void n(e.url),children:t===e.url?`Pairing…`:`Pair`})})]},e.url))})})}function Z(e){return Array.isArray(e)?e.filter(e=>typeof e==`object`&&!!e&&typeof e.id==`string`).filter(e=>e.id!==`signalk-multihelm`).map(e=>({id:e.id,name:typeof e.name==`string`&&e.name!==``?e.name:e.id})).sort((e,t)=>e.name.localeCompare(t.name)):[]}function Q({status:e,onSaved:t}){let[n,r]=C(null),[i,a]=C(null),[o,s]=C([]),[c,l]=C(!1),[u,d]=C(null);b(()=>{V(`/skServer/plugins`).then(e=>{r(Z(e))},e=>{d(`Cannot list the plugins: ${H(e)}`)})},[]);let f=i??e.masterPlugins,p=i===null||[...i].sort().join()===[...e.masterPlugins].sort().join(),m=[...new Set([...e.masterPlugins,...f,...o])].filter(e=>!n?.some(t=>t.id===e)),h=(e,t)=>{a(t?[...f,e]:f.filter(t=>t!==e)),t||s([...o,e])},g=async()=>{l(!0),d(null);try{await V(`${R}/master-plugins`,{method:`PUT`,headers:{"Content-Type":`application/json`},body:JSON.stringify({plugins:f})}),a(null),s([]),await t()}catch(e){d(H(e))}finally{l(!1)}};return L(`div`,{className:`card mb-3`,children:[I(`div`,{className:`card-header`,children:`Plugins that run on the master only`}),L(`div`,{className:`card-body`,children:[I(`p`,{className:`small text-body-secondary`,children:`These plugins run on the boat's master and are stopped on the other servers, for example an autopilot provider, so only one server sends its commands. The list is the same on every server; when another server becomes the master, they move there.`}),n===null&&!u&&I(`p`,{className:`text-body-secondary`,children:`Loading the plugins…`}),n?.map(e=>L(`div`,{className:`form-check`,children:[I(`input`,{className:`form-check-input`,type:`checkbox`,id:`multihelm-master-plugin-${e.id}`,checked:f.includes(e.id),disabled:c,onChange:t=>{h(e.id,t.target.checked)}}),I(`label`,{className:`form-check-label`,htmlFor:`multihelm-master-plugin-${e.id}`,children:e.name})]},e.id)),n!==null&&m.map(e=>L(`div`,{className:`form-check`,children:[I(`input`,{className:`form-check-input`,type:`checkbox`,id:`multihelm-master-plugin-${e}`,checked:f.includes(e),disabled:c,onChange:t=>{h(e,t.target.checked)}}),L(`label`,{className:`form-check-label`,htmlFor:`multihelm-master-plugin-${e}`,children:[e,` `,I(`span`,{className:`text-body-secondary`,children:`(not installed here)`})]})]},e)),I(`button`,{type:`button`,className:`btn btn-primary mt-3`,disabled:c||p,onClick:()=>void g(),children:c?`Saving…`:`Save list`}),u&&I(`div`,{className:`alert alert-danger mt-3 mb-0`,children:u})]})]})}function $({configuration:e,save:t}){let[n,r]=C(()=>U(e)),i=(e,t)=>{r(n=>({...n,[e]:t}))},a=(e,t,r,a=!1)=>L(`div`,{className:`form-check mb-3`,children:[I(`input`,{className:`form-check-input`,type:`checkbox`,id:`multihelm-${e}`,checked:n[e],disabled:a,onChange:t=>{i(e,t.target.checked)}}),I(`label`,{className:`form-check-label`,htmlFor:`multihelm-${e}`,children:t}),I(`div`,{className:`form-text`,children:r})]}),o=n.master;return L(`div`,{className:`card mb-3`,children:[I(`div`,{className:`card-header`,children:`Settings`}),L(`div`,{className:`card-body`,children:[a(`master`,`This server is the boat's master`,`Set this on one server only. Plugins that run on the master alone run here, and the other servers use them through this one.`),a(`syncResources`,`Keep routes and waypoints in step`,`Routes, waypoints, regions and notes, kept the same on every server of the boat: a change on one reaches the others, with no master.`),o&&I(`p`,{className:`small text-body-secondary`,children:`The options below apply to the servers that are not the master.`}),a(`followVessel`,`Take the vessel data over from the master`,`Name, MMSI, call sign, dimensions and ship type from Server → Settings. Changes made here are replaced by the master's.`,o),a(`followMetadata`,`Take the path metadata over from the master`,`Display names, units, zones and alarm limits set for paths. Changes made here are replaced by the master's.`,o),a(`followSourcePriorities`,`Take the source priorities over from the master`,`Which source wins for a path, from Data → Source Priorities. They name sources by connection, so give the NMEA 2000 bus the same connection name on every server. Changes made here are replaced by the master's.`,o),a(`historyFromMaster`,`Answer this server's History API from the master`,`While the master has a history provider (signalk-questdb, for example), history graphs on this server show the master's history, so it is kept once on the boat. Needs Signal K 2.32.0 or later.`,o),a(`autopilotFromMaster`,`Operate the master's autopilot from this server`,`A chartplotter here operates the master's autopilot, while only the master sends to it. Put the autopilot provider plugin on the list of plugins that run on the master only.`,o),a(`courseFromMaster`,`Keep the course the same as on the master`,`The active destination or route set at any station is taken over by all the others.`,o),a(`alarmsFromMaster`,`Show the master's alarms here`,`Silencing or acknowledging an alarm at any station does it at all the others. An alarm this server raises itself is not shown twice.`,o),I(`button`,{type:`button`,className:`btn btn-primary`,onClick:()=>{t({...e,...n})},children:`Save settings`})]})]})}export{W as default};