From 4be1c538073992cbbe680dd745553d7b030e0dfc Mon Sep 17 00:00:00 2001 From: watsonb8 Date: Fri, 11 Dec 2020 13:39:30 -0500 Subject: [PATCH] Implement detection timeout --- src/common.ts | 2 +- src/locationAccessory.ts | 39 +++++++++++++++++++++++++++++++++------ src/monitor/monitor.ts | 30 ++++++++++++++++++++---------- 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/common.ts b/src/common.ts index 60da41f..fb34fc1 100644 --- a/src/common.ts +++ b/src/common.ts @@ -3,7 +3,7 @@ import * as path from "path"; import fs from "fs"; // SsdMobilenetv1Options -export const minConfidence = 0.5; +export const minConfidence = 0.4; // TinyFaceDetectorOptions export const inputSize = 408; diff --git a/src/locationAccessory.ts b/src/locationAccessory.ts index 45ab8b1..69232a4 100644 --- a/src/locationAccessory.ts +++ b/src/locationAccessory.ts @@ -7,13 +7,20 @@ import { Monitor, IStateChangeEventArgs } from "./monitor/monitor"; import { HomeLocationPlatform } from "./homeLocationPlatform"; import { IRoom } from "./config"; +const defaultDetectionTimeout = 180000; + +interface IMotionDetectionService { + service: Service; + detectionTimeout: NodeJS.Timeout | null; +} + /** * Platform Accessory * An instance of this class is created for each accessory your platform registers * Each accessory may expose multiple services of different service types. */ export class LocationAccessory { - private _services: Array; + private _services: Array; constructor( private readonly _platform: HomeLocationPlatform, @@ -54,7 +61,10 @@ export class LocationAccessory { this.onMotionDetectedGet(label, callback) ); - this._services.push(newService); + this._services.push({ + service: newService, + detectionTimeout: null, + }); } //Register monitor state change events @@ -78,14 +88,31 @@ export class LocationAccessory { sender: Monitor, args: IStateChangeEventArgs ) => { - const service = this._services.find( - (service) => service.displayName == args.label + const motionService = this._services.find( + (motionService) => motionService.service.displayName == args.label ); - if (service) { - service.setCharacteristic( + if (motionService) { + //Set accessory state + motionService.service.setCharacteristic( this._platform.Characteristic.MotionDetected, args.new === this._room.name ); + + //Reset detectionTimeout + clearTimeout(motionService.detectionTimeout!); + motionService.detectionTimeout = setTimeout( + () => this.onDetectionTimeout(motionService), + this._platform.config.detectionTimeout ?? defaultDetectionTimeout + ); } }; + + private onDetectionTimeout = (motionService: IMotionDetectionService) => { + //Set accessory state + motionService.service.setCharacteristic( + this._platform.Characteristic.MotionDetected, + 0 + ); + this._monitor.resetState(motionService.service.displayName); + }; } diff --git a/src/monitor/monitor.ts b/src/monitor/monitor.ts index 37d6503..d87f092 100644 --- a/src/monitor/monitor.ts +++ b/src/monitor/monitor.ts @@ -66,6 +66,11 @@ export class Monitor { return this._state[label]; } + public resetState(label: string): Monitor { + this._state[label] = null; + return this; + } + /** * @property labels * @@ -89,7 +94,7 @@ export class Monitor { * * Starts monitoring rtsp streams */ - public startStreams() { + public startStreams(): Monitor { for (const key in this._streamsByRoom) { for (const stream of this._streamsByRoom[key]) { //Start stream @@ -102,6 +107,8 @@ export class Monitor { ); } } + + return this; } /** @@ -109,7 +116,7 @@ export class Monitor { * * Stops monitoring rtsp streams */ - public closeStreams() { + public closeStreams(): Monitor { for (const key in this._streamsByRoom) { for (const stream of this._streamsByRoom[key]) { stream.rtsp.close(); @@ -120,6 +127,8 @@ export class Monitor { } } } + + return this; } private onData = async ( @@ -178,18 +187,19 @@ export class Monitor { stream.rtsp.dataEvent.push((sender: Rtsp, args: IStreamEventArgs) => this.onData(roomName, stream, args) ); - stream.rtsp.closeEvent.push((sender: Rtsp, args: ICloseEventArgs) => { - this._logger.info( - `[${connectionString}] Stream has exited: ${args.message}` - ); - }); - stream.rtsp.errorEvent.push((sender: Rtsp, args: IErrorEventArgs) => { - this._logger.info(`[${connectionString}] ${args.message}`); - }); + //Only subscribe to these events if debug if (this._config.debug) { stream.rtsp.messageEvent.push((sender: Rtsp, args: IMessageEventArgs) => { this._logger.info(`[${connectionString}] ${args.message}`); }); + stream.rtsp.errorEvent.push((sender: Rtsp, args: IErrorEventArgs) => { + this._logger.info(`[${connectionString}] ${args.message}`); + }); + stream.rtsp.closeEvent.push((sender: Rtsp, args: ICloseEventArgs) => { + this._logger.info( + `[${connectionString}] Stream has exited: ${args.message}` + ); + }); } return stream;