Skip to content

Commit

Permalink
Add HTTP automation messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunoo committed Aug 16, 2020
1 parent 3ab8598 commit 238197f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 13 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@

All notable changes to this project will be documented in this file.

## v2.4.5 (2020-08-15)

### Changes

- Return messages and error codes when using HTTP automation.

### Bug Fixes

- Fixed bug preventing MQTT/HTTP automation from working.

## v2.4.4 (2020-08-07)

### Changes

- Added support for unbridging specific cameras.This can aid with performance of the camera and Homebridge as a whole, but requires manually adding any unbridged cameras to HomeKit.
- Added support for unbridging specific cameras. This can aid with performance of the camera and Homebridge as a whole, but requires manually adding any unbridged cameras to HomeKit.

## v2.4.3 (2020-07-29)

Expand Down
1 change: 0 additions & 1 deletion config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@
"debug": {
"title": "Debug Logging",
"type": "boolean",
"default": true,
"description": "Includes debugging output from FFmpeg in the Homebridge log."
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"displayName": "Homebridge Camera FFmpeg",
"name": "homebridge-camera-ffmpeg",
"version": "2.4.4",
"version": "2.4.5",
"description": "Homebridge Plugin Providing FFmpeg-based Camera Support",
"main": "dist/index.js",
"license": "ISC",
Expand Down
70 changes: 60 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ let Accessory: typeof PlatformAccessory;
const PLUGIN_NAME = 'homebridge-camera-ffmpeg';
const PLATFORM_NAME = 'Camera-ffmpeg';

type AutomationReturn = {
error: boolean;
message: string;
};

class FfmpegPlatform implements DynamicPlatformPlugin {
private readonly log: Logger;
private readonly api: API;
Expand Down Expand Up @@ -180,7 +185,7 @@ class FfmpegPlatform implements DynamicPlatformPlugin {
this.cachedAccessories.push(accessory);
}

private doorbellHandler(accessory: PlatformAccessory, active = true): void {
private doorbellHandler(accessory: PlatformAccessory, active = true): AutomationReturn {
const doorbell = accessory.getService(hap.Service.Doorbell);
if (doorbell) {
this.log.debug('Switch doorbell ' + (active ? 'on.' : 'off.'), accessory.displayName);
Expand All @@ -204,13 +209,28 @@ class FfmpegPlatform implements DynamicPlatformPlugin {
}, timeoutConfig * 1000);
this.doorbellTimers.set(accessory.UUID, timer);
}
} else if (doorbellTrigger) {
doorbellTrigger.updateCharacteristic(hap.Characteristic.On, true);
return {
error: false,
message: 'Doorbell switched on.'
};
} else {
if (doorbellTrigger) {
doorbellTrigger.updateCharacteristic(hap.Characteristic.On, false);
}
return {
error: false,
message: 'Doorbell switched off.'
};
}
} else {
return {
error: true,
message: 'Doorbell is not enabled for this camera.'
};
}
}

private motionHandler(accessory: PlatformAccessory, active = true, minimumTimeout = 0): void {
private motionHandler(accessory: PlatformAccessory, active = true, minimumTimeout = 0): AutomationReturn {
const motionSensor = accessory.getService(hap.Service.MotionSensor);
if (motionSensor) {
this.log.debug('Switch motion detect ' + (active ? 'on.' : 'off.'), accessory.displayName);
Expand Down Expand Up @@ -240,27 +260,52 @@ class FfmpegPlatform implements DynamicPlatformPlugin {
}, timeoutConfig * 1000);
this.motionTimers.set(accessory.UUID, timer);
}
return {
error: false,
message: 'Motion switched on.'
};
} else {
motionSensor.updateCharacteristic(hap.Characteristic.MotionDetected, false);
if (motionTrigger) {
motionTrigger.updateCharacteristic(hap.Characteristic.On, false);
}
return {
error: false,
message: 'Motion switched off.'
};
}
} else {
return {
error: true,
message: 'Motion is not enabled for this camera.'
};
}
}

private automationHandler(fullpath: string, name: string): void{
const accessory = this.cachedAccessories.find((curAcc: PlatformAccessory) => curAcc.displayName == name);
private automationHandler(fullpath: string, name: string): AutomationReturn {
const accessory = this.cachedAccessories.find((curAcc: PlatformAccessory) => {
return curAcc.displayName == name;
});
if (accessory) {
const path = fullpath.split('/').filter((value) => value.length > 0);
switch (path[0]) {
case 'motion':
this.motionHandler(accessory, path[1] != 'reset');
return this.motionHandler(accessory, path[1] != 'reset');
break;
case 'doorbell':
this.doorbellHandler(accessory);
return this.doorbellHandler(accessory);
break;
default:
return {
error: true,
message: 'First directory level must be "motion" or "doorbell", got "' + path[0] + '".'
};
}
} else {
return {
error: true,
message: 'Camera "' + name + '" not found.'
};
}
}

Expand Down Expand Up @@ -295,14 +340,19 @@ class FfmpegPlatform implements DynamicPlatformPlugin {
const hostname = this.config.localhttp ? 'localhost' : undefined;
server.listen(this.config.porthttp, hostname);
server.on('request', (request: http.IncomingMessage, response: http.ServerResponse) => {
let results: AutomationReturn = {
error: true,
message: 'Malformed URL.'
};
if (request.url) {
const parseurl = url.parse(request.url);
if (parseurl.pathname && parseurl.query) {
const name = decodeURIComponent(parseurl.query);
this.automationHandler(parseurl.pathname, name);
results = this.automationHandler(parseurl.pathname, name);
}
}
response.writeHead(200);
response.writeHead(results.error ? 500 : 200);
response.write(results.message);
response.end();
});
}
Expand Down

0 comments on commit 238197f

Please sign in to comment.