UNPKG

homebridge-eufy-security

Version:
96 lines 3.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TalkbackStream = void 0; const stream_1 = require("stream"); const utils_1 = require("./utils"); class TalkbackStream extends stream_1.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; } utils_1.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; } utils_1.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; utils_1.log.debug(this.cameraName, 'starting talkback'); this.eufyClient.startStationTalkback(this.cameraSN) .catch(error => { utils_1.log.error(this.cameraName, 'talkback could not be started: ' + error); }); } } stopTalkback() { if (this.talkbackStarted) { this.talkbackStarted = false; utils_1.log.debug(this.cameraName, 'stopping talkback'); this.eufyClient.stopStationTalkback(this.cameraSN) .catch(error => { utils_1.log.error(this.cameraName, 'talkback could not be stopped: ' + error); }); } } } exports.TalkbackStream = TalkbackStream; //# sourceMappingURL=Talkback.js.map