-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
message_handler.ts
executable file
·284 lines (269 loc) · 13.7 KB
/
message_handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import { EufySecurity } from "eufy-security-client";
import { LivestreamAlreadyRunningError, LivestreamNotRunningError, UnknownCommandError } from "../error";
import { Client } from "../server";
import { DeviceCommand } from "./command";
import { DeviceEvent } from "./event";
import { IncomingCommandDeviceEnableDevice, IncomingCommandDeviceLockDevice, IncomingCommandDeviceSetAntiTheftDetection, IncomingCommandDeviceSetAutoNightVision, IncomingCommandDeviceSetMotionDetection, IncomingCommandDeviceSetPetDetection, IncomingCommandDeviceSetProperty, IncomingCommandDeviceSetRTSPStream, IncomingCommandDeviceSetSoundDetection, IncomingCommandDeviceSetStatusLed, IncomingCommandDeviceSetWatermark, IncomingMessageDevice, IncomingCommandDeviceTriggerAlarm, IncomingCommandDevicePanAndTilt, IncomingCommandDeviceQuickResponse, IncomingCommandDeviceStartDownload, IncomingCommandDeviceHasProperty, IncomingCommandDeviceHasCommand } from "./incoming_message";
import { DeviceResultTypes } from "./outgoing_message";
export class DeviceMessageHandler {
private static streamingDevices: { [index: string]: Array<Client>} = {};
static getStreamingDevices(stationSN: string): Array<Client> {
if (DeviceMessageHandler.streamingDevices[stationSN] !== undefined)
return DeviceMessageHandler.streamingDevices[stationSN];
return [];
}
static removeStreamingDevice(stationSN: string, client: Client): void {
if (DeviceMessageHandler.streamingDevices[stationSN] !== undefined)
DeviceMessageHandler.streamingDevices[stationSN] = DeviceMessageHandler.streamingDevices[stationSN].filter((cl) => cl !== client);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
static async handle(message: IncomingMessageDevice, driver: EufySecurity, client: Client): Promise<DeviceResultTypes[DeviceCommand]> {
const { serialNumber, command } = message;
const device = driver.getDevice(serialNumber);
const station = driver.getStation(device.getStationSerial());
switch (command) {
case DeviceCommand.setStatusLed:
await station.setStatusLed(device, (message as IncomingCommandDeviceSetStatusLed).value).catch((error) => {
throw error;
});
return { };
case DeviceCommand.setAutoNightVision:
await station.setAutoNightVision(device, (message as IncomingCommandDeviceSetAutoNightVision).value).catch((error) => {
throw error;
});
return { };
case DeviceCommand.setMotionDetection:
await station.setMotionDetection(device, (message as IncomingCommandDeviceSetMotionDetection).value).catch((error) => {
throw error;
});
return { };
case DeviceCommand.setSoundDetection:
await station.setSoundDetection(device, (message as IncomingCommandDeviceSetSoundDetection).value).catch((error) => {
throw error;
});
return { };
case DeviceCommand.setPetDetection:
await station.setPetDetection(device, (message as IncomingCommandDeviceSetPetDetection).value).catch((error) => {
throw error;
});
return { };
case DeviceCommand.setRTSPStream:
await station.setRTSPStream(device, (message as IncomingCommandDeviceSetRTSPStream).value).catch((error) => {
throw error;
});
return { };
case DeviceCommand.setAntiTheftDetection:
await station.setAntiTheftDetection(device, (message as IncomingCommandDeviceSetAntiTheftDetection).value).catch((error) => {
throw error;
});
return { };
case DeviceCommand.setWatermark:
await station.setWatermark(device, (message as IncomingCommandDeviceSetWatermark).value).catch((error) => {
throw error;
});
return { };
case DeviceCommand.enableDevice:
await station.enableDevice(device, (message as IncomingCommandDeviceEnableDevice).value).catch((error) => {
throw error;
});
return { };
case DeviceCommand.lockDevice:
await station.lockDevice(device, (message as IncomingCommandDeviceLockDevice).value).catch((error) => {
throw error;
});
return { };
case DeviceCommand.getPropertiesMetadata:
{
const properties = device.getPropertiesMetadata();
if (client.schemaVersion <= 3) {
return { properties: properties };
} else if (client.schemaVersion >= 4) {
return {
serialNumber: device.getSerial(),
properties: properties
};
}
}
case DeviceCommand.getProperties:
{
const properties = device.getProperties();
if (client.schemaVersion <= 3) {
return { properties: properties };
} else if (client.schemaVersion >= 4) {
return {
serialNumber: device.getSerial(),
properties: properties
};
}
}
case DeviceCommand.setProperty:
await driver.setDeviceProperty(serialNumber, (message as IncomingCommandDeviceSetProperty).name, (message as IncomingCommandDeviceSetProperty).value).catch((error) => {
throw error;
});
return { };
case DeviceCommand.startLivestream:
if (client.schemaVersion >= 2) {
if (!station.isLiveStreaming(device)) {
await station.startLivestream(device).catch((error) => {
throw error;
});
client.receiveLivestream[serialNumber] = true;
DeviceMessageHandler.streamingDevices[station.getSerial()] = [ client ];
} else if (client.receiveLivestream[serialNumber] !== true) {
//TODO: Cache last received I-Frame and send that with the rest to the new client
client.sendEvent({
source: "device",
event: DeviceEvent.livestreamStarted,
serialNumber: serialNumber,
})
client.receiveLivestream[serialNumber] = true;
if (DeviceMessageHandler.streamingDevices[station.getSerial()] !== undefined)
DeviceMessageHandler.streamingDevices[station.getSerial()].push(client);
} else {
throw new LivestreamAlreadyRunningError(`Livestream for device ${serialNumber} is already running`);
}
return { };
}
case DeviceCommand.stopLivestream:
if (client.schemaVersion >= 2) {
if (client.receiveLivestream[serialNumber] !== true) {
throw new LivestreamNotRunningError(`Start of livestream for device ${serialNumber} was not also requested by this client`);
}
if (DeviceMessageHandler.streamingDevices[station.getSerial()] !== undefined && DeviceMessageHandler.streamingDevices[station.getSerial()].includes(client)) {
if (DeviceMessageHandler.streamingDevices[station.getSerial()].length === 1) {
await station.stopLivestream(device).catch((error) => {
throw error;
});
delete DeviceMessageHandler.streamingDevices[station.getSerial()];
} else {
client.receiveLivestream[serialNumber] = false;
DeviceMessageHandler.streamingDevices[station.getSerial()] = DeviceMessageHandler.streamingDevices[station.getSerial()].filter((cl) => cl !== client);
client.sendEvent({
source: "device",
event: DeviceEvent.livestreamStopped,
serialNumber: serialNumber,
});
}
}
return { };
}
case DeviceCommand.isLiveStreaming:
{
if (client.schemaVersion >= 2) {
const result = station.isLiveStreaming(device);
if (client.schemaVersion >= 2 && client.schemaVersion <= 3) {
return { livestreaming: result };
} else if (client.schemaVersion >= 4) {
return {
serialNumber: device.getSerial(),
livestreaming: result
};
}
}
}
case DeviceCommand.triggerAlarm:
if (client.schemaVersion >= 3) {
await station.triggerDeviceAlarmSound(device, (message as IncomingCommandDeviceTriggerAlarm).seconds).catch((error) => {
throw error;
});
return { };
}
case DeviceCommand.resetAlarm:
if (client.schemaVersion >= 3) {
await station.resetDeviceAlarmSound(device).catch((error) => {
throw error;
});
return { };
}
case DeviceCommand.panAndTilt:
if (client.schemaVersion >= 3) {
await station.panAndTilt(device, (message as IncomingCommandDevicePanAndTilt).direction).catch((error) => {
throw error;
});
return { };
}
case DeviceCommand.quickResponse:
if (client.schemaVersion >= 3) {
await station.quickResponse(device, (message as IncomingCommandDeviceQuickResponse).voiceId).catch((error) => {
throw error;
});
return { };
}
case DeviceCommand.startDownload:
if (client.schemaVersion >= 3) {
await station.startDownload(device, (message as IncomingCommandDeviceStartDownload).path, (message as IncomingCommandDeviceStartDownload).cipherId).catch((error) => {
throw error;
});
return { };
}
case DeviceCommand.cancelDownload:
if (client.schemaVersion >= 3) {
await station.cancelDownload(device).catch((error) => {
throw error;
});
return { };
}
case DeviceCommand.getVoices:
if (client.schemaVersion >= 3) {
const voices = await driver.getApi().getVoices(device.getSerial()).catch((error) => {
throw error;
});
if (client.schemaVersion === 3) {
return {
serialNumber: device.getSerial(),
voices: voices
};
} else if (client.schemaVersion >= 4) {
return {
serialNumber: device.getSerial(),
voices: voices
};
}
}
case DeviceCommand.hasProperty:
{
if (client.schemaVersion >= 3) {
const result = device.hasProperty((message as IncomingCommandDeviceHasProperty).propertyName);
if (client.schemaVersion === 3) {
return { exists: result };
} else if (client.schemaVersion >= 4) {
return {
serialNumber: device.getSerial(),
exists: result
};
}
}
}
case DeviceCommand.hasCommand:
{
if (client.schemaVersion >= 3) {
const result = device.hasCommand((message as IncomingCommandDeviceHasCommand).commandName);
if (client.schemaVersion === 3) {
return { exists: result };
} else if (client.schemaVersion >= 4) {
return {
serialNumber: device.getSerial(),
exists: result
};
}
}
}
case DeviceCommand.getCommands:
{
if (client.schemaVersion >= 3) {
const result = device.getCommands();
if (client.schemaVersion === 3) {
return { commands: result };
} else if (client.schemaVersion >= 4) {
return {
serialNumber: device.getSerial(),
commands: result
};
}
}
}
default:
throw new UnknownCommandError(command);
}
}
}