Files
sci-gui-agent-benchmark/simulator.py
2023-10-16 09:50:38 +08:00

51 lines
1.9 KiB
Python

import numpy as np
import subprocess
class EmulatorSimulator:
def __init__(self, vm_name: str, username: str, password: str, snapshot_name: str = "snapshot"):
self.vm_name = vm_name
self.username = username
self.password = password
self.snapshot_name = snapshot_name
self.execute_command(["VBoxManage", "startvm", self.vm_name])
pass
def _execute_command(self, command: list[str]) -> None:
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
if process.returncode != 0:
print(f"Error executing command: {command}")
print(stderr.decode())
return None
else:
return stdout.decode()
def _execute_xdotool_command(self, command: list[str]) -> None:
command_prefix = ["VBoxManage", "guestcontrol", self.vm_name, "run",
"--exe", "/usr/bin/xdotool", "--username", self.username,
"--password", self.password, "--"]
self._execute_command(command_prefix + command)
def get_logs(self) -> str:
pass
def load_state(self) -> None:
self._execute_command(["VBoxManage", "snapshot", self.vm_name, "restore", self.snapshot_name])
def save_state(self) -> None:
self._execute_command(["VBoxManage", "snapshot", self.vm_name, "take", self.snapshot_name])
def send_click(self, click: int) -> None:
self._execute_xdotool_command(["click", str(click)])
def send_key(self) -> None:
pass
def mouse_move(self, x: int, y: int) -> None:
self._execute_xdotool_command(["mousemove", str(x), str(y),])
def get_screenshot(self) -> np.ndarray:
self._execute_command(["VBoxManage", "controlvm", self.vm_name, "screenshotpng", "./"])
def close(self) -> None:
pass