@mxtommy/kip 3.10.0-beta.3 → 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,45 +1,210 @@
1
- import { Delta, Path, Plugin, ServerAPI, Value } from '@signalk/server-api'
1
+ import { Path, Plugin, ServerAPI, SKVersion } from '@signalk/server-api'
2
+ import { Request, Response } from 'express'
3
+ import * as openapi from './openApi.json';
4
+
5
+ export default (server: ServerAPI): Plugin => {
6
+
7
+ const API_PATHS = {
8
+ DISPLAYS: `/displays`,
9
+ INSTANCE: `/displays/:displayId`,
10
+ ACTIVE_SCREEN: `/displays/:displayId/activeScreen`
11
+ } as const;
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
+ }
2
32
 
3
- export default (app: ServerAPI): Plugin => {
4
33
  const plugin: Plugin = {
5
34
  id: 'kip',
6
35
  name: 'KIP',
7
36
  description: 'KIP server plugin',
8
37
  start: (settings) => {
9
- app.debug(`${plugin.name}: Starting plugin with settings: ${JSON.stringify(settings)}`);
10
- app.registerPutHandler('vessels.self', 'plugins.displays.*', displayPutHandler);
38
+ server.debug(`Starting plugin with settings: ${JSON.stringify(settings)}`);
39
+ server.setPluginStatus(`Starting...`)
11
40
  },
12
41
  stop: () => {
13
- app.debug(`${plugin.name}: Stopping plugin`);
42
+ server.debug(`Stopping plugin`);
43
+ const msg = 'Stopped.'
44
+ server.setPluginStatus(msg)
14
45
  },
15
46
  schema: () => {
16
- }
17
- };
47
+ return {
48
+ type: "object",
49
+ properties: {}
50
+ };
51
+ },
52
+ registerWithRouter(router) {
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
+ })
62
+
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)}`);
65
+ try {
66
+ const { displayId } = req.params as { displayId: string }
67
+ const fullPath = getDisplaySelfPath(displayId)
68
+ server.debug(`Updating SK path ${fullPath} with body`)
69
+ server.handleMessage(
70
+ plugin.id,
71
+ {
72
+ updates: [
73
+ {
74
+ values: [
75
+ {
76
+ path: fullPath as Path,
77
+ value: req.body ?? null
78
+ }
79
+ ]
80
+ }
81
+ ]
82
+ },
83
+ SKVersion.v1
84
+ );
85
+ return res.status(200).json({ state: 'SUCCESS', statusCode: 200 });
86
+
87
+ } catch (error) {
88
+ const msg = `HandleMessage failed with errors!`
89
+ server.setPluginError(msg)
90
+ server.error(`Error in HandleMessage: ${error}`);
91
+
92
+ return sendFail(res, 400, (error as Error).message)
93
+ }
94
+ });
95
+
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)}`);
98
+ try {
99
+ const { displayId } = req.params as { displayId: string }
100
+ const fullPath = getDisplaySelfPath(displayId, 'activeScreen')
101
+ server.debug(`Updating SK path ${fullPath} with body.screenIdx`)
102
+ server.handleMessage(
103
+ plugin.id,
104
+ {
105
+ updates: [
106
+ {
107
+ values: [
108
+ {
109
+ path: fullPath as Path,
110
+ value: req.body.screenIdx !== undefined ? req.body.screenIdx : null
111
+ }
112
+ ]
113
+ }
114
+ ]
115
+ },
116
+ SKVersion.v1
117
+ );
118
+ return res.status(200).json({ state: 'SUCCESS', statusCode: 200 });
119
+
120
+ } catch (error) {
121
+ const msg = `HandleMessage failed with errors!`
122
+ server.setPluginError(msg)
123
+ server.error(`Error in HandleMessage: ${error}`);
124
+
125
+ return sendFail(res, 400, (error as Error).message)
126
+ }
127
+ });
18
128
 
19
- function displayPutHandler(context: string, path: string, value: Value): { state: 'COMPLETED'; statusCode: number; message?: string } {
20
- app.debug(`${plugin.name}: PUT handler called for context ${context}, path ${path}, value ${value}`);
21
- try {
22
- const delta: Delta = {
23
- updates: [
24
- {
25
- values: [
26
- {
27
- path: path as Path,
28
- value: value
29
- }
30
- ]
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')
31
157
  }
32
- ]
33
- };
34
- app.debug(`${plugin.name}: Sending message: `, JSON.stringify(delta));
35
- app.handleMessage(plugin.id, delta);
36
- return { state: "COMPLETED" as const, statusCode: 200 };
37
-
38
- } catch (error) {
39
- app.error(`${plugin.name}: Error in PUT handler: ${error}`);
40
- return { state: "COMPLETED" as const, statusCode: 500, message: (error as Error).message };
41
- }
42
- }
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)
192
+ }
193
+ });
194
+
195
+ // List all registered routes for debugging
196
+ if (router.stack) {
197
+ router.stack.forEach((layer: { route?: { path?: string; stack: { method: string }[] } }) => {
198
+ if (layer.route && layer.route.path) {
199
+ server.debug(`Registered route: ${layer.route.stack[0].method.toUpperCase()} ${layer.route.path}`);
200
+ }
201
+ });
202
+ }
203
+
204
+ server.setPluginStatus(`Providing remote display screen control`);
205
+ },
206
+ getOpenApi: () => openapi
207
+ };
43
208
 
44
209
  return plugin;
45
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.3",
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": {
@@ -25,6 +25,7 @@
25
25
  "keywords": [
26
26
  "signalk-webapp",
27
27
  "signalk-category-instruments",
28
+ "signalk-category-notifications",
28
29
  "signalk-node-server-plugin",
29
30
  "signal k",
30
31
  "signalk",
@@ -46,7 +47,7 @@
46
47
  "build:plugin": "tsc -p ./kip-plugin/tsconfig.plugin.json",
47
48
  "build:dev": "ng build --configuration=dev",
48
49
  "build:prod": "ng build --configuration=production",
49
- "build:all": "npm run build:prod && npm run build:plugin",
50
+ "build:all": "npm run build:plugin && npm run build:prod",
50
51
  "e2e": "ng e2e"
51
52
  },
52
53
  "devDependencies": {