@rhizomatics/signalk-einklabel-plugin 1.3.0-beta13 → 1.3.0-beta14

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/dist/cli/index.js CHANGED
@@ -291,6 +291,7 @@ exports.program
291
291
  mirror: parseMirrorMode(opts.mirror),
292
292
  compress: opts.compress,
293
293
  compressionFormat: parseCompressionFormat(opts.compressionFormat),
294
+ log: log_1.logDebug,
294
295
  });
295
296
  });
296
297
  console.log(`painted ${opts.address} (${bitmap.width}x${bitmap.height}) ${opts.colours}`);
@@ -168,6 +168,17 @@ async function ensureDeviceVisible(bleApi, pluginId, address, timeoutMs) {
168
168
  throw new Error(`device ${address} isn't visible to the SignalK BLE Manager yet (out of range, asleep, or never seen) after waiting ${timeoutMs}ms`);
169
169
  }
170
170
  }
171
+ /**
172
+ * Upper bound on each BLE Manager release/disconnect call this plugin waits on. Neither has a timeout
173
+ * of its own, and a release has to disconnect the device, which can wait on a Bluetooth link that's
174
+ * already gone - left unbounded, one stuck release wedges every later paint behind it (see
175
+ * `exclusiveBleManagerAccess`).
176
+ */
177
+ const CLEANUP_TIMEOUT_MS = 10000;
178
+ /** Waits for a cleanup call for at most `CLEANUP_TIMEOUT_MS`, ignoring its outcome - cleanup is best-effort. */
179
+ async function boundedCleanup(...calls) {
180
+ await (0, bleDiscovery_1.withDeadline)(Promise.allSettled(calls), CLEANUP_TIMEOUT_MS, "BLE Manager cleanup").catch((err) => console.error(`${pluginVersion_1.PLUGIN_NAME}: ${err.message} - carrying on`));
181
+ }
171
182
  /** Upper bound on how long a timed-out `bleApi.connectGATT()` is given to settle server-side before a retry - see `bleApiBackend`. */
172
183
  const CONNECT_SETTLE_GRACE_MS = 30000;
173
184
  let bleManagerQueue = Promise.resolve();
@@ -194,7 +205,7 @@ function bleApiBackend(bleApi, pluginId) {
194
205
  // registered under our own pluginId, which would otherwise block this connect with
195
206
  // "already claimed" until the server restarts - see signalk-bluetti-plugin's BleManagerDevice
196
207
  // for the same defensive call. A no-op if we don't currently hold the claim.
197
- await bleApi.releaseGATTDevice(address, pluginId).catch(() => { });
208
+ await boundedCleanup(bleApi.releaseGATTDevice(address, pluginId));
198
209
  await ensureDeviceVisible(bleApi, pluginId, address, DEVICE_DISCOVERY_TIMEOUT_MS);
199
210
  const connecting = bleApi.connectGATT(address, pluginId);
200
211
  let timedOut = false;
@@ -210,7 +221,7 @@ function bleApiBackend(bleApi, pluginId) {
210
221
  // disconnect again. The release is awaited (only the late disconnect is left in the
211
222
  // background) so a caller retrying straight away - `withRetries` - can't race it with a new
212
223
  // `connectGATT()` and get rejected with "has a GATT claim in progress" for its trouble.
213
- await bleApi.releaseGATTDevice(address, pluginId).catch(() => { });
224
+ await boundedCleanup(bleApi.releaseGATTDevice(address, pluginId));
214
225
  // ...except a claim still *connecting* isn't in the server's claim table yet, only its pending
215
226
  // set, which `releaseGATTDevice` doesn't touch - so while the server's own connect is still in
216
227
  // flight, a retry's `connectGATT()` is rejected outright with "has a GATT claim in progress".
@@ -220,7 +231,7 @@ function bleApiBackend(bleApi, pluginId) {
220
231
  (0, bleDiscovery_1.sleep)(Math.min(timeoutMs, CONNECT_SETTLE_GRACE_MS)).then(() => undefined),
221
232
  ]);
222
233
  if (late) {
223
- await Promise.allSettled([bleApi.releaseGATTDevice(address, pluginId), late.disconnect()]);
234
+ await boundedCleanup(bleApi.releaseGATTDevice(address, pluginId), late.disconnect());
224
235
  }
225
236
  else {
226
237
  void connecting.then((c) => c.disconnect()).catch(() => { });
@@ -231,7 +242,7 @@ function bleApiBackend(bleApi, pluginId) {
231
242
  // Belt-and-suspenders, matching the timeout-cleanup above: `releaseGATTDevice` is the
232
243
  // authoritative claim release, `disconnect()` a secondary teardown of this specific handle -
233
244
  // do both regardless of which (if either) itself hangs or rejects.
234
- await Promise.allSettled([bleApi.releaseGATTDevice(address, pluginId), conn.disconnect()]);
245
+ await boundedCleanup(bleApi.releaseGATTDevice(address, pluginId), conn.disconnect());
235
246
  });
236
247
  },
