diff --git a/package-lock.json b/package-lock.json index 80603d6..9a332e5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 64748c5..78c654a 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/config.ts b/src/config.ts index 9556ff7..53a2a8b 100644 --- a/src/config.ts +++ b/src/config.ts @@ -7,6 +7,7 @@ export interface IConfig extends PlatformConfig { trainOnStartup: boolean; rooms: Array; detectionTimeout: number; + debug: boolean; } export interface IRoom { diff --git a/src/events/event.ts b/src/events/event.ts deleted file mode 100644 index 2790a48..0000000 --- a/src/events/event.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { EventDelegate } from "./eventDelegate"; - -export class Event extends Array> { - constructor() { - super(); - } - public fire = (source: T, args: K) => { - for (const delegate of this) { - delegate(source, args); - } - }; -} diff --git a/src/events/eventDelegate.ts b/src/events/eventDelegate.ts deleted file mode 100644 index 5f14e97..0000000 --- a/src/events/eventDelegate.ts +++ /dev/null @@ -1 +0,0 @@ -export type EventDelegate = (sender: T, args: K) => void; diff --git a/src/monitor.ts b/src/monitor.ts index dac9842..24ebabd 100644 --- a/src/monitor.ts +++ b/src/monitor.ts @@ -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, 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`); + }; } diff --git a/src/platform.ts b/src/platform.ts index 4ed8792..b32ef45 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -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(); diff --git a/tsconfig.json b/tsconfig.json index b5b8cc8..e82e96a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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" } ] }