diff --git a/plugins/unifi-protect/package-lock.json b/plugins/unifi-protect/package-lock.json index 45776fc72..6de36107b 100644 --- a/plugins/unifi-protect/package-lock.json +++ b/plugins/unifi-protect/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/unifi-protect", - "version": "0.0.67", + "version": "0.0.68", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/unifi-protect", - "version": "0.0.67", + "version": "0.0.68", "license": "Apache", "dependencies": { "@koush/unifi-protect": "^0.0.7", diff --git a/plugins/unifi-protect/package.json b/plugins/unifi-protect/package.json index b7b183aeb..42b137379 100644 --- a/plugins/unifi-protect/package.json +++ b/plugins/unifi-protect/package.json @@ -1,6 +1,6 @@ { "name": "@scrypted/unifi-protect", - "version": "0.0.67", + "version": "0.0.68", "description": "Unifi Protect Plugin for Scrypted", "author": "Scrypted", "license": "Apache", diff --git a/plugins/unifi-protect/src/main.ts b/plugins/unifi-protect/src/main.ts index fd4d3fba2..fb533fc21 100644 --- a/plugins/unifi-protect/src/main.ts +++ b/plugins/unifi-protect/src/main.ts @@ -8,6 +8,7 @@ import { createInstanceableProviderPlugin, enableInstanceableProviderMode, isIns import { recommendRebroadcast } from "../../rtsp/src/recommend"; import { fitHeightToWidth } from "../../../common/src/resolution-utils"; import { listenZero } from "../../../common/src/listen-cluster"; +import { readLength } from "../../../common/src/read-length"; import net from 'net'; import WS from 'ws'; import { once } from "events"; @@ -24,7 +25,6 @@ class UnifiCamera extends ScryptedDeviceBase implements Camera, VideoCamera, Vid lastMotion: number; lastRing: number; lastSeen: number; - talkbackUrl: string; constructor(protect: UnifiProtect, nativeId: string, protectCamera: Readonly) { super(nativeId); @@ -281,39 +281,35 @@ class UnifiDoorbell extends UnifiCamera implements Intercom, Notifier { const ffmpegInput = JSON.parse(buffer.toString()) as FFMpegInput; const camera = this.findCamera(); - if (!this.talkbackUrl) { - const params = new URLSearchParams({ camera: camera.id }); - const response = await this.protect.api.loginFetch(this.protect.api.wsUrl() + "/talkback?" + params.toString()); - const tb = await response.json() as Record; + const params = new URLSearchParams({ camera: camera.id }); + const response = await this.protect.api.loginFetch(this.protect.api.wsUrl() + "/talkback?" + params.toString()); + const tb = await response.json() as Record; - // Adjust the URL for our address. - const tbUrl = new URL(tb.url); - tbUrl.hostname = this.protect.getSetting('ip'); - this.talkbackUrl = tbUrl.toString(); - } + // Adjust the URL for our address. + const tbUrl = new URL(tb.url); + tbUrl.hostname = this.protect.getSetting('ip'); + const talkbackUrl = tbUrl.toString(); - const websocket = new WS(this.talkbackUrl, { rejectUnauthorized: false }); + const websocket = new WS(talkbackUrl, { rejectUnauthorized: false }); const server = new net.Server(async (socket) => { server.close(); - this.console.log('sending audio data to', this.talkbackUrl); + this.console.log('sending audio data to', talkbackUrl); try { while (true) { - await once(socket, 'readable'); - while (true) { - const data = socket.read(); - if (!data) - break; - websocket.send(data, e => { - if (e) - socket.destroy(); - }); - } + const data = await readLength(socket, 1024); + if (!data) + break; + websocket.send(data, e => { + if (e) + socket.destroy(); + }); } } finally { + websocket.close(); this.cp.kill(); } });