UNPKG

homebridge-eufy-security

Version:
92 lines 3.11 kB
import { Duplex } from 'stream'; import { log } from './utils.js'; export class TalkbackStream extends Duplex { eufyClient; cameraName; cameraSN; cacheData = []; talkbackStarted = false; stopTalkbackTimeout; targetStream; constructor(platform, camera) { super(); this.eufyClient = platform.eufyClient; this.cameraName = camera.getName(); this.cameraSN = camera.getSerial(); this.eufyClient.on('station talkback start', this.onTalkbackStarted); this.eufyClient.on('station talkback stop', this.onTalkbackStopped); } onTalkbackStarted(station, device, stream) { if (device.getSerial() !== this.cameraSN) { return; } log.debug(this.cameraName, 'talkback started event from station ' + station.getName()); if (this.targetStream) { this.unpipe(this.targetStream); } this.targetStream = stream; this.pipe(this.targetStream); } onTalkbackStopped(station, device) { if (device.getSerial() !== this.cameraSN) { return; } log.debug(this.cameraName, 'talkback stopped event from station ' + station.getName()); if (this.targetStream) { this.unpipe(this.targetStream); } this.targetStream = undefined; } stopTalkbackStream() { // remove event listeners this.eufyClient.removeListener('station talkback start', this.onTalkbackStarted); this.eufyClient.removeListener('station talkback stop', this.onTalkbackStopped); this.stopTalkback(); this.unpipe(); this.destroy(); } _read() { let pushReturn = true; while (this.cacheData.length > 0 && pushReturn) { const data = this.cacheData.shift(); pushReturn = this.push(data); } } _write(chunk, encoding, callback) { if (this.stopTalkbackTimeout) { clearTimeout(this.stopTalkbackTimeout); } this.stopTalkbackTimeout = setTimeout(() => { this.stopTalkback(); }, 2000); if (this.targetStream) { this.push(chunk); } else { this.cacheData.push(chunk); this.startTalkback(); } callback(); } startTalkback() { if (!this.talkbackStarted) { this.talkbackStarted = true; log.debug(this.cameraName, 'starting talkback'); this.eufyClient.startStationTalkback(this.cameraSN) .catch(error => { log.error(this.cameraName, 'talkback could not be started: ' + error); }); } } stopTalkback() { if (this.talkbackStarted) { this.talkbackStarted = false; log.debug(this.cameraName, 'stopping talkback'); this.eufyClient.stopStationTalkback(this.cameraSN) .catch(error => { log.error(this.cameraName, 'talkback could not be stopped: ' + error); }); } } } //# sourceMappingURL=Talkback.js.map