FromWebAPI.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "FromWebAPI.h"
  2. FromEQM::FromEQM()
  3. {
  4. }
  5. FromEQM::~FromEQM()
  6. {
  7. if(m_httpApi != nullptr)
  8. {
  9. delete m_httpApi;
  10. m_httpApi = nullptr;
  11. }
  12. }
  13. /* 初始化WebApi */
  14. bool FromEQM::initWebApi(const QString& url, const QString& serverIP, const QString& serID)
  15. {
  16. if(m_httpApi == nullptr)
  17. {
  18. m_httpApi = new lhhttpapi;
  19. }
  20. if(!m_httpApi->Load())
  21. {
  22. SPDLOG_ERROR("Load WebApi failed");
  23. return false;
  24. }
  25. int ret = 0;
  26. auto pHttp = m_httpApi->DBInit(url.toStdString().c_str(), true);
  27. // if(ret < 0)
  28. // {
  29. // SPDLOG_LOGGER_ERROR(m_logger,"Init WebApi failed:{}, error Info:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  30. // return false;
  31. // }
  32. // SPDLOG_LOGGER_TRACE(m_logger,"初始化WebApi成功!");
  33. // QString serverList;
  34. char serverList[8192] = {0};
  35. ret = m_httpApi->DBGetServerList(serverList, 8192-1);
  36. if(ret < 0)
  37. {
  38. SPDLOG_ERROR("Get server list failed:{}, error info:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  39. // return false;
  40. }
  41. // SPDLOG_LOGGER_TRACE(m_logger,"Server list:{}",serverList.toStdString());
  42. SPDLOG_DEBUG("WebAPI Sucess!");
  43. /* 登录,第二个参数是限制的服务 */
  44. ret = m_httpApi->DBLogin(serverIP, serID, "SPSC", m_userToken, true, pHttp);
  45. if(ret < 0)
  46. {
  47. SPDLOG_ERROR("WebAPI Login failed:{}, error info:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  48. return false;
  49. }
  50. SPDLOG_INFO("WebAPI Login sucess!");
  51. return true;
  52. }
  53. bool FromEQM::test()
  54. {
  55. if(m_httpApi == nullptr)
  56. {
  57. SPDLOG_ERROR("WebAPI is nullptr");
  58. return false;
  59. }
  60. nJson json0 = nJson::array();
  61. nJson jsonData1;
  62. jsonData1["opName"] = "SPSC_test";
  63. jsonData1["Key"] = 1;
  64. nJson jsonParam1;
  65. jsonParam1["pkid"] = 2807;
  66. jsonData1["paramList"] = jsonParam1;
  67. json0.push_back(jsonData1);
  68. nJson jsonData2;
  69. jsonData2["opName"] = "SPSC_test";
  70. // jsonData2["Key"] = 2;
  71. nJson jsonParam2;
  72. jsonParam2["pkid"] = 2808;
  73. jsonData2["paramList"] = jsonParam2;
  74. json0.push_back(jsonData2);
  75. SPDLOG_INFO("json: {}", jsonData2.dump(4));
  76. QString strCmd = QString::fromStdString(jsonData2.dump());
  77. QString strRet;
  78. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  79. if(ret != 0)
  80. {
  81. SPDLOG_ERROR("DBDoInterface failed: {}, error info: {}", ret, m_httpApi->DoGetLastError(&ret).toStdString());
  82. return false;
  83. }
  84. SPDLOG_INFO("DBDoInterface sucess: {}", nJson::parse(strRet.toStdString()).dump(4));
  85. return true;
  86. }
  87. bool FromEQM::test2()
  88. {
  89. nJson json0;
  90. json0["opName"] = "SPSC_GetOnWorkInfoByTime";
  91. nJson jsonData;
  92. jsonData["startTime"] = "2024-10-01 00:00:00";
  93. jsonData["endTime"] = "2024-10-02 23:59:59";
  94. json0["paramList"] = jsonData;
  95. SPDLOG_INFO("strCmd: {}", json0.dump(4));
  96. QString strCmd = QString::fromStdString(json0.dump());
  97. QString strRet;
  98. int ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  99. if(ret != 0)
  100. {
  101. SPDLOG_ERROR("DBDoInterface failed: {}, error info: {}", ret, m_httpApi->DoGetLastError(&ret).toStdString());
  102. return false;
  103. }
  104. SPDLOG_INFO("DBDoInterface sucess: {}", nJson::parse(strRet.toStdString()).dump(4));
  105. return true;
  106. }