@mxtommy/kip 3.10.0-beta.30 → 3.10.0-beta.31

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.
@@ -0,0 +1,64 @@
1
+ # KIP Plugin – Displays API
2
+
3
+ Base path for plugin routes: `/plugins/kip`
4
+
5
+ ## Endpoints
6
+
7
+ - GET `/plugins/kip/displays`
8
+ - Returns: `[{ displayId: string, displayName: string | null }]`
9
+
10
+ - GET `/plugins/kip/displays/{displayId}`
11
+ - Returns the raw object stored at `self.displays.{displayId}` (or `null`).
12
+
13
+ - PUT `/plugins/kip/displays/{displayId}`
14
+ - Body: arbitrary JSON object to store under `self.displays.{displayId}` (or `null` to clear).
15
+ - Returns: `{ state: "SUCCESS", statusCode: 200 }` on success.
16
+
17
+ - GET `/plugins/kip/displays/{displayId}/activeScreen`
18
+ - Returns: `number | null` – the active screen index.
19
+
20
+ - PUT `/plugins/kip/displays/{displayId}/activeScreen`
21
+ - Body: `{ "screenIdx": number | null }`
22
+ - Returns: `{ state: "SUCCESS", statusCode: 200 }` on success.
23
+
24
+ ## Examples
25
+
26
+ - List displays
27
+
28
+ ```sh
29
+ curl -s http://localhost:3000/plugins/kip/displays | jq
30
+ ```
31
+
32
+ - Read a display entry
33
+
34
+ ```sh
35
+ curl -s http://localhost:3000/plugins/kip/displays/<displayId> | jq
36
+ ```
37
+
38
+ - Set a display entry
39
+
40
+ ```sh
41
+ curl -s -X PUT \
42
+ -H 'Content-Type: application/json' \
43
+ -d '{"displayName":"Mast"}' \
44
+ http://localhost:3000/plugins/kip/displays/<displayId>
45
+ ```
46
+
47
+ - Get active screen
48
+
49
+ ```sh
50
+ curl -s http://localhost:3000/plugins/kip/displays/<displayId>/activeScreen
51
+ ```
52
+
53
+ - Set active screen
54
+
55
+ ```sh
56
+ curl -s -X PUT \
57
+ -H 'Content-Type: application/json' \
58
+ -d '{"screenIdx":1}' \
59
+ http://localhost:3000/plugins/kip/displays/<displayId>/activeScreen
60
+ ```
61
+
62
+ Notes:
63
+ - Replace `<displayId>` with the KIP instance UUID under `self.displays`.
64
+ - If your Signal K server requires auth, include cookies or bearer token accordingly.
@@ -1,14 +1,35 @@
1
1
  import { Path, Plugin, ServerAPI, SKVersion } from '@signalk/server-api'
2
2
  import { Request, Response } from 'express'
3
- //import * as openapi from './openApi.json';
3
+ import * as openapi from './openApi.json';
4
4
 
