Skip to content

Commit

Permalink
Queue events while not synced
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleBoyer committed Aug 3, 2024
1 parent a757c11 commit bb32d23
Show file tree
Hide file tree
Showing 10 changed files with 93 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.27",
"version": "0.2.28",
"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
43 changes: 41 additions & 2 deletions src/accessory/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export const correctAPIBaseURL = (inputURL: string) => {
export class BaseBlaQAccessory implements BaseBlaQAccessoryInterface {
protected apiBaseURL: string;
protected firmwareVersion?: string;
protected synced?: boolean;
protected queuedEvents: {
type: 'state' | 'log' | 'ping';
event: StateUpdateMessageEvent | LogMessageEvent | PingMessageEvent;
}[] = [];

protected readonly accessory: PlatformAccessory;
protected readonly accessoryInformationService: Service;
protected readonly logger: Logger;
Expand Down Expand Up @@ -83,17 +89,50 @@ export class BaseBlaQAccessory implements BaseBlaQAccessoryInterface {
}
}

handleStateEvent(stateEvent: StateUpdateMessageEvent){
processQueuedEvents() {
while(this.queuedEvents.length){
const event = this.queuedEvents.shift()!;
const funcToCall = {
'ping': (this as BaseBlaQAccessoryInterface).handlePingEvent?.bind(this),
'log': (this as BaseBlaQAccessoryInterface).handleLogEvent?.bind(this),
'state': (this as BaseBlaQAccessoryInterface).handleStateEvent?.bind(this),
}[event.type];
if(funcToCall){
funcToCall(event.event);
}
}
}

handlePingEvent(logEvent: LogMessageEvent){
if(!this.synced){
this.queuedEvents.push({type: 'ping', event: logEvent});
}
}

handleLogEvent(logEvent: LogMessageEvent){
if(!this.synced){
this.queuedEvents.push({type: 'log', event: logEvent});
}
}

handleStateEvent(stateEvent: StateUpdateMessageEvent): void {
try {
const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord;
if (['text_sensor-esphome_version', 'text_sensor-firmware_version'].includes(stateInfo.id)) {
if (['binary_sensor-synced'].includes(stateInfo.id)) {
this.synced = stateInfo.value as boolean | undefined;
if(this.synced){
this.processQueuedEvents();
}
}else if (['text_sensor-esphome_version', 'text_sensor-firmware_version'].includes(stateInfo.id)) {
const b = stateInfo as BlaQTextSensorEvent;
if (b.value && b.value === b.state) {
this.setFirmwareVersion(b.value);
} else {
this.logger.error('Mismatched firmware versions in value/state:', b.value, b.state);
this.firmwareVersion = undefined;
}
} else if(!this.synced){
this.queuedEvents.push({type: 'state', event: stateEvent});
}
} catch(e) {
this.logger.error('Cannot deserialize message:', stateEvent);
Expand Down
7 changes: 7 additions & 0 deletions src/accessory/garage-door.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ export class BlaQGarageDoorAccessory extends BaseBlaQAccessory {

handleStateEvent(stateEvent: StateUpdateMessageEvent){
super.handleStateEvent(stateEvent);
if(!this.synced){
return;
}
try {
const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord;
if (['cover-garage_door', 'cover-door'].includes(stateInfo.id)) {
Expand Down Expand Up @@ -379,6 +382,10 @@ export class BlaQGarageDoorAccessory extends BaseBlaQAccessory {
}

handleLogEvent(logEvent: LogMessageEvent){
super.handleLogEvent(logEvent);
if(!this.synced){
return;
}
try {
const logStr = logEvent.data;
const lowercaseLogStr = logStr.toLowerCase();
Expand Down
7 changes: 7 additions & 0 deletions src/accessory/garage-learn-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export class BlaQGarageLearnModeAccessory extends BaseBlaQAccessory {

handleStateEvent(stateEvent: StateUpdateMessageEvent){
super.handleStateEvent(stateEvent);
if(!this.synced){
return;
}
try {
const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord;
if (['switch-learn'].includes(stateInfo.id)) {
Expand All @@ -68,6 +71,10 @@ export class BlaQGarageLearnModeAccessory extends BaseBlaQAccessory {
}

handleLogEvent(logEvent: LogMessageEvent){
super.handleLogEvent(logEvent);
if(!this.synced){
return;
}
try {
const logStr = logEvent.data;
const lowercaseLogStr = logStr.toLowerCase();
Expand Down
7 changes: 7 additions & 0 deletions src/accessory/garage-light.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ export class BlaQGarageLightAccessory extends BaseBlaQAccessory {

handleStateEvent(stateEvent: StateUpdateMessageEvent){
super.handleStateEvent(stateEvent);
if(!this.synced){
return;
}
try {
const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord;
if (['light-garage_light', 'light-light'].includes(stateInfo.id)) {
Expand All @@ -74,6 +77,10 @@ export class BlaQGarageLightAccessory extends BaseBlaQAccessory {
}

handleLogEvent(logEvent: LogMessageEvent){
super.handleLogEvent(logEvent);
if(!this.synced){
return;
}
try {
const logStr = logEvent.data;
const lowercaseLogStr = logStr.toLowerCase();
Expand Down
7 changes: 7 additions & 0 deletions src/accessory/garage-lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ export class BlaQGarageLockAccessory extends BaseBlaQAccessory {

handleStateEvent(stateEvent: StateUpdateMessageEvent){
super.handleStateEvent(stateEvent);
if(!this.synced){
return;
}
try {
const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord;
if (['lock-lock', 'lock-lock_remotes'].includes(stateInfo.id)) {
Expand All @@ -87,6 +90,10 @@ export class BlaQGarageLockAccessory extends BaseBlaQAccessory {
}

handleLogEvent(logEvent: LogMessageEvent){
super.handleLogEvent(logEvent);
if(!this.synced){
return;
}
try {
const logStr = logEvent.data;
const lowercaseLogStr = logStr.toLowerCase();
Expand Down
7 changes: 7 additions & 0 deletions src/accessory/garage-motion-sensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export class BlaQGarageMotionSensorAccessory extends BaseBlaQAccessory {

handleStateEvent(stateEvent: StateUpdateMessageEvent){
super.handleStateEvent(stateEvent);
if(!this.synced){
return;
}
try {
const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord;
if (['binary_sensor-motion'].includes(stateInfo.id)) {
Expand All @@ -61,6 +64,10 @@ export class BlaQGarageMotionSensorAccessory extends BaseBlaQAccessory {
}

handleLogEvent(logEvent: LogMessageEvent){
super.handleLogEvent(logEvent);
if(!this.synced){
return;
}
try {
const logStr = logEvent.data;
const lowercaseLogStr = logStr.toLowerCase();
Expand Down
7 changes: 7 additions & 0 deletions src/accessory/garage-obstruction-sensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export class BlaQGarageObstructionSensorAccessory extends BaseBlaQAccessory {

handleStateEvent(stateEvent: StateUpdateMessageEvent){
super.handleStateEvent(stateEvent);
if(!this.synced){
return;
}
try {
const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord;
if (['binary_sensor-obstruction'].includes(stateInfo.id)) {
Expand All @@ -63,6 +66,10 @@ export class BlaQGarageObstructionSensorAccessory extends BaseBlaQAccessory {
}

handleLogEvent(logEvent: LogMessageEvent){
super.handleLogEvent(logEvent);
if(!this.synced){
return;
}
try {
const logStr = logEvent.data;
const lowercaseLogStr = logStr.toLowerCase();
Expand Down
7 changes: 7 additions & 0 deletions src/accessory/garage-pre-close-warning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export class BlaQGaragePreCloseWarningAccessory extends BaseBlaQAccessory {

handleStateEvent(stateEvent: StateUpdateMessageEvent){
super.handleStateEvent(stateEvent);
if(!this.synced){
return;
}
try {
const stateInfo = JSON.parse(stateEvent.data) as StateUpdateRecord;
if (['button-pre-close_warning'].includes(stateInfo.id)) {
Expand All @@ -67,6 +70,10 @@ export class BlaQGaragePreCloseWarningAccessory extends BaseBlaQAccessory {
}

handleLogEvent(logEvent: LogMessageEvent){
super.handleLogEvent(logEvent);
if(!this.synced){
return;
}
try {
const logStr = logEvent.data;
const lowercaseLogStr = logStr.toLowerCase();
Expand Down

0 comments on commit bb32d23

Please sign in to comment.