1
0
mirror of https://github.com/0O0o0oOoO00/Alas.git synced 2026-05-17 04:19:29 +08:00
Files
Alas/module/cloud_phone/run.py

103 lines
3.6 KiB
Python
Raw Normal View History

2025-05-18 21:20:19 +08:00
from module.logger import logger
2025-05-18 20:43:18 +08:00
import time
2025-05-18 21:20:19 +08:00
import json
2025-05-18 20:43:18 +08:00
from module.base.base import ModuleBase
from module.base.timer import Timer
from module.cloud_phone.chinac_api import *
from module.config.config import deep_get
from module.exception import ChinacError
class CloudPhoneRestart(ModuleBase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
config = self.config.data
self.access_key = deep_get(config, "CloudPhoneRestart.CloudPhoneSetting.AccessKey", "")
self.access_key_secret = deep_get(config, "CloudPhoneRestart.CloudPhoneSetting.AccessKeySecret", "")
self.region = deep_get(config, "CloudPhoneRestart.CloudPhoneSetting.Region", "")
self.phone_id = deep_get(config, "CloudPhoneRestart.CloudPhoneSetting.PhoneId", "")
self.api = None
def check_config(self):
def value_check(v: str):
if not v:
raise ValueError("Invalid cloud phone setting")
value_check(self.access_key)
value_check(self.access_key_secret)
value_check(self.region)
value_check(self.phone_id)
def load_chinac_api(self):
self.api = ChinacOpenApi(self.access_key, self.access_key_secret)
def phone_restart(self):
self.api.set_action(CloudPhoneAction.RebootCloudPhone)
2025-05-18 20:47:48 +08:00
self.api.set_http_method("POST")
2025-05-18 20:43:18 +08:00
self.api.set_request_params({
"CloudPhones": [
{
2025-05-18 21:20:19 +08:00
"Region": self.region,
"Id": self.phone_id
2025-05-18 20:43:18 +08:00
}
],
"Operate": "reboot"
})
res = self.api.do()
2025-05-18 21:20:19 +08:00
info = json.loads(res["Info"])
status_code = res["Status"]
if status_code != 200 and info["code"] == 10000:
2025-05-18 20:43:18 +08:00
raise ChinacError(f"Chinac api error, code: {status_code}")
2025-05-18 21:20:19 +08:00
logger.info("Cloud phone restart request success")
2025-05-18 20:43:18 +08:00
def is_ready(self):
self.api.set_action(CloudPhoneAction.DescribeCloudPhone)
2025-05-18 20:47:48 +08:00
self.api.set_http_method("POST")
2025-05-18 20:43:18 +08:00
self.api.set_request_params({
'Region': self.region,
"Id": self.phone_id
})
res = self.api.do()
2025-05-18 21:20:19 +08:00
info = json.loads(res["Info"])
status_code = res["Status"]
if status_code != 200 and info["code"] == 10000:
2025-05-18 20:43:18 +08:00
raise ChinacError(f"Chinac api error, code: {status_code}")
2025-05-18 21:20:19 +08:00
phone_status = info["data"]["BasicInfo"]["Status"]
logger.info(f"Cloud phone current status: {phone_status}")
2025-05-18 20:43:18 +08:00
if phone_status == "START":
2025-05-18 21:20:19 +08:00
logger.info("Cloud phone ready")
2025-05-18 20:43:18 +08:00
return True
return False
2025-05-18 21:25:52 +08:00
def wait_for_adb(self):
logger.info("Wait for adb of cloud phone")
time.sleep(deep_get(self.config.data, "CloudPhoneRestart.CloudPhoneSetting.AdbWaitTime", 60))
2025-05-18 20:43:18 +08:00
def wait_for_ready(self):
wait_timer = Timer(deep_get(self.config.data, "CloudPhoneRestart.CloudPhoneSetting.PhoneWaitTime", 60))
2025-05-18 21:20:19 +08:00
wait_timer.start()
2025-05-18 20:43:18 +08:00
while 1:
2025-05-18 21:20:19 +08:00
if wait_timer.reached():
2025-05-18 20:43:18 +08:00
raise ChinacError("Waiting for cloud phone restart too long")
if self.is_ready():
break
time.sleep(1)
def run(self):
try:
self.check_config()
self.load_chinac_api()
self.phone_restart()
self.wait_for_ready()
2025-05-18 21:25:52 +08:00
self.wait_for_adb()
2025-05-18 21:25:06 +08:00
self.config.task_delay(server_update=True)
self.config.task_call('Restart')
2025-05-18 20:43:18 +08:00
except Exception as e:
2025-05-18 21:20:19 +08:00
logger.exception(e)
2025-05-18 21:25:06 +08:00
self.config.task_delay(server_update=True)
self.config.task_call('Restart')
2025-05-18 20:43:18 +08:00
return False
return True