Skip to content

Commit 2f4d992

Browse files
committed
Node will upload ui dump every 5 minutes
1 parent fa58cda commit 2f4d992

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

server/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import threading
23
from typing import Any
34
from fastapi import APIRouter, FastAPI
45
from fastapi.middleware.cors import CORSMiddleware
@@ -7,7 +8,7 @@
78
from server.connect import router as connect_router
89
from server.evaluator import router as evaluator_router
910
from server.node_operator import router as operator_router
10-
from server.mobile import router as mobile_router
11+
from server.mobile import router as mobile_router, upload_android_ui_dump
1112
from server.mac import router as mac_router
1213

1314
class EndpointFilter(logging.Filter):
@@ -26,6 +27,8 @@ def filter(self, record: logging.LogRecord) -> bool:
2627
def main() -> FastAPI:
2728
# Filter out /endpoint
2829
logging.getLogger("uvicorn.access").addFilter(EndpointFilter(path="/status"))
30+
thread = threading.Thread(target=upload_android_ui_dump, daemon=True)
31+
thread.start()
2932

3033
v1router = APIRouter(
3134
prefix="/api/v1",

server/mobile.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
import os
22
import subprocess
33
import base64
4+
import time
45
from typing import Literal
6+
7+
import requests
58
from fastapi import APIRouter
69
from pydantic import BaseModel
710

11+
from Framework.Utilities import ConfigModule, CommonUtil
12+
813
ADB_PATH = "adb" # Ensure ADB is in PATH
914
UI_XML_PATH = "ui.xml"
1015
SCREENSHOT_PATH = "screen.png"
1116

1217
router = APIRouter(prefix="/mobile", tags=["mobile"])
1318

19+
1420
class InspectorResponse(BaseModel):
1521
"""Response model for the /inspector endpoint."""
1622

@@ -19,6 +25,7 @@ class InspectorResponse(BaseModel):
1925
screenshot: str | None = None # Base64 encoded image
2026
error: str | None = None
2127

28+
2229
class DeviceInfo(BaseModel):
2330
"""Model for device information."""
2431
serial: str
@@ -128,3 +135,32 @@ def capture_screenshot():
128135
out = run_adb_command(f"{ADB_PATH} pull /sdcard/screen.png {SCREENSHOT_PATH}")
129136
if out.startswith("Error:"):
130137
return
138+
139+
140+
def upload_android_ui_dump():
141+
while True:
142+
try:
143+
capture_ui_dump()
144+
try:
145+
with open(UI_XML_PATH, 'r') as xml_file:
146+
xml_content = xml_file.read()
147+
except FileNotFoundError:
148+
CommonUtil.ExecLog("", "UI XML file not found, skipping upload.", iLogLevel=2)
149+
time.sleep(5)
150+
continue
151+
url = ConfigModule.get_config_value("Authentication", "server_address").strip() + "/node_ai_contents/"
152+
apiKey = ConfigModule.get_config_value("Authentication", "api-key").strip()
153+
response = requests.post(
154+
url,
155+
headers={"X-Api-Key": apiKey},
156+
json={
157+
"dom_mob": {"dom": xml_content},
158+
"node_id": CommonUtil.MachineInfo().getLocalUser().lower()
159+
})
160+
if response.status_code == 200:
161+
CommonUtil.ExecLog("", f"UI dump uploaded successfully: {response.json()}", iLogLevel=1)
162+
else:
163+
CommonUtil.ExecLog("", f"Failed to upload UI dump: {response.status_code} - {response.text}", iLogLevel=3)
164+
except Exception as e:
165+
CommonUtil.ExecLog("", f"Error uploading UI dump: {str(e)}", iLogLevel=3)
166+
time.sleep(5)

0 commit comments

Comments
 (0)