@jessicaisleep/dsh-mihome 1.0.0 → 2.0.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/README.md +141 -78
- package/client.js +1149 -19
- package/cordis.patch.yml +2 -2
- package/docs/REPAIR-VALIDATION.md +62 -0
- package/lib/bridge.js +126 -44
- package/lib/connections.js +46 -0
- package/lib/format.js +148 -0
- package/lib/index.js +274 -31
- package/lib/panel.js +138 -7
- package/locale/en.json +3 -3
- package/locale/zh.json +2 -2
- package/package.json +13 -4
- package/python/app/core/service.py +62 -31
- package/python/mihome_bridge.py +211 -9
- package/python/smart_home/__init__.py +24 -0
- package/python/smart_home/adapters/__init__.py +50 -0
- package/python/smart_home/adapters/base.py +143 -0
- package/python/smart_home/adapters/ble.py +100 -0
- package/python/smart_home/adapters/dlna.py +227 -0
- package/python/smart_home/adapters/home_assistant.py +454 -0
- package/python/smart_home/adapters/hue.py +225 -0
- package/python/smart_home/adapters/kasa.py +239 -0
- package/python/smart_home/adapters/mihome.py +342 -0
- package/python/smart_home/adapters/network.py +250 -0
- package/python/smart_home/adapters/shelly.py +289 -0
- package/python/smart_home/adapters/tuya.py +95 -0
- package/python/smart_home/adapters/tv.py +26 -0
- package/python/smart_home/catalog.py +336 -0
- package/python/smart_home/cloud.py +85 -0
- package/python/smart_home/discovery.py +934 -0
- package/python/smart_home/localize.py +787 -0
- package/python/smart_home/manager.py +672 -0
- package/python/smart_home/oui_table.py +40550 -0
- package/python/smart_home/scan.py +73 -0
- package/python/smart_home/test_discovery_repairs.py +186 -0
- package/python/smart_home/test_localization_repairs.py +229 -0
- package/python/smart_home/test_offline.py +195 -0
- package/python/smart_home/test_repairs.py +107 -0
- package/python/smart_home/util.py +320 -0
- package/python/tests/test_bridge_integration.py +108 -0
- package/python/tests/test_ha_discovery.py +260 -0
- package/python/tests/test_home_assistant.py +365 -0
- package/python/tests/test_manager_integration.py +150 -0
- package/python/tests/test_mihome_repairs.py +117 -0
- package/python/tests/test_network_read_contract.py +50 -0
- package/python/tests/test_power_confirmation.py +95 -0
- package/python/tests/test_protocol_repairs.py +369 -0
- package/python/tests/test_yeelight_repairs.py +70 -0
- package/requirements-optional.txt +4 -0
- package/requirements.txt +5 -0
- package/scripts/update-oui.py +108 -0
package/lib/index.js
CHANGED
|
@@ -18,14 +18,16 @@ import { fileURLToPath } from "node:url";
|
|
|
18
18
|
import z from "@deepseek-ai/schemastery";
|
|
19
19
|
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
20
20
|
import { Bridge } from "./bridge.js";
|
|
21
|
-
import {
|
|
21
|
+
import { credentialKey } from "@deepseek-ai/dsh-credentials";
|
|
22
|
+
import { createConnections } from "./connections.js";
|
|
23
|
+
import { formatDeviceList, formatDeviceDetail, formatScenes, formatSmartHomeList, formatSmartHomeDetail, formatSmartHomeScenes } from "./format.js";
|
|
22
24
|
import { mountPanelRoutes } from "./panel.js";
|
|
23
25
|
|
|
24
26
|
/** Cordis 插件名,loader 诊断用。 */
|
|
25
27
|
const name = "mihome";
|
|
26
28
|
|
|
27
29
|
/** 只依赖工具服务与系统提示词——没有它们插件没什么可做的。 */
|
|
28
|
-
const inject = ["tools", "systemPrompt"];
|
|
30
|
+
const inject = ["tools", "systemPrompt", "credentials"];
|
|
29
31
|
|
|
30
32
|
const PLUGIN_ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
31
33
|
|
|
@@ -92,7 +94,8 @@ function apply(ctx, config) {
|
|
|
92
94
|
const script = join(PLUGIN_ROOT, "python", "mihome_bridge.py");
|
|
93
95
|
const cwd = join(PLUGIN_ROOT, "python");
|
|
94
96
|
|
|
95
|
-
const
|
|
97
|
+
const connections = createConnections(ctx.credentials, credentialKey("mihome", "devices"));
|
|
98
|
+
const bridge = connections.wrap(new Bridge({ pythonExe, script, dataDir, cwd }));
|
|
96
99
|
|
|
97
100
|
// 登录专用桥接:长轮询会独占进程最长约 2 分钟,不能和数据通道共用,
|
|
98
101
|
// 否则扫码期间面板上的设备列表/开关全部排队超时。
|
|
@@ -107,18 +110,27 @@ function apply(ctx, config) {
|
|
|
107
110
|
name: "tool:mihome",
|
|
108
111
|
order: 165,
|
|
109
112
|
interpolate: false,
|
|
110
|
-
text: "
|
|
113
|
+
text: "你有一组智能家居工具,用于发现设备线索、查看适配器已确认的能力、执行受支持的操作和管理手动场景。设备类别或协议识别不代表可控制;没有适配器确认的设备只能识别或读取,不得猜协议或写入。\n\n" +
|
|
114
|
+
"两条路径:\n" +
|
|
115
|
+
"- 全屋智能(smart_home_*):smart_home_scan 通过 ARP/ping/端口/SSDP/mDNS/涂鸦/miIO/BLE 等线索扫描本网段," +
|
|
116
|
+
"识别常见设备类别和协议线索;仅对适配器确认的能力提供控制;" +
|
|
117
|
+
"smart_home_devices 列出已发现/配置的设备与米家云端设备;smart_home_device 看单台的已确认能力与当前值;" +
|
|
118
|
+
"smart_home_control 统一控制(power/write/action/scrape);smart_home_scrape 读取路由器/光猫/NAS 的公开管理页,不等于已登录或能修改设置;" +
|
|
119
|
+
"smart_home_scenes 管理自定义场景并执行;smart_home_catalog 查能力项的中英对照。\n" +
|
|
120
|
+
"- 米家云端(mihome_*):mihome_devices / mihome_device / mihome_control / mihome_scenes / mihome_sensors," +
|
|
121
|
+
"面向米家账号下的设备。\n\n" +
|
|
111
122
|
"典型流程:\n" +
|
|
112
|
-
"1.
|
|
113
|
-
"2.
|
|
114
|
-
"3.
|
|
115
|
-
"4.
|
|
116
|
-
"5. mihome_sensors 读传感器数值(温度/湿度/电量/耗材余量)。\n\n" +
|
|
123
|
+
"1. smart_home_scan 扫描全屋,或直接 smart_home_devices 拿设备清单。设备 id 是后续所有操作的键。\n" +
|
|
124
|
+
"2. smart_home_device 看某台设备能控制什么——能力项已翻译成中文,别猜属性名。\n" +
|
|
125
|
+
"3. smart_home_control 开关、写值、执行动作;要看管理页设置时用 scrape 模式。\n" +
|
|
126
|
+
"4. smart_home_scenes 列出 / 保存 / 执行场景(如「回家时开灯、关窗帘」)。\n\n" +
|
|
117
127
|
"注意:\n" +
|
|
118
|
-
"-
|
|
128
|
+
"- 未登录米家时,云端操作返回 need_login,此时用 mihome_login(action=start)取二维码给用户扫。\n" +
|
|
129
|
+
"- 局域网扫描到的第三方设备(涂鸦 / Shelly / TP-Link 等)不需要米家登录,但可能需要本地密钥," +
|
|
130
|
+
"控制失败时如实说明,不要假装成功。\n" +
|
|
119
131
|
"- 修改设备状态是对用户家里真实设备的写操作:用户没说清楚哪台设备、改成什么值时先问,不要自行猜测后执行。\n" +
|
|
120
|
-
"-
|
|
121
|
-
"-
|
|
132
|
+
"- 设备名可能有重名,跨房间时用设备 id 或「房间+名称」指明。\n" +
|
|
133
|
+
"- 读取类操作(扫描、设备列表、读数、状态)可以直接执行,不需要额外确认。",
|
|
122
134
|
});
|
|
123
135
|
|
|
124
136
|
const register = (tool) => {
|
|
@@ -149,10 +161,10 @@ function apply(ctx, config) {
|
|
|
149
161
|
render: (_args, value) => [{ type: "text", text: renderLogin(value) }],
|
|
150
162
|
},
|
|
151
163
|
timeoutMs: loginTimeoutMs + 15_000,
|
|
152
|
-
async execute(args) {
|
|
164
|
+
async execute(args, exec) {
|
|
153
165
|
const action = String(args.action ?? "");
|
|
154
166
|
if (action === "status") {
|
|
155
|
-
const result = await
|
|
167
|
+
const result = await loginBridge.request("status", {}, requestTimeoutMs, exec?.signal);
|
|
156
168
|
return {
|
|
157
169
|
status: "status",
|
|
158
170
|
logged_in: Boolean(result.logged_in),
|
|
@@ -161,7 +173,7 @@ function apply(ctx, config) {
|
|
|
161
173
|
};
|
|
162
174
|
}
|
|
163
175
|
if (action === "start") {
|
|
164
|
-
const result = await
|
|
176
|
+
const result = await loginBridge.request("login_begin", {}, loginTimeoutMs, exec?.signal);
|
|
165
177
|
if (result.need_scan === false) {
|
|
166
178
|
return { status: "already_logged_in", logged_in: Boolean(result.logged_in) };
|
|
167
179
|
}
|
|
@@ -175,7 +187,7 @@ function apply(ctx, config) {
|
|
|
175
187
|
}
|
|
176
188
|
if (action === "poll") {
|
|
177
189
|
if (!args.login_data) throw new Error("action=poll 需要 login_data(先调用 action=start)");
|
|
178
|
-
const result = await
|
|
190
|
+
const result = await loginBridge.request("login_wait", { login_data: args.login_data }, loginTimeoutMs, exec?.signal);
|
|
179
191
|
return { status: result.logged_in ? "logged_in" : "pending", logged_in: Boolean(result.logged_in) };
|
|
180
192
|
}
|
|
181
193
|
throw new Error(`未知 action:${action}(可选 start / poll / status)`);
|
|
@@ -199,12 +211,13 @@ function apply(ctx, config) {
|
|
|
199
211
|
render: (_args, value) => [{ type: "text", text: formatDeviceList(value) }],
|
|
200
212
|
},
|
|
201
213
|
timeoutMs: requestTimeoutMs + 5_000,
|
|
202
|
-
async execute() {
|
|
214
|
+
async execute(args, exec) {
|
|
203
215
|
try {
|
|
204
|
-
const result = await bridge.request("devices", {}, requestTimeoutMs);
|
|
216
|
+
const result = await bridge.request("devices", {}, requestTimeoutMs, exec?.signal);
|
|
205
217
|
const devices = Array.isArray(result.devices) ? result.devices : [];
|
|
206
218
|
return { count: devices.length, devices };
|
|
207
219
|
} catch (error) {
|
|
220
|
+
if (error?.name === "AbortError") throw error;
|
|
208
221
|
throw new Error(describeError(error));
|
|
209
222
|
}
|
|
210
223
|
},
|
|
@@ -224,12 +237,13 @@ function apply(ctx, config) {
|
|
|
224
237
|
render: (_args, value) => [{ type: "text", text: formatDeviceDetail(value) }],
|
|
225
238
|
},
|
|
226
239
|
timeoutMs: requestTimeoutMs + 5_000,
|
|
227
|
-
async execute(args) {
|
|
240
|
+
async execute(args, exec) {
|
|
228
241
|
try {
|
|
229
|
-
const detail = await bridge.request("device_detail", { did: String(args.did) }, requestTimeoutMs);
|
|
242
|
+
const detail = await bridge.request("device_detail", { did: String(args.did) }, requestTimeoutMs, exec?.signal);
|
|
230
243
|
if (!detail || !detail.did) throw Object.assign(new Error("设备不存在或未返回规格"), { code: "not_found" });
|
|
231
244
|
return detail;
|
|
232
245
|
} catch (error) {
|
|
246
|
+
if (error?.name === "AbortError") throw error;
|
|
233
247
|
throw new Error(describeError(error));
|
|
234
248
|
}
|
|
235
249
|
},
|
|
@@ -259,38 +273,39 @@ function apply(ctx, config) {
|
|
|
259
273
|
render: (_args, value) => [{ type: "text", text: renderControl(value) }],
|
|
260
274
|
},
|
|
261
275
|
timeoutMs: requestTimeoutMs + 5_000,
|
|
262
|
-
async execute(args) {
|
|
276
|
+
async execute(args, exec) {
|
|
263
277
|
const did = String(args.did);
|
|
264
278
|
const mode = String(args.mode ?? "");
|
|
265
279
|
try {
|
|
266
280
|
if (mode === "power") {
|
|
267
281
|
const payload = { did };
|
|
268
282
|
if (typeof args.state === "boolean") payload.state = args.state;
|
|
269
|
-
const result = await bridge.request("power", payload, requestTimeoutMs);
|
|
283
|
+
const result = await bridge.request("power", payload, requestTimeoutMs, exec?.signal);
|
|
270
284
|
return { mode, did, on: Boolean(result.on) };
|
|
271
285
|
}
|
|
272
286
|
if (mode === "read") {
|
|
273
287
|
const names = Array.isArray(args.names) ? args.names.map(String) : [];
|
|
274
288
|
if (!names.length && args.name) names.push(String(args.name));
|
|
275
289
|
if (!names.length) throw new Error("mode=read 需要 name 或 names");
|
|
276
|
-
const result = await bridge.request("read_props", { did, names }, requestTimeoutMs);
|
|
290
|
+
const result = await bridge.request("read_props", { did, names }, requestTimeoutMs, exec?.signal);
|
|
277
291
|
return { mode, did, values: result.values ?? {} };
|
|
278
292
|
}
|
|
279
293
|
if (mode === "write") {
|
|
280
294
|
if (!args.name) throw new Error("mode=write 需要 name");
|
|
281
295
|
if (args.value === undefined) throw new Error("mode=write 需要 value");
|
|
282
|
-
await bridge.request("write_prop", { did, name: String(args.name), value: args.value }, requestTimeoutMs);
|
|
296
|
+
await bridge.request("write_prop", { did, name: String(args.name), value: args.value }, requestTimeoutMs, exec?.signal);
|
|
283
297
|
return { mode, did, name: String(args.name), value: args.value, ok: true };
|
|
284
298
|
}
|
|
285
299
|
if (mode === "action") {
|
|
286
300
|
if (!args.name) throw new Error("mode=action 需要 name");
|
|
287
301
|
const payload = { did, name: String(args.name) };
|
|
288
302
|
if (args.params && typeof args.params === "object") payload.params = args.params;
|
|
289
|
-
await bridge.request("action", payload, requestTimeoutMs);
|
|
303
|
+
await bridge.request("action", payload, requestTimeoutMs, exec?.signal);
|
|
290
304
|
return { mode, did, name: String(args.name), ok: true };
|
|
291
305
|
}
|
|
292
306
|
throw new Error(`未知 mode:${mode}(可选 read / write / action / power)`);
|
|
293
307
|
} catch (error) {
|
|
308
|
+
if (error?.name === "AbortError") throw error;
|
|
294
309
|
throw new Error(describeError(error));
|
|
295
310
|
}
|
|
296
311
|
},
|
|
@@ -302,6 +317,231 @@ function apply(ctx, config) {
|
|
|
302
317
|
}),
|
|
303
318
|
}));
|
|
304
319
|
|
|
320
|
+
// ---- 全屋智能(不限米家,覆盖局域网设备)----
|
|
321
|
+
|
|
322
|
+
register(defineTool({
|
|
323
|
+
name: "smart_home_scan",
|
|
324
|
+
description:
|
|
325
|
+
"通过 ARP/ping/端口、SSDP/mDNS、Tuya/miIO UDP 与 BLE 等线索扫描本网段。" +
|
|
326
|
+
"输出设备身份、类别和协议线索;只有适配器确认的能力才提供控制,未支持的设备仅识别或读取,不猜写入命令。" +
|
|
327
|
+
"需要全屋清单时先调用这个,再对单台用 smart_home_device。",
|
|
328
|
+
parameters: {
|
|
329
|
+
subnets: { type: "string", description: "要扫描的子网,可逗号分隔,如 192.168.0.0/24。缺省用本机所在网段" },
|
|
330
|
+
prefix: { type: "number", description: "自动选择本机子网时的 IPv4 前缀长度(0–32),默认 24" },
|
|
331
|
+
extra_subnets: { type: "string", description: "额外补充的子网,可逗号分隔" },
|
|
332
|
+
ports: { type: "string", description: "额外探测的端口,逗号分隔,如 80,443,55443" },
|
|
333
|
+
scan_ping: { type: "boolean", description: "是否用 ping 兜底扫描,默认 true" },
|
|
334
|
+
force: { type: "boolean", description: "是否忽略缓存强制重新扫描,默认 false" },
|
|
335
|
+
},
|
|
336
|
+
output: {
|
|
337
|
+
schema: { type: "json" },
|
|
338
|
+
render: (_args, value) => [{ type: "text", text: formatSmartHomeList(value) }],
|
|
339
|
+
},
|
|
340
|
+
timeoutMs: 200_000,
|
|
341
|
+
async execute(args, exec) {
|
|
342
|
+
try {
|
|
343
|
+
return await bridge.request(
|
|
344
|
+
"smart_home_scan",
|
|
345
|
+
{
|
|
346
|
+
subnets: args.subnets,
|
|
347
|
+
prefix: args.prefix,
|
|
348
|
+
extra_subnets: args.extra_subnets,
|
|
349
|
+
ports: args.ports,
|
|
350
|
+
scan_ping: args.scan_ping,
|
|
351
|
+
force: args.force,
|
|
352
|
+
},
|
|
353
|
+
Math.max(requestTimeoutMs, 195_000),
|
|
354
|
+
exec?.signal,
|
|
355
|
+
);
|
|
356
|
+
} catch (error) {
|
|
357
|
+
if (error?.name === "AbortError") throw error;
|
|
358
|
+
throw new Error(describeError(error));
|
|
359
|
+
}
|
|
360
|
+
},
|
|
361
|
+
presentCall: () => ({ card: "generic", title: "全屋扫描", kind: "execute", rawInput: "" }),
|
|
362
|
+
}));
|
|
363
|
+
|
|
364
|
+
register(defineTool({
|
|
365
|
+
name: "smart_home_devices",
|
|
366
|
+
description:
|
|
367
|
+
"列出全屋已发现/已连接的智能家居设备:局域网扫描结果 + 米家云端设备。" +
|
|
368
|
+
"返回的 id 是 smart_home_device / smart_home_control 的键。",
|
|
369
|
+
parameters: {
|
|
370
|
+
refresh: { type: "boolean", description: "是否重新扫描刷新,默认 false(用最近缓存)" },
|
|
371
|
+
},
|
|
372
|
+
output: {
|
|
373
|
+
schema: { type: "json" },
|
|
374
|
+
render: (_args, value) => [{ type: "text", text: formatSmartHomeList(value) }],
|
|
375
|
+
},
|
|
376
|
+
timeoutMs: 200_000,
|
|
377
|
+
async execute(args, exec) {
|
|
378
|
+
try {
|
|
379
|
+
return await bridge.request("smart_home_devices", { refresh: args.refresh }, Math.max(requestTimeoutMs, 195_000), exec?.signal);
|
|
380
|
+
} catch (error) {
|
|
381
|
+
if (error?.name === "AbortError") throw error;
|
|
382
|
+
throw new Error(describeError(error));
|
|
383
|
+
}
|
|
384
|
+
},
|
|
385
|
+
presentCall: () => ({ card: "generic", title: "全屋设备列表", kind: "fetch", rawInput: "" }),
|
|
386
|
+
}));
|
|
387
|
+
|
|
388
|
+
register(defineTool({
|
|
389
|
+
name: "smart_home_device",
|
|
390
|
+
description:
|
|
391
|
+
"查看某台全屋智能设备的详情:按统一目录翻译后的能力项(电源/亮度/色温…)与当前值。设备 id 来自 smart_home_devices。",
|
|
392
|
+
parameters: {
|
|
393
|
+
id: { type: "string", required: true, description: "设备 id,形如 mihome:123 或 shelly:192.168.0.5" },
|
|
394
|
+
refresh: { type: "boolean", description: "是否重新读取状态,默认 false" },
|
|
395
|
+
},
|
|
396
|
+
output: {
|
|
397
|
+
schema: { type: "json" },
|
|
398
|
+
render: (_args, value) => [{ type: "text", text: formatSmartHomeDetail(value) }],
|
|
399
|
+
},
|
|
400
|
+
timeoutMs: 200_000,
|
|
401
|
+
async execute(args, exec) {
|
|
402
|
+
try {
|
|
403
|
+
return await bridge.request("smart_home_device", { id: String(args.id), refresh: args.refresh }, Math.max(requestTimeoutMs, 195_000), exec?.signal);
|
|
404
|
+
} catch (error) {
|
|
405
|
+
if (error?.name === "AbortError") throw error;
|
|
406
|
+
throw new Error(describeError(error));
|
|
407
|
+
}
|
|
408
|
+
},
|
|
409
|
+
presentCall: (args) => ({ card: "generic", title: String(args.id), kind: "fetch", rawInput: String(args.id) }),
|
|
410
|
+
}));
|
|
411
|
+
|
|
412
|
+
register(defineTool({
|
|
413
|
+
name: "smart_home_control",
|
|
414
|
+
description:
|
|
415
|
+
"控制一台全屋智能设备。mode=power 开关(state 缺省取反);mode=write 写属性(name/value);" +
|
|
416
|
+
"mode=action 执行动作(name,可带 params);mode=scrape 扒设置页(路由器/光猫/NAS)。" +
|
|
417
|
+
"对真实设备写操作前先确认用户意图。",
|
|
418
|
+
parameters: {
|
|
419
|
+
device_id: { type: "string", required: true, description: "目标设备 id" },
|
|
420
|
+
mode: { type: "string", description: "控制方式,默认 power。power | write | action | scrape" },
|
|
421
|
+
state: { type: "boolean", description: "mode=power 的目标状态,缺省取反" },
|
|
422
|
+
name: { type: "string", description: "mode=write/action 的属性名或动作名" },
|
|
423
|
+
value: { type: "json", description: "mode=write 要写入的值" },
|
|
424
|
+
params: { type: "json", description: "mode=action 的参数;按设备详情的 params_schema/inputs 填写对象或数组,保持原始值类型" },
|
|
425
|
+
},
|
|
426
|
+
output: {
|
|
427
|
+
schema: { type: "json" },
|
|
428
|
+
render: (_args, value) => [{ type: "text", text: JSON.stringify(value ?? {}) }],
|
|
429
|
+
},
|
|
430
|
+
timeoutMs: 200_000,
|
|
431
|
+
async execute(args, exec) {
|
|
432
|
+
try {
|
|
433
|
+
return await bridge.request(
|
|
434
|
+
"smart_home_control",
|
|
435
|
+
{
|
|
436
|
+
device_id: String(args.device_id),
|
|
437
|
+
mode: args.mode ?? "power",
|
|
438
|
+
state: args.state,
|
|
439
|
+
name: args.name,
|
|
440
|
+
value: args.value,
|
|
441
|
+
params: args.params,
|
|
442
|
+
},
|
|
443
|
+
Math.max(requestTimeoutMs, 195_000),
|
|
444
|
+
exec?.signal,
|
|
445
|
+
);
|
|
446
|
+
} catch (error) {
|
|
447
|
+
if (error?.name === "AbortError") throw error;
|
|
448
|
+
throw new Error(describeError(error));
|
|
449
|
+
}
|
|
450
|
+
},
|
|
451
|
+
presentCall: (args) => ({
|
|
452
|
+
card: "generic",
|
|
453
|
+
title: `${String(args.device_id)} ${String(args.mode ?? "power")}`,
|
|
454
|
+
kind: "execute",
|
|
455
|
+
rawInput: JSON.stringify(args),
|
|
456
|
+
}),
|
|
457
|
+
}));
|
|
458
|
+
|
|
459
|
+
register(defineTool({
|
|
460
|
+
name: "smart_home_scrape",
|
|
461
|
+
description:
|
|
462
|
+
"读取设备(路由器 / 光猫 / NAS 等)公开管理页的标题、链接、表单字段和可见文本。" +
|
|
463
|
+
"不会通用登录、提权或写入设置;结果可能仅为登录页,不代表已获取设备配置。",
|
|
464
|
+
parameters: {
|
|
465
|
+
device_id: { type: "string", required: true, description: "目标设备 id" },
|
|
466
|
+
},
|
|
467
|
+
output: {
|
|
468
|
+
schema: { type: "json" },
|
|
469
|
+
render: (_args, value) => [{ type: "text", text: JSON.stringify(value ?? {}) }],
|
|
470
|
+
},
|
|
471
|
+
timeoutMs: 200_000,
|
|
472
|
+
async execute(args, exec) {
|
|
473
|
+
try {
|
|
474
|
+
return await bridge.request("smart_home_scrape", { device_id: String(args.device_id) }, Math.max(requestTimeoutMs, 195_000), exec?.signal);
|
|
475
|
+
} catch (error) {
|
|
476
|
+
if (error?.name === "AbortError") throw error;
|
|
477
|
+
throw new Error(describeError(error));
|
|
478
|
+
}
|
|
479
|
+
},
|
|
480
|
+
presentCall: (args) => ({ card: "generic", title: `扒设置页 ${String(args.device_id)}`, kind: "execute", rawInput: String(args.device_id) }),
|
|
481
|
+
}));
|
|
482
|
+
|
|
483
|
+
register(defineTool({
|
|
484
|
+
name: "smart_home_scenes",
|
|
485
|
+
description:
|
|
486
|
+
"管理自定义场景:mode=list 列出已保存场景(默认);mode=save 保存场景(scene.name + scene.actions," +
|
|
487
|
+
"如「回家时开灯、关窗帘」);mode=run 执行;mode=delete 删除。",
|
|
488
|
+
parameters: {
|
|
489
|
+
mode: { type: "string", description: "操作类型,默认 list。list | save | run | delete" },
|
|
490
|
+
scene: {
|
|
491
|
+
type: "object",
|
|
492
|
+
additionalProperties: true,
|
|
493
|
+
description: "mode=save 时的完整场景:{ name, actions: [{ device, mode, name, value }] }",
|
|
494
|
+
},
|
|
495
|
+
scene_id: { type: "string", description: "mode=run/delete 时的场景 id" },
|
|
496
|
+
},
|
|
497
|
+
output: {
|
|
498
|
+
schema: { type: "json" },
|
|
499
|
+
render: (_args, value) => [{ type: "text", text: formatSmartHomeScenes(value) }],
|
|
500
|
+
},
|
|
501
|
+
timeoutMs: 200_000,
|
|
502
|
+
async execute(args, exec) {
|
|
503
|
+
try {
|
|
504
|
+
return await bridge.request(
|
|
505
|
+
"smart_home_scenes",
|
|
506
|
+
{ mode: args.mode ?? "list", scene: args.scene, scene_id: args.scene_id },
|
|
507
|
+
Math.max(requestTimeoutMs, 195_000),
|
|
508
|
+
exec?.signal,
|
|
509
|
+
);
|
|
510
|
+
} catch (error) {
|
|
511
|
+
if (error?.name === "AbortError") throw error;
|
|
512
|
+
throw new Error(describeError(error));
|
|
513
|
+
}
|
|
514
|
+
},
|
|
515
|
+
presentCall: (args) => ({
|
|
516
|
+
card: "generic",
|
|
517
|
+
title: `全屋场景 ${String(args.mode ?? "list")}`,
|
|
518
|
+
kind: "execute",
|
|
519
|
+
rawInput: JSON.stringify(args),
|
|
520
|
+
}),
|
|
521
|
+
}));
|
|
522
|
+
|
|
523
|
+
register(defineTool({
|
|
524
|
+
name: "smart_home_catalog",
|
|
525
|
+
description:
|
|
526
|
+
"返回能力项目录:设备类别(灯具/开关/插座/扫地机/冰箱/电视/洗衣机/烘干机…)" +
|
|
527
|
+
"与统一控制项(电源/亮度/色温/模式/风速/目标温度…)的中英对照。实际控制 key 以 smart_home_device 返回为准。",
|
|
528
|
+
parameters: {},
|
|
529
|
+
output: {
|
|
530
|
+
schema: { type: "json" },
|
|
531
|
+
render: (_args, value) => [{ type: "text", text: JSON.stringify(value ?? {}) }],
|
|
532
|
+
},
|
|
533
|
+
timeoutMs: 15_000,
|
|
534
|
+
async execute(args, exec) {
|
|
535
|
+
try {
|
|
536
|
+
return await bridge.request("smart_home_catalog", {}, 15_000, exec?.signal);
|
|
537
|
+
} catch (error) {
|
|
538
|
+
if (error?.name === "AbortError") throw error;
|
|
539
|
+
throw new Error(describeError(error));
|
|
540
|
+
}
|
|
541
|
+
},
|
|
542
|
+
presentCall: () => ({ card: "generic", title: "能力项目录", kind: "fetch", rawInput: "" }),
|
|
543
|
+
}));
|
|
544
|
+
|
|
305
545
|
register(defineTool({
|
|
306
546
|
name: "mihome_scenes",
|
|
307
547
|
description: "列出米家场景(如「回家模式」「睡眠模式」),或用 run 模式执行指定场景。执行场景会改动真实设备状态。",
|
|
@@ -317,13 +557,13 @@ function apply(ctx, config) {
|
|
|
317
557
|
render: (_args, value) => [{ type: "text", text: value.mode === "run" ? renderSceneRun(value) : formatScenes(value) }],
|
|
318
558
|
},
|
|
319
559
|
timeoutMs: requestTimeoutMs + 5_000,
|
|
320
|
-
async execute(args) {
|
|
560
|
+
async execute(args, exec) {
|
|
321
561
|
const mode = String(args.mode ?? "");
|
|
322
562
|
try {
|
|
323
563
|
if (mode === "list") {
|
|
324
564
|
const payload = {};
|
|
325
565
|
if (args.home_name) payload.home_name = String(args.home_name);
|
|
326
|
-
const result = await bridge.request("scenes", payload, requestTimeoutMs);
|
|
566
|
+
const result = await bridge.request("scenes", payload, requestTimeoutMs, exec?.signal);
|
|
327
567
|
const scenes = Array.isArray(result.scenes) ? result.scenes : [];
|
|
328
568
|
return { mode, count: scenes.length, scenes };
|
|
329
569
|
}
|
|
@@ -332,11 +572,12 @@ function apply(ctx, config) {
|
|
|
332
572
|
if (!args.home_id) throw new Error("mode=run 需要 home_id");
|
|
333
573
|
const payload = { scene_id: String(args.scene_id), home_id: String(args.home_id) };
|
|
334
574
|
if (args.name) payload.name = String(args.name);
|
|
335
|
-
await bridge.request("run_scene", payload, requestTimeoutMs);
|
|
575
|
+
await bridge.request("run_scene", payload, requestTimeoutMs, exec?.signal);
|
|
336
576
|
return { mode, ok: true, scene_id: payload.scene_id, name: payload.name ?? "" };
|
|
337
577
|
}
|
|
338
578
|
throw new Error(`未知 mode:${mode}(可选 list / run)`);
|
|
339
579
|
} catch (error) {
|
|
580
|
+
if (error?.name === "AbortError") throw error;
|
|
340
581
|
throw new Error(describeError(error));
|
|
341
582
|
}
|
|
342
583
|
},
|
|
@@ -362,17 +603,17 @@ function apply(ctx, config) {
|
|
|
362
603
|
render: (_args, value) => [{ type: "text", text: renderSensors(value) }],
|
|
363
604
|
},
|
|
364
605
|
timeoutMs: requestTimeoutMs + 5_000,
|
|
365
|
-
async execute(args) {
|
|
606
|
+
async execute(args, exec) {
|
|
366
607
|
const mode = String(args.mode ?? "");
|
|
367
608
|
try {
|
|
368
609
|
if (mode === "metrics") {
|
|
369
610
|
const dids = Array.isArray(args.dids) ? args.dids.map(String) : [];
|
|
370
611
|
if (!dids.length) throw new Error("mode=metrics 需要 dids");
|
|
371
|
-
const result = await bridge.request("metrics", { dids }, requestTimeoutMs);
|
|
612
|
+
const result = await bridge.request("metrics", { dids }, requestTimeoutMs, exec?.signal);
|
|
372
613
|
return { mode, dids, values: result.values ?? {}, text: result.text ?? "" };
|
|
373
614
|
}
|
|
374
615
|
if (mode === "consumables") {
|
|
375
|
-
const result = await bridge.request("consumables", {}, requestTimeoutMs);
|
|
616
|
+
const result = await bridge.request("consumables", {}, requestTimeoutMs, exec?.signal);
|
|
376
617
|
return {
|
|
377
618
|
mode,
|
|
378
619
|
items: Array.isArray(result.items) ? result.items : [],
|
|
@@ -381,6 +622,7 @@ function apply(ctx, config) {
|
|
|
381
622
|
}
|
|
382
623
|
throw new Error(`未知 mode:${mode}(可选 metrics / consumables)`);
|
|
383
624
|
} catch (error) {
|
|
625
|
+
if (error?.name === "AbortError") throw error;
|
|
384
626
|
throw new Error(describeError(error));
|
|
385
627
|
}
|
|
386
628
|
},
|
|
@@ -401,6 +643,7 @@ function apply(ctx, config) {
|
|
|
401
643
|
mountPanelRoutes(webCtx, {
|
|
402
644
|
bridge,
|
|
403
645
|
loginBridge,
|
|
646
|
+
connections,
|
|
404
647
|
qrDir: join(dataDir, "qr"),
|
|
405
648
|
dataDir,
|
|
406
649
|
requestTimeoutMs,
|
package/lib/panel.js
CHANGED
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
|
|
20
20
|
import { existsSync, readFileSync } from "node:fs";
|
|
21
21
|
import { basename, join } from "node:path";
|
|
22
|
+
import { createConnections } from "./connections.js";
|
|
22
23
|
|
|
23
24
|
/** 前端取数的固定前缀。 */
|
|
24
25
|
const API = "/dsh-mihome/api";
|
|
@@ -61,10 +62,14 @@ function hostMatchesListener(host, localAddress) {
|
|
|
61
62
|
return bare(String(hostname).toLowerCase()) === bare(String(localAddress).toLowerCase());
|
|
62
63
|
}
|
|
63
64
|
|
|
64
|
-
function originTrusted(
|
|
65
|
-
|
|
65
|
+
function originTrusted(req) {
|
|
66
|
+
const origin = req.headers.origin;
|
|
67
|
+
if (!origin || !String(origin).trim()) return req.method !== "POST";
|
|
66
68
|
try {
|
|
67
|
-
|
|
69
|
+
const parsed = new URL(origin);
|
|
70
|
+
const protocol = req.socket?.encrypted ? "https:" : "http:";
|
|
71
|
+
const expected = new URL(`${protocol}//${req.headers.host}`);
|
|
72
|
+
return parsed.origin === expected.origin;
|
|
68
73
|
} catch {
|
|
69
74
|
return false;
|
|
70
75
|
}
|
|
@@ -75,7 +80,7 @@ function trusted(req, res) {
|
|
|
75
80
|
json(res, 403, { error: "host-not-trusted" });
|
|
76
81
|
return false;
|
|
77
82
|
}
|
|
78
|
-
if (!originTrusted(req
|
|
83
|
+
if (!originTrusted(req)) {
|
|
79
84
|
json(res, 403, { error: "origin-not-trusted" });
|
|
80
85
|
return false;
|
|
81
86
|
}
|
|
@@ -84,6 +89,10 @@ function trusted(req, res) {
|
|
|
84
89
|
|
|
85
90
|
function readJsonBody(req, maxBytes = BODY_LIMIT) {
|
|
86
91
|
return new Promise((resolve, reject) => {
|
|
92
|
+
if (!/^application\/json(?:\s*;|$)/i.test(String(req.headers["content-type"] ?? ""))) {
|
|
93
|
+
reject(Object.assign(new Error("Content-Type must be application/json"), { status: 415 }));
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
87
96
|
let size = 0;
|
|
88
97
|
const chunks = [];
|
|
89
98
|
let done = false;
|
|
@@ -160,6 +169,7 @@ function describe(error) {
|
|
|
160
169
|
*/
|
|
161
170
|
export function mountPanelRoutes(ctx, opts) {
|
|
162
171
|
const { bridge, loginBridge, qrDir, dataDir, requestTimeoutMs, loginTimeoutMs } = opts;
|
|
172
|
+
const connections = opts.connections ?? createConnections(ctx.credentials, "mihome/devices");
|
|
163
173
|
// 产品图标落在 <data>/icons(与 Python 侧 icon_store 同一个目录)
|
|
164
174
|
const iconDir = join(dataDir ?? join(qrDir, ".."), "icons");
|
|
165
175
|
|
|
@@ -173,8 +183,9 @@ export function mountPanelRoutes(ctx, opts) {
|
|
|
173
183
|
kind: "exact",
|
|
174
184
|
path: spec.path,
|
|
175
185
|
handler: async (req, res) => {
|
|
176
|
-
|
|
177
|
-
|
|
186
|
+
const methods = Array.isArray(spec.method) ? spec.method : [spec.method];
|
|
187
|
+
if (!methods.includes(req.method)) {
|
|
188
|
+
res.writeHead(405, { allow: methods.join(", ") });
|
|
178
189
|
res.end();
|
|
179
190
|
return;
|
|
180
191
|
}
|
|
@@ -182,7 +193,7 @@ export function mountPanelRoutes(ctx, opts) {
|
|
|
182
193
|
try {
|
|
183
194
|
await run(req, res);
|
|
184
195
|
} catch (error) {
|
|
185
|
-
json(res, 500, describe(error));
|
|
196
|
+
json(res, Number(error?.status) || 500, describe(error));
|
|
186
197
|
}
|
|
187
198
|
},
|
|
188
199
|
});
|
|
@@ -275,6 +286,122 @@ export function mountPanelRoutes(ctx, opts) {
|
|
|
275
286
|
});
|
|
276
287
|
});
|
|
277
288
|
|
|
289
|
+
wrap({ path: `${API}/smart-home/credentials`, method: ["GET", "POST"] }, async (req, res) => {
|
|
290
|
+
if (!ctx.credentials) {
|
|
291
|
+
json(res, 503, { ok: false, error: "DSH 凭据服务不可用" });
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
if (req.method === "GET") {
|
|
295
|
+
json(res, 200, { ok: true, devices: await connections.list() });
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
const body = await readJsonBody(req);
|
|
299
|
+
const deviceId = String(body.device_id ?? "");
|
|
300
|
+
try {
|
|
301
|
+
if (body.mode === "save") {
|
|
302
|
+
await connections.save(deviceId, body.payload);
|
|
303
|
+
json(res, 200, { ok: true, configured: true });
|
|
304
|
+
} else if (body.mode === "delete") {
|
|
305
|
+
await connections.delete(deviceId);
|
|
306
|
+
json(res, 200, { ok: true, configured: false });
|
|
307
|
+
} else {
|
|
308
|
+
json(res, 400, { ok: false, error: "mode 必须是 save 或 delete" });
|
|
309
|
+
}
|
|
310
|
+
} catch (error) {
|
|
311
|
+
json(res, 400, { ok: false, error: error.message });
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
// ---- 全屋智能(局域网扫描 + 统一控制 + 自定义场景)-----------------
|
|
315
|
+
// 与上面米家云端接口互不依赖:局域网设备不需要登录,云端设备走米家。
|
|
316
|
+
// 扫描可能遍历整个子网,超时给足(Python 侧内部也是 180s 预算)。
|
|
317
|
+
const scanTimeout = Math.max(requestTimeoutMs, 195_000);
|
|
318
|
+
|
|
319
|
+
wrap({ path: `${API}/smart-home/scan`, method: "GET" }, async (req, res) => {
|
|
320
|
+
const q = queryOf(req);
|
|
321
|
+
const scanPingRaw = q.get("scan_ping");
|
|
322
|
+
const result = await bridge.request(
|
|
323
|
+
"smart_home_scan",
|
|
324
|
+
{
|
|
325
|
+
subnets: q.get("subnets") || undefined,
|
|
326
|
+
prefix: q.get("prefix") || undefined,
|
|
327
|
+
extra_subnets: q.get("extra_subnets") || undefined,
|
|
328
|
+
ports: q.get("ports") || undefined,
|
|
329
|
+
scan_ping: scanPingRaw === null || scanPingRaw === "" ? undefined : scanPingRaw === "1" || scanPingRaw === "true",
|
|
330
|
+
force: q.get("force") === "1" || q.get("force") === "true",
|
|
331
|
+
},
|
|
332
|
+
scanTimeout,
|
|
333
|
+
);
|
|
334
|
+
json(res, 200, { ok: true, ...(result ?? {}) });
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
wrap({ path: `${API}/smart-home/devices`, method: "GET" }, async (req, res) => {
|
|
338
|
+
const q = queryOf(req);
|
|
339
|
+
const refresh = q.get("refresh") === "1" || q.get("refresh") === "true";
|
|
340
|
+
const result = await bridge.request("smart_home_devices", { refresh }, scanTimeout);
|
|
341
|
+
json(res, 200, { ok: true, devices: Array.isArray(result?.devices) ? result.devices : [] });
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
wrap({ path: `${API}/smart-home/device`, method: "GET" }, async (req, res) => {
|
|
345
|
+
const id = queryOf(req).get("id") ?? "";
|
|
346
|
+
if (!id) {
|
|
347
|
+
json(res, 400, { error: "需要 id" });
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
const refresh = queryOf(req).get("refresh") === "1" || queryOf(req).get("refresh") === "true";
|
|
351
|
+
const detail = await bridge.request("smart_home_device", { id, refresh }, scanTimeout);
|
|
352
|
+
json(res, 200, { ok: true, detail: detail ?? {} });
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
wrap({ path: `${API}/smart-home/catalog`, method: "GET" }, async (_req, res) => {
|
|
356
|
+
const result = await bridge.request("smart_home_catalog", {}, 15_000);
|
|
357
|
+
json(res, 200, { ok: true, catalog: result ?? {} });
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
wrap({ path: `${API}/smart-home/scenes`, method: "GET" }, async (_req, res) => {
|
|
361
|
+
const result = await bridge.request("smart_home_scenes", { mode: "list" }, scanTimeout);
|
|
362
|
+
json(res, 200, { ok: true, scenes: Array.isArray(result?.scenes) ? result.scenes : [] });
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
// 全屋控制:统一走 smart_home_control,写操作真实改动设备状态
|
|
366
|
+
wrap({ path: `${API}/smart-home/control`, method: "POST" }, async (req, res) => {
|
|
367
|
+
const body = await readJsonBody(req);
|
|
368
|
+
const deviceId = String(body.device_id ?? "");
|
|
369
|
+
if (!deviceId) {
|
|
370
|
+
json(res, 400, { error: "需要 device_id" });
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
if ((body.mode ?? "power") === "power" && Object.hasOwn(body, "state") && typeof body.state !== "boolean") {
|
|
374
|
+
json(res, 400, { error: "state 必须为布尔值" });
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const result = await bridge.request(
|
|
378
|
+
"smart_home_control",
|
|
379
|
+
{
|
|
380
|
+
device_id: deviceId,
|
|
381
|
+
mode: body.mode ?? "power",
|
|
382
|
+
state: body.state,
|
|
383
|
+
name: body.name,
|
|
384
|
+
value: body.value,
|
|
385
|
+
params: body.params,
|
|
386
|
+
},
|
|
387
|
+
scanTimeout,
|
|
388
|
+
);
|
|
389
|
+
const payload = result ?? {};
|
|
390
|
+
json(res, payload.ok === false ? 502 : 200, { ...payload, ok: payload.ok === true });
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
// 自定义场景:保存 / 执行 / 删除
|
|
394
|
+
wrap({ path: `${API}/smart-home/scene`, method: "POST" }, async (req, res) => {
|
|
395
|
+
const body = await readJsonBody(req);
|
|
396
|
+
const result = await bridge.request(
|
|
397
|
+
"smart_home_scenes",
|
|
398
|
+
{ mode: body.mode ?? "list", scene: body.scene, scene_id: body.scene_id },
|
|
399
|
+
scanTimeout,
|
|
400
|
+
);
|
|
401
|
+
const payload = result ?? {};
|
|
402
|
+
json(res, payload.ok === false ? 502 : 200, { ...payload, ok: payload.ok === true });
|
|
403
|
+
});
|
|
404
|
+
|
|
278
405
|
// ---- 写操作(会真的改设备状态)-------------------------------------
|
|
279
406
|
wrap({ path: `${API}/control`, method: "POST" }, async (req, res) => {
|
|
280
407
|
const body = await readJsonBody(req);
|
|
@@ -285,6 +412,10 @@ export function mountPanelRoutes(ctx, opts) {
|
|
|
285
412
|
return;
|
|
286
413
|
}
|
|
287
414
|
if (mode === "power") {
|
|
415
|
+
if (Object.hasOwn(body, "state") && typeof body.state !== "boolean") {
|
|
416
|
+
json(res, 400, { error: "state 必须为布尔值" });
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
288
419
|
const payload = { did };
|
|
289
420
|
if (typeof body.state === "boolean") payload.state = body.state;
|
|
290
421
|
const result = await bridge.request("power", payload, requestTimeoutMs);
|