feat.新增UUID回调字段

This commit is contained in:
MistEO
2022-03-04 22:56:52 +08:00
parent e91183dcef
commit efb04e2f9b
11 changed files with 96 additions and 21 deletions

View File

@@ -17,7 +17,7 @@ typedef void(ASST_CALL* AsstCallback)(int msg, const char* details, void* custom
/* Global Info */
InternalError = 0, // 内部错误
InitFailed, // 初始化失败
ConnectionError, // 连接相关错误
ConnectionInfo, // 连接相关信息
AllTasksCompleted, // 全部任务完成
/* TaskChain Info */
TaskChainError = 10000, // 任务链执行/识别错误
@@ -53,22 +53,33 @@ Todo
}
```
### ConnectionError
### ConnectionInfo
```jsonc
{
"what": string, // 错误类型
"why": string, // 错误原因
"details": object, // 错误详情
"uuid": string // 设备唯一码
}
```
#### 常见 `What` 字段
- `ConnectFailed`
连接失败
- `Connected`
已连接,注意此时的 `uuid` 字段值为空(下一步才是获取)
- `UuidGetted`
已获取到设备唯一码
### AllTasksCompleted
```jsonc
{
"taskchain": string, // 最后的任务链
"pre_taskchain": string // 上一个任务链
"pre_taskchain": string, // 上一个任务链
"uuid": string // 设备唯一码
}
```
@@ -96,7 +107,8 @@ Todo
```jsonc
{
"taskchain": string, // 当前的任务链
"pre_taskchain": string // 上一个任务链
"pre_taskchain": string, // 上一个任务链
"uuid": string // 设备唯一码
}
```
@@ -112,6 +124,7 @@ Todo
"class": string, // 子任务符号名
"taskchain": string, // 当前任务链
"details": object, // 详情
"uuid": string // 设备唯一码
}
```
@@ -184,6 +197,7 @@ Todo
"class": string, // 子任务类型
"what": string, // 信息类型
"details": object, // 信息详情
"uuid": string, // 设备唯一码
}
```

View File

@@ -24,6 +24,7 @@
"devices": "[Adb] devices",
"addressRegex": "(.+)\tdevice",
"connect": "[Adb] connect [Address]",
"uuid": "[Adb] -s [Address] shell \"service call iphonesubinfo 1 | awk -F \\''\\' '{print $2}' | sed '1 d' | tr -d '.' | awk '{print}' ORS=\"",
"click": "[Adb] -s [Address] shell input tap [x] [y]",
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init=",
@@ -36,6 +37,7 @@
"devices": "[Adb] devices",
"addressRegex": "(.+)\tdevice",
"connect": "[Adb] connect [Address]",
"uuid": "[Adb] -s [Address] shell \"service call iphonesubinfo 1 | awk -F \\''\\' '{print $2}' | sed '1 d' | tr -d '.' | awk '{print}' ORS=\"",
"click": "[Adb] -s [Address] shell input tap [x] [y]",
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
@@ -48,6 +50,7 @@
"devices": "[Adb] devices",
"addressRegex": "(.+)\tdevice",
"connect": "[Adb] connect [Address]",
"uuid": "[Adb] -s [Address] shell \"service call iphonesubinfo 1 | awk -F \\''\\' '{print $2}' | sed '1 d' | tr -d '.' | awk '{print}' ORS=\"",
"click": "[Adb] -s [Address] shell input tap [x] [y]",
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
@@ -60,6 +63,7 @@
"devices": "[Adb] devices",
"addressRegex": "(.+)\tdevice",
"connect": "[Adb] connect [Address]",
"uuid": "[Adb] -s [Address] shell \"service call iphonesubinfo 1 | awk -F \\''\\' '{print $2}' | sed '1 d' | tr -d '.' | awk '{print}' ORS=\"",
"click": "[Adb] -s [Address] shell input tap [x] [y]",
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
@@ -72,6 +76,7 @@
"devices": "[Adb] devices",
"addressRegex": "(.+)\tdevice",
"connect": "[Adb] connect [Address]",
"uuid": "[Adb] -s [Address] shell \"service call iphonesubinfo 1 | awk -F \\''\\' '{print $2}' | sed '1 d' | tr -d '.' | awk '{print}' ORS=\"",
"click": "[Adb] -s [Address] shell input tap [x] [y]",
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
@@ -84,6 +89,7 @@
"devices": "[Adb] devices",
"addressRegex": "(.+)\tdevice",
"connect": "[Adb] connect [Address]",
"uuid": "[Adb] -s [Address] shell \"service call iphonesubinfo 1 | awk -F \\''\\' '{print $2}' | sed '1 d' | tr -d '.' | awk '{print}' ORS=\"",
"click": "[Adb] -s [Address] shell input tap [x] [y]",
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init=",
@@ -97,6 +103,7 @@
"addressRegex": "(.+)\tdevice",
"connect": "[Adb] connect [Address]",
"displayId": "[Adb] -s [Address] shell dumpsys display | grep mUniqueId=virtual:com.microsoft.windows.systemapp:com.hypergryph.arknights",
"uuid": "[Adb] -s [Address] shell \"service call iphonesubinfo 1 | awk -F \\''\\' '{print $2}' | sed '1 d' | tr -d '.' | awk '{print}' ORS=\"",
"click": "[Adb] -s [Address] shell input -d [DisplayId] tap [x] [y]",
"swipe": "[Adb] -s [Address] shell input -d [DisplayId] swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s [Address] shell \"wm size -d [DisplayId]\"",

View File

@@ -87,6 +87,9 @@ bool asst::Assistant::connect(const std::string& adb_path, const std::string& ad
stop(false);
bool ret = m_ctrler->connect(adb_path, address, config.empty() ? "General" : config);
if (ret) {
m_uuid = m_ctrler->get_uuid();
}
m_inited = ret;
return ret;
}
@@ -497,7 +500,7 @@ void Assistant::working_proc()
std::string cur_taskchain = task_ptr->get_task_chain();
std::string next_taskchain = m_tasks_queue.empty() ? std::string() : m_tasks_queue.front()->get_task_chain();
json::value callback_json = json::object {
json::value callback_json = json::object{
{ "taskchain", cur_taskchain },
{ "pre_taskchain", pre_taskchain }
};
@@ -563,20 +566,21 @@ void Assistant::msg_proc()
void Assistant::task_callback(AsstMsg msg, const json::value& detail, void* custom_arg)
{
Log.trace("Assistant::task_callback |", msg, detail.to_string());
Assistant* p_this = (Assistant*)custom_arg;
json::value more_detail = detail;
more_detail["uuid"] = p_this->m_uuid;
switch (msg) {
case AsstMsg::InternalError:
case AsstMsg::InitFailed:
case AsstMsg::ConnectionError:
p_this->stop(false);
break;
default:
break;
}
Log.trace("Assistant::task_callback |", msg, more_detail.to_string());
// 加入回调消息队列,由回调消息线程外抛给外部
p_this->append_callback(msg, std::move(more_detail));
}

View File

@@ -98,8 +98,9 @@ namespace asst
void append_callback(AsstMsg msg, json::value detail);
void clear_cache();
bool m_inited = false;
std::string m_dirname;
bool m_inited = false;
std::string m_uuid;
std::shared_ptr<Controller> m_ctrler;
@@ -109,13 +110,14 @@ namespace asst
void* m_callback_arg = nullptr;
bool m_thread_idle = true;
std::thread m_working_thread;
std::mutex m_mutex;
std::condition_variable m_condvar;
std::thread m_msg_thread;
std::queue<std::pair<AsstMsg, json::value>> m_msg_queue;
std::mutex m_msg_mutex;
std::condition_variable m_msg_condvar;
std::thread m_msg_thread;
std::thread m_working_thread;
};
}

View File

@@ -13,7 +13,7 @@ namespace asst
/* Global Info */
InternalError = 0, // 内部错误
InitFailed, // 初始化失败
ConnectionError, // 连接相关错误
ConnectionInfo, // 连接相关错误
AllTasksCompleted, // 全部任务完成
/* TaskChain Info */
TaskChainError = 10000, // 任务链执行/识别错误
@@ -33,7 +33,7 @@ namespace asst
/* Global Info */
{ AsstMsg::InternalError, "InternalError" },
{ AsstMsg::InitFailed, "InitFailed" },
{ AsstMsg::ConnectionError, "ConnectionError" },
{ AsstMsg::ConnectionInfo, "ConnectionInfo" },
{ AsstMsg::AllTasksCompleted, "AllTasksCompleted" },
/* TaskChain Info */
{ AsstMsg::TaskChainError, "TaskChainError" },

View File

@@ -621,9 +621,10 @@ bool asst::Controller::connect(const std::string & adb_path, const std::string &
{"why", "ConfigNotFound"},
{"details", json::object{}}
};
m_callback(AsstMsg::ConnectionError, info, m_callback_arg);
m_callback(AsstMsg::ConnectionInfo, info, m_callback_arg);
return false;
}
const auto adb_cfg = std::move(adb_ret.value());
std::string display_id;
@@ -650,9 +651,17 @@ bool asst::Controller::connect(const std::string & adb_path, const std::string &
{"why", "Connection command failed to exec"},
{"details", json::object{}}
};
m_callback(AsstMsg::ConnectionError, info, m_callback_arg);
m_callback(AsstMsg::ConnectionInfo, info, m_callback_arg);
return false;
}
else {
json::value info = json::object{
{"what", "Connected"},
{"why", ""},
{"details", json::object{}}
};
m_callback(AsstMsg::ConnectionInfo, info, m_callback_arg);
}
}
// 按需获取display ID 信息
@@ -686,7 +695,7 @@ bool asst::Controller::connect(const std::string & adb_path, const std::string &
{"why", "Display command failed to exec"},
{"details", json::object{}}
};
m_callback(AsstMsg::ConnectionError, info, m_callback_arg);
m_callback(AsstMsg::ConnectionInfo, info, m_callback_arg);
return false;
}
@@ -726,6 +735,36 @@ bool asst::Controller::connect(const std::string & adb_path, const std::string &
}
}
/* get uuid (imei) */
{
auto uuid_ret = call_command(cmd_replace(adb_cfg.uuid));
if (!uuid_ret) {
json::value info = json::object{
{"what", "ConnectFailed"},
{"why", "Uuid command failed to exec"},
{"details", json::object{}}
};
m_callback(AsstMsg::ConnectionInfo, info, m_callback_arg);
return false;
}
auto& uuid_result = uuid_ret.value();
std::string uuid_str(
std::make_move_iterator(uuid_result.begin()),
std::make_move_iterator(uuid_result.end()));
uuid_str.erase(std::remove(uuid_str.begin(), uuid_str.end(), ' '), uuid_str.end());
m_uuid = std::move(uuid_str);
json::value info = json::object{
{"what", "UuidGetted"},
{"why", ""},
{"details", json::object {
{ "uuid", m_uuid}
}}
};
m_callback(AsstMsg::ConnectionInfo, info, m_callback_arg);
}
m_adb.click = cmd_replace(adb_cfg.click);
m_adb.swipe = cmd_replace(adb_cfg.swipe);
m_adb.screencap_raw_with_gzip = cmd_replace(adb_cfg.screencap_raw_with_gzip);
@@ -735,6 +774,11 @@ bool asst::Controller::connect(const std::string & adb_path, const std::string &
return true;
}
const std::string& asst::Controller::get_uuid() const
{
return m_uuid;
}
cv::Mat asst::Controller::get_image()
{
// 有些模拟器adb偶尔会莫名其妙截图失败多试几次
@@ -747,7 +791,7 @@ cv::Mat asst::Controller::get_image()
return get_resized_image();
}
std::vector<uchar> asst::Controller::get_image_encode()
std::vector<uchar> asst::Controller::get_image_encode() const
{
cv::Mat img = get_resized_image();
std::vector<uchar> buf;

View File

@@ -32,8 +32,9 @@ namespace asst
bool connect(const std::string& adb_path, const std::string& address, const std::string& config);
const std::string& get_uuid() const;
cv::Mat get_image();
std::vector<uchar> get_image_encode();
std::vector<uchar> get_image_encode() const;
// 点击和滑动都是异步执行返回该任务的id
int click(const Point& p, bool block = true);
@@ -123,6 +124,7 @@ namespace asst
} screencap_method = ScreencapMethod::UnknownYet;
} m_adb;
std::string m_uuid;
std::pair<int, int> m_scale_size;
double m_control_scale = 1.0;
int m_width = 0;

View File

@@ -36,6 +36,7 @@ bool asst::GeneralConfiger::parse(const json::value& json)
adb.address_regex = cfg_json.at("addressRegex").as_string();
adb.connect = cfg_json.at("connect").as_string();
adb.display_id = cfg_json.get("displayId", std::string());
adb.uuid = cfg_json.at("uuid").as_string();
adb.click = cfg_json.at("click").as_string();
adb.swipe = cfg_json.at("swipe").as_string();
adb.display = cfg_json.at("display").as_string();

View File

@@ -49,6 +49,7 @@ namespace asst
std::string devices;
std::string connect;
std::string display_id;
std::string uuid;
std::string click;
std::string swipe;
std::string display;

View File

@@ -270,7 +270,7 @@ xcopy /e /y /i /c $(SolutionDir)3rdparty\resource $(TargetDir)resource</Command>
</ImportGroup>
<ProjectExtensions>
<VisualStudio>
<UserProperties />
<UserProperties _1_1_4_1_1_4resource_4config_1json__JsonSchema="" />
</VisualStudio>
</ProjectExtensions>
</Project>

View File

@@ -31,8 +31,8 @@ int main(int argc, char** argv)
char ch = 0;
while (ch != 'q') {
//AsstAppendRoguelike(ptr, 0);
AsstAppendDebug(ptr);
// AsstAppendStartUp(ptr);
//AsstAppendDebug(ptr);
AsstAppendStartUp(ptr);
//AsstAppendFight(ptr, "CE-5", 0, 0, 99999);
// AsstAppendVisit(ptr, true);
//{