From 238197fc05f0b048d161ef9c988d713acc807010 Mon Sep 17 00:00:00 2001 From: David Maher Date: Sat, 15 Aug 2020 20:41:27 -0400 Subject: [PATCH] Add HTTP automation messages --- CHANGELOG.md | 12 +++++++- config.schema.json | 1 - package.json | 2 +- src/index.ts | 70 +++++++++++++++++++++++++++++++++++++++------- 4 files changed, 72 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1a53c76..79f2f4a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/config.schema.json b/config.schema.json index b2a69a93..4d56580d 100644 --- a/config.schema.json +++ b/config.schema.json @@ -253,7 +253,6 @@ "debug": { "title": "Debug Logging", "type": "boolean", - "default": true, "description": "Includes debugging output from FFmpeg in the Homebridge log." } } diff --git a/package.json b/package.json index 2ffcc8b5..2314df3c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index 0744ce47..6ca71c51 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; @@ -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); @@ -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); @@ -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.' + }; } } @@ -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(); }); }