bb-com-extension 0.1.1 → 0.1.2

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 ADDED
@@ -0,0 +1,214 @@
1
+ # bb-com-extension
2
+
3
+ Blackbox CLI extension for creating serial (COM) based integrations in a Blackbox project.
4
+ It is built around the [ellipsis-com](https://www.npmjs.com/package/ellipsis-com) library for serial communications.
5
+
6
+ This extension adds custom Blackbox types and commands for:
7
+ - defining COM device types,
8
+ - attaching COM macros to OpenAPI operations,
9
+ - generating a COM loader utility.
10
+
11
+ ## What this extension adds
12
+
13
+ When installed into a Blackbox project, this extension contributes:
14
+
15
+ - Types:
16
+ - `com-type` — Type of serial device.
17
+ - `com-macro` — A macro to send over the serial link.
18
+ - `com-utils` — Helper utilities for serial communications.
19
+ - Commands supported by this extension:
20
+ - `add`
21
+ - `delete` (currently placeholder / not implemented)
22
+ - `generate`
23
+
24
+ ## Install
25
+
26
+ ### 1) Install package (project-local)
27
+
28
+ From your Blackbox project directory:
29
+
30
+ ```bash
31
+ npm install --save-dev bb-com-extension
32
+ ```
33
+
34
+ ### 2) Register extension with blackbox-cli
35
+
36
+ ```bash
37
+ bb add extension -p bb-com-extension
38
+ ```
39
+
40
+ This stores extension metadata in your `blackbox.json`.
41
+
42
+ ## Usage
43
+
44
+ ### Add a COM type
45
+
46
+ Creates/updates `x-bb-com-types.<name>` in `openapi.json`.
47
+
48
+ ```bash
49
+ bb add com-type \
50
+ --name sensor-a \
51
+ --baud 9600 \
52
+ --init-commands "ATZ;ATE0" \
53
+ --init-responses "/OK/;/READY/" \
54
+ --startup-delay 1000
55
+ ```
56
+
57
+ Notes:
58
+ - `--init-commands` is semicolon-separated.
59
+ - `--init-responses` is semicolon-separated regex entries in slash form.
60
+ - `--init-commands` and `--init-responses` must have the same number of entries.
61
+
62
+ ### Add a COM macro to an operation
63
+
64
+ Attaches `x-bb-com-macro` to a specific OpenAPI path + HTTP method.
65
+
66
+ ```bash
67
+ bb add com-macro \
68
+ --name sensor-a \
69
+ --path /devices/#/readings \
70
+ --method get \
71
+ --commands "READ;STATUS" \
72
+ --responses "/TEMP:.*/;/OK/"
73
+ ```
74
+
75
+ Notes:
76
+ - `--path` uses Blackbox path format (`#` placeholders for path params).
77
+ - Method must exist on the matched OpenAPI path.
78
+ - Only one COM macro per path/method is allowed.
79
+ - `--responses` is semicolon-separated regex entries in slash form.
80
+ - `--commands` and `--init-responses` must have the same number of entries.
81
+
82
+ ### Generate COM utilities
83
+
84
+ ```bash
85
+ bb generate com-utils
86
+ ```
87
+
88
+ This will:
89
+ - create `gensrc/ComLoader.ts` if it does not already exist,
90
+ - add `ellipsis-com` to `package.json` dependencies (if missing).
91
+
92
+ Then run:
93
+
94
+ ```bash
95
+ npm install
96
+ ```
97
+
98
+ ## OpenAPI extensions produced
99
+
100
+ ### `x-bb-com-types`
101
+
102
+ Added at the document root:
103
+
104
+ ```json
105
+ {
106
+ "x-bb-com-types": {
107
+ "sensor-a": {
108
+ "baud": 9600,
109
+ "startupDelay": 1000,
110
+ "initCommands": ["ATZ", "ATE0"],
111
+ "initResponses": ["OK", "READY"]
112
+ }
113
+ }
114
+ }
115
+ ```
116
+
117
+ ### `x-bb-com-macro`
118
+
119
+ Added at operation level:
120
+
121
+ ```json
122
+ {
123
+ "paths": {
124
+ "/service/path": {
125
+ "get": {
126
+ "operationId": "getThing",
127
+ "x-bb-com-macro": {
128
+ "type": "sensor-a",
129
+ "commands": ["READ", "STATUS"],
130
+ "responses": ["TEMP:.*", "OK"]
131
+ }
132
+ }
133
+ }
134
+ }
135
+ }
136
+ ```
137
+
138
+ ## COM loader utility
139
+
140
+ The generated `ComLoader.ts` provides a `ComLoader` class that can be used to initialise and manage COM device connections based on the defined types and macros in your OpenAPI spec. It uses the `ellipsis-com` library to handle serial communications, sending init commands, and matching responses according to the configurations specified in your OpenAPI document.
141
+
142
+ The loader class will:
143
+ - Read COM type definitions from `x-bb-com-types`.
144
+ - Read COM macro definitions from `x-bb-com-macro`.
145
+ - Create and initialise a ComManager from the `ellipsis-com` library.
146
+
147
+ The ComManager can then be used to:
148
+ - Connect to devices based on the defined COM types.
149
+ - Send macro commands by referencing the macro name defined in `x-bb-com-macro`.
150
+
151
+ Example usage of the generated `ComLoader`:
152
+
153
+ ```typescript
154
+ @serviceClass('comm')
155
+ export class CommService {
156
+
157
+ @autowired('openapi-doc')
158
+ openapiDoc: any
159
+
160
+ @autowired('com-manager')
161
+ comManager!: ComManager
162
+
163
+ constructor() {
164
+ }
165
+
166
+ async getComm(): Promise<string> {
167
+ const port = this.comManager.getComPort('sensor-a', 0) // Get the first port of type 'sensor-a'.
168
+ const responses = await port.send('getThing') // Send macro by operationId.
169
+ return responses[0]
170
+ }
171
+
172
+ async createComm({data}: {data: any}): Promise<string> {
173
+ const port = this.comManager.getComPort('com1', 0)
174
+ const responses = await port.send('createThing', {data})
175
+ return responses[0]
176
+ }
177
+ }
178
+
179
+ ```
180
+
181
+ ## Option reference
182
+
183
+ - `--init-commands <string>`: Serial init commands, semicolon-separated.
184
+ - `--init-responses <string>`: Expected init responses as slash-wrapped regex entries, semicolon-separated.
185
+ - `--commands <string>`: Macro commands, semicolon-separated.
186
+ - `--responses <string>`: Macro responses as slash-wrapped regex entries, semicolon-separated.
187
+ - `--baud <string>`: Baud rate.
188
+ - `--method <string>`: HTTP method (`get`, `post`, `put`, `patch`, `delete`).
189
+ - `--startup-delay <number>`: Delay (ms) before init commands.
190
+
191
+ ## Behavior notes
192
+
193
+ - COM loader uses OpenAPI `operationId` values to map macros.
194
+ - Missing `operationId` for an operation with `x-bb-com-macro` will throw an error.
195
+ - Regex flags are supported in init responses when provided as `/pattern/flags`.
196
+ - `delete` for extension types is currently not implemented.
197
+
198
+ ## Development
199
+
200
+ Build:
201
+
202
+ ```bash
203
+ npm run build
204
+ ```
205
+
206
+ Watch/dev build:
207
+
208
+ ```bash
209
+ npm run dev-build
210
+ ```
211
+
212
+ ## License
213
+
214
+ ISC
package/index.ts CHANGED
@@ -93,7 +93,7 @@ export default class BBComExtension {
93
93
  }
94
94
 
95
95
  const macroDef = {
96
- name: this.config.name,
96
+ type: this.config.name,
97
97
  commands: this.config.commands.split(';'),
98
98
  responses: this.parseResponses(this.config.responses)
99
99
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bb-com-extension",
3
3
  "type": "module",
4
- "version": "0.1.1",
4
+ "version": "0.1.2",
5
5
  "description": "Blackbox extension to allow creation of com port based services.",
6
6
  "main": "bundle/index.js",
7
7
  "scripts": {
@@ -60,7 +60,7 @@ export class ComLoader {
60
60
  Object.values(this.openapiDoc.paths).forEach( (path: any) => {
61
61
  Object.keys(path)
62
62
  .filter(method => ['get', 'post', 'put', 'patch', 'delete'].includes(method))
63
- .filter(method => path[method]['x-bb-com-macro'])
63
+ .filter(method => path[method]['x-bb-com-macro']?.type === name)
64
64
  .forEach(method => {
65
65
  if(!path[method]['x-bb-com-macro'].commands ||
66
66
  !path[method]['x-bb-com-macro'].responses ||