1+ import  hashlib 
12import  os 
23import  subprocess 
34import  base64 
5+ import  time 
46from  typing  import  Literal 
7+ 
8+ import  requests 
59from  fastapi  import  APIRouter 
610from  pydantic  import  BaseModel 
711
12+ from  Framework .Utilities  import  ConfigModule , CommonUtil 
13+ 
814ADB_PATH  =  "adb"   # Ensure ADB is in PATH 
915UI_XML_PATH  =  "ui.xml" 
1016SCREENSHOT_PATH  =  "screen.png" 
1117
1218router  =  APIRouter (prefix = "/mobile" , tags = ["mobile" ])
1319
20+ 
1421class  InspectorResponse (BaseModel ):
1522    """Response model for the /inspector endpoint.""" 
1623
@@ -19,6 +26,7 @@ class InspectorResponse(BaseModel):
1926    screenshot : str  |  None  =  None   # Base64 encoded image 
2027    error : str  |  None  =  None 
2128
29+ 
2230class  DeviceInfo (BaseModel ):
2331    """Model for device information.""" 
2432    serial : str 
@@ -128,3 +136,38 @@ def capture_screenshot():
128136        out  =  run_adb_command (f"{ ADB_PATH } { SCREENSHOT_PATH }  )
129137        if  out .startswith ("Error:" ):
130138            return 
139+ 
140+ 
141+ def  upload_android_ui_dump ():
142+     prev_xml_hash  =  "" 
143+     while  True :
144+         try :
145+             capture_ui_dump ()
146+             try :
147+                 with  open (UI_XML_PATH , 'r' ) as  xml_file :
148+                     xml_content  =  xml_file .read ()
149+                     xml_content  =  xml_content .replace ("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>" , "" , 1 )
150+                     new_xml_hash  =  hashlib .sha256 (xml_content .encode ('utf-8' )).hexdigest ()
151+                     # Don't upload if the content hasn't changed 
152+                     if  prev_xml_hash  ==  new_xml_hash :
153+                         time .sleep (5 )
154+                         continue 
155+                     prev_xml_hash  =  new_xml_hash 
156+ 
157+             except  FileNotFoundError :
158+                 time .sleep (5 )
159+                 continue 
160+             url  =  ConfigModule .get_config_value ("Authentication" , "server_address" ).strip () +  "/node_ai_contents/" 
161+             apiKey  =  ConfigModule .get_config_value ("Authentication" , "api-key" ).strip ()
162+             res  =  requests .post (
163+                 url ,
164+                 headers = {"X-Api-Key" : apiKey },
165+                 json = {
166+                     "dom_mob" : {"dom" : xml_content },
167+                     "node_id" : CommonUtil .MachineInfo ().getLocalUser ().lower ()
168+                 })
169+             if  res .ok :
170+                 CommonUtil .ExecLog ("" , "UI dump uploaded successfully" , iLogLevel = 1 )
171+         except  Exception  as  e :
172+             CommonUtil .ExecLog ("" , f"Error uploading UI dump: { str (e )}  , iLogLevel = 3 )
173+         time .sleep (5 )
0 commit comments