From bd045ffb484be9f9b8263adf93b46e87b6fd2e40 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Sun, 12 Dec 2021 11:18:14 -0800 Subject: [PATCH] homekit: autopopulate bridge addresses from recording requests. --- external/HAP-NodeJS | 2 +- plugins/homekit/package-lock.json | 4 +-- plugins/homekit/package.json | 2 +- plugins/homekit/src/common.ts | 1 + plugins/homekit/src/main.ts | 42 +++++++++++++++++++++++------ plugins/homekit/src/types/camera.ts | 5 ++-- 6 files changed, 42 insertions(+), 14 deletions(-) diff --git a/external/HAP-NodeJS b/external/HAP-NodeJS index 4b9f639a9..a400094fc 160000 --- a/external/HAP-NodeJS +++ b/external/HAP-NodeJS @@ -1 +1 @@ -Subproject commit 4b9f639a9375457b7e9e857d1031e7a17520ac89 +Subproject commit a400094fc3b62d74a07a2a4d5104fc1b1fc9849c diff --git a/plugins/homekit/package-lock.json b/plugins/homekit/package-lock.json index 0494aa962..4f7d91691 100644 --- a/plugins/homekit/package-lock.json +++ b/plugins/homekit/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/homekit", - "version": "0.0.133", + "version": "0.0.134", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/homekit", - "version": "0.0.133", + "version": "0.0.134", "dependencies": { "hap-nodejs": "file:../../external/HAP-NodeJS", "lodash": "^4.17.21", diff --git a/plugins/homekit/package.json b/plugins/homekit/package.json index 88a065c58..4a5d84299 100644 --- a/plugins/homekit/package.json +++ b/plugins/homekit/package.json @@ -40,5 +40,5 @@ "@types/qrcode": "^1.4.1", "@types/url-parse": "^1.4.3" }, - "version": "0.0.133" + "version": "0.0.134" } diff --git a/plugins/homekit/src/common.ts b/plugins/homekit/src/common.ts index 250610ad0..4d522edb3 100644 --- a/plugins/homekit/src/common.ts +++ b/plugins/homekit/src/common.ts @@ -15,6 +15,7 @@ export interface SnapshotThrottle { export interface HomeKitSession { snapshotThrottles: Map; isHomeKitHub(ip: string): boolean; + detectedHomeKitHub(ip: string): void; } interface SupportedType { diff --git a/plugins/homekit/src/main.ts b/plugins/homekit/src/main.ts index af036fa38..6be278882 100644 --- a/plugins/homekit/src/main.ts +++ b/plugins/homekit/src/main.ts @@ -44,14 +44,6 @@ class HomeKit extends ScryptedDeviceBase implements MixinProvider, Settings, Hom return username; } - getHomeKitHubs(): string[] { - try { - return JSON.parse(this.storage.getItem('homekitHubs')); - } - catch (e) { - } - } - async getSettings(): Promise { const addresses = Object.entries(os.networkInterfaces()).filter(([iface]) => iface.startsWith('en') || iface.startsWith('eth') || iface.startsWith('wlan')).map(([_, addr]) => addr).flat().map(info => info.address).filter(address => address); return [ @@ -292,10 +284,44 @@ class HomeKit extends ScryptedDeviceBase implements MixinProvider, Settings, Hom return parseInt(this.storage.getItem('portOverride')) || 0; } + getHomeKitHubs(): string[] { + try { + return JSON.parse(this.storage.getItem('homekitHubs')); + } + catch (e) { + } + } + + getAutoHomeKitHubs(): string[] { + try { + return JSON.parse(this.storage.getItem('autoHomekitHubs')); + } + catch (e) { + return []; + } + } + isHomeKitHub(address: string) { return !!this.getHomeKitHubs()?.find(check => check.endsWith(address)); } + detectedHomeKitHub(ip: string) { + try { + const homekitHubs = this.getHomeKitHubs(); + if (homekitHubs?.includes(ip)) + return; + const autoHomekitHubs = this.getAutoHomeKitHubs(); + if (autoHomekitHubs.includes(ip)) + return; + autoHomekitHubs.push(ip); + homekitHubs.push(ip); + this.storage.setItem('autoHomekitHubs', JSON.stringify(autoHomekitHubs)); + this.storage.setItem('homekitHubs', JSON.stringify(homekitHubs)); + } + catch (e) { + } + } + async canMixin(type: ScryptedDeviceType, interfaces: string[]) { const supportedType = supportedTypes[type]; if (!supportedType?.probe({ diff --git a/plugins/homekit/src/types/camera.ts b/plugins/homekit/src/types/camera.ts index 311d4b7c7..b9b263f8d 100644 --- a/plugins/homekit/src/types/camera.ts +++ b/plugins/homekit/src/types/camera.ts @@ -19,7 +19,7 @@ import { Active, ContactSensor } from 'hap-nodejs/src/lib/definitions'; import { handleFragmentsRequests, iframeIntervalSeconds } from './camera/camera-recording'; import { createSnapshotHandler } from './camera/camera-snapshot'; import { evalRequest } from './camera/camera-transcode'; -import { CharacteristicEventTypes, Service, WithUUID } from 'hap-nodejs/src'; +import { CharacteristicEventTypes, DataStreamConnection, Service, WithUUID } from 'hap-nodejs/src'; import { RecordingManagement } from 'hap-nodejs/src/lib/camera'; import { defaultObjectDetectionContactSensorTimeout } from '../camera-mixin'; @@ -446,7 +446,8 @@ addSupportedType({ if (linkedMotionSensor || device.interfaces.includes(ScryptedInterface.MotionSensor) || needAudioMotionService) { recordingDelegate = { - handleFragmentsRequests(): AsyncGenerator { + handleFragmentsRequests(connection: DataStreamConnection): AsyncGenerator { + homekitSession.detectedHomeKitHub(connection.remoteAddress); const configuration = RecordingManagement.parseSelectedConfiguration(storage.getItem(storageKeySelectedRecordingConfiguration)) return handleFragmentsRequests(device, configuration, console) }