Skip to content

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
martin308 committed Dec 29, 2022
1 parent 681ec43 commit 231c4d6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/eventsource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 10 additions & 1 deletion src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -53,10 +57,11 @@ class Hub {
private readonly events: EventSource;
private readonly shades: Map<number, Shade> = 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();
}

Expand Down Expand Up @@ -126,6 +131,10 @@ class Hub {
shade.currentPositions = event.currentPositions;
}
}

private handleError(error: object | string) {
this.logger.error('error', error);
}
}

export { Shade };
Expand Down
2 changes: 1 addition & 1 deletion src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 231c4d6

Please sign in to comment.