From 455ad5ac1187dd6749291fb91e6317a3a256baf0 Mon Sep 17 00:00:00 2001 From: Northern Man <19808920+NorthernMan54@users.noreply.github.com> Date: Thu, 7 Nov 2024 09:33:11 -0500 Subject: [PATCH 1/6] Updated functions --- CHANGELOG.md | 7 +++++++ package-lock.json | 4 ++-- package.json | 4 ++-- src/index.ts | 26 ++++++++++++++++++++++---- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0424f0..0199631 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to `@homebridge/hap-client` will be documented in this file. This project tries to adhere to [Semantic Versioning](http://semver.org/). +## v2.0.4 (2024-11-07) + +### Changed + +- Added public method destroy, to be used for testing +- Update public method monitorCharacteristics to allow passing of a filtered services array. + ## v2.0.2 (2024-08-31) ### Changed diff --git a/package-lock.json b/package-lock.json index 293e049..9712c67 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@homebridge/hap-client", - "version": "2.0.2", + "version": "2.0.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@homebridge/hap-client", - "version": "2.0.2", + "version": "2.0.4", "license": "MIT", "dependencies": { "axios": "1.7.6", diff --git a/package.json b/package.json index 4c2cb51..952cd71 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@homebridge/hap-client", - "version": "2.0.2", + "version": "2.0.4", "description": "A client for HAP-NodeJS.", "main": "./dist/index.js", "scripts": { @@ -55,4 +55,4 @@ "ts-node": "^10.9.2", "typescript": "^5.5.4" } -} +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 8cfd9c8..978bc4e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,6 +37,9 @@ export class HapClient extends EventEmitter { Characteristics.Name, ]; + private resetInstancePoolTimeout: NodeJS.Timeout | undefined = undefined + private startDiscoveryTimeout: NodeJS.Timeout | undefined = undefined + constructor(opts: { pin: string; logger?: any; @@ -66,7 +69,7 @@ export class HapClient extends EventEmitter { this.instances = []; - setTimeout(() => { + this.resetInstancePoolTimeout = setTimeout(() => { this.refreshInstances(); }, 6000); } @@ -94,7 +97,7 @@ export class HapClient extends EventEmitter { this.debug(`[HapClient] Discovery :: Started`); // stop discovery after 20 seconds - setTimeout(() => { + this.startDiscoveryTimeout = setTimeout(() => { this.browser.stop(); this.debug(`[HapClient] Discovery :: Ended`); this.discoveryInProgress = false; @@ -225,8 +228,9 @@ export class HapClient extends EventEmitter { return accessories; } - public async monitorCharacteristics() { - const services = await this.getAllServices(); + public async monitorCharacteristics(services?: ServiceType[]) { + // If `services` is not provided, retrieve all services + services = services ?? await this.getAllServices(); return new HapMonitor(this.logger, this.debug.bind(this), this.pin, services); } @@ -447,4 +451,18 @@ export class HapClient extends EventEmitter { return titleize(decamelize(string)); } + /** + * Destroy the HapClient instance, used for testing + */ + public destroy() { + this.browser?.stop() + this.discoveryInProgress = false + if (this.resetInstancePoolTimeout) { + clearTimeout(this.resetInstancePoolTimeout) + } + if (this.startDiscoveryTimeout) { + clearTimeout(this.startDiscoveryTimeout) + } + } + } From 0873ebbbfa821fa447058d0ead6c6b3c848d3870 Mon Sep 17 00:00:00 2001 From: Northern Man <19808920+NorthernMan54@users.noreply.github.com> Date: Thu, 7 Nov 2024 09:43:32 -0500 Subject: [PATCH 2/6] Fix interface --- src/interfaces.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/interfaces.ts b/src/interfaces.ts index 79d4f60..9f953f5 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -75,12 +75,7 @@ export interface ServiceType { setCharacteristic?: (iid: number, value: number | string | boolean) => Promise; getCharacteristic?: (type: string) => CharacteristicType; values: any; - instance: { - ipAddress: string; - port: number; - username: string; - name: string; - }; + instance: HapInstance; uniqueId?: string; } From 01f15399af65a13ecc4eaa061f1e5e05a0aecb53 Mon Sep 17 00:00:00 2001 From: Northern Man <19808920+NorthernMan54@users.noreply.github.com> Date: Thu, 7 Nov 2024 09:51:08 -0500 Subject: [PATCH 3/6] Fixed --- src/interfaces.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/interfaces.ts b/src/interfaces.ts index 9f953f5..30d69f2 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -20,12 +20,7 @@ export interface HapEvInstance { export interface HapAccessoriesRespType { accessories: Array<{ - instance: { - ipAddress: string; - port: number; - username: string; - name: string; - }; + instance: HapInstance; aid: number; services: Array<{ iid: number; From d08989d7364a32bd2fa55f3af1c3be51a560d76b Mon Sep 17 00:00:00 2001 From: Northern Man <19808920+NorthernMan54@users.noreply.github.com> Date: Fri, 8 Nov 2024 09:20:26 -0500 Subject: [PATCH 4/6] Add comments and monitor to destroy --- README.md | 7 +++++++ src/index.ts | 36 ++++++++++++++++++++++++++++-------- src/monitor.ts | 3 +++ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ebf28f5..0f4b9d3 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,13 @@ A client for an insecure HAP-NodeJS instance. +# Dependant Applications + +- homebridge-config-ui-x +- homebridge-gsh + +(NPM Dependants)[https://www.npmjs.com/package/@homebridge/hap-client?activeTab=dependents] + ## Credits - HAP Client was originally created by [oznu](https://github.com/oznu). diff --git a/src/index.ts b/src/index.ts index 978bc4e..7c81742 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,8 +37,9 @@ export class HapClient extends EventEmitter { Characteristics.Name, ]; - private resetInstancePoolTimeout: NodeJS.Timeout | undefined = undefined - private startDiscoveryTimeout: NodeJS.Timeout | undefined = undefined + private resetInstancePoolTimeout: NodeJS.Timeout | undefined = undefined; + private startDiscoveryTimeout: NodeJS.Timeout | undefined = undefined; + private hapMonitor: HapMonitor; constructor(opts: { pin: string; @@ -60,6 +61,9 @@ export class HapClient extends EventEmitter { } } + /** + * resetInstancePool - Reset the instance pool, useful for when a Homebridge instance is restarted + */ public resetInstancePool() { if (this.discoveryInProgress) { this.browser.stop(); @@ -74,6 +78,9 @@ export class HapClient extends EventEmitter { }, 6000); } + /** + * refreshInstances - Refresh the instance pool + */ public refreshInstances() { if (!this.discoveryInProgress) { this.startDiscovery(); @@ -228,12 +235,24 @@ export class HapClient extends EventEmitter { return accessories; } + /** + * monitorCharacteristics + * @param services - Optional array of services to monitor + * + * Creates connections to all Homebridge instances and monitors all characteristics for changes. Will emit `service-update` events when characteristics change, which can be listened to. + * @returns + */ public async monitorCharacteristics(services?: ServiceType[]) { // If `services` is not provided, retrieve all services services = services ?? await this.getAllServices(); - return new HapMonitor(this.logger, this.debug.bind(this), this.pin, services); + this.hapMonitor = new HapMonitor(this.logger, this.debug.bind(this), this.pin, services); + return this.hapMonitor; } + /** + * + * @returns Array of all services from all Homebridge instances + */ public async getAllServices() { /* Get Accessories from HAP */ const accessories = await this.getAccessories(); @@ -452,11 +471,12 @@ export class HapClient extends EventEmitter { } /** - * Destroy the HapClient instance, used for testing - */ - public destroy() { - this.browser?.stop() - this.discoveryInProgress = false + * Destroy the HAP client, used by testing when shutting down + */ + public async destroy() { + this.browser?.stop(); + this.hapMonitor?.finish(); + this.discoveryInProgress = false; if (this.resetInstancePoolTimeout) { clearTimeout(this.resetInstancePoolTimeout) } diff --git a/src/monitor.ts b/src/monitor.ts index f384540..9015c7a 100644 --- a/src/monitor.ts +++ b/src/monitor.ts @@ -3,6 +3,9 @@ import { EventEmitter } from 'node:events'; import { ServiceType, HapEvInstance } from './interfaces'; import { createConnection, parseMessage } from './eventedHttpClient'; +/** + * HapMonitor - Creates a monitor to watch for changes in accessory characteristics. And generates 'service-update' events when they change. + */ export class HapMonitor extends EventEmitter { private pin; private evInstances: HapEvInstance[]; From 9cceb599a0c8d9bfb1e28fe92a53f96dc6b44138 Mon Sep 17 00:00:00 2001 From: Northern Man <19808920+NorthernMan54@users.noreply.github.com> Date: Fri, 8 Nov 2024 09:20:41 -0500 Subject: [PATCH 5/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0f4b9d3..825ff4e 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ A client for an insecure HAP-NodeJS instance. - homebridge-config-ui-x - homebridge-gsh -(NPM Dependants)[https://www.npmjs.com/package/@homebridge/hap-client?activeTab=dependents] +- [NPM Dependants](https://www.npmjs.com/package/@homebridge/hap-client?activeTab=dependents) ## Credits From 672185f4071aa4786b083fcf852482e8126f4a94 Mon Sep 17 00:00:00 2001 From: Northern Man <19808920+NorthernMan54@users.noreply.github.com> Date: Fri, 8 Nov 2024 09:27:21 -0500 Subject: [PATCH 6/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 825ff4e..678aa38 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ -A client for an insecure HAP-NodeJS instance. +A client for an insecure HAP-NodeJS instance. Provides a Typescript based interface based on the homekit accessory protocol, allowing the creation of clients able to connect to and control Homebridge devices. # Dependant Applications