Skip to content

Commit

Permalink
Merge pull request #219 from Pbatch/Leviaria-patch-1
Browse files Browse the repository at this point in the history
Enhanced Device Search
  • Loading branch information
Leviaria authored Aug 29, 2024
2 parents 83f3f61 + 42b0f7f commit e0f0e96
Showing 1 changed file with 52 additions and 4 deletions.
56 changes: 52 additions & 4 deletions clashroyalebuildabot/emulator/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,63 @@ def __init__(self, device_serial, ip):

self._install_adb()
self._restart_server()
self.device_serial = self._get_valid_device_serial()
self.width, self.height = self._get_width_and_height()
self._start_recording()
self._start_updating_frame()

def _get_valid_device_serial(self):
try:
logger.info(
f"Trying to connect to device '{self.device_serial}' from config."
)
self._run_command(["get-state"])
logger.info(
f"Successfully connected to the device '{self.device_serial}' from config."
)
return self.device_serial
except Exception as e:
logger.warning(
f"Device '{self.device_serial}' not found or not accessible: {str(e)}"
)
logger.warning(
"Trying to find a connected device via adb devices..."
)

try:
devices_output = subprocess.check_output(
[ADB_PATH, "devices"]
).decode("utf-8")
available_devices = [
line.split()[0]
for line in devices_output.splitlines()
if "\tdevice" in line
]

if not available_devices:
raise RuntimeError("No connected devices found") from e

fallback_device_serial = available_devices[0]
logger.info(
f"Using the first available device: {fallback_device_serial}"
)
return fallback_device_serial
except subprocess.CalledProcessError as adb_error:
logger.error(
f"Failed to execute adb devices: {str(adb_error)}"
)
raise RuntimeError(
"Could not find a valid device to connect to."
) from adb_error

def _start_recording(self):
cmd = f"""#!/bin/bash
while true; do
screenrecord --output-format=h264 --time-limit "179" --size "{self.width}x{self.height}" --bit-rate "5M" -
done\n"""
cmd = (
f"""#!/bin/bash
while true; do
screenrecord --output-format=h264 --time-limit "179" """
f"""--size "{self.width}x{self.height}" --bit-rate "5M" -
done\n"""
)
cmd = base64.standard_b64encode(cmd.encode("utf-8")).decode("utf-8")
cmd = ["echo", cmd, "|", "base64", "-d", "|", "sh"]
cmd = " ".join(cmd) + "\n"
Expand Down

0 comments on commit e0f0e96

Please sign in to comment.