5
5
  export default (server: ServerAPI): Plugin => {
6
6
 
7
7
  const API_PATHS = {
8
- DISPLAYS_PATH: `/displays/:kipId`,
9
- ACTIVESCREEN_PATH: `/displays/:kipId/activeScreen`
8
+ DISPLAYS: `/displays`,
9
+ INSTANCE: `/displays/:displayId`,
10
+ ACTIVE_SCREEN: `/displays/:displayId/activeScreen`
10
11
  } as const;
11
12
 
13
+ // Helpers
14
+ function getDisplaySelfPath(displayId: string, suffix?: string): string {
15
+ const tail = suffix ? `.${suffix}` : ''
16
+ return server.getSelfPath(`displays.${displayId}${tail}`)
17
+ }
18
+
19
+ function readSelfDisplays(): Record<string, { displayName?: string }> | undefined {
20
+ const fullPath = server.getSelfPath('displays'); // e.g., vessels.self.displays
21
+ return server.getPath(fullPath) as Record<string, { displayName?: string }> | undefined;
22
+ }
23
+
24
+ function sendOk(res: Response, body?: unknown) {
25
+ if (body === undefined) return res.status(204).end()
26
+ return res.status(200).json(body)
27
+ }
28
+
29
+ function sendFail(res: Response, statusCode: number, message: string) {
30
+ return res.status(statusCode).json({ state: 'FAILED', statusCode, message })
31
+ }
32
+
12
33
  const plugin: Plugin = {
13
34
  id: 'kip',
14
35
  name: 'KIP',
@@ -16,9 +37,6 @@ export default (server: ServerAPI): Plugin => {
16
37
  start: (settings) => {
17
38
  server.debug(`Starting plugin with settings: ${JSON.stringify(settings)}`);
18
39
  server.setPluginStatus(`Starting...`)
19
-
20
- const p = server.getSelfPath('displays.*');
21
- server.debug(`Self path for displays.*: ${p}`);
22
40
  },
23
41
  stop: () => {
24
42
  server.debug(`Stopping plugin`);
@@ -26,19 +44,28 @@ export default (server: ServerAPI): Plugin => {
26
44
  server.setPluginStatus(msg)
27
45
  },
28
46
  schema: () => {
29
- return {
30
- type: "object",
31
- properties: {}
32
- };
47
+ return {
48
+ type: "object",
49
+ properties: {}
50
+ };
33
51
  },
34
52
  registerWithRouter(router) {
35
- server.debug(`Registering plugin routes: ${API_PATHS.DISPLAYS_PATH}`);
53
+ server.debug(`Registering plugin routes: ${API_PATHS.DISPLAYS}, ${API_PATHS.INSTANCE}, ${API_PATHS.ACTIVE_SCREEN}`);
54
+
55
+ // Validate :displayId where present
56
+ router.param('displayId', (req, res, next, displayId) => {
57
+ if (!displayId || typeof displayId !== 'string') {
58
+ return sendFail(res, 400, 'Missing or invalid displayId parameter')
59
+ }
60
+ next()
61
+ })
36
62
 
37
- router.put(`${API_PATHS.DISPLAYS_PATH}`, async (req: Request, res: Response) => {
38
- server.debug(`** PUT path ${API_PATHS.DISPLAYS_PATH}. Request Params: ${JSON.stringify(req.params)}, Body: ${JSON.stringify(req.body)}, Method: ${req.method}, Path: ${req.path}`);
63
+ router.put(`${API_PATHS.INSTANCE}`, async (req: Request, res: Response) => {
64
+ server.debug(`** PUT ${API_PATHS.INSTANCE}. Params: ${JSON.stringify(req.params)} Body: ${JSON.stringify(req.body)}`);
39
65
  try {
40
- const dottedPath = pathToDotNotation(req.path);
41
- server.debug(`Converted request path ${req.path} to dot notation ${dottedPath} and updating SK path with req.body`);
66
+ const { displayId } = req.params as { displayId: string }
67
+ const fullPath = getDisplaySelfPath(displayId)
68
+ server.debug(`Updating SK path ${fullPath} with body`)
42
69
  server.handleMessage(
43
70
  plugin.id,
44
71
  {
@@ -46,8 +73,8 @@ export default (server: ServerAPI): Plugin => {
46
73
  {
47
74
  values: [
48
75
  {
49
- path: dottedPath as Path,
50
- value: req.body ? req.body : null
76
+ path: fullPath as Path,
77
+ value: req.body ?? null
51
78
  }
52
79
  ]
53
80
  }
@@ -55,29 +82,23 @@ export default (server: ServerAPI): Plugin => {
55
82
  },
56
83
  SKVersion.v1
57
84
  );
58
- return res.status(200).json({
59
- state: 'SUCCESS',
60
- statusCode: 200
61
- });
85
+ return res.status(200).json({ state: 'SUCCESS', statusCode: 200 });
62
86
 
63
87
  } catch (error) {
64
88
  const msg = `HandleMessage failed with errors!`
65
89
  server.setPluginError(msg)
66
90
  server.error(`Error in HandleMessage: ${error}`);
67
91
 
68
- return res.status(400).json({
69
- state: 'FAILED',
70
- statusCode: 400,
71
- message: (error as Error).message
72
- });
92
+ return sendFail(res, 400, (error as Error).message)
73
93
  }
74
94
  });
75
95
 
76
- router.put(`${API_PATHS.ACTIVESCREEN_PATH}`, async (req: Request, res: Response) => {
77
- server.debug(`** PUT path ${API_PATHS.ACTIVESCREEN_PATH}. Request Params: ${JSON.stringify(req.params)}, Body: ${JSON.stringify(req.body)}, Method: ${req.method}, Path: ${req.path}`);
96
+ router.put(`${API_PATHS.ACTIVE_SCREEN}`, async (req: Request, res: Response) => {
97
+ server.debug(`** PUT ${API_PATHS.ACTIVE_SCREEN}. Params: ${JSON.stringify(req.params)} Body: ${JSON.stringify(req.body)}`);
78
98
  try {
79
- const dottedPath = pathToDotNotation(req.path);
80
- server.debug(`Converted request path ${req.path} to dot notation ${dottedPath} and updating SK path with req.body`);
99
+ const { displayId } = req.params as { displayId: string }
100
+ const fullPath = getDisplaySelfPath(displayId, 'activeScreen')
101
+ server.debug(`Updating SK path ${fullPath} with body.screenIdx`)
81
102
  server.handleMessage(
82
103
  plugin.id,
83
104
  {
@@ -85,8 +106,8 @@ export default (server: ServerAPI): Plugin => {
85
106
  {
86
107
  values: [
87
108
  {
88
- path: dottedPath as Path,
89
- value: req.body.screenId
109
+ path: fullPath as Path,
110
+ value: req.body.screenIdx !== undefined ? req.body.screenIdx : null
90
111
  }
91
112
  ]
92
113
  }
@@ -94,21 +115,80 @@ export default (server: ServerAPI): Plugin => {
94
115
  },
95
116
  SKVersion.v1
96
117
  );
97
- return res.status(200).json({
98
- state: 'SUCCESS',
99
- statusCode: 200
100
- });
118
+ return res.status(200).json({ state: 'SUCCESS', statusCode: 200 });
101
119
 
102
120
  } catch (error) {
103
121
  const msg = `HandleMessage failed with errors!`
104
122
  server.setPluginError(msg)
105
123
  server.error(`Error in HandleMessage: ${error}`);
106
124
 
107
- return res.status(400).json({
108
- state: 'FAILED',
109
- statusCode: 400,
110
- message: (error as Error).message
111
- });
125
+ return sendFail(res, 400, (error as Error).message)
126
+ }
127
+ });
128
+
129
+ router.get(API_PATHS.DISPLAYS, (req: Request, res: Response) => {
130
+ server.debug(`** GET ${API_PATHS.DISPLAYS}. Params: ${JSON.stringify(req.params)}`);
131
+ try {
132
+ const displays = readSelfDisplays();
133
+ const items =
134
+ displays && typeof displays === 'object'
135
+ ? Object.entries(displays)
136
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
137
+ .filter(([_, v]) => v && typeof v === 'object')
138
+ .map(([displayId, v]: [string, { displayName?: string }]) => ({
139
+ displayId,
140
+ displayName: v.displayName ?? null
141
+ }))
142
+ : [];
143
+
144
+ return res.status(200).json(items);
145
+ } catch (error) {
146
+ server.error(`Error reading displays: ${String((error as Error).message || error)}`);
147
+ return sendFail(res, 400, (error as Error).message)
148
+ }
149
+ });
150
+
151
+ router.get(`${API_PATHS.INSTANCE}`, (req: Request, res: Response) => {
152
+ server.debug(`** GET ${API_PATHS.INSTANCE}. Params: ${JSON.stringify(req.params)}`);
153
+ try {
154
+ const { displayId } = req.params as { displayId?: string };
155
+ if (!displayId) {
156
+ return sendFail(res, 400, 'Missing displayId parameter')
157
+ }
158
+
159
+ const fullPath = getDisplaySelfPath(displayId);
160
+ const value = server.getPath(fullPath);
161
+
162
+ if (value === undefined) {
163
+ return sendFail(res, 404, `Display ${displayId} not found`)
164
+ }
165
+
166
+ return sendOk(res, value);
167
+ } catch (error) {
168
+ server.error(`Error reading display ${req.params?.displayId}: ${String((error as Error).message || error)}`);
169
+ return sendFail(res, 400, (error as Error).message)
170
+ }
171
+ });
172
+
173
+ router.get(`${API_PATHS.ACTIVE_SCREEN}`, (req: Request, res: Response) => {
174
+ server.debug(`** GET ${API_PATHS.ACTIVE_SCREEN}. Params: ${JSON.stringify(req.params)}`);
175
+ try {
176
+ const { displayId } = req.params as { displayId?: string };
177
+ if (!displayId) {
178
+ return sendFail(res, 400, 'Missing displayId parameter')
179
+ }
180
+
181
+ const fullPath = getDisplaySelfPath(displayId, 'activeScreen');
182
+ const value = server.getPath(fullPath);
183
+
184
+ if (value === undefined) {
185
+ return sendFail(res, 404, `Active screen for display ${displayId} not found`)
186
+ }
187
+
188
+ return sendOk(res, value);
189
+ } catch (error) {
190
+ server.error(`Error reading activeScreen for ${req.params?.displayId}: ${String((error as Error).message || error)}`);
191
+ return sendFail(res, 400, (error as Error).message)
112
192
  }
113
193
  });
114
194
 
@@ -121,16 +201,10 @@ export default (server: ServerAPI): Plugin => {
121
201
  });
122
202
  }
123
203
 
124
- server.setPluginStatus(`Providing remote display control`);
125
- }
204
+ server.setPluginStatus(`Providing remote display screen control`);
205
+ },
206
+ getOpenApi: () => openapi
126
207
  };
127
208
 
128
- /*
129
- * Replace all / with . and remove leading.
130
- */
131
- function pathToDotNotation(path: string): string {
132
- return path.replace(/\//g, '.').replace(/^\./, '');
133
- }
134
-
135
209
  return plugin;
136
210
  }
@@ -0,0 +1,141 @@
1
+ {
2
+ "openapi": "3.0.0",
3
+ "info": {
4
+ "version": "1.0.0",
5
+ "title": "KIP Remote Displays API",
6
+ "description": "API endpoints to list KIP displays and control the active screen for a given KIP instance via Signal K self.displays tree.\n\nUsage:\n- List displays:\n curl -s http://localhost:3000/plugins/kip/displays\n- Read a display entry:\n curl -s http://localhost:3000/plugins/kip/displays/{displayId}\n- Set a display entry:\n curl -s -X PUT -H 'Content-Type: application/json' -d '{\"displayName\":\"Mast\"}' http://localhost:3000/plugins/kip/displays/{displayId}\n- Get active screen:\n curl -s http://localhost:3000/plugins/kip/displays/{displayId}/activeScreen\n- Set active screen:\n curl -s -X PUT -H 'Content-Type: application/json' -d '{\"screenIdx\":1}' http://localhost:3000/plugins/kip/displays/{displayId}/activeScreen",
7
+ "termsOfService": "http://signalk.org/terms/",
8
+ "license": {
9
+ "name": "Apache 2.0",
10
+ "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
11
+ }
12
+ },
13
+ "externalDocs": {
14
+ "url": "http://signalk.org/specification/",
15
+ "description": "Signal K specification."
16
+ },
17
+ "servers": [
18
+ { "url": "/" }
19
+ ],
20
+ "tags": [
21
+ { "name": "Displays", "description": "KIP display discovery and control." }
22
+ ],
23
+ "components": {
24
+ "schemas": {
25
+ "DisplayInfo": {
26
+ "type": "object",
27
+ "properties": {
28
+ "displayId": { "type": "string", "description": "KIP instance UUID" },
29
+ "displayName": { "type": "string", "nullable": true }
30
+ },
31
+ "required": ["displayId"]
32
+ },
33
+ "SuccessResponse": {
34
+ "type": "object",
35
+ "properties": {
36
+ "state": { "type": "string", "enum": ["SUCCESS"] },
37
+ "statusCode": { "type": "integer", "enum": [200] }
38
+ },
39
+ "required": ["state", "statusCode"]
40
+ },
41
+ "ErrorResponse": {
42
+ "type": "object",
43
+ "properties": {
44
+ "state": { "type": "string", "enum": ["FAILED"] },
45
+ "statusCode": { "type": "integer" },
46
+ "message": { "type": "string" }
47
+ },
48
+ "required": ["state", "statusCode", "message"]
49
+ },
50
+ "ActiveScreenSetRequest": {
51
+ "type": "object",
52
+ "properties": {
53
+ "screenIdx": { "type": "integer", "nullable": true, "description": "Index of active screen or null to clear" }
54
+ }
55
+ },
56
+ "AnyObjectOrNull": {
57
+ "oneOf": [
58
+ { "type": "object", "additionalProperties": true },
59
+ { "type": "null" }
60
+ ]
61
+ }
62
+ },
63
+ "parameters": {
64
+ "DisplayIdParam": {
65
+ "in": "path",
66
+ "required": true,
67
+ "name": "displayId",
68
+ "description": "KIP instance UUID",
69
+ "schema": { "type": "string" }
70
+ }
71
+ },
72
+ "securitySchemes": {
73
+ "bearerAuth": { "type": "http", "scheme": "bearer", "bearerFormat": "JWT" },
74
+ "cookieAuth": { "type": "apiKey", "in": "cookie", "name": "JAUTHENTICATION" }
75
+ }
76
+ },
77
+ "security": [{ "cookieAuth": [] }, { "bearerAuth": [] }],
78
+ "paths": {
79
+ "/plugins/kip/displays": {
80
+ "get": {
81
+ "tags": ["Displays"],
82
+ "summary": "List available KIP displays",
83
+ "responses": {
84
+ "200": {
85
+ "description": "List of KIP instances discovered under self.displays",
86
+ "content": { "application/json": { "schema": { "type": "array", "items": { "$ref": "#/components/schemas/DisplayInfo" } } } }
87
+ },
88
+ "400": { "description": "Bad request", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } }
89
+ }
90
+ }
91
+ },
92
+ "/plugins/kip/displays/{displayId}": {
93
+ "parameters": [ { "$ref": "#/components/parameters/DisplayIdParam" } ],
94
+ "get": {
95
+ "tags": ["Displays"],
96
+ "summary": "Get raw display entry under self.displays.{displayId}",
97
+ "responses": {
98
+ "200": { "description": "Display object", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/AnyObjectOrNull" } } } },
99
+ "404": { "description": "Not found", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
100
+ "400": { "description": "Bad request", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } }
101
+ }
102
+ },
103
+ "put": {
104
+ "tags": ["Displays"],
105
+ "summary": "Set display entry under self.displays.{displayId}",
106
+ "requestBody": {
107
+ "required": false,
108
+ "content": { "application/json": { "schema": { "$ref": "#/components/schemas/AnyObjectOrNull" } } }
109
+ },
110
+ "responses": {
111
+ "200": { "description": "Updated", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/SuccessResponse" } } } },
112
+ "400": { "description": "Bad request", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } }
113
+ }
114
+ }
115
+ },
116
+ "/plugins/kip/displays/{displayId}/activeScreen": {
117
+ "parameters": [ { "$ref": "#/components/parameters/DisplayIdParam" } ],
118
+ "get": {
119
+ "tags": ["Displays"],
120
+ "summary": "Get active screen index for display",
121
+ "responses": {
122
+ "200": { "description": "Active screen index or null", "content": { "application/json": { "schema": { "type": "integer", "nullable": true } } } },
123
+ "404": { "description": "Not found", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
124
+ "400": { "description": "Bad request", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } }
125
+ }
126
+ },
127
+ "put": {
128
+ "tags": ["Displays"],
129
+ "summary": "Set active screen index for display",
130
+ "requestBody": {
131
+ "required": false,
132
+ "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ActiveScreenSetRequest" } } }
133
+ },
134
+ "responses": {
135
+ "200": { "description": "Updated", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/SuccessResponse" } } } },
136
+ "400": { "description": "Bad request", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } }
137
+ }
138
+ }
139
+ }
140
+ }
141
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mxtommy/kip",
3
- "version": "3.10.0-beta.30",
3
+ "version": "3.10.0-beta.31",
4
4
  "description": "An advanced and versatile marine instrumentation package to display Signal K data.",
5
5
  "license": "MIT",
6
6
  "author": {