11import os
22import subprocess
33import base64
4+ import time
45from typing import Literal
6+
7+ import requests
58from fastapi import APIRouter
69from pydantic import BaseModel
710
11+ from Framework .Utilities import ConfigModule , CommonUtil
12+
813ADB_PATH = "adb" # Ensure ADB is in PATH
914UI_XML_PATH = "ui.xml"
1015SCREENSHOT_PATH = "screen.png"
1116
1217router = APIRouter (prefix = "/mobile" , tags = ["mobile" ])
1318
19+
1420class 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+
2229class 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