FromSuperBrain.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "FromSuperBrain.h"
  2. #include "CurlHttp.h"
  3. #include "fmt/format.h"
  4. #include "GlobalInfo.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. nJson json0 = nJson::parse(response);
  39. auto code = json0["code"].get<std::string>();
  40. if(code != "0")
  41. {
  42. SPDLOG_LOGGER_ERROR(m_logger, "Get Token failed");
  43. return false;
  44. }
  45. json0["data"]["accessToken"].get_to(m_token);
  46. // SPDLOG_LOGGER_DEBUG(m_logger, "Token:{}", m_token);
  47. return true;
  48. }
  49. /* 获取算法列表 */
  50. bool FromSuperBrain::getTaskTypeList(std::vector<AlgorithmInfo>& vecInfo)
  51. {
  52. if(m_token.empty())
  53. {
  54. SPDLOG_LOGGER_ERROR(m_logger, "Token is empty");
  55. return false;
  56. }
  57. std::string response;
  58. std::vector<std::string> vecHeader;
  59. vecHeader.push_back(fmt::format("accessToken: {}", m_token));
  60. std::string url = m_url + m_taskTypeListPath;
  61. if(!CurlHttp::Get(url, vecHeader, response))
  62. {
  63. SPDLOG_LOGGER_ERROR(m_logger, "{}}", response);
  64. return false;
  65. }
  66. // SPDLOG_LOGGER_DEBUG(m_logger, "TaskTypeList: \n{}", response);
  67. /* 解析json信息 */
  68. nJson json0 = nJson::parse(response);
  69. auto retCode = json0.at("code").get<std::string>();
  70. if(retCode != "0")
  71. {
  72. SPDLOG_LOGGER_ERROR(m_logger, "Get TaskTypeList failed");
  73. return false;
  74. }
  75. auto data = json0.at("data");
  76. for(auto& it : data)
  77. {
  78. // SPDLOG_LOGGER_DEBUG(m_logger, "muAiName:{}", it["muAiName"].get<std::string>());
  79. AlgorithmInfo info;
  80. info.ActionID = it.at("ability").get<std::string>();
  81. info.ActionName = it.at("muAiName").get<std::string>();
  82. info.ActionTaskID = it["taskTypeId"].get<int>();
  83. vecInfo.push_back(info);
  84. }
  85. return true;
  86. }
  87. /* 获取设备列表信息 */
  88. bool FromSuperBrain::getDeviceList(std::vector<DeviceInfo>& vecInfo)
  89. {
  90. if(m_token.empty())
  91. {
  92. SPDLOG_LOGGER_ERROR(m_logger, "Token is empty");
  93. return false;
  94. }
  95. std::string response;
  96. std::vector<std::string> vecHeader;
  97. vecHeader.push_back(fmt::format("accessToken: {}", m_token));
  98. std::string url = m_url + m_deviceListPath;
  99. if(!CurlHttp::Get(url, vecHeader, response))
  100. {
  101. SPDLOG_LOGGER_ERROR(m_logger, "{}", response);
  102. return false;
  103. }
  104. // SPDLOG_LOGGER_DEBUG(m_logger, "DeivceList: \n{}", response);
  105. /* 解析JSON信息 */
  106. nJson json0 = nJson::parse(response);
  107. auto retCode = json0.at("code").get<std::string>();
  108. if(retCode != "0")
  109. {
  110. SPDLOG_LOGGER_ERROR(m_logger, "Get DeviceList failed");
  111. return false;
  112. }
  113. DeviceInfo info;
  114. nJson json1 = json0["data"];
  115. for(const auto& it : json1)
  116. {
  117. info.DeviceID = it["deviceId"].get<int>();
  118. info.DeviceName = it["deviceName"].is_null() ? "" : it["deviceName"].get<std::string>();
  119. info.DeviceSerial = it["deviceSerial"].is_null() ? "" : it["deviceSerial"].get<std::string>();
  120. info.DeviceType = it["deviceType"].is_null() ? "" : it["deviceType"].get<std::string>();
  121. /* 这三个可能是null */
  122. info.DevicePort = it["port"].is_null() ? 0 : it["port"].get<int>();
  123. info.UserAccount = it["userAccount"].is_null() ? "" : it["userAccount"].get<std::string>();
  124. info.UserPassword = it["userPWD"].is_null() ? "" : it["userPWD"].get<std::string>();
  125. /* 解析任务类型列表 */
  126. nJson json2 = it["taskTypeList"];
  127. for(const auto& it1 : json2)
  128. {
  129. AlgorithmInfo info1;
  130. info1.ActionID = it1["ability"].get<std::string>();
  131. info1.ActionName = it1["taskTypeName"].get<std::string>();
  132. info1.ActionTaskID = it1["taskTypeId"].get<int>();
  133. info.vecAlgorithmInfo.push_back(info1);
  134. }
  135. vecInfo.push_back(info);
  136. }
  137. return true;
  138. }
  139. /* 图片识别 */
  140. bool FromSuperBrain::imageRecognition()
  141. {
  142. if(m_token.empty())
  143. {
  144. SPDLOG_LOGGER_ERROR(m_logger, "Token is empty");
  145. return false;
  146. }
  147. std::string response;
  148. std::vector<std::string> vecHeader;
  149. vecHeader.push_back(fmt::format("accessToken: {}", m_token));
  150. /* 拼接地址 */
  151. std::string url = m_url + m_imageRecognitionPath;
  152. if(!CurlHttp::Get(url, vecHeader, response))
  153. {
  154. SPDLOG_LOGGER_ERROR(m_logger, "Get imageRecognition failed");
  155. return false;
  156. }
  157. SPDLOG_LOGGER_DEBUG(m_logger, "imageRecognition: \n{}", response);
  158. return true;
  159. }