add controller

This commit is contained in:
Jing Hua
2023-10-24 02:26:24 +08:00
parent 14a4f5c008
commit a7144824d2
6 changed files with 61 additions and 13 deletions

View File

@@ -1,14 +1,15 @@
import numpy as np
import subprocess
from fabric import Connection
class EmulatorSimulator:
def __init__(self, vm_name: str, username: str, password: str, snapshot_name: str = "snapshot"):
def __init__(self, vm_name: str, username: str, password: str, host:str, snapshot_name: str = "snapshot"):
self.vm_name = vm_name
self.username = username
self.password = password
self.host = host
self.snapshot_name = snapshot_name
self.execute_command(["VBoxManage", "startvm", self.vm_name])
pass
self.ssh_connection = Connection(host=self.host, user=self.username, connect_kwargs={"password": password})
self._execute_command(["VBoxManage", "startvm", self.vm_name])
def _execute_command(self, command: list[str]) -> None:
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@@ -21,10 +22,8 @@ class EmulatorSimulator:
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)
result = self.ssh_connection.run(f"DISPLAY=:0 xdotool {command}", hide=True)
return result.stdout.strip()
def get_logs(self) -> str:
pass
@@ -36,16 +35,21 @@ class EmulatorSimulator:
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)])
self._execute_xdotool_command(f"click {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),])
self._execute_xdotool_command(f"mousemove {x} {y}")
def get_screenshot(self) -> np.ndarray:
self._execute_command(["VBoxManage", "controlvm", self.vm_name, "screenshotpng", "./"])
def keyboard_type(self, text: str) -> None:
self._execute_xdotool_command(f"type {text}")
def get_screenshot(self) -> str:
image_path = "./screenshot.png"
self._execute_command(["VBoxManage", "controlvm", self.vm_name, "screenshotpng", image_path])
return image_path
def close(self) -> None:
pass