Using events from common lib

This commit is contained in:
watsonb8
2020-12-10 13:54:06 -05:00
parent 65f11bec09
commit 0792147dc4
8 changed files with 43 additions and 38 deletions

View File

@ -7,6 +7,7 @@ export interface IConfig extends PlatformConfig {
trainOnStartup: boolean;
rooms: Array<IRoom>;
detectionTimeout: number;
debug: boolean;
}
export interface IRoom {

View File

@ -1,12 +0,0 @@
import { EventDelegate } from "./eventDelegate";
export class Event<T, K> extends Array<EventDelegate<T, K>> {
constructor() {
super();
}
public fire = (source: T, args: K) => {
for (const delegate of this) {
delegate(source, args);
}
};
}

View File

@ -1 +0,0 @@
export type EventDelegate<T, K> = (sender: T, args: K) => void;

View File

@ -6,7 +6,7 @@ import * as faceapi from "@vladmandic/face-api";
import { getFaceDetectorOptions, saveFile } from "./common";
import { nets } from "@vladmandic/face-api";
import { Logger } from "homebridge";
import { Event } from "./events/event";
import { Event } from "common/events";
const { Canvas, Image, ImageData } = canvas;
export type MonitorState = { [label: string]: string | null };
@ -25,7 +25,8 @@ export class Monitor {
constructor(
private _rooms: Array<IRoom>,
private _matcher: FaceMatcher,
private _logger: Logger
private _logger: Logger,
private _isDebug: boolean
) {
this._stateChangedEvent = new Event();
@ -34,13 +35,14 @@ export class Monitor {
this._streamsByRoom[room.name] = [
...room.rtspConnectionStrings.map((connectionString) => {
return new Rtsp(connectionString, {
rate: 0.5,
rate: 1,
image: true,
})
.on("data", async (data: Buffer) => this.onData(room.name, data))
.on("error", async (error: string) =>
.on("data", (data: Buffer) => this.onData(room.name, data))
.on("error", (error: string) =>
this.onError(error, connectionString)
);
)
.on("exit", () => this.onExit(connectionString));
}),
];
@ -112,22 +114,24 @@ export class Monitor {
.detectAllFaces(out, getFaceDetectorOptions(this._faceDetectionNet))
.withFaceLandmarks()
.withFaceDescriptors();
switch (room) {
case "Kitchen": {
saveFile(
"/Users/brandonwatson/Documents/Git/Gitea/homebridge-face-location/out",
"Kitchen.jpg",
data
);
break;
}
case "LivingRoom": {
saveFile(
"/Users/brandonwatson/Documents/Git/Gitea/homebridge-face-location/out",
"LivingRoom.jpg",
data
);
break;
if (this._isDebug) {
switch (room) {
case "Kitchen": {
saveFile(
"/Users/brandonwatson/Documents/Git/Gitea/homebridge-face-location/out",
"Kitchen.jpg",
data
);
break;
}
case "LivingRoom": {
saveFile(
"/Users/brandonwatson/Documents/Git/Gitea/homebridge-face-location/out",
"LivingRoom.jpg",
data
);
break;
}
}
}
@ -145,7 +149,10 @@ export class Monitor {
}
};
private onError = async (error: string, streamName: string) => {
private onError = (error: string, streamName: string) => {
this._logger.info(`[${streamName}] ${error}`);
};
private onExit = (streamName: string) => {
this._logger.info(`[${streamName}] Stream has exited`);
};
}

View File

@ -103,7 +103,8 @@ export class HomeLocationPlatform implements DynamicPlatformPlugin {
const locationMonitor = new Monitor(
this.config.rooms,
faceMatcher,
this.log
this.log,
this.config.debug
);
locationMonitor.startStreams();