node-miio 0.7.0 → 0.8.0

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.
Files changed (77) hide show
  1. package/README.md +4 -0
  2. package/dist/cli/commands/roborock/call.d.ts +10 -0
  3. package/dist/cli/commands/roborock/call.js +34 -0
  4. package/dist/cli/commands/roborock/call.js.map +1 -0
  5. package/dist/cli/commands/roborock/devices.d.ts +8 -0
  6. package/dist/cli/commands/roborock/devices.js +58 -0
  7. package/dist/cli/commands/roborock/devices.js.map +1 -0
  8. package/dist/cli/commands/roborock/login.d.ts +7 -0
  9. package/dist/cli/commands/roborock/login.js +34 -0
  10. package/dist/cli/commands/roborock/login.js.map +1 -0
  11. package/dist/cli/commands/roborock/logout.d.ts +4 -0
  12. package/dist/cli/commands/roborock/logout.js +14 -0
  13. package/dist/cli/commands/roborock/logout.js.map +1 -0
  14. package/dist/cli/commands/roborock/map.d.ts +10 -0
  15. package/dist/cli/commands/roborock/map.js +74 -0
  16. package/dist/cli/commands/roborock/map.js.map +1 -0
  17. package/dist/cli/commands/roborock/robot.d.ts +30 -0
  18. package/dist/cli/commands/roborock/robot.js +61 -0
  19. package/dist/cli/commands/roborock/robot.js.map +1 -0
  20. package/dist/cli/commands/roborock/rooms.d.ts +9 -0
  21. package/dist/cli/commands/roborock/rooms.js +40 -0
  22. package/dist/cli/commands/roborock/rooms.js.map +1 -0
  23. package/dist/cli/commands/roborock.d.ts +5 -0
  24. package/dist/cli/commands/roborock.js +55 -0
  25. package/dist/cli/commands/roborock.js.map +1 -0
  26. package/dist/cli/index.js +2 -0
  27. package/dist/cli/index.js.map +1 -1
  28. package/dist/lib/device_info.d.ts +6 -0
  29. package/dist/lib/devices/vacuums/vacuum.d.ts +3 -3
  30. package/dist/lib/devices/vacuums/vacuum.js +13 -3
  31. package/dist/lib/devices/vacuums/vacuum.js.map +1 -1
  32. package/dist/lib/devices/vacuums/vacuum_modes.d.ts +2 -0
  33. package/dist/lib/devices/vacuums/vacuum_modes.js +15 -0
  34. package/dist/lib/devices/vacuums/vacuum_modes.js.map +1 -1
  35. package/dist/lib/index.d.ts +1 -0
  36. package/dist/lib/index.js +1 -0
  37. package/dist/lib/index.js.map +1 -1
  38. package/dist/lib/logger.js +1 -1
  39. package/dist/lib/logger.js.map +1 -1
  40. package/dist/lib/management.d.ts +2 -2
  41. package/dist/lib/management.js.map +1 -1
  42. package/dist/lib/roborock/device.d.ts +118 -0
  43. package/dist/lib/roborock/device.js +273 -0
  44. package/dist/lib/roborock/device.js.map +1 -0
  45. package/dist/lib/roborock/errors.d.ts +18 -0
  46. package/dist/lib/roborock/errors.js +37 -0
  47. package/dist/lib/roborock/errors.js.map +1 -0
  48. package/dist/lib/roborock/hawk.d.ts +39 -0
  49. package/dist/lib/roborock/hawk.js +48 -0
  50. package/dist/lib/roborock/hawk.js.map +1 -0
  51. package/dist/lib/roborock/index.d.ts +9 -0
  52. package/dist/lib/roborock/index.js +36 -0
  53. package/dist/lib/roborock/index.js.map +1 -0
  54. package/dist/lib/roborock/local_connection.d.ts +72 -0
  55. package/dist/lib/roborock/local_connection.js +232 -0
  56. package/dist/lib/roborock/local_connection.js.map +1 -0
  57. package/dist/lib/roborock/map_response.d.ts +26 -0
  58. package/dist/lib/roborock/map_response.js +104 -0
  59. package/dist/lib/roborock/map_response.js.map +1 -0
  60. package/dist/lib/roborock/message.d.ts +110 -0
  61. package/dist/lib/roborock/message.js +313 -0
  62. package/dist/lib/roborock/message.js.map +1 -0
  63. package/dist/lib/roborock/mqtt.d.ts +119 -0
  64. package/dist/lib/roborock/mqtt.js +305 -0
  65. package/dist/lib/roborock/mqtt.js.map +1 -0
  66. package/dist/lib/roborock/rooms.d.ts +17 -0
  67. package/dist/lib/roborock/rooms.js +23 -0
  68. package/dist/lib/roborock/rooms.js.map +1 -0
  69. package/dist/lib/roborock/session_store.d.ts +21 -0
  70. package/dist/lib/roborock/session_store.js +52 -0
  71. package/dist/lib/roborock/session_store.js.map +1 -0
  72. package/dist/lib/roborock/web_api.d.ts +142 -0
  73. package/dist/lib/roborock/web_api.js +334 -0
  74. package/dist/lib/roborock/web_api.js.map +1 -0
  75. package/docs/roborock.md +91 -0
  76. package/package.json +2 -1
  77. package/snapshots/vacuum-specs.json.gz +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map_response.js","sourceRoot":"","sources":["../../../src/lib/roborock/map_response.ts"],"names":[],"mappings":";;;;;;AAAA;;;;;;;;;;;GAWG;AACH,8DAAiC;AACjC,yCAAuC;AAEvC,qCAAyC;AACzC,uCAAmE;AAEnE,MAAM,aAAa,GAAG,EAAE,CAAC;AACzB,iFAAiF;AACjF,MAAM,WAAW,GAAG,CAAC,CAAC;AAQtB;;GAEG;AACH,MAAa,oBAAoB;IAaZ;IAZF,QAAQ,CAAS;IACjB,OAAO,GAAG,IAAI,GAAG,EAG/B,CAAC;IAEJ;;;OAGG;IACH,YACE,QAAgB,EACC,KAAa;QAAb,UAAK,GAAL,KAAK,CAAQ;QAE9B,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IAClD,CAAC;IAED;;;OAGG;IACH,IAAI,CAAC,OAAwB;QAC3B,IACE,OAAO,CAAC,QAAQ,KAAK,0BAAgB,CAAC,YAAY;YAClD,OAAO,CAAC,QAAQ,KAAK,0BAAgB,CAAC,iBAAiB,EACvD,CAAC;YACD,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;QAChC,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC;YACrB,MAAM,IAAI,sBAAa,CAAC,8BAA8B,CAAC,CAAC;QAC1D,CAAC;QACD,MAAM,IAAI,GAAG,OAAO,CAAC,QAAQ,KAAK,0BAAgB,CAAC,YAAY,CAAC;QAChE,MAAM,GAAG,GAAG,GAAG,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,SAAS,EAA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;QACJ,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,IAAI,sBAAa,CACrB,4BAA6B,GAAa,CAAC,OAAO,EAAE,CACrD,CAAC;QACJ,CAAC;QACD,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;IAC7B,CAAC;CACF;AA7FD,oDA6FC"}
