Skip to content

Commit

Permalink
Reset sync state when reconnecting; debounce set/update cmds for 100ms
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleBoyer committed Aug 4, 2024
1 parent 8e2e40b commit 2adc9fa
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
],
"dependencies": {
"bonjour-service": "^1.2.1",
"debounce": "^2.1.0",
"eventsource": "^2.0.2",
"node-fetch": "^3.3.2",
"strip-ansi": "^7.1.0"
Expand Down
31 changes: 29 additions & 2 deletions src/accessory/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CharacteristicValue, Logger, PlatformAccessory, Service, WithUUID } from 'homebridge';
import debounce, { DebouncedFunction } from 'debounce';
import { LogMessageEvent, PingMessageEvent, StateUpdateMessageEvent, StateUpdateRecord } from '../utils/eventsource';
import { BlaQHomebridgePluginPlatform } from '../platform';
import { BlaQTextSensorEvent } from '../types';
Expand All @@ -8,6 +9,7 @@ export interface BaseBlaQAccessoryInterface {
handleStateEvent: (stateEvent: StateUpdateMessageEvent) => void;
handleLogEvent?: (logEvent: LogMessageEvent) => void;
handlePingEvent?: (pingEvent: PingMessageEvent) => void;
resetSyncState: () => void;
}

export type BaseBlaQAccessoryConstructorParams = {
Expand All @@ -29,6 +31,11 @@ export const correctAPIBaseURL = (inputURL: string) => {
return correctedAPIBaseURL;
};

type DebouncedService = (typeof Service | Service) & {
setCharacteristic: DebouncedFunction<(name: string, value: CharacteristicValue) => Service>;
updateCharacteristic: DebouncedFunction<(name: string, value: CharacteristicValue) => Service>;
};

export class BaseBlaQAccessory implements BaseBlaQAccessoryInterface {
protected apiBaseURL: string;
protected firmwareVersion?: string;
Expand Down Expand Up @@ -77,9 +84,10 @@ export class BaseBlaQAccessory implements BaseBlaQAccessoryInterface {
return this.constructor.name;
}

protected getOrAddService(service: WithUUID<typeof Service> | Service): Service{
return this.accessory.getService(service as WithUUID<typeof Service>) ||
protected getOrAddService(service: WithUUID<typeof Service> | Service): Service {
const retService = this.accessory.getService(service as WithUUID<typeof Service>) ||
this.accessory.addService(service as Service);
return this.debounceService(retService);
}

protected removeService(service: WithUUID<typeof Service> | Service): void{
Expand All @@ -89,6 +97,21 @@ export class BaseBlaQAccessory implements BaseBlaQAccessoryInterface {
}
}

protected debounceService(service: Service): Service {
const originalSet = service.setCharacteristic.bind(service);
const originalUpdate = service.updateCharacteristic.bind(service);
const retService: DebouncedService = service as DebouncedService;
retService.setCharacteristic = debounce(
(name: string, value: CharacteristicValue) => originalSet(name, value),
100,
);
retService.updateCharacteristic = debounce(
(name: string, value: CharacteristicValue) => originalUpdate(name, value),
100,
);
return retService as Service;
}

processQueuedEvents() {
while(this.queuedEvents.length){
const event = this.queuedEvents.shift()!;
Expand All @@ -103,6 +126,10 @@ export class BaseBlaQAccessory implements BaseBlaQAccessoryInterface {
}
}

resetSyncState(){
this.synced = false;
}

handlePingEvent(pingEvent: PingMessageEvent){
if(!this.synced){
this.queuedEvents.push({type: 'ping', event: pingEvent});
Expand Down
1 change: 1 addition & 0 deletions src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export class BlaQHub {
if(this.eventSource){
this.eventSource.close();
}
this.accessories.forEach(accessory => accessory.resetSyncState());
this.eventSource = new AutoReconnectingEventSource({
host: this.host,
port: this.port,
Expand Down

0 comments on commit 2adc9fa

Please sign in to comment.