Skip to content

Commit b4734c4

Browse files
committed
Improved error handling
1 parent 4be49da commit b4734c4

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ All notable changes to `@homebridge/hap-client` will be documented in this file.
1111
- Added new setCharacteristic option, setCharacteristicByType which allows finding the characteristic by type.
1212
- Added new getResource request to retrieve snapshot images from camera's
1313
- Minor tweak to serviceName, and if the name is blank, use the name value from Accessory Information
14+
- Fixed issue of error handler triggering error when contacting a homebridge instance that is down
15+
- Added restart of monitor when client connections close
1416

1517
## v2.0.4 (2024-11-07)
1618

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ export class HapClient extends EventEmitter {
470470
if (this.logger) {
471471
this.logger.error(`[HapClient] [${service.instance.ipAddress}:${service.instance.port} (${service.instance.username})] ` +
472472
`Failed to set value for ${service.serviceName}.`);
473-
if (e.response && e.response.status === 470 || e.response.status === 401) {
473+
if (e.response && e.response?.status === 470 || e.response?.status === 401) {
474474
this.logger.warn(`[HapClient] [${service.instance.ipAddress}:${service.instance.port} (${service.instance.username})] ` +
475475
`Make sure Homebridge pin for this instance is set to ${this.pin}.`);
476476
throw new Error(`Failed to control accessory. Make sure the Homebridge pin for ${service.instance.ipAddress}:${service.instance.port} ` +
@@ -514,7 +514,7 @@ export class HapClient extends EventEmitter {
514514
if (this.logger) {
515515
this.logger.error(`[HapClient] [${service.instance.ipAddress}:${service.instance.port} (${service.instance.username})] ` +
516516
`Failed to request resource from accessory ${service.serviceName}.`);
517-
if (e.response && e.response.status === 470 || e.response.status === 401) {
517+
if (e.response && e.response?.status === 470 || e.response?.status === 401) {
518518
this.logger.warn(`[HapClient] [${service.instance.ipAddress}:${service.instance.port} (${service.instance.username})] ` +
519519
`Make sure Homebridge pin for this instance is set to ${this.pin}.`);
520520
throw new Error(`Failed to request resource from accessory. Make sure the Homebridge pin for ${service.instance.ipAddress}:${service.instance.port} ` +

src/monitor.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { EventEmitter } from 'node:events';
22

3-
import { ServiceType, HapEvInstance } from './interfaces';
43
import { createConnection, parseMessage } from './eventedHttpClient';
4+
import { HapEvInstance, ServiceType } from './interfaces';
55

66
/**
77
* HapMonitor - Creates a monitor to watch for changes in accessory characteristics. And generates 'service-update' events when they change.
@@ -76,6 +76,18 @@ export class HapMonitor extends EventEmitter {
7676
}
7777
}
7878
});
79+
let closeTimeout: NodeJS.Timeout | null = null;
80+
instance.socket.on('close', (data) => {
81+
this.emit('monitor-close', data);
82+
if (closeTimeout) {
83+
clearTimeout(closeTimeout); // Clear the existing timeout
84+
}
85+
closeTimeout = setTimeout(() => {
86+
this.finish();
87+
this.start();
88+
closeTimeout = null; // Reset the timeout
89+
}, 10000); // 10-second debounce period
90+
});
7991
}
8092
}
8193

0 commit comments

Comments
 (0)