FromSuperBrain.cpp 7.2 KB

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