UNPKG

homebridge-loxone-proxy

Version:

Homebridge Dynamic Platform Plugin which exposes a Loxone System to Homekit.

163 lines 6.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Intercom = void 0; const LoxoneAccessory_1 = require("../../LoxoneAccessory"); const Doorbell_1 = require("../../homekit/services/Doorbell"); const Switch_1 = require("../../homekit/services/Switch"); const Camera_1 = require("../../homekit/services/Camera"); const CameraMotionSensor_1 = require("../../homekit/services/CameraMotionSensor"); class Intercom extends LoxoneAccessory_1.LoxoneAccessory { configureServices() { this.ItemStates = { [this.device.states.bell]: { service: 'PrimaryService', state: 'bell', }, }; void this.configureCamera(); this.registerChildItems(); this.Service.PrimaryService = new Doorbell_1.Doorbell(this.platform, this.Accessory); } async configureCamera() { var _a, _b, _c, _d, _e; const secured = await this.platform.LoxoneHandler.getsecuredDetails(this.device.uuidAction); const parsed = this.parseSecuredDetails(secured); if (!parsed) { this.platform.log.warn(`[${this.device.name}] Could not parse securedDetails JSON.`); return; } const videoInfo = parsed.videoInfo; const audioInfo = parsed.audioInfo; if (!videoInfo) { this.platform.log.debug(`[${this.device.name}] No videoInfo available.`); return; } let streamUrl; let snapshotUrl; let base64auth; if (this.device.details.deviceType === 0) { streamUrl = videoInfo.streamUrl; const user = (_a = videoInfo.user) !== null && _a !== void 0 ? _a : ''; const pass = (_b = videoInfo.pass) !== null && _b !== void 0 ? _b : ''; base64auth = Buffer.from(`${user}:${pass}`).toString('base64'); if (videoInfo.alertImage) { snapshotUrl = videoInfo.alertImage; } else { const host = this.extractHost(streamUrl); if (host) { snapshotUrl = `http://${host}/jpg/image.jpg`; } } if (!streamUrl) { this.platform.log.warn(`[${this.device.name}] Missing streamUrl for third-party camera.`); return; } this.initializeCamera(streamUrl, snapshotUrl, base64auth, undefined, this.buildTwoWayTemplateVars(audioInfo)); return; } if (videoInfo.alertImage) { const host = this.extractHost(videoInfo.alertImage); if (host) { streamUrl = `http://${host}/mjpg/video.mjpg`; snapshotUrl = `http://${host}/jpg/image.jpg`; } } if (!streamUrl && videoInfo.streamUrl) { streamUrl = videoInfo.streamUrl; snapshotUrl = (_c = videoInfo.alertImage) !== null && _c !== void 0 ? _c : undefined; } const user = (_d = videoInfo.user) !== null && _d !== void 0 ? _d : ''; const pass = (_e = videoInfo.pass) !== null && _e !== void 0 ? _e : ''; base64auth = Buffer.from(`${user}:${pass}`).toString('base64'); if (!streamUrl) { this.platform.log.warn(`[${this.device.name}] No usable stream URL for intercom.`); return; } this.initializeCamera(streamUrl, snapshotUrl, base64auth, undefined, this.buildTwoWayTemplateVars(audioInfo)); } initializeCamera(streamUrl, snapshotUrl, auth, enableSnapshotMotion, twoWayAudioTemplateVars, twoWayAudioContext) { var _a; if (enableSnapshotMotion === void 0) { enableSnapshotMotion = ((_a = this.platform.config.enableHKSV) !== null && _a !== void 0 ? _a : false); } this.camera = new Camera_1.CameraService(this.platform, this.Accessory, streamUrl, snapshotUrl, auth || '', twoWayAudioTemplateVars, twoWayAudioContext); if (enableSnapshotMotion) { this.Service['CameraMotion'] = new CameraMotionSensor_1.CameraMotionSensor(this.platform, this.Accessory, this.camera, { triggerDoorbell: () => this.Service.PrimaryService.triggerDoorbell(), }); this.platform.log.info(`[${this.device.name}] Motion sensor enabled (HKSV active).`); } else { this.platform.log.debug(`[${this.device.name}] Snapshot motion sensor not enabled for this camera.`); } this.platform.log.info(`[${this.device.name}] Camera configured: ${streamUrl}`); } registerChildItems() { if (!this.device.subControls) { return; } for (const childUuid in this.device.subControls) { const child = this.device.subControls[childUuid]; const serviceName = child.name.replace(/\s/g, ''); this.Service[serviceName] = new Switch_1.Switch(this.platform, this.Accessory, child); for (const stateName in child.states) { const uuid = child.states[stateName]; this.ItemStates[uuid] = { service: serviceName, state: stateName, }; } } } parseSecuredDetails(raw) { var _a; const parseJson = (value) => { if (typeof value === 'string') { return JSON.parse(value); } if (value && typeof value === 'object') { return value; } return undefined; }; try { const objectValue = raw; if (((_a = objectValue === null || objectValue === void 0 ? void 0 : objectValue.LL) === null || _a === void 0 ? void 0 : _a.value) !== undefined) { return parseJson(objectValue.LL.value); } return parseJson(raw); } catch (_b) { return undefined; } } extractHost(input) { if (!input) { return undefined; } try { return new URL(input).host; } catch (_a) { const match = input.match(/^(?:https?:\/\/)?([^/?#]+)/i); return match === null || match === void 0 ? void 0 : match[1]; } } buildTwoWayTemplateVars(audioInfo) { const vars = {}; if (!audioInfo) { return vars; } if (audioInfo.host) { vars.audio_host = audioInfo.host; } if (audioInfo.user) { vars.audio_user = audioInfo.user; } if (audioInfo.pass) { vars.audio_pass = audioInfo.pass; } return vars; } } exports.Intercom = Intercom; //# sourceMappingURL=Intercom.js.map