Skip to content

Commit

Permalink
Cache events before accessory init
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleBoyer committed Jul 23, 2024
1 parent 954ddf3 commit 68e2206
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": false,
"displayName": "Konnected BlaQ",
"name": "homebridge-blaq",
"version": "0.2.19",
"version": "0.2.20",
"description": "Control and view your garage door(s) remotely with real-time updates using Konnected's BlaQ hardware",
"license": "Apache-2.0",
"type": "module",
Expand Down
31 changes: 29 additions & 2 deletions src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export class BlaQHub {
private friendlyName?: string;
private deviceMac?: string;
private port: number;
private eventsBeforeAccessoryInit: {
type: 'state' | 'log' | 'ping';
event: StateUpdateMessageEvent | LogMessageEvent | PingMessageEvent;
}[] = [];

private readonly initAccessoryCallback: BlaQInitAccessoryCallback;
private readonly logger: Logger;

Expand Down Expand Up @@ -96,13 +101,29 @@ export class BlaQHub {
friendlyName: this.friendlyName,
serialNumber: this.deviceMac,
});
this.logger.debug('[init] Accessories initialized!');
this.initialized = true;
this.reinitializeEventSource();
this.eventsBeforeAccessoryInit.forEach(oldEvent => {
const getFuncToCall = {
'ping': (accessory: BaseBlaQAccessoryInterface) => accessory.handlePingEvent?.bind(accessory),
'log': (accessory: BaseBlaQAccessoryInterface) => accessory.handleLogEvent?.bind(accessory),
'state': (accessory: BaseBlaQAccessoryInterface) => accessory.handleStateEvent?.bind(accessory),
}[oldEvent.type];
this.accessories.forEach(accessory => {
const funcToCall = getFuncToCall(accessory);
if(funcToCall){
funcToCall(oldEvent.event);
}
});
});
this.eventsBeforeAccessoryInit = [];
this.logger.debug('[init] Accessories initialized!');
}
}

private handleStateUpdate(msg: StateUpdateMessageEvent){
if(!this.initialized){
this.eventsBeforeAccessoryInit.push({ type: 'state', event: msg });
}
if (!this.initialized && msg.data !== '' ) {
try {
const b = JSON.parse(msg.data) as BlaQTextSensorEvent;
Expand All @@ -125,6 +146,9 @@ export class BlaQHub {
}

private handleLogUpdate(msg: LogMessageEvent){
if(!this.initialized){
this.eventsBeforeAccessoryInit.push({ type: 'log', event: msg });
}
this.logger.debug('BlaQ log:', msg.data);
this.accessories.forEach(accessory => {
if(accessory.handleLogEvent){
Expand Down Expand Up @@ -203,6 +227,9 @@ export class BlaQHub {
}

private handlePingUpdate(msg: PingMessageEvent){
if(!this.initialized){
this.eventsBeforeAccessoryInit.push({ type: 'ping', event: msg });
}
if (!this.initialized && msg.data !== '' ) {
try {
const b = JSON.parse(msg.data) as BlaQPingEvent;
Expand Down

0 comments on commit 68e2206

Please sign in to comment.