This commit is contained in:
eric
2026-03-29 02:33:37 -05:00
parent 16dc9abbbb
commit ef87c43b33
17 changed files with 1197 additions and 407 deletions

57
web.py
View File

@@ -16,8 +16,12 @@ from fastapi.staticfiles import StaticFiles
from src.android_control import (
adb_available,
adb_connect_address,
adb_disconnect_address,
adb_pair_code,
android_action,
android_device_overview,
android_display_metrics,
android_logcat_recent,
android_packages,
android_screenshot,
@@ -305,6 +309,20 @@ class ConfigRestoreBody(BaseModel):
backup_id: str = Field(..., min_length=1, max_length=320)
class AndroidConnectBody(BaseModel):
address: str = Field(..., min_length=1, max_length=200)
class AndroidDisconnectBody(BaseModel):
address: str = Field(default="", max_length=200)
class AndroidPairBody(BaseModel):
host: str = Field(..., min_length=1, max_length=160)
port: int = Field(default=37_777, ge=1, le=65535)
code: str = Field(..., min_length=4, max_length=16)
async def read_log_path(path: Optional[str], lines: int) -> str:
if not path:
return "无日志路径\n"
@@ -371,6 +389,30 @@ async def get_android_devices():
}
@app.post("/android/connect")
async def post_android_connect(body: AndroidConnectBody):
try:
return await adb_connect_address(body.address.strip())
except (RuntimeError, ValueError) as exc:
return JSONResponse({"error": str(exc)}, status_code=400)
@app.post("/android/disconnect")
async def post_android_disconnect(body: AndroidDisconnectBody):
try:
return await adb_disconnect_address((body.address or "").strip())
except (RuntimeError, ValueError) as exc:
return JSONResponse({"error": str(exc)}, status_code=400)
@app.post("/android/pair")
async def post_android_pair(body: AndroidPairBody):
try:
return await adb_pair_code(body.host.strip(), body.port, body.code.strip())
except (RuntimeError, ValueError) as exc:
return JSONResponse({"error": str(exc)}, status_code=400)
@app.get("/android/packages")
async def get_android_packages(
serial: str = Query(..., description="Android device serial"),
@@ -415,9 +457,12 @@ async def get_android_logcat(
@app.get("/android/screenshot")
async def get_android_screenshot(serial: str = Query(..., description="Android device serial")):
async def get_android_screenshot(
serial: str = Query(..., description="Android device serial"),
fast: bool = Query(False, description="更短超时,适合 Live 轮询(无线/Redroid 仍可能回落慢路径)"),
):
try:
content = await android_screenshot(serial)
content = await android_screenshot(serial, exec_timeout=8.5 if fast else 14.0)
except (RuntimeError, ValueError) as exc:
return JSONResponse({"error": str(exc)}, status_code=400)
return Response(
@@ -427,6 +472,14 @@ async def get_android_screenshot(serial: str = Query(..., description="Android d
)
@app.get("/android/display_metrics")
async def get_android_display_metrics(serial: str = Query(..., description="Android device serial")):
try:
return await android_display_metrics(serial)
except (RuntimeError, ValueError) as exc:
return JSONResponse({"error": str(exc)}, status_code=400)
@app.post("/android/action")
async def post_android_action(request: Request):
try: