Skip to content

Commit

Permalink
Working again! (stream does not work due to: homebridge-plugins/homeb…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Van den Abeele committed Jun 24, 2020
1 parent 1ad60ed commit 901c21b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ resources/test-config/persist/
resources/test-config/unifi-protect-google-settings.json

.idea/

.DS_Store
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 28 additions & 12 deletions src/unifi-protect-motion-platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]);
}
});

Expand Down
1 change: 1 addition & 0 deletions src/unifi/unifi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ export interface UnifiSession {

export interface UnifiCamera {
id: string;
uuid: string;
name: string;
ip: string;
mac: string;
Expand Down

0 comments on commit 901c21b

Please sign in to comment.