From 3d73ddf4d53182d1a980b7e39a4aac5ba9fa16eb Mon Sep 17 00:00:00 2001 From: watsonb8 Date: Sat, 12 Dec 2020 00:14:56 -0500 Subject: [PATCH] Working on linux host --- deploy.sh | 1 - src/common.ts | 50 ++++++++++++++++++++++++------------- src/homeLocationPlatform.ts | 8 +++--- src/monitor/monitor.ts | 12 ++++++++- src/rtsp/rtsp.ts | 17 ++++++++++--- 5 files changed, 60 insertions(+), 28 deletions(-) diff --git a/deploy.sh b/deploy.sh index 30dfeb2..19b37d5 100755 --- a/deploy.sh +++ b/deploy.sh @@ -18,7 +18,6 @@ #restart service ssh -t ssh -t $remote_user@$remote_server "sudo systemctl restart homebridge.service" - ssh -t $remote_user@$remote_server "sudo systemctl status homebridge.service" echo done exit \ No newline at end of file diff --git a/src/common.ts b/src/common.ts index fb34fc1..a3f7981 100644 --- a/src/common.ts +++ b/src/common.ts @@ -15,34 +15,48 @@ export const getFaceDetectorOptions = (net: faceapi.NeuralNetwork) => { : new faceapi.TinyFaceDetectorOptions({ inputSize, scoreThreshold }); }; -export function saveFile( +export const saveFile = async ( basePath: string, fileName: string, buf: Buffer -): Promise { - const writeFile = (): Promise => { - return new Promise((resolve, reject) => { - fs.writeFile(path.resolve(basePath, fileName), buf, "base64", (err) => { - if (err) { - return reject(err); - } - resolve(); - }); - }); - }; +): Promise => { return new Promise(async (resolve, reject) => { - if (!fs.existsSync(basePath)) { - fs.mkdir(basePath, async (err) => { + try { + //Create directory if it does not exist + await makeDirectory(basePath); + } catch (err) { + return reject(err); + } + //Write file to directory + try { + const asdf = fs.writeFileSync( + path.join(basePath, fileName), + buf, + "base64" + ); + } catch (err) { + return reject(err); + } + + return resolve(); + }); +}; + +export const makeDirectory = (path: string): Promise => { + return new Promise(async (resolve, reject) => { + if (!fs.existsSync(path)) { + fs.mkdir(path, async (err) => { if (err) { return reject(err); } - resolve(await writeFile()); + + return resolve(); }); - } else { - resolve(await writeFile()); } + + return resolve(); }); -} +}; export const delay = (ms: number): Promise => { return new Promise((resolve) => { diff --git a/src/homeLocationPlatform.ts b/src/homeLocationPlatform.ts index bf3ac36..070a704 100644 --- a/src/homeLocationPlatform.ts +++ b/src/homeLocationPlatform.ts @@ -175,7 +175,7 @@ export class HomeLocationPlatform implements DynamicPlatformPlugin { if (!mimeType || !mimeType.startsWith("image")) { return; } - console.log(path.join(this.config.refImageDirectory, dir, file)); + this.log.info(path.join(this.config.refImageDirectory, dir, file)); try { const referenceImage = (await canvas.loadImage( @@ -193,7 +193,7 @@ export class HomeLocationPlatform implements DynamicPlatformPlugin { const faceDescriptors = [descriptor.descriptor]; return new faceapi.LabeledFaceDescriptors(dir, faceDescriptors); } catch (err) { - console.log( + this.log.info( "An error occurred loading image at path: " + path.join(this.config.refImageDirectory, dir, file) ); @@ -217,10 +217,10 @@ export class HomeLocationPlatform implements DynamicPlatformPlugin { "utf8", (err) => { if (err) { - console.log(`An error occurred while writing data model to file`); + this.log.info(`An error occurred while writing data model to file`); } - console.log(`Successfully wrote data model to file`); + this.log.info(`Successfully wrote data model to file`); } ); diff --git a/src/monitor/monitor.ts b/src/monitor/monitor.ts index d87f092..c9e59ed 100644 --- a/src/monitor/monitor.ts +++ b/src/monitor/monitor.ts @@ -155,7 +155,6 @@ export class Monitor { if (this._config.writeOutput) { await saveFile(this._config.outputDirectory, room + ".jpg", args.data); } - for (const res of resultsQuery) { const bestMatch = this._matcher.matchDescriptor(res.descriptor); const old = this._state[bestMatch.label]; @@ -183,6 +182,8 @@ export class Monitor { connectionString: connectionString, }; + connectionString = this.getRedactedConnectionString(connectionString); + //Subscribe to rtsp events stream.rtsp.dataEvent.push((sender: Rtsp, args: IStreamEventArgs) => this.onData(roomName, stream, args) @@ -223,4 +224,13 @@ export class Monitor { ); stream.rtsp.start(); }; + + private getRedactedConnectionString(connectionString: string) { + const pwSepIdx = connectionString.lastIndexOf(":") + 1; + const pwEndIdx = connectionString.indexOf("@"); + return ( + connectionString.substring(0, pwSepIdx) + + connectionString.substring(pwEndIdx) + ); + } } diff --git a/src/rtsp/rtsp.ts b/src/rtsp/rtsp.ts index 7ad56d1..f47fe94 100644 --- a/src/rtsp/rtsp.ts +++ b/src/rtsp/rtsp.ts @@ -93,11 +93,20 @@ export class Rtsp { } this._childProcess.stdout?.on("data", this.onData); - this._childProcess.stdout?.on("error", (err) => - console.log("And error occurred" + err) + + this._childProcess.stdout?.on("error", (error: Error) => + this._errorEvent.fire(this, { err: error }) + ); + this._childProcess.stdout?.on("close", () => + this._closeEvent.fire(this, { + message: "Stream closed", + }) + ); + this._childProcess.stdout?.on("end", () => + this._closeEvent.fire(this, { + message: "Stream ended", + }) ); - this._childProcess.stdout?.on("close", () => console.log("Stream closed")); - this._childProcess.stdout?.on("end", () => console.log("Stream ended")); //Only register this event if there are subscribers if (this._childProcess.stderr && this._messageEvent.length > 0) {