123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- #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<std::string> 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<std::string>();
- 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<AlgorithmInfo>& vecInfo)
- {
- if(m_token.empty())
- {
- SPDLOG_LOGGER_ERROR(m_logger, "Token is empty");
- return false;
- }
- std::string response;
- std::vector<std::string> 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<std::string>();
- 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<std::string>());
- AlgorithmInfo info;
- info.ActionID = it.at("ability").get<std::string>();
- info.ActionName = it.at("muAiName").get<std::string>();
- info.ActionTaskID = it["taskTypeId"].get<int>();
- 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<DeviceInfo>& vecInfo)
- {
- if(m_token.empty())
- {
- SPDLOG_LOGGER_ERROR(m_logger, "Token is empty");
- return false;
- }
- std::string response;
- std::vector<std::string> 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<std::string>();
- 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<int>();
- info.DeviceName = it["deviceName"].is_null() ? "" : it["deviceName"].get<std::string>();
- info.DeviceSerial = it["deviceSerial"].is_null() ? "" : it["deviceSerial"].get<std::string>();
- info.DeviceType = it["deviceType"].is_null() ? "" : it["deviceType"].get<std::string>();
- /* 这三个可能是null */
- info.DevicePort = it["port"].is_null() ? 0 : it["port"].get<int>();
- info.UserAccount = it["userAccount"].is_null() ? "" : it["userAccount"].get<std::string>();
- info.UserPassword = it["userPWD"].is_null() ? "" : it["userPWD"].get<std::string>();
- /* 解析任务类型列表 */
- nJson json2 = it["taskTypeList"];
- for(const auto& it1 : json2)
- {
- AlgorithmInfo info1;
- info1.ActionID = it1["ability"].get<std::string>();
- info1.ActionName = it1["taskTypeName"].get<std::string>();
- info1.ActionTaskID = it1["taskTypeId"].get<int>();
- 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<std::string> 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;
- }
|