#include "FromSuperBrain.h" #include "CurlHttp.h" #include "fmt.h" #include "GlobalVariable.h" FromSuperBrain::FromSuperBrain() { m_logger = spdlog::get("FromSuperBrain"); if(m_logger == nullptr) { SPDLOG_ERROR("FromSuperBrain logger is nullptr"); return; } m_url = "http://192.1.2.64:30000/vos"; m_appKey = "x1pcq13r"; m_appSecret = "Setyir9yxvSiFGB/wsv4YHWiNmrgeaZhzJKi7c7Gmk04Z5Kd/hCifL+0WnfCRDJF"; } FromSuperBrain::~FromSuperBrain() { } /* 获取token */ bool FromSuperBrain::getToken() { std::string response; /* fmt需要 {{ 才能输出一个 { */ std::string httpBody = fmt::format(R"({{"appSecret":"{}","appKey":"{}"}})", m_appSecret, m_appKey); std::vector vecHeader; vecHeader.push_back("Content-Type: application/json"); std::string url = m_url + m_tokenPath; // SPDLOG_LOGGER_DEBUG(m_logger, "url:{}", url); // SPDLOG_LOGGER_DEBUG(m_logger, "httpBody:{}", httpBody); if(!CurlHttp::Post(url, vecHeader, httpBody, response)) { SPDLOG_LOGGER_ERROR(m_logger, "{}", response); return false; } // SPDLOG_LOGGER_DEBUG(m_logger, "response:{}", response); /* 解析json信息 */ try { nJson json0 = nJson::parse(response); auto code = json0["code"].get(); if(code != "0") { SPDLOG_LOGGER_ERROR(m_logger, "Get Token failed"); return false; } json0["data"]["accessToken"].get_to(m_token); // SPDLOG_LOGGER_DEBUG(m_logger, "Token:{}", m_token); }catch (const nJson::parse_error& e) { SPDLOG_LOGGER_ERROR(m_logger,"解析 Token 数据失败:{}, 错误ID:{}",e.what(), e.id); return false; } catch (const nJson::type_error& e) { SPDLOG_LOGGER_ERROR(m_logger,"解析 Token 数据失败:{}, 错误ID:{}",e.what(), e.id); return false; } catch(const std::exception& e) { SPDLOG_LOGGER_ERROR(m_logger,"解析 Token 数据失败:{}",e.what()); return false; } catch(...) { SPDLOG_LOGGER_ERROR(m_logger,"解析 Token 数据失败"); return false; } return true; } /* 获取算法列表 */ bool FromSuperBrain::getTaskTypeList(std::vector& vecInfo) { if(m_token.empty()) { SPDLOG_LOGGER_ERROR(m_logger, "Token is empty"); return false; } std::string response; std::vector vecHeader; vecHeader.push_back(fmt::format("accessToken: {}", m_token)); std::string url = m_url + m_taskTypeListPath; if(!CurlHttp::Get(url, vecHeader, response)) { SPDLOG_LOGGER_ERROR(m_logger, "{}}", response); return false; } // SPDLOG_LOGGER_DEBUG(m_logger, "TaskTypeList: \n{}", response); /* 解析json信息 */ try { nJson json0 = nJson::parse(response); auto retCode = json0.at("code").get(); if(retCode != "0") { SPDLOG_LOGGER_ERROR(m_logger, "Get TaskTypeList failed"); return false; } auto data = json0.at("data"); for(auto& it : data) { // SPDLOG_LOGGER_DEBUG(m_logger, "muAiName:{}", it["muAiName"].get()); AlgorithmInfo info; info.ActionID = it.at("ability").get(); info.ActionName = it.at("muAiName").get(); info.ActionTaskID = it["taskTypeId"].get(); vecInfo.push_back(info); } } catch (const nJson::parse_error& e) { SPDLOG_LOGGER_ERROR(m_logger,"解析 算法列表 数据失败:{}, 错误ID:{}",e.what(), e.id); return false; } catch (const nJson::type_error& e) { SPDLOG_LOGGER_ERROR(m_logger,"解析 算法列表 数据失败:{}, 错误ID:{}",e.what(), e.id); return false; } catch(...) { SPDLOG_LOGGER_ERROR(m_logger,"解析 算法列表 数据失败"); return false; } return true; } /* 获取设备列表信息 */ bool FromSuperBrain::getDeviceList(std::vector& vecInfo) { if(m_token.empty()) { SPDLOG_LOGGER_ERROR(m_logger, "Token is empty"); return false; } std::string response; std::vector vecHeader; vecHeader.push_back(fmt::format("accessToken: {}", m_token)); std::string url = m_url + m_deviceListPath; if(!CurlHttp::Get(url, vecHeader, response)) { SPDLOG_LOGGER_ERROR(m_logger, "{}", response); return false; } // SPDLOG_LOGGER_DEBUG(m_logger, "DeivceList: \n{}", response); /* 解析JSON信息 */ try { nJson json0 = nJson::parse(response); auto retCode = json0.at("code").get(); if(retCode != "0") { SPDLOG_LOGGER_ERROR(m_logger, "Get DeviceList failed"); return false; } DeviceInfo info; nJson json1 = json0["data"]; for(const auto& it : json1) { info.DeviceID = it["deviceId"].get(); info.DeviceName = it["deviceName"].is_null() ? "" : it["deviceName"].get(); info.DeviceSerial = it["deviceSerial"].is_null() ? "" : it["deviceSerial"].get(); info.DeviceType = it["deviceType"].is_null() ? "" : it["deviceType"].get(); /* 这三个可能是null */ info.DevicePort = it["port"].is_null() ? 0 : it["port"].get(); info.UserAccount = it["userAccount"].is_null() ? "" : it["userAccount"].get(); info.UserPassword = it["userPWD"].is_null() ? "" : it["userPWD"].get(); /* 解析任务类型列表 */ nJson json2 = it["taskTypeList"]; for(const auto& it1 : json2) { AlgorithmInfo info1; info1.ActionID = it1["ability"].get(); info1.ActionName = it1["taskTypeName"].get(); info1.ActionTaskID = it1["taskTypeId"].get(); info.vecAlgorithmInfo.push_back(info1); } vecInfo.push_back(info); } } catch (const nJson::parse_error& e) { SPDLOG_LOGGER_ERROR(m_logger,"解析 设备列表 数据失败:{}, 错误ID:{}",e.what(), e.id); return false; } catch (const nJson::type_error& e) { SPDLOG_LOGGER_ERROR(m_logger,"解析 设备列表 数据失败:{}, 错误ID:{}",e.what(), e.id); return false; } catch(...) { SPDLOG_LOGGER_ERROR(m_logger,"解析 设备列表 数据失败"); return false; } return true; } /* 图片识别 */ bool FromSuperBrain::imageRecognition() { if(m_token.empty()) { SPDLOG_LOGGER_ERROR(m_logger, "Token is empty"); return false; } std::string response; std::vector vecHeader; vecHeader.push_back(fmt::format("accessToken: {}", m_token)); /* 拼接地址 */ std::string url = m_url + m_imageRecognitionPath; if(!CurlHttp::Get(url, vecHeader, response)) { SPDLOG_LOGGER_ERROR(m_logger, "Get imageRecognition failed"); return false; } SPDLOG_LOGGER_DEBUG(m_logger, "imageRecognition: \n{}", response); return true; }