| 127 | } |
| 128 | |
| 129 | bool AdbControlUnitMgr::screencap(cv::Mat& image) |
| 130 | { |
| 131 | using namespace std::chrono_literals; |
| 132 | |
| 133 | constexpr int kMaxReconnectTimes = 3; |
| 134 | constexpr int kMaxRescreencapTimes = 10; |
| 135 | constexpr auto kRescreencapDelay = 500ms; |
| 136 | constexpr auto kReconnectDelay = 5000ms; |
| 137 | |
| 138 | for (int i = 0; i < kMaxReconnectTimes; ++i) { |
| 139 | // 先检查连接状态,断开则直接重连,避免无谓的截图失败重试 |
| 140 | if (!connection_.is_alive()) { |
| 141 | LogWarn << "connection lost, re-connect"; |
| 142 | connection_.kill_server(); |
| 143 | if (!connect()) { |
| 144 | LogError << "re-connect failed" << VAR(i); |
| 145 | std::this_thread::sleep_for(i * kReconnectDelay); |
| 146 | continue; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | for (int j = 0; j < kMaxRescreencapTimes; ++j) { |
| 151 | if (_screencap(image)) { |
| 152 | screencap_available_ = true; |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | if (!screencap_available_) { |
| 157 | // first time |
| 158 | return false; |
| 159 | } |
| 160 | LogWarn << "re-screencap" << VAR(j); |
| 161 | std::this_thread::sleep_for(j * kRescreencapDelay); |
| 162 | } |
| 163 | |
| 164 | LogWarn << "screencap failed after retries, force re-connect" << VAR(i); |
| 165 | connection_.kill_server(); |
| 166 | if (!connect()) { |
| 167 | LogError << "re-connect failed" << VAR(i); |
| 168 | std::this_thread::sleep_for(i * kReconnectDelay); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | bool AdbControlUnitMgr::click(int x, int y) |
| 176 | { |
nothing calls this directly
no test coverage detected