2023-11-08 09:22:43 +08:00
|
|
|
import json
|
2024-01-09 23:14:06 +08:00
|
|
|
import logging
|
|
|
|
|
import random
|
2024-01-10 17:36:59 +08:00
|
|
|
from typing import Any, Dict, Optional
|
2024-05-21 21:08:22 +08:00
|
|
|
import time
|
2023-12-02 17:52:00 +08:00
|
|
|
import requests
|
2024-01-09 23:14:06 +08:00
|
|
|
|
2024-06-15 20:52:29 +08:00
|
|
|
from desktop_env.actions import KEYBOARD_KEYS
|
2023-12-02 17:52:00 +08:00
|
|
|
|
2024-01-05 15:20:47 +08:00
|
|
|
logger = logging.getLogger("desktopenv.pycontroller")
|
2023-11-29 20:21:57 +08:00
|
|
|
|
2024-01-09 23:14:06 +08:00
|
|
|
|
2023-11-08 09:22:43 +08:00
|
|
|
class PythonController:
|
2024-05-20 00:47:43 +08:00
|
|
|
def __init__(self, vm_ip: str,
|
2024-09-28 21:10:40 +08:00
|
|
|
server_port: int,
|
2024-05-20 00:47:43 +08:00
|
|
|
pkgs_prefix: str = "import pyautogui; import time; pyautogui.FAILSAFE = False; {command}"):
|
2024-01-12 17:24:47 +08:00
|
|
|
self.vm_ip = vm_ip
|
2024-09-28 21:10:40 +08:00
|
|
|
self.http_server = f"http://{vm_ip}:{server_port}"
|
2023-12-02 17:52:00 +08:00
|
|
|
self.pkgs_prefix = pkgs_prefix # fixme: this is a hacky way to execute python commands. fix it and combine it with installation of packages
|
2024-05-21 21:08:22 +08:00
|
|
|
self.retry_times = 3
|
|
|
|
|
self.retry_interval = 5
|
2023-12-02 17:52:00 +08:00
|
|
|
|
2024-05-21 21:08:22 +08:00
|
|
|
def get_screenshot(self) -> Optional[bytes]:
|
2023-12-02 17:52:00 +08:00
|
|
|
"""
|
2024-05-21 21:08:22 +08:00
|
|
|
Gets a screenshot from the server. With the cursor. None -> no screenshot or unexpected error.
|
2023-12-02 17:52:00 +08:00
|
|
|
"""
|
2024-05-21 21:08:22 +08:00
|
|
|
|
|
|
|
|
for _ in range(self.retry_times):
|
|
|
|
|
try:
|
2024-05-20 00:47:43 +08:00
|
|
|
response = requests.get(self.http_server + "/screenshot")
|
|
|
|
|
if response.status_code == 200:
|
2024-05-21 21:08:22 +08:00
|
|
|
logger.info("Got screenshot successfully")
|
2024-05-20 00:47:43 +08:00
|
|
|
return response.content
|
2024-05-21 21:08:22 +08:00
|
|
|
else:
|
|
|
|
|
logger.error("Failed to get screenshot. Status code: %d", response.status_code)
|
|
|
|
|
logger.info("Retrying to get screenshot.")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error("An error occurred while trying to get the screenshot: %s", e)
|
|
|
|
|
logger.info("Retrying to get screenshot.")
|
|
|
|
|
time.sleep(self.retry_interval)
|
|
|
|
|
|
|
|
|
|
logger.error("Failed to get screenshot.")
|
|
|
|
|
return None
|
2023-11-29 20:21:57 +08:00
|
|
|
|
2024-05-21 21:08:22 +08:00
|
|
|
def get_accessibility_tree(self) -> Optional[str]:
|
2024-01-12 18:09:05 +08:00
|
|
|
"""
|
2024-05-21 21:08:22 +08:00
|
|
|
Gets the accessibility tree from the server. None -> no accessibility tree or unexpected error.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
for _ in range(self.retry_times):
|
|
|
|
|
try:
|
|
|
|
|
response: requests.Response = requests.get(self.http_server + "/accessibility")
|
2024-05-20 00:47:43 +08:00
|
|
|
if response.status_code == 200:
|
2024-05-21 21:08:22 +08:00
|
|
|
logger.info("Got accessibility tree successfully")
|
|
|
|
|
return response.json()["AT"]
|
|
|
|
|
else:
|
|
|
|
|
logger.error("Failed to get accessibility tree. Status code: %d", response.status_code)
|
|
|
|
|
logger.info("Retrying to get accessibility tree.")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error("An error occurred while trying to get the accessibility tree: %s", e)
|
|
|
|
|
logger.info("Retrying to get accessibility tree.")
|
|
|
|
|
time.sleep(self.retry_interval)
|
2024-01-12 18:09:05 +08:00
|
|
|
|
2024-05-21 21:08:22 +08:00
|
|
|
logger.error("Failed to get accessibility tree.")
|
|
|
|
|
return None
|
2024-01-10 17:36:59 +08:00
|
|
|
|
2024-05-21 21:08:22 +08:00
|
|
|
def get_terminal_output(self) -> Optional[str]:
|
|
|
|
|
"""
|
|
|
|
|
Gets the terminal output from the server. None -> no terminal output or unexpected error.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
for _ in range(self.retry_times):
|
|
|
|
|
try:
|
|
|
|
|
response = requests.get(self.http_server + "/terminal")
|
2024-05-20 00:47:43 +08:00
|
|
|
if response.status_code == 200:
|
2024-05-21 21:08:22 +08:00
|
|
|
logger.info("Got terminal output successfully")
|
|
|
|
|
return response.json()["output"]
|
|
|
|
|
else:
|
|
|
|
|
logger.error("Failed to get terminal output. Status code: %d", response.status_code)
|
|
|
|
|
logger.info("Retrying to get terminal output.")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error("An error occurred while trying to get the terminal output: %s", e)
|
|
|
|
|
logger.info("Retrying to get terminal output.")
|
|
|
|
|
time.sleep(self.retry_interval)
|
|
|
|
|
|
|
|
|
|
logger.error("Failed to get terminal output.")
|
|
|
|
|
return None
|
2024-01-10 17:36:59 +08:00
|
|
|
|
2024-05-21 21:08:22 +08:00
|
|
|
def get_file(self, file_path: str) -> Optional[bytes]:
|
2023-12-12 18:10:55 +08:00
|
|
|
"""
|
|
|
|
|
Gets a file from the server.
|
|
|
|
|
"""
|
2024-05-21 21:08:22 +08:00
|
|
|
|
|
|
|
|
for _ in range(self.retry_times):
|
|
|
|
|
try:
|
|
|
|
|
response = requests.post(self.http_server + "/file", data={"file_path": file_path})
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
|
logger.info("File downloaded successfully")
|
|
|
|
|
return response.content
|
|
|
|
|
else:
|
|
|
|
|
logger.error("Failed to get file. Status code: %d", response.status_code)
|
|
|
|
|
logger.info("Retrying to get file.")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error("An error occurred while trying to get the file: %s", e)
|
|
|
|
|
logger.info("Retrying to get file.")
|
|
|
|
|
time.sleep(self.retry_interval)
|
|
|
|
|
|
|
|
|
|
logger.error("Failed to get file.")
|
|
|
|
|
return None
|
2023-12-12 18:10:55 +08:00
|
|
|
|
2023-12-02 17:52:00 +08:00
|
|
|
def execute_python_command(self, command: str) -> None:
|
|
|
|
|
"""
|
|
|
|
|
Executes a python command on the server.
|
|
|
|
|
It can be used to execute the pyautogui commands, or... any other python command. who knows?
|
|
|
|
|
"""
|
2024-01-29 21:42:16 +08:00
|
|
|
# command_list = ["python", "-c", self.pkgs_prefix.format(command=command)]
|
2024-03-20 22:22:57 +08:00
|
|
|
command_list = ["python", "-c", self.pkgs_prefix.format(command=command)]
|
2024-01-12 18:09:05 +08:00
|
|
|
payload = json.dumps({"command": command_list, "shell": False})
|
2024-05-21 21:08:22 +08:00
|
|
|
|
|
|
|
|
for _ in range(self.retry_times):
|
|
|
|
|
try:
|
|
|
|
|
response = requests.post(self.http_server + "/execute", headers={'Content-Type': 'application/json'},
|
|
|
|
|
data=payload, timeout=90)
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
|
logger.info("Command executed successfully: %s", response.text)
|
|
|
|
|
return response.json()
|
|
|
|
|
else:
|
|
|
|
|
logger.error("Failed to execute command. Status code: %d", response.status_code)
|
|
|
|
|
logger.info("Retrying to execute command.")
|
|
|
|
|
except requests.exceptions.ReadTimeout:
|
|
|
|
|
break
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error("An error occurred while trying to execute the command: %s", e)
|
|
|
|
|
logger.info("Retrying to execute command.")
|
|
|
|
|
time.sleep(self.retry_interval)
|
|
|
|
|
|
|
|
|
|
logger.error("Failed to execute command.")
|
|
|
|
|
return None
|
2023-12-02 18:02:06 +08:00
|
|
|
|
2023-12-03 00:59:02 +08:00
|
|
|
def execute_action(self, action: Dict[str, Any]):
|
2023-12-02 18:02:06 +08:00
|
|
|
"""
|
|
|
|
|
Executes an action on the server computer.
|
|
|
|
|
"""
|
2024-01-16 16:43:32 +08:00
|
|
|
if action in ['WAIT', 'FAIL', 'DONE']:
|
|
|
|
|
return
|
2023-12-02 18:02:06 +08:00
|
|
|
|
2023-12-03 00:59:02 +08:00
|
|
|
action_type = action["action_type"]
|
2024-06-11 14:22:31 +08:00
|
|
|
parameters = action["parameters"] if "parameters" in action else {param: action[param] for param in action if param != 'action_type'}
|
2024-01-09 23:14:06 +08:00
|
|
|
move_mode = random.choice(
|
|
|
|
|
["pyautogui.easeInQuad", "pyautogui.easeOutQuad", "pyautogui.easeInOutQuad", "pyautogui.easeInBounce",
|
|
|
|
|
"pyautogui.easeInElastic"])
|
|
|
|
|
duration = random.uniform(0.5, 1)
|
2023-12-03 00:59:02 +08:00
|
|
|
|
|
|
|
|
if action_type == "MOVE_TO":
|
|
|
|
|
if parameters == {} or None:
|
2024-01-10 17:36:59 +08:00
|
|
|
self.execute_python_command("pyautogui.moveTo()")
|
2023-12-03 00:59:02 +08:00
|
|
|
elif "x" in parameters and "y" in parameters:
|
|
|
|
|
x = parameters["x"]
|
|
|
|
|
y = parameters["y"]
|
2024-01-09 23:14:06 +08:00
|
|
|
self.execute_python_command(f"pyautogui.moveTo({x}, {y}, {duration}, {move_mode})")
|
2023-12-03 00:59:02 +08:00
|
|
|
else:
|
|
|
|
|
raise Exception(f"Unknown parameters: {parameters}")
|
|
|
|
|
|
|
|
|
|
elif action_type == "CLICK":
|
|
|
|
|
if parameters == {} or None:
|
2024-01-10 17:36:59 +08:00
|
|
|
self.execute_python_command("pyautogui.click()")
|
2023-12-03 00:59:02 +08:00
|
|
|
elif "button" in parameters and "x" in parameters and "y" in parameters:
|
|
|
|
|
button = parameters["button"]
|
|
|
|
|
x = parameters["x"]
|
|
|
|
|
y = parameters["y"]
|
2023-12-04 00:51:33 +08:00
|
|
|
if "num_clicks" in parameters:
|
|
|
|
|
num_clicks = parameters["num_clicks"]
|
2024-01-04 17:05:17 +08:00
|
|
|
self.execute_python_command(
|
|
|
|
|
f"pyautogui.click(button='{button}', x={x}, y={y}, clicks={num_clicks})")
|
2023-12-04 00:51:33 +08:00
|
|
|
else:
|
|
|
|
|
self.execute_python_command(f"pyautogui.click(button='{button}', x={x}, y={y})")
|
2023-12-03 00:59:02 +08:00
|
|
|
elif "button" in parameters and "x" not in parameters and "y" not in parameters:
|
|
|
|
|
button = parameters["button"]
|
2023-12-04 00:51:33 +08:00
|
|
|
if "num_clicks" in parameters:
|
|
|
|
|
num_clicks = parameters["num_clicks"]
|
|
|
|
|
self.execute_python_command(f"pyautogui.click(button='{button}', clicks={num_clicks})")
|
|
|
|
|
else:
|
|
|
|
|
self.execute_python_command(f"pyautogui.click(button='{button}')")
|
2023-12-03 00:59:02 +08:00
|
|
|
elif "button" not in parameters and "x" in parameters and "y" in parameters:
|
|
|
|
|
x = parameters["x"]
|
|
|
|
|
y = parameters["y"]
|
2023-12-04 00:51:33 +08:00
|
|
|
if "num_clicks" in parameters:
|
|
|
|
|
num_clicks = parameters["num_clicks"]
|
|
|
|
|
self.execute_python_command(f"pyautogui.click(x={x}, y={y}, clicks={num_clicks})")
|
|
|
|
|
else:
|
|
|
|
|
self.execute_python_command(f"pyautogui.click(x={x}, y={y})")
|
2023-12-03 00:59:02 +08:00
|
|
|
else:
|
|
|
|
|
raise Exception(f"Unknown parameters: {parameters}")
|
|
|
|
|
|
|
|
|
|
elif action_type == "MOUSE_DOWN":
|
|
|
|
|
if parameters == {} or None:
|
2024-01-10 17:36:59 +08:00
|
|
|
self.execute_python_command("pyautogui.mouseDown()")
|
2023-12-03 00:59:02 +08:00
|
|
|
elif "button" in parameters:
|
|
|
|
|
button = parameters["button"]
|
|
|
|
|
self.execute_python_command(f"pyautogui.mouseDown(button='{button}')")
|
|
|
|
|
else:
|
|
|
|
|
raise Exception(f"Unknown parameters: {parameters}")
|
|
|
|
|
|
|
|
|
|
elif action_type == "MOUSE_UP":
|
|
|
|
|
if parameters == {} or None:
|
2024-01-10 17:36:59 +08:00
|
|
|
self.execute_python_command("pyautogui.mouseUp()")
|
2023-12-03 00:59:02 +08:00
|
|
|
elif "button" in parameters:
|
|
|
|
|
button = parameters["button"]
|
|
|
|
|
self.execute_python_command(f"pyautogui.mouseUp(button='{button}')")
|
|
|
|
|
else:
|
|
|
|
|
raise Exception(f"Unknown parameters: {parameters}")
|
|
|
|
|
|
|
|
|
|
elif action_type == "RIGHT_CLICK":
|
|
|
|
|
if parameters == {} or None:
|
2024-01-10 17:36:59 +08:00
|
|
|
self.execute_python_command("pyautogui.rightClick()")
|
2023-12-03 00:59:02 +08:00
|
|
|
elif "x" in parameters and "y" in parameters:
|
|
|
|
|
x = parameters["x"]
|
|
|
|
|
y = parameters["y"]
|
|
|
|
|
self.execute_python_command(f"pyautogui.rightClick(x={x}, y={y})")
|
|
|
|
|
else:
|
|
|
|
|
raise Exception(f"Unknown parameters: {parameters}")
|
|
|
|
|
|
|
|
|
|
elif action_type == "DOUBLE_CLICK":
|
|
|
|
|
if parameters == {} or None:
|
2024-01-10 17:36:59 +08:00
|
|
|
self.execute_python_command("pyautogui.doubleClick()")
|
2023-12-03 00:59:02 +08:00
|
|
|
elif "x" in parameters and "y" in parameters:
|
|
|
|
|
x = parameters["x"]
|
|
|
|
|
y = parameters["y"]
|
|
|
|
|
self.execute_python_command(f"pyautogui.doubleClick(x={x}, y={y})")
|
|
|
|
|
else:
|
|
|
|
|
raise Exception(f"Unknown parameters: {parameters}")
|
|
|
|
|
|
|
|
|
|
elif action_type == "DRAG_TO":
|
|
|
|
|
if "x" in parameters and "y" in parameters:
|
|
|
|
|
x = parameters["x"]
|
|
|
|
|
y = parameters["y"]
|
2024-01-04 17:05:17 +08:00
|
|
|
self.execute_python_command(
|
|
|
|
|
f"pyautogui.dragTo({x}, {y}, duration=1.0, button='left', mouseDownUp=True)")
|
2023-12-03 00:59:02 +08:00
|
|
|
|
|
|
|
|
elif action_type == "SCROLL":
|
|
|
|
|
# todo: check if it is related to the operating system, as https://github.com/TheDuckAI/DuckTrack/blob/main/ducktrack/playback.py pointed out
|
|
|
|
|
if "dx" in parameters and "dy" in parameters:
|
|
|
|
|
dx = parameters["dx"]
|
|
|
|
|
dy = parameters["dy"]
|
|
|
|
|
self.execute_python_command(f"pyautogui.hscroll({dx})")
|
|
|
|
|
self.execute_python_command(f"pyautogui.vscroll({dy})")
|
|
|
|
|
elif "dx" in parameters and "dy" not in parameters:
|
|
|
|
|
dx = parameters["dx"]
|
|
|
|
|
self.execute_python_command(f"pyautogui.hscroll({dx})")
|
|
|
|
|
elif "dx" not in parameters and "dy" in parameters:
|
|
|
|
|
dy = parameters["dy"]
|
|
|
|
|
self.execute_python_command(f"pyautogui.vscroll({dy})")
|
|
|
|
|
else:
|
|
|
|
|
raise Exception(f"Unknown parameters: {parameters}")
|
|
|
|
|
|
|
|
|
|
elif action_type == "TYPING":
|
|
|
|
|
if "text" not in parameters:
|
|
|
|
|
raise Exception(f"Unknown parameters: {parameters}")
|
2024-01-12 18:09:05 +08:00
|
|
|
# deal with special ' and \ characters
|
2024-01-14 23:53:31 +08:00
|
|
|
# text = parameters["text"].replace("\\", "\\\\").replace("'", "\\'")
|
|
|
|
|
# self.execute_python_command(f"pyautogui.typewrite('{text}')")
|
|
|
|
|
text = parameters["text"]
|
|
|
|
|
self.execute_python_command("pyautogui.typewrite({:})".format(repr(text)))
|
2023-12-03 00:59:02 +08:00
|
|
|
|
|
|
|
|
elif action_type == "PRESS":
|
|
|
|
|
if "key" not in parameters:
|
|
|
|
|
raise Exception(f"Unknown parameters: {parameters}")
|
|
|
|
|
key = parameters["key"]
|
|
|
|
|
if key.lower() not in KEYBOARD_KEYS:
|
|
|
|
|
raise Exception(f"Key must be one of {KEYBOARD_KEYS}")
|
|
|
|
|
self.execute_python_command(f"pyautogui.press('{key}')")
|
|
|
|
|
|
|
|
|
|
elif action_type == "KEY_DOWN":
|
|
|
|
|
if "key" not in parameters:
|
|
|
|
|
raise Exception(f"Unknown parameters: {parameters}")
|
|
|
|
|
key = parameters["key"]
|
|
|
|
|
if key.lower() not in KEYBOARD_KEYS:
|
|
|
|
|
raise Exception(f"Key must be one of {KEYBOARD_KEYS}")
|
|
|
|
|
self.execute_python_command(f"pyautogui.keyDown('{key}')")
|
|
|
|
|
|
|
|
|
|
elif action_type == "KEY_UP":
|
|
|
|
|
if "key" not in parameters:
|
|
|
|
|
raise Exception(f"Unknown parameters: {parameters}")
|
|
|
|
|
key = parameters["key"]
|
|
|
|
|
if key.lower() not in KEYBOARD_KEYS:
|
|
|
|
|
raise Exception(f"Key must be one of {KEYBOARD_KEYS}")
|
|
|
|
|
self.execute_python_command(f"pyautogui.keyUp('{key}')")
|
|
|
|
|
|
|
|
|
|
elif action_type == "HOTKEY":
|
|
|
|
|
if "keys" not in parameters:
|
|
|
|
|
raise Exception(f"Unknown parameters: {parameters}")
|
|
|
|
|
keys = parameters["keys"]
|
|
|
|
|
if not isinstance(keys, list):
|
2024-01-10 17:36:59 +08:00
|
|
|
raise Exception("Keys must be a list of keys")
|
2023-12-03 00:59:02 +08:00
|
|
|
for key in keys:
|
|
|
|
|
if key.lower() not in KEYBOARD_KEYS:
|
|
|
|
|
raise Exception(f"Key must be one of {KEYBOARD_KEYS}")
|
|
|
|
|
|
|
|
|
|
keys_para_rep = "', '".join(keys)
|
|
|
|
|
self.execute_python_command(f"pyautogui.hotkey('{keys_para_rep}')")
|
|
|
|
|
|
2024-01-14 23:36:19 +08:00
|
|
|
elif action_type in ['WAIT', 'FAIL', 'DONE']:
|
|
|
|
|
pass
|
|
|
|
|
|
2023-12-03 00:59:02 +08:00
|
|
|
else:
|
|
|
|
|
raise Exception(f"Unknown action type: {action_type}")
|
2024-01-04 17:05:17 +08:00
|
|
|
|
2024-01-15 13:49:48 +08:00
|
|
|
# Record video
|
|
|
|
|
def start_recording(self):
|
|
|
|
|
"""
|
|
|
|
|
Starts recording the screen.
|
|
|
|
|
"""
|
2024-05-21 21:08:22 +08:00
|
|
|
|
|
|
|
|
for _ in range(self.retry_times):
|
|
|
|
|
try:
|
|
|
|
|
response = requests.post(self.http_server + "/start_recording")
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
|
logger.info("Recording started successfully")
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
logger.error("Failed to start recording. Status code: %d", response.status_code)
|
|
|
|
|
logger.info("Retrying to start recording.")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error("An error occurred while trying to start recording: %s", e)
|
|
|
|
|
logger.info("Retrying to start recording.")
|
|
|
|
|
time.sleep(self.retry_interval)
|
|
|
|
|
|
|
|
|
|
logger.error("Failed to start recording.")
|
2024-01-15 13:49:48 +08:00
|
|
|
|
|
|
|
|
def end_recording(self, dest: str):
|
|
|
|
|
"""
|
|
|
|
|
Ends recording the screen.
|
|
|
|
|
"""
|
2024-05-21 21:08:22 +08:00
|
|
|
|
|
|
|
|
for _ in range(self.retry_times):
|
|
|
|
|
try:
|
|
|
|
|
response = requests.post(self.http_server + "/end_recording")
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
|
logger.info("Recording stopped successfully")
|
|
|
|
|
with open(dest, 'wb') as f:
|
|
|
|
|
for chunk in response.iter_content(chunk_size=8192):
|
|
|
|
|
if chunk:
|
|
|
|
|
f.write(chunk)
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
logger.error("Failed to stop recording. Status code: %d", response.status_code)
|
|
|
|
|
logger.info("Retrying to stop recording.")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error("An error occurred while trying to stop recording: %s", e)
|
|
|
|
|
logger.info("Retrying to stop recording.")
|
|
|
|
|
time.sleep(self.retry_interval)
|
|
|
|
|
|
|
|
|
|
logger.error("Failed to stop recording.")
|
2024-01-15 13:49:48 +08:00
|
|
|
|
2024-01-11 21:27:40 +08:00
|
|
|
# Additional info
|
|
|
|
|
def get_vm_platform(self):
|
|
|
|
|
"""
|
|
|
|
|
Gets the size of the vm screen.
|
|
|
|
|
"""
|
|
|
|
|
return self.execute_python_command("import platform; print(platform.system())")['output'].strip()
|
|
|
|
|
|
2024-01-09 23:14:06 +08:00
|
|
|
def get_vm_screen_size(self):
|
|
|
|
|
"""
|
|
|
|
|
Gets the size of the vm screen.
|
|
|
|
|
"""
|
2024-05-21 21:08:22 +08:00
|
|
|
|
|
|
|
|
for _ in range(self.retry_times):
|
|
|
|
|
try:
|
|
|
|
|
response = requests.post(self.http_server + "/screen_size")
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
|
logger.info("Got screen size successfully")
|
|
|
|
|
return response.json()
|
|
|
|
|
else:
|
|
|
|
|
logger.error("Failed to get screen size. Status code: %d", response.status_code)
|
|
|
|
|
logger.info("Retrying to get screen size.")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error("An error occurred while trying to get the screen size: %s", e)
|
|
|
|
|
logger.info("Retrying to get screen size.")
|
|
|
|
|
time.sleep(self.retry_interval)
|
|
|
|
|
|
|
|
|
|
logger.error("Failed to get screen size.")
|
|
|
|
|
return None
|
2024-01-09 23:14:06 +08:00
|
|
|
|
|
|
|
|
def get_vm_window_size(self, app_class_name: str):
|
|
|
|
|
"""
|
|
|
|
|
Gets the size of the vm app window.
|
|
|
|
|
"""
|
2024-05-21 21:08:22 +08:00
|
|
|
|
|
|
|
|
for _ in range(self.retry_times):
|
|
|
|
|
try:
|
|
|
|
|
response = requests.post(self.http_server + "/window_size", data={"app_class_name": app_class_name})
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
|
logger.info("Got window size successfully")
|
|
|
|
|
return response.json()
|
|
|
|
|
else:
|
|
|
|
|
logger.error("Failed to get window size. Status code: %d", response.status_code)
|
|
|
|
|
logger.info("Retrying to get window size.")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error("An error occurred while trying to get the window size: %s", e)
|
|
|
|
|
logger.info("Retrying to get window size.")
|
|
|
|
|
time.sleep(self.retry_interval)
|
|
|
|
|
|
|
|
|
|
logger.error("Failed to get window size.")
|
|
|
|
|
return None
|
2024-01-04 17:05:17 +08:00
|
|
|
|
2024-01-10 23:18:30 +08:00
|
|
|
def get_vm_wallpaper(self):
|
|
|
|
|
"""
|
|
|
|
|
Gets the wallpaper of the vm.
|
|
|
|
|
"""
|
2024-01-13 22:56:50 +08:00
|
|
|
|
2024-05-21 21:08:22 +08:00
|
|
|
for _ in range(self.retry_times):
|
|
|
|
|
try:
|
|
|
|
|
response = requests.post(self.http_server + "/wallpaper")
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
|
logger.info("Got wallpaper successfully")
|
|
|
|
|
return response.content
|
|
|
|
|
else:
|
|
|
|
|
logger.error("Failed to get wallpaper. Status code: %d", response.status_code)
|
|
|
|
|
logger.info("Retrying to get wallpaper.")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error("An error occurred while trying to get the wallpaper: %s", e)
|
|
|
|
|
logger.info("Retrying to get wallpaper.")
|
|
|
|
|
time.sleep(self.retry_interval)
|
|
|
|
|
|
|
|
|
|
logger.error("Failed to get wallpaper.")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def get_vm_desktop_path(self) -> Optional[str]:
|
2024-01-13 22:56:50 +08:00
|
|
|
"""
|
|
|
|
|
Gets the desktop path of the vm.
|
|
|
|
|
"""
|
|
|
|
|
|
2024-05-21 21:08:22 +08:00
|
|
|
for _ in range(self.retry_times):
|
|
|
|
|
try:
|
|
|
|
|
response = requests.post(self.http_server + "/desktop_path")
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
|
logger.info("Got desktop path successfully")
|
|
|
|
|
return response.json()["desktop_path"]
|
|
|
|
|
else:
|
|
|
|
|
logger.error("Failed to get desktop path. Status code: %d", response.status_code)
|
|
|
|
|
logger.info("Retrying to get desktop path.")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error("An error occurred while trying to get the desktop path: %s", e)
|
|
|
|
|
logger.info("Retrying to get desktop path.")
|
|
|
|
|
time.sleep(self.retry_interval)
|
|
|
|
|
|
|
|
|
|
logger.error("Failed to get desktop path.")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def get_vm_directory_tree(self, path) -> Optional[Dict[str, Any]]:
|
2024-01-13 22:56:50 +08:00
|
|
|
"""
|
|
|
|
|
Gets the directory tree of the vm.
|
|
|
|
|
"""
|
|
|
|
|
payload = json.dumps({"path": path})
|
2024-05-21 21:08:22 +08:00
|
|
|
|
|
|
|
|
for _ in range(self.retry_times):
|
|
|
|
|
try:
|
|
|
|
|
response = requests.post(self.http_server + "/list_directory", headers={'Content-Type': 'application/json'}, data=payload)
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
|
logger.info("Got directory tree successfully")
|
|
|
|
|
return response.json()["directory_tree"]
|
|
|
|
|
else:
|
|
|
|
|
logger.error("Failed to get directory tree. Status code: %d", response.status_code)
|
|
|
|
|
logger.info("Retrying to get directory tree.")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error("An error occurred while trying to get directory tree: %s", e)
|
|
|
|
|
logger.info("Retrying to get directory tree.")
|
|
|
|
|
time.sleep(self.retry_interval)
|
|
|
|
|
|
|
|
|
|
logger.error("Failed to get directory tree.")
|
|
|
|
|
return None
|