homebridge-bedjet 0.1.4 → 0.1.6

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "homebridge-bedjet",
3
3
  "displayName": "BedJet",
4
- "version": "0.1.4",
4
+ "version": "0.1.6",
5
5
  "description": "Homebridge plugin for BedJet V3 via Bluetooth LE",
6
6
  "license": "MIT",
7
7
  "main": "dist/index.js",
@@ -152,14 +152,20 @@ export class BedJet extends EventEmitter {
152
152
  await peripheral.connectAsync();
153
153
  this.log.info(`[${this.config.name}] Connected — discovering services…`);
154
154
 
155
- // Wrap service discovery in a timeout — it hangs indefinitely on Linux/BlueZ
156
- // if the adapter is busy or the device is in a bad state.
157
- const { characteristics } = await Promise.race([
158
- peripheral.discoverAllServicesAndCharacteristicsAsync(),
159
- new Promise<never>((_, reject) =>
160
- setTimeout(() => reject(new Error(`[${this.config.name}] Service discovery timed out`)), 10_000),
161
- ),
162
- ]);
155
+ // discoverAllServicesAndCharacteristicsAsync can hang on Linux/BlueZ even after
156
+ // returning characteristics use the callback API directly instead.
157
+ const characteristics = await new Promise<Characteristic[]>((resolve, reject) => {
158
+ const timer = setTimeout(() =>
159
+ reject(new Error(`[${this.config.name}] Service discovery timed out`)), 10_000);
160
+ peripheral.discoverAllServicesAndCharacteristics((err, _services, chars) => {
161
+ clearTimeout(timer);
162
+ if (err) {
163
+ reject(new Error(`[${this.config.name}] Discovery error: ${err}`));
164
+ } else {
165
+ resolve(chars ?? []);
166
+ }
167
+ });
168
+ });
163
169
 
164
170
  this.log.info(`[${this.config.name}] Found ${characteristics.length} characteristics`);
165
171
  for (const char of characteristics) {
@@ -180,12 +186,19 @@ export class BedJet extends EventEmitter {
180
186
 
181
187
  // Subscribe to notifications on the status characteristic
182
188
  this.log.info(`[${this.config.name}] Subscribing to notifications…`);
183
- await Promise.race([
184
- this.statusChar.subscribeAsync(),
185
- new Promise<never>((_, reject) =>
186
- setTimeout(() => reject(new Error(`[${this.config.name}] Subscribe timed out`)), 10_000),
187
- ),
188
- ]);
189
+ const statusChar = this.statusChar;
190
+ await new Promise<void>((resolve, reject) => {
191
+ const timer = setTimeout(() =>
192
+ reject(new Error(`[${this.config.name}] Subscribe timed out`)), 10_000);
193
+ statusChar.subscribe((err) => {
194
+ clearTimeout(timer);
195
+ if (err) {
196
+ reject(new Error(`[${this.config.name}] Subscribe error: ${err}`));
197
+ } else {
198
+ resolve();
199
+ }
200
+ });
201
+ });
189
202
  this.log.info(`[${this.config.name}] Subscribed — reading device status…`);
190
203
  this.statusChar.on('data', (data: Buffer) => this._handleNotification(data));
191
204