update android_env to simplify code

This commit is contained in:
better629 2024-03-27 22:25:22 +08:00
parent 0b939f3078
commit a27b081ab3
18 changed files with 138 additions and 202 deletions

View file

@ -8,6 +8,8 @@ from metagpt.environment.android_env.android_ext_env import AndroidExtEnv
from metagpt.environment.base_env import Environment
class AndroidEnv(Environment, AndroidExtEnv):
class AndroidEnv(AndroidExtEnv, Environment):
"""in order to use actual `reset`&`observe`, inherited order: AndroidExtEnv, Environment"""
rows: int = Field(default=0, description="rows of a grid on the screenshot")
cols: int = Field(default=0, description="cols of a grid on the screenshot")

View file

@ -28,11 +28,18 @@ class AndroidExtEnv(ExtEnv):
def __init__(self, **data: Any):
super().__init__(**data)
if data.get("device_id"):
device_id = data.get("device_id")
if device_id:
devices = self.list_devices()
if device_id not in devices:
raise RuntimeError(f"device-id: {device_id} not found")
(width, height) = self.device_shape
self.width = data.get("width", width)
self.height = data.get("height", height)
self.create_device_path(self.screenshot_dir)
self.create_device_path(self.xml_dir)
def reset(
self,
*,
@ -108,6 +115,12 @@ class AndroidExtEnv(ExtEnv):
exec_res = res.stdout.strip()
return exec_res
def create_device_path(self, folder_path: Path):
adb_cmd = f"{self.adb_prefix_shell} mkdir {folder_path} -p"
res = self.execute_adb_with_cmd(adb_cmd)
if res == ADB_EXEC_FAIL:
raise RuntimeError(f"create device path: {folder_path} failed")
@property
def device_shape(self) -> tuple[int, int]:
adb_cmd = f"{self.adb_prefix_shell} wm size"

View file

@ -2,6 +2,8 @@
# -*- coding: utf-8 -*-
# @Desc :
from pathlib import Path
from typing import Union
import numpy as np
import numpy.typing as npt
@ -61,7 +63,7 @@ class EnvObsParams(BaseEnvObsParams):
obs_type: int = Field(default=EnvObsType.NONE, description="observation type")
ss_name: str = Field(default="", description="screenshot file name")
xml_name: str = Field(default="", description="xml file name")
local_save_dir: str = Field(default="", description="local dir to save file")
local_save_dir: Union[str, Path] = Field(default="", description="local dir to save file")
EnvObsValType = str