Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion server/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import threading
from typing import Any
from fastapi import APIRouter, FastAPI
from fastapi.middleware.cors import CORSMiddleware
Expand All @@ -7,7 +8,7 @@
from server.connect import router as connect_router
from server.evaluator import router as evaluator_router
from server.node_operator import router as operator_router
from server.mobile import router as mobile_router
from server.mobile import router as mobile_router, upload_android_ui_dump
from server.mac import router as mac_router

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

v1router = APIRouter(
prefix="/api/v1",
Expand Down
43 changes: 43 additions & 0 deletions server/mobile.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import hashlib
import os
import subprocess
import base64
import time
from typing import Literal

import requests
from fastapi import APIRouter
from pydantic import BaseModel

from Framework.Utilities import ConfigModule, CommonUtil

ADB_PATH = "adb" # Ensure ADB is in PATH
UI_XML_PATH = "ui.xml"
SCREENSHOT_PATH = "screen.png"

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


class InspectorResponse(BaseModel):
"""Response model for the /inspector endpoint."""

Expand All @@ -19,6 +26,7 @@ class InspectorResponse(BaseModel):
screenshot: str | None = None # Base64 encoded image
error: str | None = None


class DeviceInfo(BaseModel):
"""Model for device information."""
serial: str
Expand Down Expand Up @@ -128,3 +136,38 @@ def capture_screenshot():
out = run_adb_command(f"{ADB_PATH} pull /sdcard/screen.png {SCREENSHOT_PATH}")
if out.startswith("Error:"):
return


def upload_android_ui_dump():
prev_xml_hash = ""
while True:
try:
capture_ui_dump()
try:
with open(UI_XML_PATH, 'r') as xml_file:
xml_content = xml_file.read()
xml_content = xml_content.replace("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>", "", 1)
new_xml_hash = hashlib.sha256(xml_content.encode('utf-8')).hexdigest()
# Don't upload if the content hasn't changed
if prev_xml_hash == new_xml_hash:
time.sleep(5)
continue
prev_xml_hash = new_xml_hash

except FileNotFoundError:
time.sleep(5)
continue
url = ConfigModule.get_config_value("Authentication", "server_address").strip() + "/node_ai_contents/"
apiKey = ConfigModule.get_config_value("Authentication", "api-key").strip()
res = requests.post(
url,
headers={"X-Api-Key": apiKey},
json={
"dom_mob": {"dom": xml_content},
"node_id": CommonUtil.MachineInfo().getLocalUser().lower()
})
if res.ok:
CommonUtil.ExecLog("", "UI dump uploaded successfully", iLogLevel=1)
except Exception as e:
CommonUtil.ExecLog("", f"Error uploading UI dump: {str(e)}", iLogLevel=3)
time.sleep(5)
Loading