// // Created by Matthew on 2025/3/4. // #include "HangYuCtrl.h" #include "netcamera.h" #include "httpclient.h" #include HangYuCtrl::~HangYuCtrl() { } bool HangYuCtrl::SetResolution(uint8_t channel, uint8_t streamID, uint32_t resX, uint32_t resY) { //流类型范围1-4,1为主流 char url[128] = { 0 }; snprintf(url, sizeof(url), "http://%s/Streams/%u/%u", m_ip.c_str(), (uint32_t)channel, (uint32_t)streamID); std::vector resData; int res = DoGetRequest(url, HTTP_AUTH_TYPE_BASIC, m_userName.c_str(), m_password.c_str(), m_netHandle, resData); if (res != 0 || resData.empty()) { return 0; } std::string xmlString(resData.begin(), resData.end()); size_t widthStart = xmlString.find(""); size_t widthEnd = xmlString.find(""); if (widthStart != std::string::npos && widthEnd != std::string::npos) { widthStart += std::string("").length(); xmlString.replace(widthStart, widthEnd - widthStart, std::to_string(resX)); } size_t heightStart = xmlString.find(""); size_t heightEnd = xmlString.find(""); if (heightStart != std::string::npos && heightEnd != std::string::npos) { heightStart += std::string("").length(); xmlString.replace(heightStart, heightEnd - heightStart, std::to_string(resY)); } res = DoPutRequest(url, HTTP_AUTH_TYPE_BASIC, m_userName.c_str(), m_password.c_str(), m_netHandle, xmlString.c_str(), resData); if (res != 0) { return 0; } return true; } bool HangYuCtrl::SetOsd(uint8_t channel, std::string osdstring, uint8_t pos) { // /LAPI/V1.0/Channels//Media/OSDs/Contents //左上OSD bool hasDateTime = (osdstring.find("$$DATETIME$$") != std::string::npos); size_t posi = osdstring.find("$$DATETIME$$"); if (posi != std::string::npos) { size_t endPos = posi + 12; while (endPos < osdstring.size() && osdstring[endPos] == ' ') { endPos++; } osdstring.erase(posi, endPos - posi); } char url[128] = { 0 }; snprintf(url, sizeof(url), "http://%s/Pictures/%u/MultiOSDV2", m_ip.c_str(), (uint32_t)channel); std::vector resData; std::replace(osdstring.begin(), osdstring.end(), '\n', '^'); string xmlString = "" + string(hasDateTime ? "true" : "false") + "801true"+ osdstring+ "8" + string(hasDateTime ? "24" : "0") + ""; int res = DoPutRequest(url, HTTP_AUTH_TYPE_BASIC, m_userName.c_str(), m_password.c_str(), m_netHandle, xmlString.c_str(), resData); return res; } void HangYuCtrl::EnableOsd(bool enable, uint8_t channel) { //航煜 只能显示时间和一个OSD char url[128] = { 0 }; snprintf(url, sizeof(url), "http://%s/Pictures/%u/MultiOSDV2", m_ip.c_str(), (uint32_t)channel); std::vector resData; int res = DoGetRequest(url, HTTP_AUTH_TYPE_BASIC, m_userName.c_str(), m_password.c_str(), m_netHandle, resData); if (res != 0 || resData.empty()) { return; } std::string xmlString(resData.begin(), resData.end()); std::string enableStartTag = ""; std::string enableEndTag = ""; size_t pos = 0; while ((pos = xmlString.find(enableStartTag, pos)) != std::string::npos) { size_t startPos = pos + enableStartTag.length(); size_t endPos = xmlString.find(enableEndTag, startPos); if (endPos == std::string::npos) { break; } std::string newValue = enable ? "true" : "false"; xmlString.replace(startPos, endPos - startPos, newValue); pos = endPos + enableEndTag.length(); } res = DoPutRequest(url, HTTP_AUTH_TYPE_BASIC, m_userName.c_str(), m_password.c_str(), m_netHandle, xmlString.c_str(), resData); if (res != 0) { // return; } } std::string HangYuCtrl::GetStreamingUrl(uint8_t channel) { // /LAPI/V1.0/Channels//Media/Video/Streams//LiveStreamURL?TransType=&TransProtocol= char url[128] = { 0 }; snprintf(url, sizeof(url), "http://%s/Streams/%u/1/Transport", m_ip.c_str(), (uint32_t)channel); std::vector resData; int res = 0; for (int idx = 0; idx < 10; idx++) { res = DoGetRequest(url, HTTP_AUTH_TYPE_BASIC, m_userName.c_str(), m_password.c_str(), m_netHandle, resData); if (res == 0 && !resData.empty()) { break; } } if (res != 0 || resData.empty()) { return ""; } resData.push_back(0); const char* start = strstr((const char*)&resData[0], ""); if (start == NULL) { return ""; } start += 9; const char* end = strstr(start, ""); if (end == NULL) { return ""; } return std::string(start, end); } bool HangYuCtrl::UpdateTime(time_t ts) { // /LAPI/V1.0/System/Time // // std::string reqData = ""; std::string url = "http://" + m_ip + "/System/Time"; std::vector resData; int res = DoPutRequest(url.c_str(), HTTP_AUTH_TYPE_BASIC, m_userName.c_str(), m_password.c_str(), m_netHandle, reqData.c_str(), resData); if (res != 0) { return false; } return true; } bool HangYuCtrl::TakePhoto(std::vector& img) { return false; } bool HangYuCtrl::TakeVideo(uint32_t duration, std::string path) { return false; }