海康ISAPI协议测试

测试接口:PUT /ISAPI/System/Video/inputs/channels/<channelID>/overlays

功能:配置指定通道字符叠加参数
说明:channelID选择1,认证方式为摘要认证

#include <iostream>
#include <string>
#include <cstring>
#include <curl/curl.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <chrono>

using namespace std;

class AuthType {
public:
    static const int BASIC = 1;
    static const int DIGEST = 2;
    static const int UNAUTHORISED = 3;
};

// libcurl 写回调函数
size_t write_callback(void* contents, size_t size, size_t nmemb, string* output) {
    size_t total_size = size * nmemb;
    output->append((char*)contents, total_size);
    return total_size;
}

// 解析返回的 XML 错误信息
void parse_error_xml(const string& answer_text) {
    xmlDocPtr doc = xmlParseMemory(answer_text.c_str(), answer_text.size());
    if (doc == nullptr) {
        cerr << "Failed to parse XML" << endl;
        return;
    }

    xmlNodePtr root_element = xmlDocGetRootElement(doc);
    if (root_element == nullptr) {
        cerr << "Empty XML response" << endl;
        xmlFreeDoc(doc);
        return;
    }

    xmlNodePtr statusNode = nullptr;
    xmlNodePtr subStatusNode = nullptr;
    for (xmlNodePtr node = root_element->children; node; node = node->next) {
        if (xmlStrcmp(node->name, BAD_CAST "statusString") == 0) {
            statusNode = node;
        }
        if (xmlStrcmp(node->name, BAD_CAST "subStatusCode") == 0) {
            subStatusNode = node;
        }
    }

    if (statusNode && subStatusNode) {
        const char* status = (const char*)xmlNodeGetContent(statusNode);
        const char* substatus = (const char*)xmlNodeGetContent(subStatusNode);
        cout << status << ": " << substatus << endl;
    } else {
        cout << "Invalid response XML: " << answer_text << endl;
    }

    xmlFreeDoc(doc);
}

// 检查响应的状态
bool check_status(const string& answer_text, const string& expected_status_text = "OK") {
    xmlDocPtr doc = xmlParseMemory(answer_text.c_str(), answer_text.size());
    if (doc == nullptr) {
        cerr << "Failed to parse XML" << endl;
        return false;
    }

    xmlNodePtr root_element = xmlDocGetRootElement(doc);
    if (root_element == nullptr) {
        cerr << "Empty XML response" << endl;
        xmlFreeDoc(doc);
        return false;
    }

    xmlNodePtr statusNode = nullptr;
    for (xmlNodePtr node = root_element->children; node; node = node->next) {
        if (xmlStrcmp(node->name, BAD_CAST "statusString") == 0) {
            statusNode = node;
        }
    }

    bool result = false;
    if (statusNode) {
        const char* status = (const char*)xmlNodeGetContent(statusNode);
        result = (status && strcmp(status, expected_status_text.c_str()) == 0);
    }

    xmlFreeDoc(doc);
    return result;
}

// 打印响应状态
void print_answer_status(const string& operation_text, const string& answer_text, const string& status_text) {
    if (check_status(answer_text, status_text)) {
        cout << operation_text << ": success" << endl;
    } else {
        cout << operation_text << " error, answer is:\n" << answer_text << endl;
        parse_error_xml(answer_text);
        throw runtime_error(operation_text + " failed");
    }
}

// 获取认证头
void get_auth_header(int auth_type, const string& username, const string& password, CURL* curl) {
    if (auth_type == AuthType::BASIC) {
        curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_easy_setopt(curl, CURLOPT_USERPWD, (username + ":" + password).c_str());
    } else if (auth_type == AuthType::DIGEST) {
        curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
        curl_easy_setopt(curl, CURLOPT_USERPWD, (username + ":" + password).c_str());
    }
}

// 发送请求并处理响应
void process_request(int auth_type, const string& cam_ip, const string& request_url, const string& password, const string& request_data, const string& operation, const string& expected_status_text = "OK") {
    CURL* curl;
    CURLcode res;
    string response_data;

    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();

    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, ("http://" + cam_ip + request_url).c_str());
        get_auth_header(auth_type, "admin", password, curl);

        // 设置请求为 PUT
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request_data.c_str());

        // 设置回调函数来处理响应
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_data);

        // 执行请求
        res = curl_easy_perform(curl);

        if (res != CURLE_OK) {
            cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << endl;
        } else {
            print_answer_status(operation, response_data, expected_status_text);
        }

        curl_easy_cleanup(curl);
    }

    curl_global_cleanup();
}

// 设置 OSD 信息
void set_osd(int auth_type, const string& cam_ip, const string& password, int speed) {
    const string osd_url = "/ISAPI/System/Video/inputs/channels/1/overlays";
    const string osd_xml = string(R"(
    <?xml version="1.0" encoding="UTF-8"?>
    <VideoOverlay>
    <normalizedScreenSize>
    <normalizedScreenWidth>704</normalizedScreenWidth>
    <normalizedScreenHeight>576</normalizedScreenHeight>
    </normalizedScreenSize>
    <attribute>
    <transparent>false</transparent>
    <flashing>false</flashing>
    </attribute>
    <fontSize>adaptive</fontSize>
    <TextOverlayList size="4">
    <TextOverlay>
    <id>1</id>
    <enabled>true</enabled>
    <positionX>30</positionX>
    <positionY>500</positionY>
    <displayText>==========</displayText>
    <OverlayRegion>
    <width>100</width>
    <height>50</height>
    </OverlayRegion>
    </TextOverlay>
    <TextOverlay>
    <id>2</id>
    <enabled>true</enabled>
    <positionX>30</positionX>
    <positionY>530</positionY>
    <displayText>)")+std::string("速度:")+std::to_string(speed)+std::string(" KM/H")+string(R"(</displayText>
    <OverlayRegion>
    <width>100</width>
    <height>100</height>
    </OverlayRegion>
    </TextOverlay>
    </TextOverlayList>
    </VideoOverlay>
    )");

    process_request(auth_type, cam_ip, osd_url, password, osd_xml, "OSD set");
}

int main() {
    try {
        while(true){
        // 调用函数设置 OSD 信息
        auto start = std::chrono::high_resolution_clock::now();
        time_t now;
        time(&now);
        set_osd(AuthType::DIGEST, "192.168.0.64", "password", now);
        auto end = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double> duration = end - start;
        std::cout << "Function execution time: " << duration.count() << " seconds" << std::endl;
        }
    } catch (const runtime_error& e) {
        cerr << "Error: " << e.what() << endl;
    }

    return 0;
}

效果图如下: 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值