diff --git a/src/eventsource.ts b/src/eventsource.ts index fb8a261..964f00f 100644 --- a/src/eventsource.ts +++ b/src/eventsource.ts @@ -4,6 +4,7 @@ import { URL } from 'url'; declare interface EventSource { on(event: 'event', listener: (arg0: object) => void): this; + on(event: 'error', listener: (arg0: object | string) => void): this; } class EventSource extends EventEmitter { diff --git a/src/hub.ts b/src/hub.ts index 19b47a5..8a8ba78 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -2,6 +2,10 @@ import fetch, { Response } from 'node-fetch'; import { URL } from 'url'; import EventSource from './eventsource'; +interface Logger { + error(message: string, ...parameters: any[]): void; +} + interface Event { evt: string; } @@ -53,10 +57,11 @@ class Hub { private readonly events: EventSource; private readonly shades: Map = new Map(); - constructor(private readonly host: URL) { + constructor(private readonly host: URL, private readonly logger: Logger) { const events = new URL('/home/shades/events', host); this.events = new EventSource(events); this.events.on('event', this.handleEvent.bind(this)); + this.events.on('error', this.handleError.bind(this)); this.events.connect(); } @@ -126,6 +131,10 @@ class Hub { shade.currentPositions = event.currentPositions; } } + + private handleError(error: object | string) { + this.logger.error('error', error); + } } export { Shade }; diff --git a/src/platform.ts b/src/platform.ts index fc18750..139f73d 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -29,7 +29,7 @@ export class PowerViewHomebridgePlatform implements DynamicPlatformPlugin { ) { this.config = config as PowerViewConfig; const url = new URL(this.config.Host); - this.hub = new Hub(url); + this.hub = new Hub(url, this.log); this.log.debug('Finished initializing platform:', this.config.name); // When this event is fired it means Homebridge has restored all cached accessories from disk.