ToEQMDataBase.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "ToEQMDataBase.h"
  2. #include <QJsonDocument>
  3. #include <QJsonObject>
  4. #include <QVector>
  5. ToEQMDataBase::ToEQMDataBase()
  6. {
  7. m_logger = spdlog::get("ToEQMDataBase");
  8. if(m_logger == nullptr)
  9. {
  10. SPDLOG_ERROR("ToEQMDataBase logger is nullptr");
  11. return;
  12. }
  13. }
  14. ToEQMDataBase::~ToEQMDataBase()
  15. {
  16. if(m_httpApi != nullptr)
  17. {
  18. delete m_httpApi;
  19. m_httpApi = nullptr;
  20. }
  21. }
  22. /* 初始化WebApi */
  23. bool ToEQMDataBase::initWebApi(const QString& url, const QString& serverIP, const QString& serID)
  24. {
  25. if(m_httpApi == nullptr)
  26. {
  27. m_httpApi = new lhhttpapi;
  28. }
  29. int ret = 0;
  30. ret = m_httpApi->DBQInit(url);
  31. if(ret < 0)
  32. {
  33. SPDLOG_LOGGER_ERROR(m_logger,"Init WebApi failed:{}, error Info:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  34. return false;
  35. }
  36. // SPDLOG_LOGGER_TRACE(m_logger,"初始化WebApi成功!");
  37. QString serverList;
  38. ret = m_httpApi->DBQGetServerList(serverList);
  39. if(ret < 0)
  40. {
  41. SPDLOG_LOGGER_DEBUG(m_logger,"Get server list failed:{}, error info:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  42. return false;
  43. }
  44. SPDLOG_LOGGER_TRACE(m_logger,"Server list:{}",serverList.toStdString());
  45. SPDLOG_LOGGER_DEBUG(m_logger,"WebAPI Sucess!");
  46. /* 登录,第二个参数是限制的服务 */
  47. ret = m_httpApi->DBQLogin(serverIP, serID, "SPSS", m_userToken);
  48. if(ret < 0)
  49. {
  50. SPDLOG_LOGGER_ERROR(m_logger,"Login failed:{}, error info:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  51. return false;
  52. }
  53. SPDLOG_LOGGER_TRACE(m_logger,"Login sucess!");
  54. return true;
  55. }
  56. /* 写入算法信息,写入tAction表 */
  57. bool ToEQMDataBase::writeAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo)
  58. {
  59. if(m_httpApi == nullptr)
  60. {
  61. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  62. return false;
  63. }
  64. for(const auto& it : vecInfo)
  65. {
  66. QString retStr;
  67. /* 操作名称,现在一次性将设备位置和线条信息都下载下来 */
  68. nJson json0;
  69. json0["opName"] = "SPSS_InsertToAction";
  70. nJson json1;
  71. json1["actionID"] = it.ActionID; /* 算法ID */
  72. json1["actionName"] = it.ActionName; /* 算法名称 */
  73. json1["actionTaskID"] = it.ActionTaskID; /* 算法类型 */
  74. json0["paramList"] = json1;
  75. QString strCmd = QString::fromStdString(json0.dump());
  76. int ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, retStr);
  77. if(ret < 0)
  78. {
  79. SPDLOG_LOGGER_DEBUG(m_logger,"写入tAction失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  80. }
  81. SPDLOG_LOGGER_DEBUG(m_logger,"写入一条算法 {} 到 tAction成功!", it.ActionID);
  82. }
  83. return true;
  84. }
  85. /* 删除算法信息 */
  86. bool ToEQMDataBase::deleteAlgorithmInfo(std::vector<AlgorithmInfo>& vecDeleteInfo)
  87. {
  88. if(m_httpApi == nullptr)
  89. {
  90. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  91. return false;
  92. }
  93. for(const auto& it : vecDeleteInfo)
  94. {
  95. nJson json0;
  96. json0["opName"] = "SPSS_DeleteFromAction";
  97. nJson json1;
  98. json1["actionID"] = it.ActionID;
  99. json0["paramList"] = json1;
  100. QString strCmd = QString::fromStdString(json0.dump());
  101. QString strRet;
  102. int ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet);
  103. if(ret < 0)
  104. {
  105. SPDLOG_LOGGER_DEBUG(m_logger,"删除tAction失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  106. }
  107. SPDLOG_LOGGER_DEBUG(m_logger,"从tAction 删除算法 {} 成功!", it.ActionID);
  108. }
  109. return true;
  110. }
  111. /* 获取tAction数据 */
  112. bool ToEQMDataBase::getAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo)
  113. {
  114. if(m_httpApi == nullptr)
  115. {
  116. SPDLOG_LOGGER_ERROR(m_logger, "WebAPI is nullptr");
  117. return false;
  118. }
  119. /* 清空内容 */
  120. vecInfo.clear();
  121. nJson json0;
  122. json0["opName"] = "SPSS_SelectFromAction";
  123. QString strCmd = QString::fromStdString(json0.dump());
  124. QString strRet;
  125. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  126. if(ret < 0)
  127. {
  128. SPDLOG_LOGGER_DEBUG(m_logger,"获取tAction失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  129. return false;
  130. }
  131. /* 解析获取到的JSON数据 */
  132. // SPDLOG_LOGGER_DEBUG(m_logger,"\n{}",strRet.toStdString());
  133. nJson json1 = nJson::parse(strRet.toStdString());
  134. int retCode = json1["code"].get<int>();
  135. if(retCode != 0)
  136. {
  137. SPDLOG_LOGGER_ERROR(m_logger,"获取tAction失败");
  138. return false;
  139. }
  140. nJson result = json1["result"];
  141. for(auto& it : result)
  142. {
  143. AlgorithmInfo info;
  144. info.ActionID = it["actionId"].is_null() ? "" : it["actionId"].get<std::string>();
  145. info.ActionName = it["actionName"].get<std::string>();
  146. info.ActionTaskID = it["actionTaskid"].get<int>();
  147. // SPDLOG_LOGGER_DEBUG(m_logger,"ActionID:{}, ActionName:{}, ActionTaskID:{}",info.ActionID,info.ActionName,info.ActionTaskID);
  148. vecInfo.push_back(info);
  149. }
  150. return true;
  151. }