@@ -0,0 +1,110 @@
1
+ /** The message types (`protocol` field) */
2
+ export declare const RoborockProtocol: {
3
+ readonly HELLO_REQUEST: 0;
4
+ readonly HELLO_RESPONSE: 1;
5
+ readonly PING_REQUEST: 2;
6
+ readonly PING_RESPONSE: 3;
7
+ /** RPC over the local connection */
8
+ readonly GENERAL_REQUEST: 4;
9
+ readonly GENERAL_RESPONSE: 5;
10
+ /** RPC over MQTT */
11
+ readonly RPC_REQUEST: 101;
12
+ readonly RPC_RESPONSE: 102;
13
+ readonly MAP_RESPONSE_PART: 300;
14
+ readonly MAP_RESPONSE: 301;
15
+ };
16
+ export interface RoborockMessage {
17
+ version: string;
18
+ seq: number;
19
+ random: number;
20
+ timestamp: number;
21
+ protocol: number;
22
+ /** The decrypted payload (usually JSON) */
23
+ payload?: Buffer;
24
+ }
25
+ /**
26
+ * @param timestamp Unix time in seconds
27
+ * @returns The timestamp as 8 hex digits, shuffled the way the robots expect
28
+ */
29
+ export declare function encodeTimestamp(timestamp: number): string;
30
+ /** The protocol versions of the messages: `1.0` (AES-ECB) or `L01` (AES-GCM, newer local connections) */
31
+ export type RoborockMessageVersion = "1.0" | "L01";
32
+ /**
33
+ * The nonces of an `L01` connection, exchanged in the hello: `connectNonce` is the `random` of our hello,
34
+ * `ackNonce` the `random` of the robot's response. Both are part of every message's authenticated data.
35
+ */
36
+ export interface L01Nonces {
37
+ connectNonce: number;
38
+ ackNonce?: number;
39
+ }
40
+ /**
41
+ * @param message The message
42
+ * @param localKey The robot's local key
43
+ * @param nonces The connection's nonces (`L01` messages only)
44
+ * @returns The encoded message (without the TCP length prefix)
45
+ */
46
+ export declare function encodeMessage(message: RoborockMessage, localKey: string, nonces?: L01Nonces): Buffer<ArrayBuffer>;
47
+ /**
48
+ * @param data One encoded message (without the TCP length prefix)
49
+ * @param localKey The robot's local key
50
+ * @param nonces The connection's nonces (`L01` messages only)
51
+ * @returns The message
52
+ */
53
+ export declare function decodeMessage(data: Buffer, localKey: string, nonces?: L01Nonces): RoborockMessage;
54
+ /**
55
+ * @param message The encoded message
56
+ * @returns The message with its TCP length prefix
57
+ */
58
+ export declare function withLengthPrefix(message: Buffer): Buffer<ArrayBuffer>;
59
+ /**
60
+ * Splits the TCP stream into messages (length-prefixed).
61
+ */
62
+ export declare class LengthPrefixedReader {
63
+ private buffer;
64
+ /**
65
+ * @param chunk The data received
66
+ * @returns The complete messages received so far (without their prefix)
67
+ */
68
+ push(chunk: Buffer): Buffer[];
69
+ }
70
+ /** An RPC request, as sent in the payload (`dps.101`) */
71
+ export interface RoborockRpcRequest {
72
+ id: number;
73
+ method: string;
74
+ params: unknown;
75
+ security?: {
76
+ endpoint: string;
77
+ nonce: string;
78
+ };
79
+ }
80
+ /**
81
+ * @param request The RPC request
82
+ * @param timestamp Unix time in seconds
83
+ * @returns The payload of an RPC message
84
+ */
85
+ export declare function rpcPayload(request: RoborockRpcRequest, timestamp: number): Buffer<ArrayBuffer>;
86
+ /**
87
+ * @param message A received message
88
+ * @returns The data points of its payload, by code (`102` = RPC response, `121`… = status pushes)
89
+ */
90
+ export declare function dataPoints(message: RoborockMessage): Record<string, unknown> | undefined;
91
+ export interface RoborockRpcResponse {
92
+ id: number;
93
+ result?: unknown;
94
+ error?: {
95
+ code?: number;
96
+ message?: string;
97
+ } | string;
98
+ }
99
+ /**
100
+ * @param message A received message
101
+ * @returns The RPC response it carries, if any
102
+ */
103
+ export declare function rpcResponse(message: RoborockMessage): RoborockRpcResponse | undefined;
104
+ /**
105
+ * @param method The method that was called
106
+ * @param rpc Its response
107
+ * @returns The result
108
+ * @throws {RoborockApiError} If the robot returned an error, or doesn't know the method
109
+ */
110
+ export declare function rpcResult<T>(method: string, rpc: RoborockRpcResponse): T;
@@ -0,0 +1,313 @@
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.LengthPrefixedReader = exports.RoborockProtocol = void 0;
7
+ exports.encodeTimestamp = encodeTimestamp;
8
+ exports.encodeMessage = encodeMessage;
9
+ exports.decodeMessage = decodeMessage;
10
+ exports.withLengthPrefix = withLengthPrefix;
11
+ exports.rpcPayload = rpcPayload;
12
+ exports.dataPoints = dataPoints;
13
+ exports.rpcResponse = rpcResponse;
14
+ exports.rpcResult = rpcResult;
15
+ /**
16
+ * The messages of Roborock robots (versions `1.0` and `L01`), used by the local (TCP 58867) and MQTT connections.
17
+ *
18
+ * ```
19
+ * version "1.0" (3) | seq u32 | random u32 | timestamp u32 | protocol u16 | [payload length u16 | payload] | [crc32 u32]
20
+ * ```
21
+ *
22
+ * The payload is AES-128-ECB encrypted (PKCS#7) with `md5(encodeTimestamp(timestamp) + localKey + SALT)`; the CRC32
23
+ * covers everything before it. `L01` (newer local connections) uses AES-256-GCM instead: see {@link l01Cipher}. Over TCP, each message is prefixed with its length (u32). All integers are big-endian.
24
+ *
25
+ * Format documented by python-roborock (Apache-2.0), `roborock/protocol.py`. No code is copied from it.
26
+ */
27
+ const node_crypto_1 = __importDefault(require("node:crypto"));
28
+ const node_zlib_1 = __importDefault(require("node:zlib"));
29
+ const errors_1 = require("./errors");
30
+ const SALT = "TXdfu$jyZ#TZHsg4";
31
+ const HEADER_LENGTH = 3 + 4 + 4 + 4 + 2;
32
+ /** The message types (`protocol` field) */
33
+ exports.RoborockProtocol = {
34
+ HELLO_REQUEST: 0,
35
+ HELLO_RESPONSE: 1,
36
+ PING_REQUEST: 2,
37
+ PING_RESPONSE: 3,
38
+ /** RPC over the local connection */
39
+ GENERAL_REQUEST: 4,
40
+ GENERAL_RESPONSE: 5,
41
+ /** RPC over MQTT */
42
+ RPC_REQUEST: 101,
43
+ RPC_RESPONSE: 102,
44
+ MAP_RESPONSE_PART: 300,
45
+ MAP_RESPONSE: 301,
46
+ };
47
+ /**
48
+ * @param timestamp Unix time in seconds
49
+ * @returns The timestamp as 8 hex digits, shuffled the way the robots expect
50
+ */
51
+ function encodeTimestamp(timestamp) {
52
+ const hex = (timestamp >>> 0).toString(16).padStart(8, "0");
53
+ return [5, 6, 3, 7, 1, 2, 0, 4].map((i) => hex[i]).join("");
54
+ }
55
+ /**
56
+ * @param timestamp The timestamp of the message
57
+ * @param localKey The robot's local key
58
+ * @returns The AES key of the message's payload
59
+ */
60
+ function payloadKey(timestamp, localKey) {
61
+ return node_crypto_1.default
62
+ .createHash("md5")
63
+ .update(encodeTimestamp(timestamp) + localKey + SALT)
64
+ .digest();
65
+ }
66
+ /**
67
+ * @param version The version of a message
68
+ */
69
+ function checkVersion(version) {
70
+ if (version !== "1.0" && version !== "L01") {
71
+ throw new errors_1.RoborockError(`Protocol version ${version} is not supported yet`);
72
+ }
73
+ }
74
+ const u32 = (value) => {
75
+ const buffer = Buffer.alloc(4);
76
+ buffer.writeUInt32BE(value >>> 0);
77
+ return buffer;
78
+ };
79
+ /**
80
+ * @param message The message (its header)
81
+ * @param localKey The robot's local key
82
+ * @param nonces The connection's nonces
83
+ * @returns The key, IV and authenticated data of an `L01` message (AES-256-GCM)
84
+ */
85
+ function l01Cipher(message, localKey, nonces) {
86
+ if (!nonces) {
87
+ throw new errors_1.RoborockError("L01 messages need the connection's nonces");
88
+ }
89
+ const key = node_crypto_1.default
90
+ .createHash("sha256")
91
+ .update(encodeTimestamp(message.timestamp) + localKey + SALT)
92
+ .digest();
93
+ const iv = node_crypto_1.default
94
+ .createHash("sha256")
95
+ .update(Buffer.concat([
96
+ u32(message.seq),
97
+ u32(message.random),
98
+ u32(message.timestamp),
99
+ ]))
100
+ .digest()
101
+ .subarray(0, 12);
102
+ const aad = Buffer.concat([
103
+ u32(message.seq),
104
+ u32(nonces.connectNonce),
105
+ ...(nonces.ackNonce === undefined ? [] : [u32(nonces.ackNonce)]),
106
+ u32(message.random),
107
+ u32(message.timestamp),
108
+ ]);
109
+ return { key, iv, aad };
110
+ }
111
+ /**
112
+ * @param message The message with its payload
113
+ * @param localKey The robot's local key
114
+ * @param nonces The connection's nonces (`L01` only)
115
+ * @returns The encrypted payload
116
+ */
117
+ function encryptPayload(message, localKey, nonces) {
118
+ const payload = message.payload ?? Buffer.alloc(0);
119
+ if (message.version === "L01") {
120
+ const { key, iv, aad } = l01Cipher(message, localKey, nonces);
121
+ const cipher = node_crypto_1.default.createCipheriv("aes-256-gcm", key, iv);
122
+ cipher.setAAD(aad);
123
+ return Buffer.concat([
124
+ cipher.update(payload),
125
+ cipher.final(),
126
+ cipher.getAuthTag(),
127
+ ]);
128
+ }
129
+ const cipher = node_crypto_1.default.createCipheriv("aes-128-ecb", payloadKey(message.timestamp, localKey), null);
130
+ return Buffer.concat([cipher.update(payload), cipher.final()]);
131
+ }
132
+ /**
133
+ * @param message The message (its header)
134
+ * @param encrypted The encrypted payload
135
+ * @param localKey The robot's local key
136
+ * @param nonces The connection's nonces (`L01` only)
137
+ * @returns The decrypted payload
138
+ */
139
+ function decryptPayload(message, encrypted, localKey, nonces) {
140
+ try {
141
+ if (message.version === "L01") {
142
+ const { key, iv, aad } = l01Cipher(message, localKey, nonces);
143
+ if (encrypted.length < 16)
144
+ throw new Error("payload too short");
145
+ const decipher = node_crypto_1.default.createDecipheriv("aes-256-gcm", key, iv);
146
+ decipher.setAAD(aad);
147
+ decipher.setAuthTag(encrypted.subarray(-16));
148
+ return Buffer.concat([
149
+ decipher.update(encrypted.subarray(0, -16)),
150
+ decipher.final(),
151
+ ]);
152
+ }
153
+ const decipher = node_crypto_1.default.createDecipheriv("aes-128-ecb", payloadKey(message.timestamp, localKey), null);
154
+ return Buffer.concat([decipher.update(encrypted), decipher.final()]);
155
+ }
156
+ catch (err) {
157
+ if (err instanceof errors_1.RoborockError)
158
+ throw err;
159
+ throw new errors_1.RoborockError(`Couldn't decrypt the message (${err.message})`);
160
+ }
161
+ }
162
+ /**
163
+ * @param message The message
164
+ * @param localKey The robot's local key
165
+ * @param nonces The connection's nonces (`L01` messages only)
166
+ * @returns The encoded message (without the TCP length prefix)
167
+ */
168
+ function encodeMessage(message, localKey, nonces) {
169
+ checkVersion(message.version);
170
+ const header = Buffer.alloc(HEADER_LENGTH);
171
+ header.write(message.version, 0, "latin1");
172
+ header.writeUInt32BE(message.seq >>> 0, 3);
173
+ header.writeUInt32BE(message.random >>> 0, 7);
174
+ header.writeUInt32BE(message.timestamp >>> 0, 11);
175
+ header.writeUInt16BE(message.protocol, 15);
176
+ const parts = [header];
177
+ if (message.payload) {
178
+ const encrypted = encryptPayload(message, localKey, nonces);
179
+ const length = Buffer.alloc(2);
180
+ length.writeUInt16BE(encrypted.length);
181
+ parts.push(length, encrypted);
182
+ }
183
+ const body = Buffer.concat(parts);
184
+ const crc = Buffer.alloc(4);
185
+ crc.writeUInt32BE(node_zlib_1.default.crc32(body));
186
+ return Buffer.concat([body, crc]);
187
+ }
188
+ /**
189
+ * @param data One encoded message (without the TCP length prefix)
190
+ * @param localKey The robot's local key
191
+ * @param nonces The connection's nonces (`L01` messages only)
192
+ * @returns The message
193
+ */
194
+ function decodeMessage(data, localKey, nonces) {
195
+ if (data.length < HEADER_LENGTH) {
196
+ throw new errors_1.RoborockError(`Message too short (${data.length} bytes)`);
197
+ }
198
+ const message = {
199
+ version: data.toString("latin1", 0, 3),
200
+ seq: data.readUInt32BE(3),
201
+ random: data.readUInt32BE(7),
202
+ timestamp: data.readUInt32BE(11),
203
+ protocol: data.readUInt16BE(15),
204
+ };
205
+ checkVersion(message.version);
206
+ // Messages without payload may come with or without CRC
207
+ const rest = data.length - HEADER_LENGTH;
208
+ if (rest === 0 || rest === 4)
209
+ return message;
210
+ const length = data.readUInt16BE(HEADER_LENGTH);
211
+ const end = HEADER_LENGTH + 2 + length;
212
+ if (end > data.length) {
213
+ throw new errors_1.RoborockError("Message truncated");
214
+ }
215
+ if (data.length >= end + 4) {
216
+ const crc = data.readUInt32BE(end);
217
+ if (crc !== node_zlib_1.default.crc32(data.subarray(0, end))) {
218
+ throw new errors_1.RoborockError("Wrong message checksum");
219
+ }
220
+ }
221
+ if (length > 0) {
222
+ message.payload = decryptPayload(message, data.subarray(HEADER_LENGTH + 2, end), localKey, nonces);
223
+ }
224
+ return message;
225
+ }
226
+ /**
227
+ * @param message The encoded message
228
+ * @returns The message with its TCP length prefix
229
+ */
230
+ function withLengthPrefix(message) {
231
+ const prefix = Buffer.alloc(4);
232
+ prefix.writeUInt32BE(message.length);
233
+ return Buffer.concat([prefix, message]);
234
+ }
235
+ /**
236
+ * Splits the TCP stream into messages (length-prefixed).
237
+ */
238
+ class LengthPrefixedReader {
239
+ buffer = Buffer.alloc(0);
240
+ /**
241
+ * @param chunk The data received
242
+ * @returns The complete messages received so far (without their prefix)
243
+ */
244
+ push(chunk) {
245
+ this.buffer = Buffer.concat([this.buffer, chunk]);
246
+ const messages = [];
247
+ while (this.buffer.length >= 4) {
248
+ const length = this.buffer.readUInt32BE(0);
249
+ if (this.buffer.length < 4 + length)
250
+ break;
251
+ messages.push(this.buffer.subarray(4, 4 + length));
252
+ this.buffer = this.buffer.subarray(4 + length);
253
+ }
254
+ return messages;
255
+ }
256
+ }
257
+ exports.LengthPrefixedReader = LengthPrefixedReader;
258
+ /**
259
+ * @param request The RPC request
260
+ * @param timestamp Unix time in seconds
261
+ * @returns The payload of an RPC message
262
+ */
263
+ function rpcPayload(request, timestamp) {
264
+ return Buffer.from(JSON.stringify({ dps: { "101": JSON.stringify(request) }, t: timestamp }));
265
+ }
266
+ /**
267
+ * @param message A received message
268
+ * @returns The data points of its payload, by code (`102` = RPC response, `121`… = status pushes)
269
+ */
270
+ function dataPoints(message) {
271
+ if (!message.payload?.length)
272
+ return undefined;
273
+ try {
274
+ const dps = JSON.parse(message.payload.toString("utf8"))?.dps;
275
+ return dps && typeof dps === "object" ? dps : undefined;
276
+ }
277
+ catch {
278
+ return undefined;
279
+ }
280
+ }
281
+ /**
282
+ * @param message A received message
283
+ * @returns The RPC response it carries, if any
284
+ */
285
+ function rpcResponse(message) {
286
+ const value = dataPoints(message)?.["102"];
287
+ if (typeof value !== "string")
288
+ return undefined;
289
+ try {
290
+ const response = JSON.parse(value);
291
+ return typeof response?.id === "number" ? response : undefined;
292
+ }
293
+ catch {
294
+ return undefined;
295
+ }
296
+ }
297
+ /**
298
+ * @param method The method that was called
299
+ * @param rpc Its response
300
+ * @returns The result
301
+ * @throws {RoborockApiError} If the robot returned an error, or doesn't know the method
302
+ */
303
+ function rpcResult(method, rpc) {
304
+ if (rpc.error !== undefined) {
305
+ const error = typeof rpc.error === "string" ? { message: rpc.error } : rpc.error;
306
+ throw new errors_1.RoborockApiError(`${method} failed: ${error.message ?? JSON.stringify(error)}`, error.code);
307
+ }
308
+ if (rpc.result === "unknown_method") {
309
+ throw new errors_1.RoborockApiError(`${method} is not supported by this robot`, "unknown_method");
310
+ }
311
+ return rpc.result;
312
+ }
313
+ //# sourceMappingURL=message.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"message.js","sourceRoot":"","sources":["../../../src/lib/roborock/message.ts"],"names":[],"mappings":";;;;;;AAkDA,0CAGC;AA6JD,sCAwBC;AAQD,sCAwCC;AAMD,4CAIC;AAsCD,gCAIC;AAMD,gCAUC;AAYD,kCAWC;AAQD,8BAgBC;AA7YD;;;;;;;;;;;GAWG;AACH,8DAAiC;AACjC,0DAA6B;AAE7B,qCAA2D;AAE3D,MAAM,IAAI,GAAG,kBAAkB,CAAC;AAChC,MAAM,aAAa,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAExC,2CAA2C;AAC9B,QAAA,gBAAgB,GAAG;IAC9B,aAAa,EAAE,CAAC;IAChB,cAAc,EAAE,CAAC;IACjB,YAAY,EAAE,CAAC;IACf,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QACrE,MAAM,IAAI,yBAAgB,CACxB,GAAG,MAAM,YAAY,KAAK,CAAC,OAAO,IAAI,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,EAC7D,KAAK,CAAC,IAAI,CACX,CAAC;IACJ,CAAC;IACD,IAAI,GAAG,CAAC,MAAM,KAAK,gBAAgB,EAAE,CAAC;QACpC,MAAM,IAAI,yBAAgB,CACxB,GAAG,MAAM,iCAAiC,EAC1C,gBAAgB,CACjB,CAAC;IACJ,CAAC;IACD,OAAO,GAAG,CAAC,MAAW,CAAC;AACzB,CAAC"}
@@ -0,0 +1,119 @@
1
+ import { EventEmitter } from "node:events";
2
+ import { type MiioLogger } from "../logger";
3
+ import type { RoborockRriot } from "./hawk";
4
+ /** The minimal MQTT client used (mqtt.js' `MqttClient`), injectable for tests */
5
+ export interface MqttClientLike {
6
+ on(event: "message", listener: (topic: string, payload: Buffer) => void): unknown;
7
+ on(event: "close" | "connect", listener: () => void): unknown;
8
+ subscribeAsync(topic: string): Promise<unknown>;
9
+ unsubscribeAsync(topic: string): Promise<unknown>;
10
+ publishAsync(topic: string, payload: Buffer): Promise<unknown>;
11
+ endAsync(): Promise<void>;
12
+ }
13
+ export type MqttConnect = (url: string, options: {
14
+ username: string;
15
+ password: string;
16
+ clientId: string;
17
+ connectTimeout: number;
18
+ reconnectPeriod: number;
19
+ keepalive: number;
20
+ }) => Promise<MqttClientLike>;
21
+ /**
22
+ * @param rriot The account's `rriot` credentials
23
+ * @returns The broker URL and credentials
24
+ */
25
+ export declare function mqttParams(rriot: RoborockRriot): {
26
+ url: string;
27
+ username: string;
28
+ password: string;
29
+ };
30
+ export interface RoborockMqttSessionOptions {
31
+ /** Injectable MQTT client (tests). Default: mqtt.js */
32
+ connect?: MqttConnect;
33
+ /** Timeout of the connection (default 15s) */
34
+ timeoutMs?: number;
35
+ logger?: MiioLogger;
36
+ }
37
+ /**
38
+ * The MQTT connection of an account, shared by its robots.
39
+ */
40
+ export declare class RoborockMqttSession {
41
+ private readonly rriot;
42
+ private readonly options;
43
+ private client?;
44
+ private readonly handlers;
45
+ private readonly params;
46
+ private readonly debug;
47
+ constructor(rriot: RoborockRriot, options?: RoborockMqttSessionOptions);
48
+ private topic;
49
+ private connect;
50
+ /**
51
+ * @param duid The robot's ID
52
+ * @param handler Called with every message the robot sends
53
+ * @returns Unsubscribes
54
+ */
55
+ subscribe(duid: string, handler: (payload: Buffer) => void): Promise<() => Promise<void>>;
56
+ /**
57
+ * @param duid The robot's ID
58
+ * @param payload The encoded message
59
+ */
60
+ publish(duid: string, payload: Buffer): Promise<void>;
61
+ /** The `endpoint` of the requests' security block: identifies this account's requests in the map responses */
62
+ get endpoint(): string;
63
+ /** Closes the connection */
64
+ close(): Promise<void>;
65
+ }
66
+ export interface RoborockMqttConnectionOptions {
67
+ duid: string;
68
+ localKey: string;
69
+ /** Timeout of each request (default 10s) */
70
+ timeoutMs?: number;
71
+ logger?: MiioLogger;
72
+ }
73
+ /**
74
+ * A robot over the account's MQTT connection.
75
+ *
76
+ * Events: `message` (every message received, e.g. status pushes).
77
+ */
78
+ export declare class RoborockMqttConnection extends EventEmitter {
79
+ private readonly session;
80
+ private readonly options;
81
+ private unsubscribe?;
82
+ private readonly waiting;
83
+ private seq;
84
+ /** Encrypts the maps sent back (map responses come later) */
85
+ readonly nonce: NonSharedBuffer;
86
+ private readonly debug;
87
+ constructor(session: RoborockMqttSession, options: RoborockMqttConnectionOptions);
88
+ private get timeoutMs();
89
+ private subscribed;
90
+ private onPayload;
91
+ /**
92
+ * Sends an RPC request and waits for the first received message for which `accept` returns a value.
93
+ *
94
+ * @param method The method
95
+ * @param params The params
96
+ * @param accept Given the request ID, returns the function that extracts the response from the received messages
97
+ * @param timeoutMs How long to wait
98
+ * @returns The response
99
+ */
100
+ private request;
101
+ /**
102
+ * Calls a method on the robot (the same methods as miIO: `get_status`, `app_start`…).
103
+ *
104
+ * @param method The method
105
+ * @param params The params
106
+ * @returns The result
107
+ */
108
+ call<T = unknown>(method: string, params?: unknown): Promise<T>;
109
+ /**
110
+ * Downloads the current map (`get_map_v1`). The robot sends it in protocol 300/301 messages, encrypted with this
111
+ * connection's {@link nonce}.
112
+ *
113
+ * @param timeoutMs How long to wait for the map (default 30s)
114
+ * @returns The map file (`rr` format: parse it with `parseRoborockMap`)
115
+ */
116
+ getMapData(timeoutMs?: number): Promise<Buffer>;
117
+ /** Stops listening to the robot (the account's session stays open) */
118
+ close(): Promise<void>;
119
+ }