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

4
package-lock.json generated
View File

@ -1200,6 +1200,10 @@
"sax": "^1.2.4"
}
},
"node-common": {
"version": "git+ssh://git@thebword.ddns.net:3122/watsonb8/node-common.git#3ee1400be94851335e822916861ea2eddb9e344f",
"from": "git+ssh://git@thebword.ddns.net:3122/watsonb8/node-common.git"
},
"node-fetch": {
"version": "2.6.1",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz",

View File

@ -32,6 +32,7 @@
"dotenv-extended": "^2.9.0",
"mime-types": "^2.1.27",
"rtsp-stream": "file:../rtsp-stream",
"node-common": "git+ssh://git@thebword.ddns.net:3122/watsonb8/node-common.git",
"tsyringe": "^4.4.0"
},
"devDependencies": {

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();

View File

@ -39,7 +39,8 @@
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": "./" /* Base directory to resolve non-absolute module names. */,
"paths": {
"rtsp/*": ["./node_modules/rtsp-stream/lib/*"]
"rtsp/*": ["./node_modules/rtsp-stream/lib/*"],
"common/*": ["./node_modules/node-common/lib/*"]
} /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */,
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
@ -65,6 +66,9 @@
"references": [
{
"path": "./node_modules/rtsp-stream/tsconfig.json"
},
{
"path": "./node_modules/node-common/tsconfig.json"
}
]
}