homebridge-loxone-proxy 1.12.2-beta.4 → 1.13.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config.schema.json +21 -1
- package/dist/homekit/hksv/LoxoneTalkback.d.ts +70 -0
- package/dist/homekit/hksv/LoxoneTalkback.d.ts.map +1 -0
- package/dist/homekit/hksv/LoxoneTalkback.js +578 -0
- package/dist/homekit/hksv/LoxoneTalkback.js.map +1 -0
- package/dist/homekit/hksv/RecordingDelegate.d.ts +8 -1
- package/dist/homekit/hksv/RecordingDelegate.d.ts.map +1 -1
- package/dist/homekit/hksv/RecordingDelegate.js +124 -34
- package/dist/homekit/hksv/RecordingDelegate.js.map +1 -1
- package/dist/homekit/hksv/StreamingDelegate.d.ts +21 -3
- package/dist/homekit/hksv/StreamingDelegate.d.ts.map +1 -1
- package/dist/homekit/hksv/StreamingDelegate.js +291 -64
- package/dist/homekit/hksv/StreamingDelegate.js.map +1 -1
- package/dist/homekit/services/Camera.d.ts +10 -3
- package/dist/homekit/services/Camera.d.ts.map +1 -1
- package/dist/homekit/services/Camera.js +38 -27
- package/dist/homekit/services/Camera.js.map +1 -1
- package/dist/homekit/services/CameraMotionSensor.d.ts +14 -10
- package/dist/homekit/services/CameraMotionSensor.d.ts.map +1 -1
- package/dist/homekit/services/CameraMotionSensor.js +121 -83
- package/dist/homekit/services/CameraMotionSensor.js.map +1 -1
- package/dist/loxone/LoxoneHandler.d.ts +1 -0
- package/dist/loxone/LoxoneHandler.d.ts.map +1 -1
- package/dist/loxone/LoxoneHandler.js +21 -0
- package/dist/loxone/LoxoneHandler.js.map +1 -1
- package/dist/loxone/items/Intercom.d.ts +5 -2
- package/dist/loxone/items/Intercom.d.ts.map +1 -1
- package/dist/loxone/items/Intercom.js +125 -26
- package/dist/loxone/items/Intercom.js.map +1 -1
- package/dist/loxone/items/IntercomV2.d.ts +5 -3
- package/dist/loxone/items/IntercomV2.d.ts.map +1 -1
- package/dist/loxone/items/IntercomV2.js +104 -24
- package/dist/loxone/items/IntercomV2.js.map +1 -1
- package/package.json +6 -3
package/config.schema.json
CHANGED
|
@@ -62,6 +62,23 @@
|
|
|
62
62
|
"type": "boolean",
|
|
63
63
|
"title": "Use Motion to trigger Doorbell event",
|
|
64
64
|
"default": false
|
|
65
|
+
},
|
|
66
|
+
"JpegHeaderSize": {
|
|
67
|
+
"type": "integer",
|
|
68
|
+
"title": "JPEG Header Size Offset (bytes)",
|
|
69
|
+
"description": "Adjust motion detection baseline when cameras prepend variable JPEG metadata.",
|
|
70
|
+
"default": 623
|
|
71
|
+
},
|
|
72
|
+
"EnableTwoWayAudio": {
|
|
73
|
+
"type": "boolean",
|
|
74
|
+
"title": "Enable Two-Way Audio (experimental)",
|
|
75
|
+
"description": "Enables HomeKit return audio. Loxone Intercom V2 uses automatic WebRTC talkback (no FFmpeg output args needed).",
|
|
76
|
+
"default": false
|
|
77
|
+
},
|
|
78
|
+
"TwoWayAudioOutputArgs": {
|
|
79
|
+
"type": "string",
|
|
80
|
+
"title": "Two-Way Audio FFmpeg Output Args",
|
|
81
|
+
"description": "Optional for non-Loxone cameras: FFmpeg output arguments for return audio. Placeholders: {camera_host}, {stream_url}, {audio_host}, {audio_user}, {audio_pass}. Example: -ac 1 -ar 8000 -acodec pcm_mulaw -f rtp rtp://{camera_host}:5004"
|
|
65
82
|
}
|
|
66
83
|
}
|
|
67
84
|
},
|
|
@@ -240,7 +257,10 @@
|
|
|
240
257
|
"title": "Advanced",
|
|
241
258
|
"description": "",
|
|
242
259
|
"items": [
|
|
243
|
-
"Advanced.NfcCodeTouchMapping"
|
|
260
|
+
"Advanced.NfcCodeTouchMapping",
|
|
261
|
+
"Advanced.JpegHeaderSize",
|
|
262
|
+
"Advanced.EnableTwoWayAudio",
|
|
263
|
+
"Advanced.TwoWayAudioOutputArgs"
|
|
244
264
|
]
|
|
245
265
|
}
|
|
246
266
|
]
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { LoxonePlatform } from '../../LoxonePlatform';
|
|
4
|
+
export interface LoxoneTalkbackOptions {
|
|
5
|
+
platform: LoxonePlatform;
|
|
6
|
+
cameraName: string;
|
|
7
|
+
signalingBaseUrl: string;
|
|
8
|
+
username: string;
|
|
9
|
+
getToken: () => string | undefined;
|
|
10
|
+
}
|
|
11
|
+
export declare class LoxoneTalkbackSession {
|
|
12
|
+
private readonly options;
|
|
13
|
+
private readonly rpcTimeoutMs;
|
|
14
|
+
private readonly authMethod;
|
|
15
|
+
private readonly wsProtocol;
|
|
16
|
+
private readonly pcmSampleRate;
|
|
17
|
+
private readonly pcmChannelCount;
|
|
18
|
+
private readonly pcmBitsPerSample;
|
|
19
|
+
private readonly pcmFramesPerChunk;
|
|
20
|
+
private socket?;
|
|
21
|
+
private wrtc?;
|
|
22
|
+
private peerConnection?;
|
|
23
|
+
private audioSource?;
|
|
24
|
+
private localTrack?;
|
|
25
|
+
private rpcId;
|
|
26
|
+
private pendingRpc;
|
|
27
|
+
private localIceQueue;
|
|
28
|
+
private remoteIceQueue;
|
|
29
|
+
private peerReady;
|
|
30
|
+
private stopped;
|
|
31
|
+
private started;
|
|
32
|
+
private pcmBuffer;
|
|
33
|
+
private authChallengeTimer?;
|
|
34
|
+
private readyPromise?;
|
|
35
|
+
private resolveReady?;
|
|
36
|
+
private rejectReady?;
|
|
37
|
+
constructor(options: LoxoneTalkbackOptions);
|
|
38
|
+
start(): Promise<void>;
|
|
39
|
+
pushPcmChunk(chunk: Buffer): void;
|
|
40
|
+
stop(): void;
|
|
41
|
+
private connectAndAuthorize;
|
|
42
|
+
private getDeviceInfo;
|
|
43
|
+
private initializePeerConnection;
|
|
44
|
+
private flushQueuedIceCandidates;
|
|
45
|
+
private sendLocalIceCandidate;
|
|
46
|
+
private handleSocketMessage;
|
|
47
|
+
private handleRpcRequest;
|
|
48
|
+
private handleNotification;
|
|
49
|
+
private handleRpcResponse;
|
|
50
|
+
private onRemoteIceCandidate;
|
|
51
|
+
private createAuthResponse;
|
|
52
|
+
private createRsaPublicKey;
|
|
53
|
+
private callRpc;
|
|
54
|
+
private sendNotification;
|
|
55
|
+
private sendRpcSuccess;
|
|
56
|
+
private sendRpcError;
|
|
57
|
+
private sendJson;
|
|
58
|
+
private rejectAllPendingRpc;
|
|
59
|
+
private resolveReadyPromise;
|
|
60
|
+
private rejectReadyPromise;
|
|
61
|
+
private clearAuthTimers;
|
|
62
|
+
private toWsUrl;
|
|
63
|
+
private normalizeSessionDescription;
|
|
64
|
+
private rawDataToString;
|
|
65
|
+
private stringValue;
|
|
66
|
+
private numberValue;
|
|
67
|
+
private hexToBase64Url;
|
|
68
|
+
private loadWrtcModule;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=LoxoneTalkback.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LoxoneTalkback.d.ts","sourceRoot":"","sources":["../../../src/homekit/hksv/LoxoneTalkback.ts"],"names":[],"mappings":";;AASA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAkFtD,MAAM,WAAW,qBAAqB;IACpC,QAAQ,EAAE,cAAc,CAAC;IACzB,UAAU,EAAE,MAAM,CAAC;IACnB,gBAAgB,EAAE,MAAM,CAAC;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,MAAM,GAAG,SAAS,CAAC;CACpC;AAED,qBAAa,qBAAqB;IA8B9B,OAAO,CAAC,QAAQ,CAAC,OAAO;IA7B1B,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAkB;IAC7C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAsB;IACjD,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IACvC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAK;IACrC,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAM;IACvC,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAO;IAEzC,OAAO,CAAC,MAAM,CAAC,CAAY;IAC3B,OAAO,CAAC,IAAI,CAAC,CAAiB;IAC9B,OAAO,CAAC,cAAc,CAAC,CAAwB;IAC/C,OAAO,CAAC,WAAW,CAAC,CAAqB;IACzC,OAAO,CAAC,UAAU,CAAC,CAA0B;IAE7C,OAAO,CAAC,KAAK,CAAK;IAClB,OAAO,CAAC,UAAU,CAAiC;IACnD,OAAO,CAAC,aAAa,CAA6B;IAClD,OAAO,CAAC,cAAc,CAA6B;IACnD,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,SAAS,CAAmB;IAEpC,OAAO,CAAC,kBAAkB,CAAC,CAAiB;IAC5C,OAAO,CAAC,YAAY,CAAC,CAAgB;IACrC,OAAO,CAAC,YAAY,CAAC,CAAa;IAClC,OAAO,CAAC,WAAW,CAAC,CAAyB;gBAG1B,OAAO,EAAE,qBAAqB;IAGpC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAmB5B,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAkCjC,IAAI,IAAI,IAAI;YAoDL,mBAAmB;YAqCnB,aAAa;YAQb,wBAAwB;YAiFxB,wBAAwB;YAyBxB,qBAAqB;YAkBrB,mBAAmB;YA2BnB,gBAAgB;IA4B9B,OAAO,CAAC,kBAAkB;IAY1B,OAAO,CAAC,iBAAiB;YAqBX,oBAAoB;IAwClC,OAAO,CAAC,kBAAkB;IAwC1B,OAAO,CAAC,kBAAkB;YAyBZ,OAAO;YAiCP,gBAAgB;IAW9B,OAAO,CAAC,cAAc;IAgBtB,OAAO,CAAC,YAAY;IAYpB,OAAO,CAAC,QAAQ;IAOhB,OAAO,CAAC,mBAAmB;IAQ3B,OAAO,CAAC,mBAAmB;IAQ3B,OAAO,CAAC,kBAAkB;IAQ1B,OAAO,CAAC,eAAe;IAOvB,OAAO,CAAC,OAAO;IAMf,OAAO,CAAC,2BAA2B;IAenC,OAAO,CAAC,eAAe;IAgBvB,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,WAAW;IAWnB,OAAO,CAAC,cAAc;IAStB,OAAO,CAAC,cAAc;CAQvB"}
|
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.LoxoneTalkbackSession = void 0;
|
|
7
|
+
const ws_1 = __importDefault(require("ws"));
|
|
8
|
+
const crypto_1 = require("crypto");
|
|
9
|
+
class LoxoneTalkbackSession {
|
|
10
|
+
constructor(options) {
|
|
11
|
+
this.options = options;
|
|
12
|
+
this.rpcTimeoutMs = 10000;
|
|
13
|
+
this.authMethod = 'authenticate';
|
|
14
|
+
this.wsProtocol = 'webrtc-signaling';
|
|
15
|
+
this.pcmSampleRate = 48000;
|
|
16
|
+
this.pcmChannelCount = 1;
|
|
17
|
+
this.pcmBitsPerSample = 16;
|
|
18
|
+
this.pcmFramesPerChunk = 480;
|
|
19
|
+
this.rpcId = 0;
|
|
20
|
+
this.pendingRpc = new Map();
|
|
21
|
+
this.localIceQueue = [];
|
|
22
|
+
this.remoteIceQueue = [];
|
|
23
|
+
this.peerReady = false;
|
|
24
|
+
this.stopped = false;
|
|
25
|
+
this.started = false;
|
|
26
|
+
this.pcmBuffer = Buffer.alloc(0);
|
|
27
|
+
}
|
|
28
|
+
async start() {
|
|
29
|
+
if (this.started) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
this.started = true;
|
|
33
|
+
try {
|
|
34
|
+
await this.connectAndAuthorize();
|
|
35
|
+
const info = await this.getDeviceInfo();
|
|
36
|
+
await this.initializePeerConnection(info);
|
|
37
|
+
this.options.platform.log.info(`[${this.options.cameraName}] Loxone WebRTC talkback signaling established.`);
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
this.stop();
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
pushPcmChunk(chunk) {
|
|
45
|
+
if (this.stopped || !this.audioSource || chunk.length === 0) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
this.pcmBuffer = this.pcmBuffer.length === 0
|
|
49
|
+
? Buffer.from(chunk)
|
|
50
|
+
: Buffer.concat([this.pcmBuffer, chunk]);
|
|
51
|
+
const frameBytes = this.pcmFramesPerChunk * this.pcmChannelCount * (this.pcmBitsPerSample / 8);
|
|
52
|
+
while (this.pcmBuffer.length >= frameBytes) {
|
|
53
|
+
const frame = this.pcmBuffer.subarray(0, frameBytes);
|
|
54
|
+
this.pcmBuffer = this.pcmBuffer.subarray(frameBytes);
|
|
55
|
+
const samples = new Int16Array(this.pcmFramesPerChunk);
|
|
56
|
+
Buffer.from(samples.buffer).set(frame);
|
|
57
|
+
try {
|
|
58
|
+
this.audioSource.onData({
|
|
59
|
+
samples,
|
|
60
|
+
sampleRate: this.pcmSampleRate,
|
|
61
|
+
bitsPerSample: this.pcmBitsPerSample,
|
|
62
|
+
channelCount: this.pcmChannelCount,
|
|
63
|
+
numberOfFrames: this.pcmFramesPerChunk,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
68
|
+
this.options.platform.log.debug(`[${this.options.cameraName}] Failed to feed PCM frame to WebRTC source: ${message}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
stop() {
|
|
73
|
+
var _a, _b, _c;
|
|
74
|
+
if (this.stopped) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
this.stopped = true;
|
|
78
|
+
this.clearAuthTimers();
|
|
79
|
+
try {
|
|
80
|
+
this.sendJson({
|
|
81
|
+
jsonrpc: '2.0',
|
|
82
|
+
method: 'hangup',
|
|
83
|
+
id: this.rpcId++,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
catch (_d) {
|
|
87
|
+
}
|
|
88
|
+
this.rejectAllPendingRpc(new Error('Talkback stopped'));
|
|
89
|
+
this.rejectReadyPromise(new Error('Talkback stopped'));
|
|
90
|
+
try {
|
|
91
|
+
(_b = (_a = this.localTrack) === null || _a === void 0 ? void 0 : _a.stop) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
92
|
+
}
|
|
93
|
+
catch (_e) {
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
(_c = this.peerConnection) === null || _c === void 0 ? void 0 : _c.close();
|
|
97
|
+
}
|
|
98
|
+
catch (_f) {
|
|
99
|
+
}
|
|
100
|
+
this.peerConnection = undefined;
|
|
101
|
+
this.audioSource = undefined;
|
|
102
|
+
this.localTrack = undefined;
|
|
103
|
+
this.peerReady = false;
|
|
104
|
+
this.localIceQueue = [];
|
|
105
|
+
this.remoteIceQueue = [];
|
|
106
|
+
this.pcmBuffer = Buffer.alloc(0);
|
|
107
|
+
if (this.socket) {
|
|
108
|
+
try {
|
|
109
|
+
this.socket.removeAllListeners();
|
|
110
|
+
this.socket.close();
|
|
111
|
+
}
|
|
112
|
+
catch (_g) {
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
this.socket = undefined;
|
|
116
|
+
}
|
|
117
|
+
async connectAndAuthorize() {
|
|
118
|
+
const wsUrl = this.toWsUrl(this.options.signalingBaseUrl);
|
|
119
|
+
this.readyPromise = new Promise((resolve, reject) => {
|
|
120
|
+
this.resolveReady = resolve;
|
|
121
|
+
this.rejectReady = reject;
|
|
122
|
+
});
|
|
123
|
+
this.socket = new ws_1.default(wsUrl, this.wsProtocol);
|
|
124
|
+
this.socket.on('open', () => {
|
|
125
|
+
this.authChallengeTimer = setTimeout(() => {
|
|
126
|
+
this.rejectReadyPromise(new Error('No auth challenge received from Loxone intercom'));
|
|
127
|
+
}, this.rpcTimeoutMs);
|
|
128
|
+
});
|
|
129
|
+
this.socket.on('message', (data) => {
|
|
130
|
+
void this.handleSocketMessage(data);
|
|
131
|
+
});
|
|
132
|
+
this.socket.on('error', (error) => {
|
|
133
|
+
this.rejectReadyPromise(error);
|
|
134
|
+
});
|
|
135
|
+
this.socket.on('close', (code, reason) => {
|
|
136
|
+
const reasonText = reason.toString('utf8') || `code ${code}`;
|
|
137
|
+
const error = new Error(`Signaling socket closed: ${reasonText}`);
|
|
138
|
+
this.rejectReadyPromise(error);
|
|
139
|
+
if (!this.stopped) {
|
|
140
|
+
this.options.platform.log.debug(`[${this.options.cameraName}] Loxone signaling socket closed: ${reasonText}`);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
await this.readyPromise;
|
|
144
|
+
}
|
|
145
|
+
async getDeviceInfo() {
|
|
146
|
+
const info = await this.callRpc('info');
|
|
147
|
+
if (!info || typeof info !== 'object') {
|
|
148
|
+
return {};
|
|
149
|
+
}
|
|
150
|
+
return info;
|
|
151
|
+
}
|
|
152
|
+
async initializePeerConnection(info) {
|
|
153
|
+
var _a, _b;
|
|
154
|
+
const callState = this.numberValue(info.callState);
|
|
155
|
+
if (callState !== undefined && callState !== 0) {
|
|
156
|
+
throw new Error(`Loxone intercom is occupied (callState=${callState})`);
|
|
157
|
+
}
|
|
158
|
+
this.wrtc = this.loadWrtcModule();
|
|
159
|
+
const audioSourceCtor = (_a = this.wrtc.nonstandard) === null || _a === void 0 ? void 0 : _a.RTCAudioSource;
|
|
160
|
+
if (!audioSourceCtor) {
|
|
161
|
+
throw new Error('wrtc nonstandard.RTCAudioSource is not available');
|
|
162
|
+
}
|
|
163
|
+
this.audioSource = new audioSourceCtor();
|
|
164
|
+
this.localTrack = this.audioSource.createTrack();
|
|
165
|
+
const rtcConfig = {
|
|
166
|
+
iceServers: [
|
|
167
|
+
{ urls: ['stun:stun.loxonecloud.com:3478'] },
|
|
168
|
+
{ urls: ['stun:stun.l.google.com:19302'] },
|
|
169
|
+
],
|
|
170
|
+
iceTransportPolicy: 'all',
|
|
171
|
+
};
|
|
172
|
+
const turnUser = this.stringValue(info.turnuser);
|
|
173
|
+
const turnPass = this.stringValue(info.turnpass);
|
|
174
|
+
if (turnUser && turnPass) {
|
|
175
|
+
const iceServers = rtcConfig.iceServers;
|
|
176
|
+
iceServers.push({
|
|
177
|
+
urls: ['turn:stun.loxonecloud.com:3478'],
|
|
178
|
+
username: turnUser,
|
|
179
|
+
credential: turnPass,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
this.peerConnection = new this.wrtc.RTCPeerConnection(rtcConfig);
|
|
183
|
+
this.peerConnection.onicecandidate = (event) => {
|
|
184
|
+
const candidate = event.candidate;
|
|
185
|
+
if (candidate) {
|
|
186
|
+
if (this.peerReady) {
|
|
187
|
+
void this.sendLocalIceCandidate(candidate);
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
this.localIceQueue.push(candidate);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
void this.sendNotification('iceGatheringFinished', null);
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
this.peerConnection.onconnectionstatechange = () => {
|
|
198
|
+
var _a;
|
|
199
|
+
const state = (_a = this.peerConnection) === null || _a === void 0 ? void 0 : _a.connectionState;
|
|
200
|
+
if (state === 'failed' || state === 'disconnected' || state === 'closed') {
|
|
201
|
+
this.options.platform.log.debug(`[${this.options.cameraName}] Loxone WebRTC connection state: ${state}`);
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
this.peerConnection.addTrack(this.localTrack);
|
|
205
|
+
const offer = await this.peerConnection.createOffer({
|
|
206
|
+
offerToReceiveAudio: false,
|
|
207
|
+
offerToReceiveVideo: false,
|
|
208
|
+
});
|
|
209
|
+
await this.peerConnection.setLocalDescription(offer);
|
|
210
|
+
const offerPayload = {
|
|
211
|
+
type: offer.type,
|
|
212
|
+
sdp: (_b = offer.sdp) !== null && _b !== void 0 ? _b : '',
|
|
213
|
+
};
|
|
214
|
+
const answerRaw = await this.callRpc('call', [
|
|
215
|
+
offerPayload,
|
|
216
|
+
'add_audio',
|
|
217
|
+
false,
|
|
218
|
+
0,
|
|
219
|
+
]);
|
|
220
|
+
const answer = this.normalizeSessionDescription(answerRaw);
|
|
221
|
+
await this.peerConnection.setRemoteDescription(new this.wrtc.RTCSessionDescription(answer));
|
|
222
|
+
this.peerReady = true;
|
|
223
|
+
await this.flushQueuedIceCandidates();
|
|
224
|
+
}
|
|
225
|
+
async flushQueuedIceCandidates() {
|
|
226
|
+
var _a;
|
|
227
|
+
while (this.localIceQueue.length > 0) {
|
|
228
|
+
const candidate = this.localIceQueue.shift();
|
|
229
|
+
if (!candidate) {
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
await this.sendLocalIceCandidate(candidate);
|
|
233
|
+
}
|
|
234
|
+
while (this.remoteIceQueue.length > 0) {
|
|
235
|
+
const candidate = this.remoteIceQueue.shift();
|
|
236
|
+
if (!candidate) {
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
try {
|
|
240
|
+
await ((_a = this.peerConnection) === null || _a === void 0 ? void 0 : _a.addIceCandidate(candidate));
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
244
|
+
this.options.platform.log.debug(`[${this.options.cameraName}] Failed to flush remote ICE candidate: ${message}`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
async sendLocalIceCandidate(candidate) {
|
|
249
|
+
var _a;
|
|
250
|
+
if (!candidate.candidate) {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
try {
|
|
254
|
+
await this.callRpc('addIceCandidate', [
|
|
255
|
+
candidate.candidate,
|
|
256
|
+
(_a = candidate.sdpMLineIndex) !== null && _a !== void 0 ? _a : 0,
|
|
257
|
+
]);
|
|
258
|
+
}
|
|
259
|
+
catch (error) {
|
|
260
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
261
|
+
this.options.platform.log.debug(`[${this.options.cameraName}] Failed to send local ICE candidate: ${message}`);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
async handleSocketMessage(data) {
|
|
265
|
+
var _a, _b;
|
|
266
|
+
const payload = this.rawDataToString(data);
|
|
267
|
+
let rpc;
|
|
268
|
+
try {
|
|
269
|
+
rpc = JSON.parse(payload);
|
|
270
|
+
}
|
|
271
|
+
catch (error) {
|
|
272
|
+
this.options.platform.log.debug(`[${this.options.cameraName}] Invalid JSON received from signaling channel.`);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
if (typeof rpc.method === 'string') {
|
|
276
|
+
if (typeof rpc.id === 'number') {
|
|
277
|
+
await this.handleRpcRequest(rpc.id, rpc.method, (_a = rpc.params) !== null && _a !== void 0 ? _a : []);
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
this.handleNotification(rpc.method, (_b = rpc.params) !== null && _b !== void 0 ? _b : []);
|
|
281
|
+
}
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (typeof rpc.id === 'number') {
|
|
285
|
+
this.handleRpcResponse(rpc);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
async handleRpcRequest(id, method, params) {
|
|
289
|
+
switch (method) {
|
|
290
|
+
case this.authMethod: {
|
|
291
|
+
try {
|
|
292
|
+
const authData = this.createAuthResponse(params);
|
|
293
|
+
this.sendRpcSuccess(id, authData);
|
|
294
|
+
}
|
|
295
|
+
catch (error) {
|
|
296
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
297
|
+
this.sendRpcError(id, -32062, 'Invalid params', message);
|
|
298
|
+
this.rejectReadyPromise(new Error(message));
|
|
299
|
+
}
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
case 'addIceCandidate': {
|
|
303
|
+
await this.onRemoteIceCandidate(params);
|
|
304
|
+
this.sendRpcSuccess(id);
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
case 'reachMode': {
|
|
308
|
+
this.sendRpcSuccess(id, [0]);
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
default:
|
|
312
|
+
this.sendRpcError(id, -32061, `Method not found: ${method}`);
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
handleNotification(method, _params) {
|
|
317
|
+
if (method === 'ready') {
|
|
318
|
+
this.clearAuthTimers();
|
|
319
|
+
this.resolveReadyPromise();
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (method === 'kick' && !this.stopped) {
|
|
323
|
+
this.options.platform.log.debug(`[${this.options.cameraName}] Loxone signaling kick received.`);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
handleRpcResponse(rpc) {
|
|
327
|
+
var _a;
|
|
328
|
+
if (typeof rpc.id !== 'number') {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
const pending = this.pendingRpc.get(rpc.id);
|
|
332
|
+
if (!pending) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
clearTimeout(pending.timeout);
|
|
336
|
+
this.pendingRpc.delete(rpc.id);
|
|
337
|
+
if (rpc.error) {
|
|
338
|
+
pending.reject(new Error(`${pending.method}: ${rpc.error.message}`));
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
pending.resolve((_a = rpc.result) === null || _a === void 0 ? void 0 : _a.data);
|
|
342
|
+
}
|
|
343
|
+
async onRemoteIceCandidate(params) {
|
|
344
|
+
if (!this.wrtc || !this.peerConnection) {
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
const candidateStr = this.stringValue(params[0]);
|
|
348
|
+
if (!candidateStr) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
const init = {
|
|
352
|
+
candidate: candidateStr,
|
|
353
|
+
sdpMLineIndex: this.numberValue(params[1]),
|
|
354
|
+
};
|
|
355
|
+
const sdpMid = this.stringValue(params[2]);
|
|
356
|
+
if (sdpMid) {
|
|
357
|
+
init.sdpMid = sdpMid;
|
|
358
|
+
}
|
|
359
|
+
const usernameFragment = this.stringValue(params[3]);
|
|
360
|
+
if (usernameFragment) {
|
|
361
|
+
init.usernameFragment = usernameFragment;
|
|
362
|
+
}
|
|
363
|
+
const candidate = new this.wrtc.RTCIceCandidate(init);
|
|
364
|
+
if (this.peerReady) {
|
|
365
|
+
try {
|
|
366
|
+
await this.peerConnection.addIceCandidate(candidate);
|
|
367
|
+
}
|
|
368
|
+
catch (error) {
|
|
369
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
370
|
+
this.options.platform.log.debug(`[${this.options.cameraName}] Failed to add remote ICE candidate: ${message}`);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
this.remoteIceQueue.push(candidate);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
createAuthResponse(params) {
|
|
378
|
+
this.clearAuthTimers();
|
|
379
|
+
this.authChallengeTimer = setTimeout(() => {
|
|
380
|
+
this.rejectReadyPromise(new Error('Timeout: signaling authentication'));
|
|
381
|
+
}, this.rpcTimeoutMs);
|
|
382
|
+
const sessionToken = this.stringValue(params[0]);
|
|
383
|
+
const modulus = this.stringValue(params[1]);
|
|
384
|
+
const exponent = this.stringValue(params[2]);
|
|
385
|
+
const fullPublicKey = this.stringValue(params[3]);
|
|
386
|
+
const communicationToken = this.options.getToken();
|
|
387
|
+
if (!sessionToken) {
|
|
388
|
+
throw new Error('Missing signaling session token');
|
|
389
|
+
}
|
|
390
|
+
if (!communicationToken) {
|
|
391
|
+
throw new Error('No active Loxone communication token available');
|
|
392
|
+
}
|
|
393
|
+
const aesKey = (0, crypto_1.randomBytes)(32);
|
|
394
|
+
const iv = (0, crypto_1.randomBytes)(16);
|
|
395
|
+
const cipher = (0, crypto_1.createCipheriv)('aes-256-cbc', aesKey, iv);
|
|
396
|
+
const encryptedToken = Buffer.concat([
|
|
397
|
+
cipher.update(communicationToken, 'utf8'),
|
|
398
|
+
cipher.final(),
|
|
399
|
+
]).toString('base64');
|
|
400
|
+
const rsaPayload = `${aesKey.toString('hex')}:${iv.toString('hex')}:${sessionToken}`;
|
|
401
|
+
const publicKey = this.createRsaPublicKey(modulus, exponent, fullPublicKey);
|
|
402
|
+
const rsaEncrypted = (0, crypto_1.publicEncrypt)({
|
|
403
|
+
key: publicKey,
|
|
404
|
+
padding: crypto_1.constants.RSA_PKCS1_PADDING,
|
|
405
|
+
}, Buffer.from(rsaPayload, 'utf8')).toString('base64');
|
|
406
|
+
return [this.options.username, rsaEncrypted, encryptedToken];
|
|
407
|
+
}
|
|
408
|
+
createRsaPublicKey(modulus, exponent, fullPublicKey) {
|
|
409
|
+
if (fullPublicKey) {
|
|
410
|
+
return (0, crypto_1.createPublicKey)(fullPublicKey);
|
|
411
|
+
}
|
|
412
|
+
if (!modulus || !exponent) {
|
|
413
|
+
throw new Error('Missing RSA key parameters from signaling challenge');
|
|
414
|
+
}
|
|
415
|
+
const jwk = {
|
|
416
|
+
kty: 'RSA',
|
|
417
|
+
n: this.hexToBase64Url(modulus),
|
|
418
|
+
e: this.hexToBase64Url(exponent),
|
|
419
|
+
};
|
|
420
|
+
return (0, crypto_1.createPublicKey)({
|
|
421
|
+
key: jwk,
|
|
422
|
+
format: 'jwk',
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
async callRpc(method, params) {
|
|
426
|
+
if (!this.socket || this.socket.readyState !== ws_1.default.OPEN) {
|
|
427
|
+
throw new Error(`Socket not ready for method: ${method}`);
|
|
428
|
+
}
|
|
429
|
+
const id = this.rpcId++;
|
|
430
|
+
const rpc = {
|
|
431
|
+
jsonrpc: '2.0',
|
|
432
|
+
method,
|
|
433
|
+
id,
|
|
434
|
+
};
|
|
435
|
+
if (Array.isArray(params)) {
|
|
436
|
+
rpc.params = params;
|
|
437
|
+
}
|
|
438
|
+
return new Promise((resolve, reject) => {
|
|
439
|
+
const timeout = setTimeout(() => {
|
|
440
|
+
this.pendingRpc.delete(id);
|
|
441
|
+
reject(new Error(`Timeout: ${method}`));
|
|
442
|
+
}, this.rpcTimeoutMs);
|
|
443
|
+
this.pendingRpc.set(id, {
|
|
444
|
+
method,
|
|
445
|
+
resolve,
|
|
446
|
+
reject,
|
|
447
|
+
timeout,
|
|
448
|
+
});
|
|
449
|
+
this.sendJson(rpc);
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
async sendNotification(method, params) {
|
|
453
|
+
const rpc = {
|
|
454
|
+
jsonrpc: '2.0',
|
|
455
|
+
method,
|
|
456
|
+
};
|
|
457
|
+
if (Array.isArray(params)) {
|
|
458
|
+
rpc.params = params;
|
|
459
|
+
}
|
|
460
|
+
this.sendJson(rpc);
|
|
461
|
+
}
|
|
462
|
+
sendRpcSuccess(id, data) {
|
|
463
|
+
const result = {
|
|
464
|
+
code: 200,
|
|
465
|
+
message: 'Ok',
|
|
466
|
+
};
|
|
467
|
+
if (data !== undefined) {
|
|
468
|
+
result.data = data;
|
|
469
|
+
}
|
|
470
|
+
this.sendJson({
|
|
471
|
+
jsonrpc: '2.0',
|
|
472
|
+
id,
|
|
473
|
+
result,
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
sendRpcError(id, code, message, data) {
|
|
477
|
+
this.sendJson({
|
|
478
|
+
jsonrpc: '2.0',
|
|
479
|
+
id,
|
|
480
|
+
error: {
|
|
481
|
+
code,
|
|
482
|
+
message,
|
|
483
|
+
...(data !== undefined ? { data } : {}),
|
|
484
|
+
},
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
sendJson(payload) {
|
|
488
|
+
if (!this.socket || this.socket.readyState !== ws_1.default.OPEN) {
|
|
489
|
+
throw new Error('Socket is not open');
|
|
490
|
+
}
|
|
491
|
+
this.socket.send(JSON.stringify(payload));
|
|
492
|
+
}
|
|
493
|
+
rejectAllPendingRpc(error) {
|
|
494
|
+
for (const [, pending] of this.pendingRpc) {
|
|
495
|
+
clearTimeout(pending.timeout);
|
|
496
|
+
pending.reject(error);
|
|
497
|
+
}
|
|
498
|
+
this.pendingRpc.clear();
|
|
499
|
+
}
|
|
500
|
+
resolveReadyPromise() {
|
|
501
|
+
if (this.resolveReady) {
|
|
502
|
+
this.resolveReady();
|
|
503
|
+
}
|
|
504
|
+
this.resolveReady = undefined;
|
|
505
|
+
this.rejectReady = undefined;
|
|
506
|
+
}
|
|
507
|
+
rejectReadyPromise(error) {
|
|
508
|
+
if (this.rejectReady) {
|
|
509
|
+
this.rejectReady(error);
|
|
510
|
+
}
|
|
511
|
+
this.resolveReady = undefined;
|
|
512
|
+
this.rejectReady = undefined;
|
|
513
|
+
}
|
|
514
|
+
clearAuthTimers() {
|
|
515
|
+
if (this.authChallengeTimer) {
|
|
516
|
+
clearTimeout(this.authChallengeTimer);
|
|
517
|
+
this.authChallengeTimer = undefined;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
toWsUrl(baseUrl) {
|
|
521
|
+
return baseUrl.replace(/^https?:\/\//i, (protocol) => protocol.toLowerCase() === 'https://' ? 'wss://' : 'ws://');
|
|
522
|
+
}
|
|
523
|
+
normalizeSessionDescription(value) {
|
|
524
|
+
var _a;
|
|
525
|
+
if (!value || typeof value !== 'object') {
|
|
526
|
+
throw new Error('Invalid remote SDP response');
|
|
527
|
+
}
|
|
528
|
+
const objectValue = value;
|
|
529
|
+
const type = this.stringValue(objectValue.type);
|
|
530
|
+
if (!type) {
|
|
531
|
+
throw new Error('Remote SDP response missing type');
|
|
532
|
+
}
|
|
533
|
+
const sdp = (_a = this.stringValue(objectValue.sdp)) !== null && _a !== void 0 ? _a : '';
|
|
534
|
+
return { type, sdp };
|
|
535
|
+
}
|
|
536
|
+
rawDataToString(data) {
|
|
537
|
+
if (typeof data === 'string') {
|
|
538
|
+
return data;
|
|
539
|
+
}
|
|
540
|
+
if (Buffer.isBuffer(data)) {
|
|
541
|
+
return data.toString('utf8');
|
|
542
|
+
}
|
|
543
|
+
if (Array.isArray(data)) {
|
|
544
|
+
return Buffer.concat(data).toString('utf8');
|
|
545
|
+
}
|
|
546
|
+
return Buffer.from(data).toString('utf8');
|
|
547
|
+
}
|
|
548
|
+
stringValue(value) {
|
|
549
|
+
return typeof value === 'string' && value.length > 0 ? value : undefined;
|
|
550
|
+
}
|
|
551
|
+
numberValue(value) {
|
|
552
|
+
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
553
|
+
return value;
|
|
554
|
+
}
|
|
555
|
+
if (typeof value === 'string' && value.trim() !== '') {
|
|
556
|
+
const parsed = Number(value);
|
|
557
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
558
|
+
}
|
|
559
|
+
return undefined;
|
|
560
|
+
}
|
|
561
|
+
hexToBase64Url(hex) {
|
|
562
|
+
const normalized = hex.length % 2 === 0 ? hex : `0${hex}`;
|
|
563
|
+
return Buffer.from(normalized, 'hex')
|
|
564
|
+
.toString('base64')
|
|
565
|
+
.replace(/\+/g, '-')
|
|
566
|
+
.replace(/\//g, '_')
|
|
567
|
+
.replace(/=+$/g, '');
|
|
568
|
+
}
|
|
569
|
+
loadWrtcModule() {
|
|
570
|
+
const moduleValue = require('wrtc');
|
|
571
|
+
if (!(moduleValue === null || moduleValue === void 0 ? void 0 : moduleValue.RTCPeerConnection)) {
|
|
572
|
+
throw new Error('wrtc module does not expose RTCPeerConnection');
|
|
573
|
+
}
|
|
574
|
+
return moduleValue;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
exports.LoxoneTalkbackSession = LoxoneTalkbackSession;
|
|
578
|
+
//# sourceMappingURL=LoxoneTalkback.js.map
|