FromSuperBrain.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include "FromSuperBrain.h"
  2. #include "CurlHttp.h"
  3. #include "fmt.h"
  4. #include "GlobalVariable.h"
  5. FromSuperBrain::FromSuperBrain()
  6. {
  7. m_logger = spdlog::get("FromSuperBrain");
  8. if(m_logger == nullptr)
  9. {
  10. fmt::print("FromSuperBrain logger is nullptr");
  11. return;
  12. }
  13. // m_url = "http://192.1.2.64:30000/vos";
  14. // m_appKey = "x1pcq13r";
  15. // m_appSecret = "Setyir9yxvSiFGB/wsv4YHWiNmrgeaZhzJKi7c7Gmk04Z5Kd/hCifL+0WnfCRDJF";
  16. }
  17. FromSuperBrain::~FromSuperBrain()
  18. {
  19. }
  20. /* 初始化地址信息 */
  21. void FromSuperBrain::initSuperBrain(const std::string& url, const std::string& appKey, const std::string& appSecret)
  22. {
  23. m_url = url;
  24. m_appKey = appKey;
  25. m_appSecret = appSecret;
  26. }
  27. /* 获取token */
  28. bool FromSuperBrain::getToken()
  29. {
  30. std::string response;
  31. /* fmt需要 {{ 才能输出一个 { */
  32. std::string httpBody = fmt::format(R"({{"appSecret":"{}","appKey":"{}"}})", m_appSecret, m_appKey);
  33. std::vector<std::string> vecHeader;
  34. vecHeader.push_back("Content-Type: application/json");
  35. std::string url = m_url + m_tokenPath;
  36. // SPDLOG_LOGGER_DEBUG(m_logger, "url:{}", url);
  37. // SPDLOG_LOGGER_DEBUG(m_logger, "httpBody:{}", httpBody);
  38. if(!CurlHttp::Post(url, vecHeader, httpBody, response))
  39. {
  40. SPDLOG_LOGGER_ERROR(m_logger, "{}", response);
  41. return false;
  42. }
  43. // SPDLOG_LOGGER_TRACE(m_logger, "response:{}", nJson::parse(response).dump(4));
  44. /* 解析json信息 */
  45. try
  46. {
  47. nJson json0 = nJson::parse(response);
  48. auto code = json0["code"].get<std::string>();
  49. if(code != "0")
  50. {
  51. SPDLOG_LOGGER_ERROR(m_logger, "Get Token failed, URL: {}", m_url);
  52. SPDLOG_LOGGER_ERROR(m_logger, "code: {}", code);
  53. SPDLOG_LOGGER_ERROR(m_logger, "message: {}", json0["message"].get<std::string>());
  54. return false;
  55. }
  56. json0["data"]["accessToken"].get_to(m_token);
  57. // SPDLOG_LOGGER_DEBUG(m_logger, "Token:{}", m_token);
  58. }catch (const nJson::parse_error& e) {
  59. SPDLOG_LOGGER_ERROR(m_logger,"解析 Token 数据失败:{}, 错误ID:{}",e.what(), e.id);
  60. return false;
  61. }
  62. catch (const nJson::type_error& e) {
  63. SPDLOG_LOGGER_ERROR(m_logger,"解析 Token 数据失败:{}, 错误ID:{}",e.what(), e.id);
  64. return false;
  65. }
  66. catch(...)
  67. {
  68. SPDLOG_LOGGER_ERROR(m_logger,"解析 Token 数据失败");
  69. return false;
  70. }
  71. return true;
  72. }
  73. /* 获取算法列表 */
  74. bool FromSuperBrain::getTaskTypeList(std::vector<AlgorithmInfo>& vecInfo)
  75. {
  76. if(m_token.empty())
  77. {
  78. SPDLOG_LOGGER_ERROR(m_logger, "Token is empty");
  79. return false;
  80. }
  81. std::string response;
  82. std::vector<std::string> vecHeader;
  83. vecHeader.push_back(fmt::format("accessToken: {}", m_token));
  84. std::string url = m_url + m_taskTypeListPath;
  85. if(!CurlHttp::Get(url, vecHeader, response))
  86. {
  87. SPDLOG_LOGGER_ERROR(m_logger, "{}}", response);
  88. return false;
  89. }
  90. // SPDLOG_LOGGER_TRACE(m_logger, "TaskTypeList: \n{}", nJson::parse(response).dump(4));
  91. /* 解析json信息 */
  92. try
  93. {
  94. nJson json0 = nJson::parse(response);
  95. auto retCode = json0.at("code").get<std::string>();
  96. if(retCode != "0")
  97. {
  98. SPDLOG_LOGGER_ERROR(m_logger, "Get TaskTypeList failed");
  99. return false;
  100. }
  101. auto data = json0.at("data");
  102. for(auto& it : data)
  103. {
  104. // SPDLOG_LOGGER_DEBUG(m_logger, "muAiName:{}", it["muAiName"].get<std::string>());
  105. AlgorithmInfo info;
  106. info.ActionID = it.at("ability").get<std::string>();
  107. info.ActionName = it.at("muAiName").get<std::string>();
  108. info.ActionTaskID = it["taskTypeId"].get<int>();
  109. vecInfo.push_back(info);
  110. }
  111. }
  112. catch (const nJson::parse_error& e) {
  113. SPDLOG_LOGGER_ERROR(m_logger,"解析 算法列表 数据失败:{}, 错误ID:{}",e.what(), e.id);
  114. return false;
  115. }
  116. catch (const nJson::type_error& e) {
  117. SPDLOG_LOGGER_ERROR(m_logger,"解析 算法列表 数据失败:{}, 错误ID:{}",e.what(), e.id);
  118. return false;
  119. }
  120. catch(...) {
  121. SPDLOG_LOGGER_ERROR(m_logger,"解析 算法列表 数据失败");
  122. return false;
  123. }
  124. return true;
  125. }
  126. /* 获取设备列表信息 */
  127. bool FromSuperBrain::getDeviceList(std::vector<DeviceInfo>& vecInfo)
  128. {
  129. if(m_token.empty())
  130. {
  131. SPDLOG_LOGGER_ERROR(m_logger, "Token is empty");
  132. return false;
  133. }
  134. std::string response;
  135. std::vector<std::string> vecHeader;
  136. vecHeader.push_back(fmt::format("accessToken: {}", m_token));
  137. std::string url = m_url + m_deviceListPath;
  138. if(!CurlHttp::Get(url, vecHeader, response))
  139. {
  140. SPDLOG_LOGGER_ERROR(m_logger, "{}", response);
  141. return false;
  142. }
  143. // SPDLOG_LOGGER_TRACE(m_logger, "DeivceList: \n{}", nJson::parse(response).dump(4));
  144. /* 解析JSON信息 */
  145. try
  146. {
  147. nJson json0 = nJson::parse(response);
  148. auto retCode = json0.at("code").get<std::string>();
  149. if(retCode != "0")
  150. {
  151. SPDLOG_LOGGER_ERROR(m_logger, "Get DeviceList failed");
  152. return false;
  153. }
  154. nJson json1 = json0["data"];
  155. for(const auto& it : json1)
  156. {
  157. DeviceInfo info;
  158. info.DeviceID = it["deviceId"].get<int>();
  159. info.DeviceName = it["deviceName"].is_null() ? "" : it["deviceName"].get<std::string>();
  160. info.DeviceSerial = it["deviceSerial"].is_null() ? "" : it["deviceSerial"].get<std::string>();
  161. info.DeviceType = it["deviceType"].is_null() ? "" : it["deviceType"].get<std::string>();
  162. /* 这三个可能是null */
  163. auto tmpPort = it["port"].is_null() ? "" : it["port"].get<std::string>();
  164. if(tmpPort.empty())
  165. {
  166. info.DevicePort = 0;
  167. }else {
  168. info.DevicePort = std::stoi(tmpPort);
  169. }
  170. info.UserAccount = it["userAccount"].is_null() ? "" : it["userAccount"].get<std::string>();
  171. info.UserPassword = it["userPWD"].is_null() ? "" : it["userPWD"].get<std::string>();
  172. /* 解析任务类型列表 */
  173. nJson json2 = it["taskTypeList"];
  174. for(const auto& it1 : json2)
  175. {
  176. AlgorithmInfo info1;
  177. info1.ActionID = it1["ability"].get<std::string>();
  178. info1.ActionName = it1["taskTypeName"].get<std::string>();
  179. info1.ActionTaskID = it1["taskTypeId"].get<int>();
  180. info.vecAlgorithmInfo.push_back(info1);
  181. }
  182. vecInfo.push_back(info);
  183. }
  184. }
  185. catch (const nJson::parse_error& e) {
  186. SPDLOG_LOGGER_ERROR(m_logger,"解析 设备列表 数据失败:{}, 错误ID:{}",e.what(), e.id);
  187. return false;
  188. }
  189. catch (const nJson::type_error& e) {
  190. SPDLOG_LOGGER_ERROR(m_logger,"解析 设备列表 数据失败:{}, 错误ID:{}",e.what(), e.id);
  191. return false;
  192. }
  193. catch(...) {
  194. SPDLOG_LOGGER_ERROR(m_logger,"解析 设备列表 数据失败");
  195. return false;
  196. }
  197. return true;
  198. }
  199. /* 图片识别 */
  200. bool FromSuperBrain::imageRecognition()
  201. {
  202. if(m_token.empty())
  203. {
  204. SPDLOG_LOGGER_ERROR(m_logger, "Token is empty");
  205. return false;
  206. }
  207. std::string response;
  208. std::vector<std::string> vecHeader;
  209. vecHeader.push_back(fmt::format("accessToken: {}", m_token));
  210. /* 拼接地址 */
  211. std::string url = m_url + m_imageRecognitionPath;
  212. if(!CurlHttp::Get(url, vecHeader, response))
  213. {
  214. SPDLOG_LOGGER_ERROR(m_logger, "Get imageRecognition failed");
  215. return false;
  216. }
  217. SPDLOG_LOGGER_DEBUG(m_logger, "imageRecognition: \n{}", response);
  218. return true;
  219. }