Skip to content

Commit

Permalink
fix(plugin): improve event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
johannrichard committed Nov 28, 2020
1 parent c3f5cfc commit 26a908d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 37 deletions.
22 changes: 9 additions & 13 deletions src/dingzAccessory.ts
Expand Up @@ -45,6 +45,7 @@ import {
import { DingzDaHomebridgePlatform } from './platform';
import { PlatformEvent } from './lib/platformEventBus';
import { DingzDaBaseAccessory } from './lib/dingzDaBaseAccessory';
import { AccessoryEvent } from './lib/accessoryEventBus';

// Policy for long running tasks, retry every hour
const retrySlow = Policy.handleAll()
Expand Down Expand Up @@ -155,11 +156,6 @@ export class DingzAccessory extends DingzDaBaseAccessory {
// Now we have what we need and can create the services …
this.addOutputServices();

this.platform.eb.on(
PlatformEvent.REQUEST_STATE_UPDATE,
this.getDeviceStateUpdate.bind(this),
);

// Add auxiliary services (Motion, Temperature)
if (this.dingzDeviceInfo.has_pir) {
// dingz has a Motion sensor -- let's create it
Expand Down Expand Up @@ -327,8 +323,8 @@ export class DingzAccessory extends DingzDaBaseAccessory {
.on(CharacteristicEventTypes.GET, this.getTemperature.bind(this));
this.services.push(temperatureService);

this.platform.eb.on(
PlatformEvent.PUSH_STATE_UPDATE,
this.eb.on(
AccessoryEvent.PUSH_STATE_UPDATE,
this.updateTemperature.bind(this, temperatureService),
);
}
Expand Down Expand Up @@ -748,8 +744,8 @@ export class DingzAccessory extends DingzDaBaseAccessory {
}

// Update State
this.platform.eb.on(
PlatformEvent.PUSH_STATE_UPDATE,
this.eb.on(
AccessoryEvent.PUSH_STATE_UPDATE,
this.updateDimmerState.bind(this, index, output, newService),
);
return newService;
Expand Down Expand Up @@ -895,8 +891,8 @@ export class DingzAccessory extends DingzDaBaseAccessory {
);

// Subscribe to the update event
this.platform.eb.on(
PlatformEvent.PUSH_STATE_UPDATE,
this.eb.on(
AccessoryEvent.PUSH_STATE_UPDATE,
this.updateWindowCoveringState.bind(
this,
id as WindowCoveringId,
Expand Down Expand Up @@ -1350,8 +1346,8 @@ export class DingzAccessory extends DingzDaBaseAccessory {
.on(CharacteristicEventTypes.SET, this.setLEDSaturation.bind(this)); // SET - bind to the 'setBrightness` method below

this.services.push(ledService);
this.platform.eb.on(
PlatformEvent.PUSH_STATE_UPDATE,
this.eb.on(
AccessoryEvent.PUSH_STATE_UPDATE,
this.updateLEDState.bind(this, ledService),
);
}
Expand Down
19 changes: 19 additions & 0 deletions src/lib/accessoryEventBus.ts
@@ -0,0 +1,19 @@
import { EventEmitter } from 'events';

// Accessory elements
// EVENT TYPES
export const enum AccessoryEvent {
PUSH_STATE_UPDATE = 'pushStateUpdate',
}

export declare interface AccessoryEventBus {
on(event: AccessoryEvent.PUSH_STATE_UPDATE, listener: () => void): this;
emit(event: AccessoryEvent.PUSH_STATE_UPDATE): boolean;
}

export class AccessoryEventBus extends EventEmitter {
constructor() {
super();
this.setMaxListeners(20); // Maximum of 20 services
}
}
6 changes: 0 additions & 6 deletions src/lib/platformEventBus.ts
Expand Up @@ -8,7 +8,6 @@ export const enum PlatformEvent {
UPDATE_DEVICE_INFO = 'updateDeviceInfo',
ACTION = 'deviceAction',
REQUEST_STATE_UPDATE = 'requestStateUpdate',
PUSH_STATE_UPDATE = 'pushStateUpdate',
}

export declare interface PlatformEventBus {
Expand All @@ -25,10 +24,6 @@ export declare interface PlatformEventBus {
listener: (deviceInfo: DeviceInfo) => void,
): this;
on(event: PlatformEvent.REQUEST_STATE_UPDATE, listener: () => void): this;
on(
event: PlatformEvent.PUSH_STATE_UPDATE,
listener: (mac: string) => void,
): this;

emit(
event: PlatformEvent.ACTION,
Expand All @@ -47,7 +42,6 @@ export declare interface PlatformEventBus {
deviceInfo: DeviceInfo,
): boolean;
emit(event: PlatformEvent.REQUEST_STATE_UPDATE): boolean;
emit(event: PlatformEvent.PUSH_STATE_UPDATE, mac: string): boolean;
}

export class PlatformEventBus extends EventEmitter {
Expand Down
7 changes: 0 additions & 7 deletions src/myStromLightbulbAccessory.ts
Expand Up @@ -12,7 +12,6 @@ import qs from 'qs';

import { DingzDaHomebridgePlatform } from './platform';
import { MyStromDeviceInfo, MyStromLightbulbReport } from './lib/myStromTypes';
import { PlatformEvent } from './lib/platformEventBus';
import { DingzDaBaseAccessory } from './lib/dingzDaBaseAccessory';

/**
Expand Down Expand Up @@ -103,12 +102,6 @@ export class MyStromLightbulbAccessory extends DingzDaBaseAccessory {
.getCharacteristic(this.platform.Characteristic.Saturation)
.on(CharacteristicEventTypes.SET, this.setSaturation.bind(this)) // SET - bind to the 'setBrightness` method below
.on(CharacteristicEventTypes.GET, this.getSaturation.bind(this)); // SET - bind to the 'setBrightness` method below

// Subscribe to the REQUEST_STATE_UPDATE event
this.platform.eb.on(
PlatformEvent.REQUEST_STATE_UPDATE,
this.getDeviceStateUpdate.bind(this),
);
}

// Get updated device info and update the corresponding values
Expand Down
6 changes: 0 additions & 6 deletions src/myStromSwitchAccessory.ts
Expand Up @@ -106,12 +106,6 @@ export class MyStromSwitchAccessory extends DingzDaBaseAccessory {
.getCharacteristic(this.platform.Characteristic.CurrentTemperature)
.on(CharacteristicEventTypes.GET, this.getTemperature.bind(this));
}

// Subscribe to the REQUEST_STATE_UPDATE event
this.platform.eb.on(
PlatformEvent.REQUEST_STATE_UPDATE,
this.getDeviceStateUpdate.bind(this),
);
}

identify(): void {
Expand Down
6 changes: 1 addition & 5 deletions src/platform.ts
Expand Up @@ -110,12 +110,8 @@ export class DingzDaHomebridgePlatform implements DynamicPlatformPlugin {
// set-up the callback server ...
this.callbackServer();

// .. and finally set-up the interval that triggers updates
this.eb.on(PlatformEvent.REQUEST_STATE_UPDATE, () => {
this.log.debug('Event -> DingzEvent.REQUEST_STATE_UPDATE');
});
this.eb.on(PlatformEvent.PUSH_STATE_UPDATE, () => {
this.log.debug('Event -> DingzEvent.PUSH_STATE_UPDATE');
this.log.debug('Event -> PlatformEvent.REQUEST_STATE_UPDATE');
});

setInterval(() => {
Expand Down

0 comments on commit 26a908d

Please sign in to comment.