237
248
  async waitForManufacturerData(address, manufacturerId, timeoutMs) {
@@ -246,7 +257,7 @@ function bleApiBackend(bleApi, pluginId) {
246
257
  // this wait forever, since nothing else in this path ever calls `connectGatt` (and so never gets a
247
258
  // chance to release the stale claim) unless a fresh advertisement shows up first. A no-op if we
248
259
  // don't currently hold the claim.
249
- await bleApi.releaseGATTDevice(address, pluginId).catch(() => { });
260
+ await boundedCleanup(bleApi.releaseGATTDevice(address, pluginId));
250
261
  return new Promise((resolve) => {
251
262
  const timer = setTimeout(() => {
252
263
  unsubscribe();
@@ -40,6 +40,11 @@ export declare function forEachAdvertisedDevice(adapter: Adapter, fn: (advertise
40
40
  * last one that's eventually thrown - an early attempt's error is often the real cause, with later
41
41
  * ones just fallout from it.
42
42
  */
43
+ /**
44
+ * Rejects with `${what} timed out after ${ms}ms` if `promise` hasn't settled by then. The underlying
45
+ * work can't be cancelled - this only lets the caller stop waiting for it.
46
+ */
47
+ export declare function withDeadline<T>(promise: Promise<T>, ms: number, what: string): Promise<T>;
43
48
  export declare function withRetries<T>(attempts: number, fn: (attempt: number) => Promise<T>, { delayMs, onError }?: {
44
49
  delayMs?: number;
45
50
  onError?: (err: unknown, attempt: number) => void;
@@ -4,6 +4,7 @@ exports.sleep = sleep;
4
4
  exports.createBluetooth = createBluetooth;
5
5
  exports.getManufacturerId = getManufacturerId;
6
6
  exports.forEachAdvertisedDevice = forEachAdvertisedDevice;
7
+ exports.withDeadline = withDeadline;
7
8
  exports.withRetries = withRetries;
8
9
  exports.withDiscovery = withDiscovery;
9
10
  exports.connectWithTimeout = connectWithTimeout;
@@ -74,6 +75,23 @@ async function forEachAdvertisedDevice(adapter, fn) {
74
75
  * last one that's eventually thrown - an early attempt's error is often the real cause, with later
75
76
  * ones just fallout from it.
76
77
  */
78
+ /**
79
+ * Rejects with `${what} timed out after ${ms}ms` if `promise` hasn't settled by then. The underlying
80
+ * work can't be cancelled - this only lets the caller stop waiting for it.
81
+ */
82
+ async function withDeadline(promise, ms, what) {
83
+ let timer;
84
+ const deadline = new Promise((_, reject) => {
85
+ timer = setTimeout(() => reject(new Error(`${what} timed out after ${ms}ms`)), ms);
86
+ });
87
+ promise.catch(() => { }); // observed here too, so losing the race never surfaces as an unhandled rejection
88
+ try {
89
+ return await Promise.race([promise, deadline]);
90
+ }
91
+ finally {
92
+ clearTimeout(timer);
93
+ }
94
+ }
77
95
  async function withRetries(attempts, fn, { delayMs = 0, onError } = {}) {
78
96
  const total = Math.max(1, attempts);
79
97
  let lastErr;
@@ -73,7 +73,10 @@ class GiciskyDriver {
73
73
  const layout = (0, layout_1.withCompressionFormat)((pid !== undefined && layout_1.GICISKY_PID_LAYOUT[pid]) || (0, layout_1.defaultLayoutFor)(metadata.colours), config.compressionFormat ?? "auto", metadata.colours);
74
74
  const framed = (0, mirror_1.mirrorBitmap)((0, reframe_1.reframeBitmap)(bitmap, metadata.width, metadata.height, config.reframe ?? "crop"), config.mirror ?? "none");
75
75
  const payload = (0, encode_1.encodeBitmap)(framed, metadata, layout, config.compress ?? true);
76
+ const log = config.log ?? (() => { });
77
+ log("connecting");
76
78
  const conn = await backend.connectGatt(config.address, config.connectTimeoutMs ?? DEFAULT_PAINT_CONNECT_TIMEOUT_MS);
79
+ log("connected");
77
80
  try {
78
81
  const { cmdServiceUuid, cmdUuid, imgServiceUuid, imgUuid } = await findCommandAndImageCharacteristics(conn);
79
82
  const ack = new AckChannel();
@@ -87,6 +90,7 @@ class GiciskyDriver {
87
90
  if (!started?.ok) {
88
91
  throw new Error(`gicisky device rejected start-image-transfer request: ${startImageAck.toString("hex")}`);
89
92
  }
93
+ log(`uploading ${payload.length} bytes in ${Math.ceil(payload.length / chunkSize)} parts`);
90
94
  let part = started.nextPart;
91
95
  let lastPart = -1;
92
96
  let repeats = 0;
@@ -115,6 +119,7 @@ class GiciskyDriver {
115
119
  }
116
120
  part = decoded.nextPart;
117
121
  }
122
+ log("upload complete");
118
123
  }
119
124
  finally {
120
125
  await conn.stopNotifications(cmdServiceUuid, cmdUuid).catch(() => { });
@@ -83,6 +83,11 @@ export interface VendorDeviceConfig {
83
83
  compress?: boolean;
84
84
  /** Overrides the model's own wire format - see `CompressionFormat`. Defaults to `"auto"`. */
85
85
  compressionFormat?: CompressionFormat;
86
+ /**
87
+ * Receives one line per paint step (connecting, connected, uploading, ...), so a paint that stalls
88
+ * shows in the log where it stopped. Omitted means no step logging.
89
+ */
90
+ log?: (message: string) => void;
86
91
  /**
87
92
  * How `paint()` reaches the device's BLE hardware - omitted (always true for the CLI, which has no
88
93
  * `ServerAPI`/`app.bleApi` to source one from) means direct BlueZ access via a fresh
@@ -50,8 +50,11 @@ class ZhsunycoDriver {
50
50
  }
51
51
  async paint(bitmap, config) {
52
52
  const aesKey = (0, protocol_1.resolveAesKey)(config.aesKey);
53
+ const log = config.log ?? (() => { });
53
54
  const backend = config.gattBackend ?? (0, bleBackend_1.nodeBleBackend)();
55
+ log("connecting");
54
56
  const conn = await backend.connectGatt(config.address, config.connectTimeoutMs ?? DEFAULT_PAINT_CONNECT_TIMEOUT_MS);
57
+ log("connected");
55
58
  try {
56
59
  const info = (0, protocol_1.decodeAdvertisedInfo)(await conn.read(protocol_1.WOLINK_SERVICE_UUID, protocol_1.WOLINK_CHARACTERISTIC_UUIDS.config));
57
60
  if (!info) {
@@ -79,27 +82,34 @@ class ZhsunycoDriver {
79
82
  const challenge = await conn.read(protocol_1.WOLINK_SERVICE_UUID, protocol_1.WOLINK_CHARACTERISTIC_UUIDS.authenticate);
80
83
  await conn.write(protocol_1.WOLINK_SERVICE_UUID, protocol_1.WOLINK_CHARACTERISTIC_UUIDS.authenticate, (0, protocol_1.authResponse)(challenge, aesKey), false);
81
84
  await (0, bleDiscovery_1.sleep)(AUTH_SETTLE_DELAY_MS);
85
+ log("authenticated");
82
86
  const framed = (0, mirror_1.mirrorBitmap)((0, reframe_1.reframeBitmap)(bitmap, metadata.width, metadata.height - metadata.voffset, config.reframe ?? "crop"), config.mirror ?? "none");
83
87
  const pixelData = (0, encode_1.encodeBitmap)(framed, metadata);
84
88
  const compress = config.compress ?? true;
85
89
  // Upload offsets and the refresh length both count bytes of whatever's actually sent - the
86
90
  // compressed payload when compressing, not the raw buffer it inflates back to.
87
91
  const payload = compress ? (0, compression_1.compressWolinkBlocks)(pixelData) : pixelData;
92
+ log(`uploading ${payload.length} bytes${compress ? ` (compressed from ${pixelData.length})` : ""} in ` +
93
+ `${Math.ceil(payload.length / UPLOAD_CHUNK_SIZE)} writes`);
88
94
  for (let offset = 0; offset < payload.length; offset += UPLOAD_CHUNK_SIZE) {
89
95
  const chunk = payload.subarray(offset, offset + UPLOAD_CHUNK_SIZE);
90
96
  await conn.write(protocol_1.WOLINK_SERVICE_UUID, protocol_1.WOLINK_CHARACTERISTIC_UUIDS.data, Buffer.concat([(0, protocol_1.commandHeader)(protocol_1.COMMAND.uploadBlock, offset), chunk]), true);
91
97
  await (0, bleDiscovery_1.sleep)(CHUNK_WRITE_DELAY_MS);
92
98
  }
93
99
  await conn.write(protocol_1.WOLINK_SERVICE_UUID, protocol_1.WOLINK_CHARACTERISTIC_UUIDS.data, (0, protocol_1.commandHeader)(compress ? protocol_1.COMMAND.refreshCompressed : protocol_1.COMMAND.refreshUncompressed, payload.length), true);
100
+ log("refresh sent, waiting for the label to finish");
94
101
  // `Promise.race` can't cancel its loser, so once `statusReceived` settles (the common case)
95
102
  // this timer would otherwise sit alive for the rest of its 60s regardless - see the identical
96
103
  // reasoning on `readDeviceDetails`'s own race, below.
97
104
  let statusTimer;
98
105
  const statusTimeout = new Promise((resolve) => {
99
- statusTimer = setTimeout(resolve, STATUS_WAIT_TIMEOUT_MS);
106
+ statusTimer = setTimeout(() => resolve("timeout"), STATUS_WAIT_TIMEOUT_MS);
100
107
  });
101
108
  try {
102
- await Promise.race([statusReceived, statusTimeout]);
109
+ const outcome = await Promise.race([statusReceived, statusTimeout]);
110
+ log(outcome === "timeout"
111
+ ? `no reply from the label within ${STATUS_WAIT_TIMEOUT_MS}ms - assuming it painted`
112
+ : "label reported the paint complete");
103
113
  }
104
114
  finally {
105
115
  clearTimeout(statusTimer);
@@ -25,6 +25,13 @@ const INTERVAL_POLL_MS = 60000;
25
25
  const SUBSCRIPTION_DEBOUNCE_MS = 2000;
26
26
  /** Pause between paint attempts - see `withRetries`' `delayMs`. */
27
27
  const PAINT_RETRY_DELAY_MS = 5000;
28
+ /**
29
+ * Last-resort limit on one paint attempt, on top of its connect timeout - so an attempt stuck on
30
+ * anything, anywhere, still fails and gets retried instead of blocking this label (and, via
31
+ * `exclusiveBleManagerAccess`, every other label) forever. Longer than every inner limit it backs up,
32
+ * the longest being the 5-minute GATT session watchdog in `bleBackend.ts`.
33
+ */
34
+ const PAINT_ATTEMPT_BACKSTOP_MS = 7 * 60000;
28
35
  const RESOURCES_API_PATH = "/signalk/v2/api/resources";
29
36
  /**
30
37
  * The most recent wall-clock instant at or before `now` that an `interval`-triggered device's own
@@ -358,7 +365,7 @@ async function considerRepaint(app, config, device, target, state, getApiUrl) {
358
365
  const paintWithRetries = () => (0, bleDiscovery_1.withRetries)(attempts, async (attempt) => {
359
366
  app.debug(`${label}: attempting paint ${attempt}/${attempts}`);
360
367
  const startedAt = Date.now();
361
- await driver.paint(bitmap, {
368
+ const paint = driver.paint(bitmap, {
362
369
  address,
363
370
  pid: target.pid,
364
371
  aesKey: device.advanced?.aesKey,
@@ -368,7 +375,9 @@ async function considerRepaint(app, config, device, target, state, getApiUrl) {
368
375
  compress: device.advanced?.compress,
369
376
  compressionFormat: device.advanced?.compressionFormat,
370
377
  gattBackend,
378
+ log: (message) => app.debug(`${label}: ${message}`),
371
379
  });
380
+ await (0, bleDiscovery_1.withDeadline)(paint, connectTimeoutMs + PAINT_ATTEMPT_BACKSTOP_MS, `paint attempt ${attempt}/${attempts}`);
372
381
  paintDurationMs = Date.now() - startedAt;
373
382
  }, {
374
383
  delayMs: PAINT_RETRY_DELAY_MS,
package/docs/bluetooth.md CHANGED
@@ -2,11 +2,21 @@
2
2
 
3
3
  Advice for getting a reliable Bluetooth Low Energy (BLE) connection between the SignalK server and your labels.
4
4
 
5
- Most of this applies to direct BlueZ mode only. If the "Use the SignalK BLE Manager API" setting is enabled, the adapter and its lifecycle are managed by the SignalK server, under its own Bluetooth admin settings, once for every BLE-consuming plugin.
5
+ Bluetooth can be used in of three ways by a plugin:
6
+
7
+ - Direct access to dongle (usually `hci0` device). Not recommended
8
+ - Access via `bluez` and `dbus` services. Better but not ideal
9
+ - Using the BLE Manager added to SignalK in 2026. Recommended with caveats.
10
+ - "Use the SignalK BLE Manager API" setting is enabled, the adapter and its lifecycle are managed by the SignalK server, under its own Bluetooth admin settings, once for every BLE-consuming plugin.
11
+ - Under the hood, this uses `bluez` and `dbus` however manages them so that plugins are controlled in how they can impact each other
12
+ - It also has a very useful GUI for seeing all scanned devices, and all GATT claims (GATT being the protocol for directly connecting to BLE devices).
13
+ - This is still new and settling down, so not yet the default for this plugin
14
+
15
+ The advice here is general to Bluetooth on Linux, whichever way its being used.
6
16
 
7
17
  ## Choosing a Bluetooth Adapter
8
18
 
9
- Only needed in direct BlueZ mode - in BLE Manager mode the adapter is whatever the SignalK server's own Bluetooth settings provide.
19
+ First step is having a Bluetooth Low Energy (BLE) compatible bluetooth adapter available.
10
20
 
11
21
  - Bluetooth adapters for Linux can be tricky
12
22
  - TP-Link UB400 and Asus USB-BT500 are two well-known and available ones, though the ASUS USB-BT500 one can have problems with some Pi type boards (see [Adapter stops responding](#adapter-stops-responding-no-gpio-to-reset))
@@ -18,10 +28,14 @@ Only needed in direct BlueZ mode - in BLE Manager mode the adapter is whatever t
18
28
  > - Don't worry about the very latest Bluetooth versions, 4.0 is minimum for BLE, 5.0 is nice
19
29
  > - Home Assistant is massively more popular than SignalK, and often also run on Raspberry Pi and similar, so good source of advice
20
30
 
31
+ SignalK BLE Manager also supports BLE Gateways, which could be an MQTT topic or an ESP-32 device. The [espos-ble-gateway](https://github.com/dirkwa/espos-ble-gateway) can be used with a cheap ESP32 device (see the list of supported hardware), which allows positioning of the gateway closer to devices, or having multiple gateways on a big boat.
32
+
21
33
  ## Weak Signal
22
34
 
23
35
  If the label is too far from the SignalK server's adapter, try a BLE proxy device - ESP32 is popular for this - or, with the BLE Manager API, a remote BLE gateway.
24
36
 
37
+ If your dongle is plugged into a USB3 port (usually blue-highlighted), then there's a good chance the [infamous USB3 interference on the 2.4Ghz spectrum](https://www.usb.org/sites/default/files/327216.pdf) is impacting your adapter. Switch to a USB2 port if you have one, or better, use a USB extension cable to position the dongle far away.
38
+
25
39
  ## Bluetooth Plugins Impacting Each Other
26
40
 
27
41
  Bluetooth plugins can kick off scanning, and otherwise interfere with each other. Worst case is when plugins attempt to connect directly to Bluetooth adapters. Better is when they connect using `bluez` and `dbus` Linux components, and best of all when they use the SignalK BLE Manager added in 2026.
@@ -84,7 +98,11 @@ Bluetooth: hci0: command 0x2042 tx timeout
84
98
  Bluetooth: hci0: Opcode 0x2042 failed: -110
85
99
  ```
86
100
 
87
- This happens when USB autosuspend cycles the dongle in and out of low-power suspend while idle. When bluetoothd sends an HCI command while the device is suspended or mid-resume, it never gets answered — adapters like the popular ASUS USB-500 lack a GPIO to allow reset and its stuck, and spams logs.
101
+ Adapters like the popular ASUS USB-500 lack a GPIO to allow reset when suspended and it gets stuck, spamming the logs. See [Adapter Goes to Sleep](#adapter-goes-to-sleep) for stopping the auto-suspend happening.
102
+
103
+ ## Adapter Goes to Sleep
104
+
105
+ This happens when USB autosuspend cycles the dongle in and out of low-power suspend while idle. When bluetoothd sends an HCI command while the device is suspended or mid-resume, it never gets answered
88
106
 
89
107
  ### Example udev rule fix
90
108
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rhizomatics/signalk-einklabel-plugin",
3
- "version": "1.3.0-beta13",
3
+ "version": "1.3.0-beta14",
4
4
  "description": "Display SignalK data on eInk Electronic Shelf Labels, includes working examples for tide clock and watch schedule.",
5
5
  "keywords": [
6
6
  "ble",