From 14a4f5c008e7b1d3fe6e39d4c16ee60c6bb2f5cb Mon Sep 17 00:00:00 2001 From: Jing Hua Date: Mon, 16 Oct 2023 09:50:38 +0800 Subject: [PATCH] First commit --- requirements.txt | 1 + simulator.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 requirements.txt create mode 100644 simulator.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..296d654 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +numpy \ No newline at end of file diff --git a/simulator.py b/simulator.py new file mode 100644 index 0000000..8234290 --- /dev/null +++ b/simulator.py @@ -0,0 +1,51 @@ +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 \ No newline at end of file