From 901c21bacc70070820355e2622a9abfce30c66c8 Mon Sep 17 00:00:00 2001 From: Kevin Van den Abeele Date: Wed, 24 Jun 2020 13:23:37 +0200 Subject: [PATCH] Working again! (stream does not work due to: https://github.com/homebridge-plugins/homebridge-camera-ffmpeg/issues/554) --- .gitignore | 2 ++ package.json | 2 +- src/unifi-protect-motion-platform.ts | 40 +++++++++++++++++++--------- src/unifi/unifi.ts | 1 + 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 2b035f0..9f9a84f 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,5 @@ resources/test-config/persist/ resources/test-config/unifi-protect-google-settings.json .idea/ + +.DS_Store diff --git a/package.json b/package.json index ba4c75a..789c79b 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "postinstall": "npm rebuild @tensorflow/tfjs-node --build-from-source", "prepublishOnly": "node --max-old-space-size=1024 ./node_modules/typescript/lib/tsc.js", "compile": "node --max-old-space-size=1024 ./node_modules/typescript/lib/tsc.js", - "homebridge": "tsc && homebridge -P . -U ./resources/test-config/", + "homebridge": "tsc && homebridge -D -P . -U ./resources/test-config/", "test": "jest --config jest.config.js --collect-coverage" }, "license": "ISC", diff --git a/src/unifi-protect-motion-platform.ts b/src/unifi-protect-motion-platform.ts index 9d0968b..5b5af64 100644 --- a/src/unifi-protect-motion-platform.ts +++ b/src/unifi-protect-motion-platform.ts @@ -67,12 +67,35 @@ export class UnifiProtectMotionPlatform implements DynamicPlatformPlugin { const cameraConfig = cameraAccessory.context.cameraConfig; - cameraAccessory.context.id = cameraConfig.id; + //Update the camera config! + const videoConfigCopy: VideoConfig = JSON.parse(JSON.stringify(this.config.videoConfig)); + //Assign stillImageSource, source and debug (overwrite if they are present from the videoConfig, which they should not be!) + videoConfigCopy.stillImageSource = '-i http://' + cameraConfig.camera.ip + '/snap.jpeg'; + videoConfigCopy.source = '-rtsp_transport tcp -re -i ' + this.config.unifi.controller_rtsp + '/' + Unifi.pickHighestQualityAlias(cameraConfig.camera.streams); + + //TODO: only for testing! + videoConfigCopy.debug = true; + //videoConfigCopy.debug = this.config.unifi.debug; + cameraConfig.videoConfig = videoConfigCopy; + console.debug(cameraConfig); + + //TODO: Refactor + cameraAccessory.context.id = cameraConfig.camera.id; cameraAccessory.context.motionEnabled = true; cameraAccessory.context.lastMotionId = null; cameraAccessory.context.lastMotionIdRepeatCount = 0; + + const motion = cameraAccessory.getService(this.hap.Service.MotionSensor); + const motionSwitch = cameraAccessory.getServiceById(this.hap.Service.Switch, 'MotionTrigger'); + if (motion) { + cameraAccessory.removeService(motion); + } + if (motionSwitch) { + cameraAccessory.removeService(motionSwitch); + } + cameraAccessory.addService(new this.Service.MotionSensor(cameraConfig.name + ' Motion sensor')); - cameraAccessory.addService(new this.Service.Switch(cameraConfig.name + ' Motion enabled')); + cameraAccessory.addService(new this.Service.Switch(cameraConfig.name + ' Motion enabled', 'MotionTrigger')); cameraAccessory .getService(this.Service.Switch) .getCharacteristic(this.Characteristic.On) @@ -152,20 +175,13 @@ export class UnifiProtectMotionPlatform implements DynamicPlatformPlugin { // Camera names must be unique const uuid = this.hap.uuid.generate(camera.name); + camera.uuid = uuid; const cameraAccessory = new this.Accessory(camera.name, uuid); - //Make a copy of the config so we can set each one to have its own camera sources! - const videoConfigCopy: VideoConfig = JSON.parse(JSON.stringify(this.config.videoConfig)); - //Assign stillImageSource, source and debug (overwrite if they are present from the videoConfig, which they should not be!) - videoConfigCopy.stillImageSource = '-i http://' + camera.ip + '/snap.jpeg'; - videoConfigCopy.source = '-rtsp_transport tcp -re -i ' + this.config.unifi.controller_rtsp + '/' + Unifi.pickHighestQualityAlias(camera.streams); - videoConfigCopy.debug = this.config.unifi.debug; - cameraAccessory.context.cameraConfig = { uuid: uuid, - id: camera.id, name: camera.name, - videoConfig: videoConfigCopy + camera }; const cameraAccessoryInfo = cameraAccessory.getService(this.hap.Service.AccessoryInformation); @@ -194,7 +210,7 @@ export class UnifiProtectMotionPlatform implements DynamicPlatformPlugin { this.accessories.forEach((accessory: PlatformAccessory) => { if (!cameras.find((x: UnifiCamera) => x.uuid === accessory.context.cameraConfig.uuid)) { this.infoLogger('Removing ' + accessory.context.cameraConfig.uuid); - //this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]); + this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]); } }); diff --git a/src/unifi/unifi.ts b/src/unifi/unifi.ts index 37ca2ca..b3c9e08 100644 --- a/src/unifi/unifi.ts +++ b/src/unifi/unifi.ts @@ -250,6 +250,7 @@ export interface UnifiSession { export interface UnifiCamera { id: string; + uuid: string; name: string; ip: string; mac: string;