#include "FromSuperBrain.h" #include "CurlHttp.h" #include "fmt.h" #include "GlobalVariable.h" FromSuperBrain::FromSuperBrain() { m_logger = spdlog::get("FromSuperBrain"); if(m_logger == nullptr) { fmt::print("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() { } /* 初始化地址信息 */ void FromSuperBrain::initSuperBrain(const std::string& url, const std::string& appKey, const std::string& appSecret) { m_url = url; m_appKey = appKey; m_appSecret = appSecret; } /* 获取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_TRACE(m_logger, "response:{}", nJson::parse(response).dump(4)); /* 解析json信息 */ try { nJson json0 = nJson::parse(response); auto code = json0["code"].get(); if(code != "0") { SPDLOG_LOGGER_ERROR(m_logger, "Get Token failed, URL: {}", m_url); SPDLOG_LOGGER_ERROR(m_logger, "code: {}", code); SPDLOG_LOGGER_ERROR(m_logger, "message: {}", json0["message"].get()); 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(...) { 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_TRACE(m_logger, "TaskTypeList: \n{}", nJson::parse(response).dump(4)); /* 解析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_TRACE(m_logger, "DeivceList: \n{}", nJson::parse(response).dump(4)); /* 解析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; } nJson json1 = json0["data"]; for(const auto& it : json1) { DeviceInfo info; 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 */ auto tmpPort = it["port"].is_null() ? "" : it["port"].get(); if(tmpPort.empty()) { info.DevicePort = 0; }else { info.DevicePort = std::stoi(tmpPort); } 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; }