236 lines
8.0 KiB
JavaScript
236 lines
8.0 KiB
JavaScript
// Device and test actions split from runtime_actions.js.
|
|
|
|
function selectedDeviceId() {
|
|
const el = $("device");
|
|
return el ? (el.value || "") : "";
|
|
}
|
|
|
|
function renderWifiScanResults(resultsEl, devices) {
|
|
if (!resultsEl) return;
|
|
resultsEl.innerHTML = "";
|
|
const title = document.createElement("div");
|
|
title.style.marginBottom = "4px";
|
|
title.style.color = "#555";
|
|
title.textContent = "点击下方设备使用:";
|
|
resultsEl.appendChild(title);
|
|
for (const d of devices) {
|
|
const row = document.createElement("div");
|
|
row.style.margin = "4px 0";
|
|
row.style.cursor = "pointer";
|
|
row.style.padding = "4px 6px";
|
|
row.style.border = "1px solid #ddd";
|
|
row.style.borderRadius = "4px";
|
|
row.dataset.target = String(d.target || "");
|
|
row.dataset.state = String(d.state || "");
|
|
const target = document.createElement("b");
|
|
target.textContent = String(d.target || "");
|
|
const state = document.createElement("span");
|
|
const st = String(d.state || "");
|
|
state.style.color = st === "connected" ? "#0a7d2c" : (st === "unauthorized" ? "#b35900" : "#888");
|
|
state.textContent = ` [${st}]`;
|
|
row.appendChild(target);
|
|
row.appendChild(state);
|
|
if (d.model) {
|
|
const model = document.createElement("span");
|
|
model.style.color = "#555";
|
|
model.textContent = ` ${String(d.model)}`;
|
|
row.appendChild(model);
|
|
}
|
|
resultsEl.appendChild(row);
|
|
}
|
|
}
|
|
|
|
async function postDeviceAction(path, body, successPrefix) {
|
|
try {
|
|
const result = await apiPost(path, body || {});
|
|
const msg = result.message || result.output || result.error || "完成";
|
|
appendLog([`${successPrefix}: ${msg}`]);
|
|
if (!result.error) toast(`${successPrefix}: ${msg}`, "success");
|
|
else toast(`${successPrefix}: ${msg}`, "warn");
|
|
if (result.devices) await refreshDevices();
|
|
return result;
|
|
} catch (e) {
|
|
appendLog([`${successPrefix}失败: ${e?.message || e}`]);
|
|
toast(`${successPrefix}失败:${e?.message || e}`, "error", { duration: 5000 });
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
async function forceStopRun() {
|
|
await postDeviceAction("/api/force-stop", {}, "[WEB] 强制停止");
|
|
}
|
|
|
|
async function sendBackKey() {
|
|
const deviceId = selectedDeviceId();
|
|
try {
|
|
await postDeviceAction("/api/back-key", { device_id: deviceId }, "[WEB] 返回键");
|
|
} catch (_) {
|
|
// 错误已在 postDeviceAction 内展示,避免未捕获 Promise 冒泡到全局。
|
|
}
|
|
}
|
|
|
|
async function wifiEnable() {
|
|
const deviceId = selectedDeviceId();
|
|
if (!deviceId) {
|
|
appendLog(["[WiFi ADB] 请先选择 USB 设备"]);
|
|
return;
|
|
}
|
|
const result = await postDeviceAction("/api/wifi/enable", { device_id: deviceId }, "[WiFi ADB] 启用");
|
|
setTextIfExists("wifiStatus", result.message || "已发送启用命令");
|
|
}
|
|
|
|
async function wifiConnect() {
|
|
const ip = ($("wifi_ip")?.value || "").trim();
|
|
const port = ($("wifi_port")?.value || "5555").trim() || "5555";
|
|
if (!ip) {
|
|
appendLog(["[WiFi ADB] 请填写设备 IP"]);
|
|
return;
|
|
}
|
|
const result = await postDeviceAction("/api/wifi/connect", { ip, port }, "[WiFi ADB] 连接");
|
|
setTextIfExists("wifiStatus", result.message || "连接命令已执行");
|
|
}
|
|
|
|
async function wifiDisconnect() {
|
|
const ip = ($("wifi_ip")?.value || selectedDeviceId() || "").trim();
|
|
const port = ($("wifi_port")?.value || "5555").trim() || "5555";
|
|
if (!ip) {
|
|
appendLog(["[WiFi ADB] 请填写 IP 或选择 WiFi 设备"]);
|
|
return;
|
|
}
|
|
const result = await postDeviceAction("/api/wifi/disconnect", { ip, port }, "[WiFi ADB] 断开");
|
|
setTextIfExists("wifiStatus", result.message || "断开命令已执行");
|
|
}
|
|
|
|
async function tryAutoReconnectLastWifi() {
|
|
try {
|
|
const resp = await apiPost("/api/wifi/auto-reconnect", {});
|
|
if (!resp || resp.ok === false) return;
|
|
const action = resp.action || "noop";
|
|
if (action === "noop") return;
|
|
|
|
if (action === "already_online" || action === "reconnected" || action === "matched") {
|
|
try {
|
|
await refreshDevices();
|
|
} catch (_) {}
|
|
const sel = $("device");
|
|
if (sel && resp.target) {
|
|
for (const opt of sel.options) {
|
|
if (opt.value === resp.target) {
|
|
sel.value = resp.target;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
const status = $("wifiStatus");
|
|
if (status && resp.message) status.textContent = resp.message;
|
|
appendLog([`[启动] ${resp.message || action}`]);
|
|
return;
|
|
}
|
|
|
|
if (action === "failed") {
|
|
appendLog([`[启动] ${resp.message || "WiFi 自动重连失败"}`]);
|
|
const status = $("wifiStatus");
|
|
if (status && resp.message) status.textContent = resp.message;
|
|
return;
|
|
}
|
|
} catch (e) {
|
|
console.warn("auto-reconnect failed", e);
|
|
}
|
|
}
|
|
|
|
async function wifiScan() {
|
|
const btn = $("btnWifiScan");
|
|
const status = $("wifiStatus");
|
|
const results = $("wifiScanResults");
|
|
if (btn) {
|
|
btn.disabled = true;
|
|
btn.textContent = "扫描中...";
|
|
}
|
|
if (status) status.textContent = "正在扫描局域网...";
|
|
if (results) {
|
|
results.style.display = "none";
|
|
results.innerHTML = "";
|
|
}
|
|
try {
|
|
const resp = await apiPost("/api/wifi/scan", {});
|
|
if (!resp || resp.ok === false) {
|
|
const err = (resp && resp.error) || "扫描失败";
|
|
appendLog([`[WiFi ADB] 扫描失败: ${err}`]);
|
|
if (status) status.textContent = `扫描失败: ${err}`;
|
|
return;
|
|
}
|
|
const devs = Array.isArray(resp.devices) ? resp.devices : [];
|
|
const subnet = resp.subnet || "";
|
|
if (!devs.length) {
|
|
appendLog([`[WiFi ADB] 扫描完成 (${subnet}),未发现开放 5555 端口的设备`]);
|
|
if (status) status.textContent = `扫描完成 (${subnet}),未发现设备`;
|
|
return;
|
|
}
|
|
appendLog([
|
|
`[WiFi ADB] 扫描完成 (${subnet}),发现 ${devs.length} 台候选: ` +
|
|
devs.map((d) => `${d.target}(${d.state})`).join(", "),
|
|
]);
|
|
|
|
const connected = devs.filter((d) => d.state === "connected");
|
|
if (connected.length === 1 && devs.length === 1) {
|
|
const d = connected[0];
|
|
const ipInput = $("wifi_ip");
|
|
if (ipInput) ipInput.value = d.target;
|
|
if (status)
|
|
status.textContent =
|
|
`已发现并连接 ${d.target}` + (d.model ? ` (${d.model})` : "") + ",已记住";
|
|
try {
|
|
await refreshDevices();
|
|
} catch (_) {}
|
|
return;
|
|
}
|
|
|
|
if (results) {
|
|
results.style.display = "block";
|
|
renderWifiScanResults(results, devs);
|
|
results.querySelectorAll("[data-target]").forEach((el) => {
|
|
el.addEventListener("click", () => {
|
|
const target = el.getAttribute("data-target");
|
|
const state = el.getAttribute("data-state");
|
|
if (state !== "connected") {
|
|
appendLog([
|
|
`[WiFi ADB] ${target} 状态=${state},请先在手机上确认 ADB 调试授权`,
|
|
]);
|
|
return;
|
|
}
|
|
const ipInput = $("wifi_ip");
|
|
if (ipInput) ipInput.value = target;
|
|
if (status) status.textContent = `已选 ${target}`;
|
|
appendLog([`[WiFi ADB] 已选 ${target}`]);
|
|
try {
|
|
refreshDevices();
|
|
} catch (_) {}
|
|
});
|
|
});
|
|
}
|
|
if (status) status.textContent = `扫描完成,发现 ${devs.length} 台设备`;
|
|
} catch (e) {
|
|
appendLog([`[WiFi ADB] 扫描异常: ${e}`]);
|
|
if (status) status.textContent = `扫描异常: ${e}`;
|
|
} finally {
|
|
if (btn) {
|
|
btn.disabled = false;
|
|
btn.textContent = "扫描 WiFi";
|
|
}
|
|
}
|
|
}
|
|
|
|
async function testYanghao() {
|
|
const deviceId = $("device").value || "";
|
|
const seconds = parseInt($("yanghao_recommend_duration_seconds").value || "60", 10);
|
|
await apiPost("/api/test-yanghao", { device_id: deviceId, seconds });
|
|
appendLog([`[WEB] 已启动测试养号:device=${deviceId || UI_COPY.device.autoLabel} seconds=${seconds}`]);
|
|
}
|
|
|
|
async function testComment() {
|
|
const deviceId = $("device").value || "";
|
|
const text = "";
|
|
await apiPost("/api/test-comment", { device_id: deviceId, text });
|
|
appendLog([`[WEB] 已启动测试评论:device=${deviceId || UI_COPY.device.autoLabel}${text ? ' text=自定义' : ''}`]);
|
|
}
|