node-miio 0.3.0 → 0.4.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.
- package/.claude/settings.json +5 -0
- package/.claude/skills/release/SKILL.md +62 -0
- package/AGENTS.md +8 -0
- package/LICENSE.md +28 -0
- package/README.md +43 -37
- package/dist/cli/commands/cloud/devices.js +71 -0
- package/dist/cli/commands/cloud/devices.js.map +1 -0
- package/dist/cli/commands/cloud/login.js +63 -0
- package/dist/cli/commands/cloud/login.js.map +1 -0
- package/dist/cli/commands/cloud/logout.js +14 -0
- package/dist/cli/commands/cloud/logout.js.map +1 -0
- package/dist/cli/commands/cloud.js +49 -0
- package/dist/cli/commands/cloud.js.map +1 -0
- package/dist/cli/index.js +2 -0
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/prompt.js +42 -0
- package/dist/cli/prompt.js.map +1 -0
- package/dist/cli/terminal_qr.js +14 -0
- package/dist/cli/terminal_qr.js.map +1 -0
- package/dist/lib/cloud/client.js +285 -0
- package/dist/lib/cloud/client.js.map +1 -0
- package/dist/lib/cloud/crypto.js +128 -0
- package/dist/lib/cloud/crypto.js.map +1 -0
- package/dist/lib/cloud/errors.js +50 -0
- package/dist/lib/cloud/errors.js.map +1 -0
- package/dist/lib/cloud/http.js +159 -0
- package/dist/lib/cloud/http.js.map +1 -0
- package/dist/lib/cloud/index.js +22 -0
- package/dist/lib/cloud/index.js.map +1 -0
- package/dist/lib/cloud/login.js +352 -0
- package/dist/lib/cloud/login.js.map +1 -0
- package/dist/lib/cloud/session_store.js +71 -0
- package/dist/lib/cloud/session_store.js.map +1 -0
- package/dist/lib/devices/vacuums/vacuum.js +80 -0
- package/dist/lib/devices/vacuums/vacuum.js.map +1 -1
- package/dist/lib/index.js +14 -1
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/maps/map_watcher.js +130 -0
- package/dist/lib/maps/map_watcher.js.map +1 -0
- package/dist/lib/maps/roborock_map.js +269 -0
- package/dist/lib/maps/roborock_map.js.map +1 -0
- package/docs/cloud.md +74 -0
- package/docs/devices/vacuum.md +9 -1
- package/package.json +2 -1
- package/snapshots/vacuum-specs.json.gz +0 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultSessionFile = exports.deleteSession = exports.loadSession = exports.saveSession = exports.CloudApiError = exports.CloudSessionExpiredError = exports.CloudTwoFactorRequiredError = exports.CloudCaptchaRequiredError = exports.CloudAuthError = exports.CloudError = exports.loginWithQrCode = exports.loginWithPassword = exports.CLOUD_SERVERS = exports.XiaomiCloud = void 0;
|
|
4
|
+
var client_1 = require("./client");
|
|
5
|
+
Object.defineProperty(exports, "XiaomiCloud", { enumerable: true, get: function () { return client_1.XiaomiCloud; } });
|
|
6
|
+
Object.defineProperty(exports, "CLOUD_SERVERS", { enumerable: true, get: function () { return client_1.CLOUD_SERVERS; } });
|
|
7
|
+
var login_1 = require("./login");
|
|
8
|
+
Object.defineProperty(exports, "loginWithPassword", { enumerable: true, get: function () { return login_1.loginWithPassword; } });
|
|
9
|
+
Object.defineProperty(exports, "loginWithQrCode", { enumerable: true, get: function () { return login_1.loginWithQrCode; } });
|
|
10
|
+
var errors_1 = require("./errors");
|
|
11
|
+
Object.defineProperty(exports, "CloudError", { enumerable: true, get: function () { return errors_1.CloudError; } });
|
|
12
|
+
Object.defineProperty(exports, "CloudAuthError", { enumerable: true, get: function () { return errors_1.CloudAuthError; } });
|
|
13
|
+
Object.defineProperty(exports, "CloudCaptchaRequiredError", { enumerable: true, get: function () { return errors_1.CloudCaptchaRequiredError; } });
|
|
14
|
+
Object.defineProperty(exports, "CloudTwoFactorRequiredError", { enumerable: true, get: function () { return errors_1.CloudTwoFactorRequiredError; } });
|
|
15
|
+
Object.defineProperty(exports, "CloudSessionExpiredError", { enumerable: true, get: function () { return errors_1.CloudSessionExpiredError; } });
|
|
16
|
+
Object.defineProperty(exports, "CloudApiError", { enumerable: true, get: function () { return errors_1.CloudApiError; } });
|
|
17
|
+
var session_store_1 = require("./session_store");
|
|
18
|
+
Object.defineProperty(exports, "saveSession", { enumerable: true, get: function () { return session_store_1.saveSession; } });
|
|
19
|
+
Object.defineProperty(exports, "loadSession", { enumerable: true, get: function () { return session_store_1.loadSession; } });
|
|
20
|
+
Object.defineProperty(exports, "deleteSession", { enumerable: true, get: function () { return session_store_1.deleteSession; } });
|
|
21
|
+
Object.defineProperty(exports, "defaultSessionFile", { enumerable: true, get: function () { return session_store_1.defaultSessionFile; } });
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/cloud/index.ts"],"names":[],"mappings":";;;AAAA,mCAMkB;AALhB,qGAAA,WAAW,OAAA;AACX,uGAAA,aAAa,OAAA;AAKf,iCAOiB;AANf,0GAAA,iBAAiB,OAAA;AACjB,wGAAA,eAAe,OAAA;AAMjB,mCAOkB;AANhB,oGAAA,UAAU,OAAA;AACV,wGAAA,cAAc,OAAA;AACd,mHAAA,yBAAyB,OAAA;AACzB,qHAAA,2BAA2B,OAAA;AAC3B,kHAAA,wBAAwB,OAAA;AACxB,uGAAA,aAAa,OAAA;AAEf,iDAKyB;AAJvB,4GAAA,WAAW,OAAA;AACX,4GAAA,WAAW,OAAA;AACX,8GAAA,aAAa,OAAA;AACb,mHAAA,kBAAkB,OAAA"}
|
|
@@ -0,0 +1,352 @@
|
|
|
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.generateAgent = generateAgent;
|
|
7
|
+
exports.loginWithPassword = loginWithPassword;
|
|
8
|
+
exports.loginWithQrCode = loginWithQrCode;
|
|
9
|
+
/*
|
|
10
|
+
* Login flows of the Xiaomi account (password, with captcha and email verification, and QR code).
|
|
11
|
+
*
|
|
12
|
+
* Ported from Xiaomi-cloud-tokens-extractor (https://github.com/PiotrMachowski/Xiaomi-cloud-tokens-extractor)
|
|
13
|
+
* Copyright (c) 2020 Piotr Machowski. Licensed under the MIT License.
|
|
14
|
+
*/
|
|
15
|
+
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
16
|
+
const debug_1 = __importDefault(require("debug"));
|
|
17
|
+
const errors_1 = require("./errors");
|
|
18
|
+
const http_1 = require("./http");
|
|
19
|
+
const debug = (0, debug_1.default)("miio:cloud");
|
|
20
|
+
const ACCOUNT = "https://account.xiaomi.com";
|
|
21
|
+
const STS_CALLBACK = "https://sts.api.io.mi.com/sts";
|
|
22
|
+
const LOGIN_QS = "%3Fsid%3Dxiaomiio%26_json%3Dtrue";
|
|
23
|
+
/**
|
|
24
|
+
* @returns A random user agent of the Mi Home app
|
|
25
|
+
*/
|
|
26
|
+
function generateAgent() {
|
|
27
|
+
const pick = (from, to, length) => String.fromCharCode(...Array.from({ length }, () => node_crypto_1.default.randomInt(from, to + 1)));
|
|
28
|
+
return `${pick(97, 122, 18)}-${pick(65, 69, 13)} APP/com.xiaomi.mihome APPV/10.5.201`;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @param fetchImpl The `fetch` implementation
|
|
32
|
+
* @returns A new HTTP session with the cookies and headers of the Mi Home app
|
|
33
|
+
*/
|
|
34
|
+
function newSession(fetchImpl) {
|
|
35
|
+
const http = new http_1.HttpSession(fetchImpl);
|
|
36
|
+
const deviceId = String.fromCharCode(...Array.from({ length: 6 }, () => node_crypto_1.default.randomInt(97, 123)));
|
|
37
|
+
for (const domain of ["mi.com", "xiaomi.com"]) {
|
|
38
|
+
http.setCookie("sdkVersion", "accountsdk-18.8.15", domain);
|
|
39
|
+
http.setCookie("deviceId", deviceId, domain);
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
http,
|
|
43
|
+
headers: {
|
|
44
|
+
"User-Agent": generateAgent(),
|
|
45
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Logs in with the Xiaomi account's username (email, phone number or user ID) and password.
|
|
51
|
+
*
|
|
52
|
+
* @param username The username
|
|
53
|
+
* @param password The password (only its MD5 hash is sent, as the Mi Home app does)
|
|
54
|
+
* @param options Callbacks for the captcha and the email verification
|
|
55
|
+
* @returns The session
|
|
56
|
+
*/
|
|
57
|
+
async function loginWithPassword(username, password, options = {}) {
|
|
58
|
+
const { http, headers } = newSession(options.fetch);
|
|
59
|
+
// Step 1: get the `_sign`
|
|
60
|
+
const step1 = await http.request(`${ACCOUNT}/pass/serviceLogin?sid=xiaomiio&_json=true`, { headers, cookies: { userId: username } });
|
|
61
|
+
const step1Json = safeJson(step1.text);
|
|
62
|
+
if (step1.status !== 200 || !step1Json) {
|
|
63
|
+
throw new errors_1.CloudAuthError(`Invalid username (HTTP ${step1.status})`);
|
|
64
|
+
}
|
|
65
|
+
let auth;
|
|
66
|
+
if (typeof step1Json.ssecurity === "string") {
|
|
67
|
+
// Already logged in
|
|
68
|
+
auth = step1Json;
|
|
69
|
+
}
|
|
70
|
+
else if (typeof step1Json._sign === "string") {
|
|
71
|
+
// Step 2: send the credentials
|
|
72
|
+
const fields = {
|
|
73
|
+
sid: "xiaomiio",
|
|
74
|
+
// The Xiaomi login protocol expects the (uppercase) MD5 of the password, like the Mi Home app sends.
|
|
75
|
+
// It's a wire format we don't choose, not a hash we store: the password and its hash are never persisted.
|
|
76
|
+
hash: node_crypto_1.default
|
|
77
|
+
.createHash("md5")
|
|
78
|
+
.update(password)
|
|
79
|
+
.digest("hex")
|
|
80
|
+
.toUpperCase(),
|
|
81
|
+
callback: STS_CALLBACK,
|
|
82
|
+
qs: LOGIN_QS,
|
|
83
|
+
user: username,
|
|
84
|
+
_sign: step1Json._sign,
|
|
85
|
+
_json: "true",
|
|
86
|
+
};
|
|
87
|
+
const loginAuth2 = () => http.request(`${ACCOUNT}/pass/serviceLoginAuth2`, {
|
|
88
|
+
method: "POST",
|
|
89
|
+
headers,
|
|
90
|
+
query: fields,
|
|
91
|
+
followRedirects: false,
|
|
92
|
+
});
|
|
93
|
+
let step2 = await loginAuth2();
|
|
94
|
+
let step2Json = step2.status === 200 ? safeJson(step2.text) : undefined;
|
|
95
|
+
if (typeof step2Json?.captchaUrl === "string" && step2Json.captchaUrl) {
|
|
96
|
+
const captchaUrl = new URL(step2Json.captchaUrl, ACCOUNT).toString();
|
|
97
|
+
if (!options.solveCaptcha)
|
|
98
|
+
throw new errors_1.CloudCaptchaRequiredError(captchaUrl);
|
|
99
|
+
const image = await http.request(captchaUrl, { headers });
|
|
100
|
+
if (image.status !== 200) {
|
|
101
|
+
throw new errors_1.CloudError(`Unable to fetch the captcha (HTTP ${image.status})`);
|
|
102
|
+
}
|
|
103
|
+
fields.captCode = await options.solveCaptcha(image.body, captchaUrl);
|
|
104
|
+
step2 = await loginAuth2();
|
|
105
|
+
step2Json = step2.status === 200 ? safeJson(step2.text) : undefined;
|
|
106
|
+
if (step2Json?.code === 87001)
|
|
107
|
+
throw new errors_1.CloudAuthError("Invalid captcha");
|
|
108
|
+
}
|
|
109
|
+
if (!step2Json) {
|
|
110
|
+
throw new errors_1.CloudAuthError(`Login failed (HTTP ${step2.status})`);
|
|
111
|
+
}
|
|
112
|
+
if (isValidSsecurity(step2Json.ssecurity)) {
|
|
113
|
+
auth = step2Json;
|
|
114
|
+
}
|
|
115
|
+
else if (typeof step2Json.notificationUrl === "string") {
|
|
116
|
+
if (!options.getVerificationCode) {
|
|
117
|
+
throw new errors_1.CloudTwoFactorRequiredError(step2Json.notificationUrl);
|
|
118
|
+
}
|
|
119
|
+
return emailVerificationFlow(http, headers, step2Json.notificationUrl, options.getVerificationCode, step2Json.userId != null ? String(step2Json.userId) : undefined);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
debug(`serviceLoginAuth2 failed with code ${step2Json.code}`);
|
|
123
|
+
throw new errors_1.CloudAuthError("Invalid username or password");
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
throw new errors_1.CloudAuthError("Invalid username");
|
|
128
|
+
}
|
|
129
|
+
// Step 3: get the service token
|
|
130
|
+
const location = stringOrUndefined(auth.location);
|
|
131
|
+
if (!location)
|
|
132
|
+
throw new errors_1.CloudError("The login didn't return a location");
|
|
133
|
+
const step3 = await http.request(location, { headers });
|
|
134
|
+
const serviceToken = http.getCookie("serviceToken");
|
|
135
|
+
if (step3.status !== 200 || !serviceToken) {
|
|
136
|
+
throw new errors_1.CloudError(`Unable to get the service token (HTTP ${step3.status})`);
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
userId: String(auth.userId ?? http.getCookie("userId") ?? ""),
|
|
140
|
+
ssecurity: auth.ssecurity,
|
|
141
|
+
serviceToken,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* The email-based two-factor authentication: Xiaomi emails a code, the user types it.
|
|
146
|
+
*
|
|
147
|
+
* @param http The HTTP session of the login
|
|
148
|
+
* @param headers The headers of the login
|
|
149
|
+
* @param notificationUrl The URL returned by the login
|
|
150
|
+
* @param getVerificationCode The callback that asks the user for the code
|
|
151
|
+
* @param userId The user ID, if known
|
|
152
|
+
* @returns The session
|
|
153
|
+
*/
|
|
154
|
+
async function emailVerificationFlow(http, headers, notificationUrl, getVerificationCode, userId) {
|
|
155
|
+
// 1) Open the notification URL (authStart)
|
|
156
|
+
await http.request(notificationUrl, { headers });
|
|
157
|
+
// 2) Fetch the identity options
|
|
158
|
+
const context = new URL(notificationUrl).searchParams.get("context") ?? "";
|
|
159
|
+
const common = { sid: "xiaomiio", context, _locale: "en_US" };
|
|
160
|
+
await http.request(`${ACCOUNT}/identity/list`, { headers, query: common });
|
|
161
|
+
// 3) Ask Xiaomi to email the code
|
|
162
|
+
const ick = () => http.getCookie("ick") ?? "";
|
|
163
|
+
await http.request(`${ACCOUNT}/identity/auth/sendEmailTicket`, {
|
|
164
|
+
method: "POST",
|
|
165
|
+
headers,
|
|
166
|
+
query: { _dc: String(Date.now()), ...common, mask: "0" },
|
|
167
|
+
form: { retry: "0", icode: "", _json: "true", ick: ick() },
|
|
168
|
+
});
|
|
169
|
+
// 4) Verify the code
|
|
170
|
+
const code = (await getVerificationCode()).trim();
|
|
171
|
+
const verify = await http.request(`${ACCOUNT}/identity/auth/verifyEmail`, {
|
|
172
|
+
method: "POST",
|
|
173
|
+
headers,
|
|
174
|
+
query: { _flag: "8", _json: "true", ...common, mask: "0" },
|
|
175
|
+
form: {
|
|
176
|
+
_flag: "8",
|
|
177
|
+
ticket: code,
|
|
178
|
+
trust: "false",
|
|
179
|
+
_json: "true",
|
|
180
|
+
ick: ick(),
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
if (verify.status !== 200) {
|
|
184
|
+
throw new errors_1.CloudAuthError(`Invalid verification code (HTTP ${verify.status})`);
|
|
185
|
+
}
|
|
186
|
+
let finish = stringOrUndefined(safeJson(verify.text)?.location) ??
|
|
187
|
+
verify.headers.get("location") ??
|
|
188
|
+
verify.text.match(/https:\/\/account\.xiaomi\.com\/identity\/result\/check\?[^"']+/)?.[0];
|
|
189
|
+
if (!finish) {
|
|
190
|
+
// Fallback: hit result/check directly
|
|
191
|
+
const check = await http.request(`${ACCOUNT}/identity/result/check`, {
|
|
192
|
+
headers,
|
|
193
|
+
query: common,
|
|
194
|
+
followRedirects: false,
|
|
195
|
+
});
|
|
196
|
+
finish = check.headers.get("location") ?? undefined;
|
|
197
|
+
}
|
|
198
|
+
if (!finish)
|
|
199
|
+
throw new errors_1.CloudError("Unable to complete the verification");
|
|
200
|
+
finish = unescapeUrl(finish);
|
|
201
|
+
// 5) result/check redirects to serviceLoginAuth2/end
|
|
202
|
+
let endUrl = finish;
|
|
203
|
+
if (finish.includes("identity/result/check")) {
|
|
204
|
+
const check = await http.request(finish, {
|
|
205
|
+
headers,
|
|
206
|
+
followRedirects: false,
|
|
207
|
+
});
|
|
208
|
+
endUrl = check.headers.get("location") ?? undefined;
|
|
209
|
+
}
|
|
210
|
+
if (!endUrl)
|
|
211
|
+
throw new errors_1.CloudError("Unable to find the end of the verification");
|
|
212
|
+
// 6) serviceLoginAuth2/end returns the ssecurity in the `extension-pragma` header
|
|
213
|
+
let end = await http.request(endUrl, { headers, followRedirects: false });
|
|
214
|
+
if (end.status === 200 && end.text.includes("Xiaomi Account - Tips")) {
|
|
215
|
+
end = await http.request(endUrl, { headers, followRedirects: false });
|
|
216
|
+
}
|
|
217
|
+
let ssecurity;
|
|
218
|
+
try {
|
|
219
|
+
ssecurity = stringOrUndefined(JSON.parse(end.headers.get("extension-pragma") ?? "{}").ssecurity);
|
|
220
|
+
}
|
|
221
|
+
catch (err) {
|
|
222
|
+
debug(`Unable to parse extension-pragma: ${err}`);
|
|
223
|
+
}
|
|
224
|
+
if (!ssecurity)
|
|
225
|
+
throw new errors_1.CloudError("The verification didn't return the ssecurity");
|
|
226
|
+
// 7) The STS redirect sets the serviceToken cookie
|
|
227
|
+
let stsUrl = end.headers.get("location") ?? undefined;
|
|
228
|
+
if (!stsUrl) {
|
|
229
|
+
const start = end.text.indexOf(STS_CALLBACK);
|
|
230
|
+
if (start !== -1) {
|
|
231
|
+
const stop = end.text.indexOf('"', start);
|
|
232
|
+
stsUrl = end.text.slice(start, stop === -1 ? start + 300 : stop);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (!stsUrl)
|
|
236
|
+
throw new errors_1.CloudError("The verification didn't return the STS URL");
|
|
237
|
+
const sts = await http.request(unescapeUrl(stsUrl), { headers });
|
|
238
|
+
const serviceToken = http.getCookie("serviceToken");
|
|
239
|
+
if (sts.status !== 200 || !serviceToken) {
|
|
240
|
+
throw new errors_1.CloudError(`Unable to get the service token (HTTP ${sts.status})`);
|
|
241
|
+
}
|
|
242
|
+
return {
|
|
243
|
+
userId: userId ?? http.getCookie("userId") ?? "",
|
|
244
|
+
ssecurity,
|
|
245
|
+
serviceToken,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Logs in by scanning a QR code with the Mi Home app (no password needed).
|
|
250
|
+
*
|
|
251
|
+
* @param options The callback that shows the QR code
|
|
252
|
+
* @returns The session
|
|
253
|
+
*/
|
|
254
|
+
async function loginWithQrCode(options) {
|
|
255
|
+
const { http, headers } = newSession(options.fetch);
|
|
256
|
+
// Step 1: get the QR code
|
|
257
|
+
const step1 = await http.request(`${ACCOUNT}/longPolling/loginUrl`, {
|
|
258
|
+
headers,
|
|
259
|
+
query: {
|
|
260
|
+
_qrsize: "480",
|
|
261
|
+
qs: LOGIN_QS,
|
|
262
|
+
callback: STS_CALLBACK,
|
|
263
|
+
_hasLogo: "false",
|
|
264
|
+
sid: "xiaomiio",
|
|
265
|
+
serviceParam: "",
|
|
266
|
+
_locale: "en_GB",
|
|
267
|
+
_dc: String(Date.now()),
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
const qr = step1.status === 200 ? safeJson(step1.text) : undefined;
|
|
271
|
+
if (typeof qr?.qr !== "string" || typeof qr.lp !== "string") {
|
|
272
|
+
throw new errors_1.CloudError(`Unable to get the login QR code (HTTP ${step1.status})`);
|
|
273
|
+
}
|
|
274
|
+
// The timeout is in seconds (values that look like milliseconds are accepted too)
|
|
275
|
+
const timeout = Number(qr.timeout) || 300;
|
|
276
|
+
const timeoutMs = timeout > 3600 ? timeout : timeout * 1000;
|
|
277
|
+
await options.onQrCode({
|
|
278
|
+
imageUrl: qr.qr,
|
|
279
|
+
loginUrl: String(qr.loginUrl ?? qr.qr),
|
|
280
|
+
timeoutMs,
|
|
281
|
+
});
|
|
282
|
+
// Step 2: long-poll until the QR code is scanned
|
|
283
|
+
const deadline = Date.now() + timeoutMs;
|
|
284
|
+
let auth;
|
|
285
|
+
while (!auth && Date.now() < deadline) {
|
|
286
|
+
try {
|
|
287
|
+
const poll = await http.request(qr.lp, { headers, timeoutMs: 30_000 });
|
|
288
|
+
const body = poll.status === 200 ? safeJson(poll.text) : undefined;
|
|
289
|
+
if (isValidSsecurity(body?.ssecurity)) {
|
|
290
|
+
auth = body;
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
await new Promise((resolve) => setTimeout(resolve, options.retryDelayMs ?? 2000));
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
catch (err) {
|
|
297
|
+
// Timeouts are expected: the server holds the request until the QR code is scanned
|
|
298
|
+
debug(`Long polling: ${err}`);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (!auth || !isValidSsecurity(auth.ssecurity)) {
|
|
302
|
+
throw new errors_1.CloudAuthError("The QR code wasn't scanned in time");
|
|
303
|
+
}
|
|
304
|
+
const ssecurity = auth.ssecurity;
|
|
305
|
+
// Step 3: get the service token
|
|
306
|
+
const location = stringOrUndefined(auth.location);
|
|
307
|
+
if (!location)
|
|
308
|
+
throw new errors_1.CloudError("The login didn't return a location");
|
|
309
|
+
const step3 = await http.request(location, { headers });
|
|
310
|
+
const serviceToken = http.getCookie("serviceToken");
|
|
311
|
+
if (step3.status !== 200 || !serviceToken) {
|
|
312
|
+
throw new errors_1.CloudError(`Unable to get the service token (HTTP ${step3.status})`);
|
|
313
|
+
}
|
|
314
|
+
return { userId: String(auth.userId), ssecurity, serviceToken };
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* @param text A response body
|
|
318
|
+
* @returns The parsed JSON object, or `undefined` if it isn't one
|
|
319
|
+
*/
|
|
320
|
+
function safeJson(text) {
|
|
321
|
+
try {
|
|
322
|
+
const parsed = (0, http_1.parseXiaomiJson)(text);
|
|
323
|
+
return parsed && typeof parsed === "object" ? parsed : undefined;
|
|
324
|
+
}
|
|
325
|
+
catch {
|
|
326
|
+
return undefined;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* @param value A value of the response
|
|
331
|
+
* @returns Whether it's a valid `ssecurity`
|
|
332
|
+
*/
|
|
333
|
+
function isValidSsecurity(value) {
|
|
334
|
+
return typeof value === "string" && value.length > 4;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* @param value A value of the response
|
|
338
|
+
* @returns The value, if it's a non-empty string
|
|
339
|
+
*/
|
|
340
|
+
function stringOrUndefined(value) {
|
|
341
|
+
return typeof value === "string" && value ? value : undefined;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* URLs embedded in JSON/HTML bodies may have escaped ampersands.
|
|
345
|
+
*
|
|
346
|
+
* @param url The URL
|
|
347
|
+
* @returns The unescaped URL
|
|
348
|
+
*/
|
|
349
|
+
function unescapeUrl(url) {
|
|
350
|
+
return url.replace(/\\u0026|&/g, "&");
|
|
351
|
+
}
|
|
352
|
+
//# sourceMappingURL=login.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"login.js","sourceRoot":"","sources":["../../../src/lib/cloud/login.ts"],"names":[],"mappings":";;;;;AAmEA,sCAMC;AAgCD,8CAuGC;AAqID,0CAqEC;AA1ZD;;;;;GAKG;AACH,8DAAiC;AAEjC,kDAAiC;AAEjC,qCAKkB;AAClB,iCAAsE;AAEtE,MAAM,KAAK,GAAG,IAAA,eAAY,EAAC,YAAY,CAAC,CAAC;AAEzC,MAAM,OAAO,GAAG,4BAA4B,CAAC;AAC7C,MAAM,YAAY,GAAG,+BAA+B,CAAC;AACrD,MAAM,QAAQ,GAAG,kCAAkC,CAAC;AA0CpD;;GAEG;AACH,SAAgB,aAAa;IAC3B,MAAM,IAAI,GAAG,CAAC,IAAY,EAAE,EAAU,EAAE,MAAc,EAAE,EAAE,CACxD,MAAM,CAAC,YAAY,CACjB,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,GAAG,EAAE,CAAC,qBAAM,CAAC,SAAS,CAAC,IAAI,EAAE,EAAE,GAAG,CA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;;GAGG;AACH,SAAS,QAAQ,CAAC,IAAY;IAC5B,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAA,sBAAe,EAAC,IAAI,CAAC,CAAC;QACrC,OAAO,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC;IACnE,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,SAAS,gBAAgB,CAAC,KAAc;IACtC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;AACvD,CAAC;AAED;;;GAGG;AACH,SAAS,iBAAiB,CAAC,KAAc;IACvC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;AAChE,CAAC;AAED;;;;;GAKG;AACH,SAAS,WAAW,CAAC,GAAW;IAC9B,OAAO,GAAG,CAAC,OAAO,CAAC,gBAAgB,EAAE,GAAG,CAAC,CAAC;AAC5C,CAAC"}
|
|
@@ -0,0 +1,71 @@
|
|
|
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.defaultConfigDir = defaultConfigDir;
|
|
7
|
+
exports.defaultSessionFile = defaultSessionFile;
|
|
8
|
+
exports.saveSession = saveSession;
|
|
9
|
+
exports.loadSession = loadSession;
|
|
10
|
+
exports.deleteSession = deleteSession;
|
|
11
|
+
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
12
|
+
const node_os_1 = __importDefault(require("node:os"));
|
|
13
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
14
|
+
/**
|
|
15
|
+
* The directory of node-miio's private config (the Xiaomi Cloud session). Override it with `MIIO_CONFIG_DIR`.
|
|
16
|
+
*
|
|
17
|
+
* @returns The absolute path to the config directory
|
|
18
|
+
*/
|
|
19
|
+
function defaultConfigDir() {
|
|
20
|
+
if (process.env.MIIO_CONFIG_DIR)
|
|
21
|
+
return process.env.MIIO_CONFIG_DIR;
|
|
22
|
+
const home = node_os_1.default.homedir();
|
|
23
|
+
switch (process.platform) {
|
|
24
|
+
case "darwin":
|
|
25
|
+
return node_path_1.default.join(home, "Library", "Application Support", "node-miio");
|
|
26
|
+
case "win32":
|
|
27
|
+
return node_path_1.default.join(process.env.APPDATA || node_path_1.default.join(home, "AppData", "Roaming"), "node-miio");
|
|
28
|
+
default:
|
|
29
|
+
return node_path_1.default.join(process.env.XDG_CONFIG_HOME || node_path_1.default.join(home, ".config"), "node-miio");
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* @returns The default path of the saved session
|
|
34
|
+
*/
|
|
35
|
+
function defaultSessionFile() {
|
|
36
|
+
return node_path_1.default.join(defaultConfigDir(), "cloud-session.json");
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Saves the session, readable only by the current user (it's as sensitive as a password).
|
|
40
|
+
*
|
|
41
|
+
* @param session The session
|
|
42
|
+
* @param file The file (defaults to {@link defaultSessionFile})
|
|
43
|
+
*/
|
|
44
|
+
async function saveSession(session, file = defaultSessionFile()) {
|
|
45
|
+
await promises_1.default.mkdir(node_path_1.default.dirname(file), { recursive: true, mode: 0o700 });
|
|
46
|
+
await promises_1.default.writeFile(file, JSON.stringify(session, null, 2), { mode: 0o600 });
|
|
47
|
+
// writeFile's mode only applies to new files
|
|
48
|
+
await promises_1.default.chmod(file, 0o600);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* @param file The file (defaults to {@link defaultSessionFile})
|
|
52
|
+
* @returns The saved session, or `undefined` if there's none
|
|
53
|
+
*/
|
|
54
|
+
async function loadSession(file = defaultSessionFile()) {
|
|
55
|
+
try {
|
|
56
|
+
const session = JSON.parse(await promises_1.default.readFile(file, "utf8"));
|
|
57
|
+
if (session?.ssecurity && session?.serviceToken)
|
|
58
|
+
return session;
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// No session saved (or unreadable)
|
|
62
|
+
}
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* @param file The file (defaults to {@link defaultSessionFile})
|
|
67
|
+
*/
|
|
68
|
+
async function deleteSession(file = defaultSessionFile()) {
|
|
69
|
+
await promises_1.default.rm(file, { force: true });
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=session_store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session_store.js","sourceRoot":"","sources":["../../../src/lib/cloud/session_store.ts"],"names":[],"mappings":";;;;;AAWA,4CAiBC;AAKD,gDAEC;AAQD,kCAQC;AAMD,kCAUC;AAKD,sCAEC;AA1ED,gEAAkC;AAClC,sDAAyB;AACzB,0DAA6B;AAI7B;;;;GAIG;AACH,SAAgB,gBAAgB;IAC9B,IAAI,OAAO,CAAC,GAAG,CAAC,eAAe;QAAE,OAAO,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC;IACpE,MAAM,IAAI,GAAG,iBAAE,CAAC,OAAO,EAAE,CAAC;IAC1B,QAAQ,OAAO,CAAC,QAAQ,EAAE,CAAC;QACzB,KAAK,QAAQ;YACX,OAAO,mBAAI,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,EAAE,qBAAqB,EAAE,WAAW,CAAC,CAAC;QACxE,KAAK,OAAO;YACV,OAAO,mBAAI,CAAC,IAAI,CACd,OAAO,CAAC,GAAG,CAAC,OAAO,IAAI,mBAAI,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,EAAE,SAAS,CAAC,EAC5D,WAAW,CACZ,CAAC;QACJ;YACE,OAAO,mBAAI,CAAC,IAAI,CACd,OAAO,CAAC,GAAG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AAE,IAAI,EAAE,CAAC,CAAC;AACrC,CAAC"}
|
|
@@ -6,6 +6,7 @@ const climate_1 = require("abstract-things/climate");
|
|
|
6
6
|
const device_1 = require("../../device");
|
|
7
7
|
const battery_level_1 = require("../capabilities/battery-level");
|
|
8
8
|
const check_result_1 = require("../../check_result");
|
|
9
|
+
const roborock_map_1 = require("../../maps/roborock_map");
|
|
9
10
|
const vacuum_modes_1 = require("./vacuum_modes");
|
|
10
11
|
/** Mop routes (`get_mop_mode`/`set_mop_mode`) of the S7 and newer */
|
|
11
12
|
exports.MOP_ROUTES = {
|
|
@@ -20,6 +21,15 @@ const CONSUMABLE_PROPERTIES = {
|
|
|
20
21
|
filter_work_time: "filterWorkTime",
|
|
21
22
|
sensor_dirty_time: "sensorDirtyTime",
|
|
22
23
|
};
|
|
24
|
+
/** Properties that are also exposed in `state()` and the `stateChanged` events */
|
|
25
|
+
const STATE_PROPERTIES = ["waterBoxAttached", "waterShortage", "mopDrying"];
|
|
26
|
+
/**
|
|
27
|
+
* Maps a 0/1 flag to a boolean. Missing values stay `undefined`, so models that don't report them don't get the property.
|
|
28
|
+
*
|
|
29
|
+
* @param value The raw value
|
|
30
|
+
* @returns The boolean value, or `undefined`
|
|
31
|
+
*/
|
|
32
|
+
const isOne = (value) => (value == null ? undefined : value === 1);
|
|
23
33
|
/**
|
|
24
34
|
* Implementation of the interface used by the Mi Robot Vacuum. This device
|
|
25
35
|
* doesn't use properties via get_prop but instead has a get_status.
|
|
@@ -227,6 +237,19 @@ class Vacuum extends climate_1.Vacuum.with(device_1.MiioApi, battery_level_1.Bat
|
|
|
227
237
|
this.defineProperty("dust_collection_status", {
|
|
228
238
|
name: "dustCollectionStatus",
|
|
229
239
|
});
|
|
240
|
+
// Water tank and dock status. Only reported by the models that have them (otherwise they're not stored).
|
|
241
|
+
this.defineProperty("water_box_status", {
|
|
242
|
+
name: "waterBoxAttached",
|
|
243
|
+
mapper: isOne,
|
|
244
|
+
});
|
|
245
|
+
this.defineProperty("water_shortage_status", {
|
|
246
|
+
name: "waterShortage",
|
|
247
|
+
mapper: isOne,
|
|
248
|
+
});
|
|
249
|
+
this.defineProperty("dry_status", {
|
|
250
|
+
name: "mopDrying",
|
|
251
|
+
mapper: isOne,
|
|
252
|
+
});
|
|
230
253
|
}
|
|
231
254
|
propertyUpdated(key, value, oldValue) {
|
|
232
255
|
if (key === "state") {
|
|
@@ -263,6 +286,10 @@ class Vacuum extends climate_1.Vacuum.with(device_1.MiioApi, battery_level_1.Bat
|
|
|
263
286
|
else if (key === "fanSpeed") {
|
|
264
287
|
this.updateFanSpeed(value);
|
|
265
288
|
}
|
|
289
|
+
else if (STATE_PROPERTIES.includes(key)) {
|
|
290
|
+
// Exposed in `state()` and the `stateChanged` events
|
|
291
|
+
this.updateState(key, value);
|
|
292
|
+
}
|
|
266
293
|
super.propertyUpdated(key, value, oldValue);
|
|
267
294
|
}
|
|
268
295
|
getDeviceInfo() {
|
|
@@ -530,6 +557,41 @@ class Vacuum extends climate_1.Vacuum.with(device_1.MiioApi, battery_level_1.Bat
|
|
|
530
557
|
name,
|
|
531
558
|
}));
|
|
532
559
|
}
|
|
560
|
+
/**
|
|
561
|
+
* The name of the current map file in the Xiaomi Cloud. The robot answers `retry` while it's uploading the
|
|
562
|
+
* map, so it's asked again a few times (with an increasing delay).
|
|
563
|
+
*
|
|
564
|
+
* @param options The options
|
|
565
|
+
* @param options.retries How many times to ask (default 5)
|
|
566
|
+
* @param options.delayMs The first delay between retries, doubled every time (default 100ms, up to 15s)
|
|
567
|
+
* @returns The map name, to use with {@link XiaomiCloud.getMapFileUrl}
|
|
568
|
+
*/
|
|
569
|
+
async getMapName(options = {}) {
|
|
570
|
+
const retries = options.retries ?? 5;
|
|
571
|
+
let delay = options.delayMs ?? 100;
|
|
572
|
+
for (let attempt = 1;; attempt++) {
|
|
573
|
+
const [name] = await this.call("get_map_v1", []);
|
|
574
|
+
if (name && name !== "retry")
|
|
575
|
+
return name;
|
|
576
|
+
if (attempt >= retries) {
|
|
577
|
+
throw new Error("The vacuum didn't return the map name");
|
|
578
|
+
}
|
|
579
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
580
|
+
delay = Math.min(delay * 2, 15_000);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* Downloads and parses the current map from the Xiaomi Cloud: rooms, robot position, current room…
|
|
585
|
+
*
|
|
586
|
+
* @param cloud A logged-in Xiaomi Cloud client
|
|
587
|
+
* @param server The server the device is registered in (see {@link XiaomiCloud.findDevice})
|
|
588
|
+
* @returns The map
|
|
589
|
+
*/
|
|
590
|
+
async getCloudMap(cloud, server) {
|
|
591
|
+
const name = await this.getMapName();
|
|
592
|
+
const url = await cloud.getMapFileUrl(server, name);
|
|
593
|
+
return (0, roborock_map_1.parseRoborockMap)(await cloud.download(url));
|
|
594
|
+
}
|
|
533
595
|
/**
|
|
534
596
|
* Switch to another saved map (multi-floor).
|
|
535
597
|
*
|
|
@@ -550,6 +612,24 @@ class Vacuum extends climate_1.Vacuum.with(device_1.MiioApi, battery_level_1.Bat
|
|
|
550
612
|
async stopMopWash() {
|
|
551
613
|
await this.call("app_stop_wash", []).then(check_result_1.checkResult);
|
|
552
614
|
}
|
|
615
|
+
/**
|
|
616
|
+
* Dry the mop in the dock (docks with a mop dryer only).
|
|
617
|
+
*/
|
|
618
|
+
async startMopDrying() {
|
|
619
|
+
await this.call("app_set_dryer_status", { status: 1 }, {
|
|
620
|
+
refresh: ["mopDrying"],
|
|
621
|
+
refreshDelay: 1000,
|
|
622
|
+
}).then(check_result_1.checkResult);
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Stop drying the mop.
|
|
626
|
+
*/
|
|
627
|
+
async stopMopDrying() {
|
|
628
|
+
await this.call("app_set_dryer_status", { status: 0 }, {
|
|
629
|
+
refresh: ["mopDrying"],
|
|
630
|
+
refreshDelay: 1000,
|
|
631
|
+
}).then(check_result_1.checkResult);
|
|
632
|
+
}
|
|
553
633
|
/**
|
|
554
634
|
* Reset a consumable after replacing it.
|
|
555
635
|
*
|