diff --git a/package-lock.json b/package-lock.json index 9336482..8d67ea5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "homebridge-blaq", - "version": "0.2.19", + "version": "0.2.20", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "homebridge-blaq", - "version": "0.2.19", + "version": "0.2.20", "funding": [ { "type": "github", diff --git a/package.json b/package.json index 66e07dc..9ad8c7b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/hub.ts b/src/hub.ts index ad24588..9e6b47f 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -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; @@ -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; @@ -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){ @@ -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;