Skip to content

Commit

Permalink
Mute event notification calls when aid/iid are not yet set
Browse files Browse the repository at this point in the history
Catch any errors which may occur when sending events
  • Loading branch information
Supereg committed Oct 8, 2020
1 parent 7073a82 commit 0ac698f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/lib/Accessory.ts
Expand Up @@ -1709,11 +1709,16 @@ export class Accessory extends EventEmitter {
return; // we're not running a HAPServer, so there's no one to notify about this event
}

if (this.aid == undefined || change.characteristic.iid == undefined) {
debug("[%s] Muting event notification for %s as ids aren't yet assigned!", this.displayName, change.characteristic.displayName);
return;
}

const uuid = change.characteristic.UUID;
const immediateDelivery = uuid === ButtonEvent.UUID || uuid === ProgrammableSwitchEvent.UUID
|| uuid === MotionDetected.UUID || uuid === ContactSensorState.UUID;

this._server.sendEventNotifications(this.aid!, change.characteristic.iid!, change.newValue, change.originator, immediateDelivery);
this._server.sendEventNotifications(this.aid, change.characteristic.iid, change.newValue, change.originator, immediateDelivery);
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/lib/HAPServer.ts
Expand Up @@ -17,7 +17,7 @@ import {
ResourceRequest
} from "../internal-types";
import { CharacteristicValue, Nullable, VoidCallback } from '../types';
import { PairingInformation, PermissionTypes } from "./model/AccessoryInfo";
import { AccessoryInfo, PairingInformation, PermissionTypes } from "./model/AccessoryInfo";
import {
EventedHTTPServer,
EventedHTTPServerEvent,
Expand Down Expand Up @@ -277,12 +277,13 @@ export declare interface HAPServer {
*/
export class HAPServer extends EventEmitter {

private accessoryInfo: AccessoryInfo;
private httpServer: EventedHTTPServer;
private unsuccessfulPairAttempts: number = 0; // after 100 unsuccessful attempts the server won't accept any further attempts. Will currently be reset on a reboot

allowInsecureRequest: boolean;

constructor(public accessoryInfo: any) {
constructor(accessoryInfo: AccessoryInfo) {
super();
this.accessoryInfo = accessoryInfo;
this.allowInsecureRequest = false;
Expand Down Expand Up @@ -314,7 +315,11 @@ export class HAPServer extends EventEmitter {
* Namely for the {@link ButtonEvent} and the {@link ProgrammableSwitchEvent} characteristics.
*/
public sendEventNotifications(aid: number, iid: number, value: Nullable<CharacteristicValue>, originator?: HAPConnection, immediateDelivery?: boolean): void {
this.httpServer.broadcastEvent(aid, iid, value, originator, immediateDelivery);
try {
this.httpServer.broadcastEvent(aid, iid, value, originator, immediateDelivery);
} catch (error) {
console.warn("[" + this.accessoryInfo.username + "] Error when sending event notifications: " + error.message);
}
}

private onListening(port: number, hostname: string): void {
Expand Down

0 comments on commit 0ac698f

Please sign in to comment.