From efb04e2f9bd9ccb1d2242054f7df7cc4675aebff Mon Sep 17 00:00:00 2001 From: MistEO Date: Fri, 4 Mar 2022 22:56:52 +0800 Subject: [PATCH] =?UTF-8?q?feat.=E6=96=B0=E5=A2=9EUUID=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/回调消息协议.md | 22 +++++++++--- resource/config.json | 7 ++++ src/MeoAssistant/Assistant.cpp | 12 ++++--- src/MeoAssistant/Assistant.h | 8 +++-- src/MeoAssistant/AsstMsg.h | 4 +-- src/MeoAssistant/Controller.cpp | 52 ++++++++++++++++++++++++--- src/MeoAssistant/Controller.h | 4 ++- src/MeoAssistant/GeneralConfiger.cpp | 1 + src/MeoAssistant/GeneralConfiger.h | 1 + src/MeoAssistant/MeoAssistant.vcxproj | 2 +- tools/TestCaller/main.cpp | 4 +-- 11 files changed, 96 insertions(+), 21 deletions(-) diff --git a/docs/回调消息协议.md b/docs/回调消息协议.md index fe7e5282a1..d6d0c5483c 100644 --- a/docs/回调消息协议.md +++ b/docs/回调消息协议.md @@ -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, // 设备唯一码 } ``` diff --git a/resource/config.json b/resource/config.json index a9d4b1ae05..5cff26a4e6 100644 --- a/resource/config.json +++ b/resource/config.json @@ -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]\"", diff --git a/src/MeoAssistant/Assistant.cpp b/src/MeoAssistant/Assistant.cpp index 2fe7d9cfc0..5449d0bd81 100644 --- a/src/MeoAssistant/Assistant.cpp +++ b/src/MeoAssistant/Assistant.cpp @@ -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)); } diff --git a/src/MeoAssistant/Assistant.h b/src/MeoAssistant/Assistant.h index 59cb297411..83c457d8c1 100644 --- a/src/MeoAssistant/Assistant.h +++ b/src/MeoAssistant/Assistant.h @@ -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 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> m_msg_queue; std::mutex m_msg_mutex; std::condition_variable m_msg_condvar; + + std::thread m_msg_thread; + std::thread m_working_thread; }; } diff --git a/src/MeoAssistant/AsstMsg.h b/src/MeoAssistant/AsstMsg.h index a6875d08b3..7afc753c65 100644 --- a/src/MeoAssistant/AsstMsg.h +++ b/src/MeoAssistant/AsstMsg.h @@ -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" }, diff --git a/src/MeoAssistant/Controller.cpp b/src/MeoAssistant/Controller.cpp index 4e5eeb68af..3c7692b264 100644 --- a/src/MeoAssistant/Controller.cpp +++ b/src/MeoAssistant/Controller.cpp @@ -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 asst::Controller::get_image_encode() +std::vector asst::Controller::get_image_encode() const { cv::Mat img = get_resized_image(); std::vector buf; diff --git a/src/MeoAssistant/Controller.h b/src/MeoAssistant/Controller.h index 04de3d7f8b..7ff9590655 100644 --- a/src/MeoAssistant/Controller.h +++ b/src/MeoAssistant/Controller.h @@ -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 get_image_encode(); + std::vector 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 m_scale_size; double m_control_scale = 1.0; int m_width = 0; diff --git a/src/MeoAssistant/GeneralConfiger.cpp b/src/MeoAssistant/GeneralConfiger.cpp index 2f328e24e4..b27b0e4c1a 100644 --- a/src/MeoAssistant/GeneralConfiger.cpp +++ b/src/MeoAssistant/GeneralConfiger.cpp @@ -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(); diff --git a/src/MeoAssistant/GeneralConfiger.h b/src/MeoAssistant/GeneralConfiger.h index 3c7b2aca4d..86129b4a35 100644 --- a/src/MeoAssistant/GeneralConfiger.h +++ b/src/MeoAssistant/GeneralConfiger.h @@ -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; diff --git a/src/MeoAssistant/MeoAssistant.vcxproj b/src/MeoAssistant/MeoAssistant.vcxproj index db85c68b84..e4c178d928 100644 --- a/src/MeoAssistant/MeoAssistant.vcxproj +++ b/src/MeoAssistant/MeoAssistant.vcxproj @@ -270,7 +270,7 @@ xcopy /e /y /i /c $(SolutionDir)3rdparty\resource $(TargetDir)resource - + \ No newline at end of file diff --git a/tools/TestCaller/main.cpp b/tools/TestCaller/main.cpp index ac6b8c2983..df035c8ea7 100644 --- a/tools/TestCaller/main.cpp +++ b/tools/TestCaller/main.cpp @@ -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); //{