From 97c8cc3038dbf9bb942dd178e328682b6e3fe863 Mon Sep 17 00:00:00 2001 From: Matthew Date: Sat, 3 May 2025 21:10:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A6=82=E6=9E=9Chttp=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E6=98=AF=E8=B6=85=E6=97=B6=E9=94=99=E8=AF=AF=EF=BC=8C=E5=88=99?= =?UTF-8?q?=E5=87=8F=E5=B0=91=E9=87=8D=E8=AF=95=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 避免耗电太高 --- app/src/main/cpp/PhoneDevice.cpp | 6 +++++- app/src/main/cpp/netcamera/HangYuCtrl.cpp | 6 ++---- app/src/main/cpp/netcamera/HikonCtrl.cpp | 2 +- app/src/main/cpp/netcamera/VendorCtrl.cpp | 6 ++++++ app/src/main/cpp/netcamera/VendorCtrl.h | 3 +++ app/src/main/cpp/netcamera/YuShiCtrl.cpp | 2 +- app/src/main/cpp/netcamera/httpclient.cpp | 13 ++++++++++--- app/src/main/cpp/netcamera/httpclient.h | 4 ++-- 8 files changed, 30 insertions(+), 12 deletions(-) diff --git a/app/src/main/cpp/PhoneDevice.cpp b/app/src/main/cpp/PhoneDevice.cpp index 3940f104..db62b7e3 100644 --- a/app/src/main/cpp/PhoneDevice.cpp +++ b/app/src/main/cpp/PhoneDevice.cpp @@ -1693,7 +1693,7 @@ bool CPhoneDevice::TakePhotoWithNetCamera(IDevice::PHOTO_INFO& localPhotoInfo, c std::vector img; cv::Mat rgb; - bool netCaptureResult = false; + int netCaptureResult = 0; std::string lastError; int idx = 0; vendorCtrl->SetResolution(localPhotoInfo.channel, localPhotoInfo.cameraId, localPhotoInfo.width, localPhotoInfo.height); @@ -1727,6 +1727,10 @@ bool CPhoneDevice::TakePhotoWithNetCamera(IDevice::PHOTO_INFO& localPhotoInfo, c break; } } + if (vendorCtrl->IsTimeout()) + { + idx += 9; + } std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } diff --git a/app/src/main/cpp/netcamera/HangYuCtrl.cpp b/app/src/main/cpp/netcamera/HangYuCtrl.cpp index d8c29226..c852346f 100644 --- a/app/src/main/cpp/netcamera/HangYuCtrl.cpp +++ b/app/src/main/cpp/netcamera/HangYuCtrl.cpp @@ -117,8 +117,6 @@ void HangYuCtrl::EnableOsd(bool enable, uint8_t channel) { // return; } - - } std::string HangYuCtrl::GetStreamingUrl(uint8_t channel) @@ -200,7 +198,7 @@ bool HangYuCtrl::TakePhoto(uint8_t streamID, std::vector& img) char url[128] = { 0 }; snprintf(url, sizeof(url), "http://%s/Snapshot/%u/1/RemoteImageCaptureV2?ImageFormat=jpg", m_ip.c_str(), (uint32_t)streamID); - int nRet = DoGetRequest(url, HTTP_AUTH_TYPE_BASIC, m_userName.c_str(), m_password.c_str(), m_netHandle, img); + int nRet = DoGetRequest(url, HTTP_AUTH_TYPE_BASIC, m_userName.c_str(), m_password.c_str(), m_netHandle, img, &m_lastErrorCode); if (0 == nRet) { bool qualityDowngraded = false; @@ -210,7 +208,7 @@ bool HangYuCtrl::TakePhoto(uint8_t streamID, std::vector& img) qualityDowngraded = DowngradeQuality(originalConfig); XYLOG(XYLOG_SEVERITY_INFO,"Reduce Img Quality"); } - nRet = DoGetRequest(url, HTTP_AUTH_TYPE_BASIC, m_userName.c_str(), m_password.c_str(), m_netHandle, img); + nRet = DoGetRequest(url, HTTP_AUTH_TYPE_BASIC, m_userName.c_str(), m_password.c_str(), m_netHandle, img, &m_lastErrorCode); if (!originalConfig.empty()) { UpdateQuality(originalConfig); diff --git a/app/src/main/cpp/netcamera/HikonCtrl.cpp b/app/src/main/cpp/netcamera/HikonCtrl.cpp index 591fb67a..eaa194d5 100644 --- a/app/src/main/cpp/netcamera/HikonCtrl.cpp +++ b/app/src/main/cpp/netcamera/HikonCtrl.cpp @@ -194,7 +194,7 @@ bool HikonCtrl::TakePhoto(uint8_t streamID, std::vector& img) { char url[128] = { 0 }; snprintf(url, sizeof(url), "http://%s/ISAPI/Streaming/channels/1/picture?", m_ip.c_str()); - int nRet = DoGetRequest(url, HTTP_AUTH_TYPE_DIGEST, m_userName.c_str(), m_password.c_str(), m_netHandle, img); + int nRet = DoGetRequest(url, HTTP_AUTH_TYPE_DIGEST, m_userName.c_str(), m_password.c_str(), m_netHandle, img, &m_lastErrorCode); return nRet == 0; } diff --git a/app/src/main/cpp/netcamera/VendorCtrl.cpp b/app/src/main/cpp/netcamera/VendorCtrl.cpp index a914b22c..8d268a12 100644 --- a/app/src/main/cpp/netcamera/VendorCtrl.cpp +++ b/app/src/main/cpp/netcamera/VendorCtrl.cpp @@ -2,6 +2,7 @@ // Created by Matthew on 2025/3/4. // #include "VendorCtrl.h" +#include VendorCtrl::VendorCtrl(const std::string& ip, const std::string& userName, const std::string& password, uint8_t channel, net_handle_t netHandle, bool syncTime/* = true*/) : m_ip(ip), m_userName(userName), m_password(password), m_channel(channel), m_netHandle(netHandle) @@ -22,4 +23,9 @@ std::string VendorCtrl::CvtJSONToString(const Json::Value& data) builder["indentation"] = ""; #endif return Json::writeString(builder, data); +} + +bool VendorCtrl::IsTimeout() const +{ + return m_lastErrorCode == CURLE_OPERATION_TIMEDOUT; } \ No newline at end of file diff --git a/app/src/main/cpp/netcamera/VendorCtrl.h b/app/src/main/cpp/netcamera/VendorCtrl.h index 60af235e..d3d33060 100644 --- a/app/src/main/cpp/netcamera/VendorCtrl.h +++ b/app/src/main/cpp/netcamera/VendorCtrl.h @@ -30,6 +30,8 @@ public: void UpdateNetHandle(net_handle_t netHandle) { m_netHandle = netHandle; } + int GetLastError() const { return m_lastErrorCode; } + bool IsTimeout() const; protected: @@ -41,6 +43,7 @@ protected: std::string m_password; uint8_t m_channel; net_handle_t m_netHandle; + int m_lastErrorCode; }; diff --git a/app/src/main/cpp/netcamera/YuShiCtrl.cpp b/app/src/main/cpp/netcamera/YuShiCtrl.cpp index 61aac1a3..46ebf67a 100644 --- a/app/src/main/cpp/netcamera/YuShiCtrl.cpp +++ b/app/src/main/cpp/netcamera/YuShiCtrl.cpp @@ -162,7 +162,7 @@ bool YuShiCtrl::TakePhoto(uint8_t streamID, std::vector& img) int streamSid = 0; // should put into config snprintf(url, sizeof(url), "/LAPI/V1.0/Channels/%u/Media/Video/Streams/%d/Snapshot", (uint32_t)streamID, streamSid); - int nRet = DoGetRequest(url, HTTP_AUTH_TYPE_DIGEST, m_userName.c_str(), m_password.c_str(), m_netHandle, img); + int nRet = DoGetRequest(url, HTTP_AUTH_TYPE_DIGEST, m_userName.c_str(), m_password.c_str(), m_netHandle, img, &m_lastErrorCode); return nRet == 0; } diff --git a/app/src/main/cpp/netcamera/httpclient.cpp b/app/src/main/cpp/netcamera/httpclient.cpp index a7e73ad4..f99d5ed4 100644 --- a/app/src/main/cpp/netcamera/httpclient.cpp +++ b/app/src/main/cpp/netcamera/httpclient.cpp @@ -21,7 +21,6 @@ static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid) static int SockOptCallback(void *clientp, curl_socket_t curlfd, curlsocktype purpose) { - net_handle_t netHandle = *((net_handle_t *)clientp); int res = android_setsocknetwork(netHandle, curlfd); @@ -34,7 +33,7 @@ static int SockOptCallback(void *clientp, curl_socket_t curlfd, curlsocktype pur return res == 0 ? CURL_SOCKOPT_OK : CURL_SOCKOPT_ERROR; } -int DoGetRequest(const char* url, int authType, const char* userName, const char* password, net_handle_t netHandle, std::vector& data) +int DoGetRequest(const char* url, int authType, const char* userName, const char* password, net_handle_t netHandle, std::vector& data, int* curlResVal/* = NULL*/) { CURLcode nRet; std::string auth; @@ -88,6 +87,10 @@ int DoGetRequest(const char* url, int authType, const char* userName, const char curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10); nRet = curl_easy_perform(curl); + if (curlResVal != NULL) + { + *curlResVal = nRet; + } long responseCode = 0; if (CURLE_OK == nRet) @@ -122,7 +125,7 @@ int DoGetRequest(const char* url, int authType, const char* userName, const char return ((0 == nRet) && (responseCode == 200)) ? 0 : 1; } -int DoPutRequest(const char* url, int authType, const char* userName, const char* password, net_handle_t netHandle, const char* contents, std::vector& data) +int DoPutRequest(const char* url, int authType, const char* userName, const char* password, net_handle_t netHandle, const char* contents, std::vector& data, int* curlResVal/* = NULL*/) { std::string auth; @@ -171,6 +174,10 @@ int DoPutRequest(const char* url, int authType, const char* userName, const char curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10); CURLcode nRet = curl_easy_perform(curl); + if (curlResVal != NULL) + { + *curlResVal = nRet; + } if (CURLE_OK != nRet) { printf("GET err=%d", nRet); diff --git a/app/src/main/cpp/netcamera/httpclient.h b/app/src/main/cpp/netcamera/httpclient.h index fe57564c..1d24d87b 100644 --- a/app/src/main/cpp/netcamera/httpclient.h +++ b/app/src/main/cpp/netcamera/httpclient.h @@ -18,7 +18,7 @@ bool setIPAddress(const char *if_name, const char *ip_addr, const char *net_mask, const char *gateway_addr); -int DoGetRequest(const char* url, int authType, const char* userName, const char* password, net_handle_t netHandle, std::vector& data); -int DoPutRequest(const char* url, int authType, const char* userName, const char* password, net_handle_t netHandle, const char* contents, std::vector& data); +int DoGetRequest(const char* url, int authType, const char* userName, const char* password, net_handle_t netHandle, std::vector& data, int* curlResVal = NULL); +int DoPutRequest(const char* url, int authType, const char* userName, const char* password, net_handle_t netHandle, const char* contents, std::vector& data, int* curlResVal = NULL); #endif // __HTTP_CLIENT__ \ No newline at end of file