123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- #include "ToEQMDataBase.h"
- #include <QJsonDocument>
- #include <QJsonObject>
- #include <QVector>
- ToEQMDataBase::ToEQMDataBase()
- {
- m_logger = spdlog::get("ToEQMDataBase");
- if(m_logger == nullptr)
- {
- SPDLOG_ERROR("ToEQMDataBase logger is nullptr");
- return;
- }
- }
- ToEQMDataBase::~ToEQMDataBase()
- {
- if(m_httpApi != nullptr)
- {
- delete m_httpApi;
- m_httpApi = nullptr;
- }
- }
- /* 初始化WebApi */
- bool ToEQMDataBase::initWebApi(const QString& url, const QString& serverIP, const QString& serID)
- {
- if(m_httpApi == nullptr)
- {
- m_httpApi = new lhhttpapi;
- }
- int ret = 0;
- ret = m_httpApi->DBQInit(url);
- if(ret < 0)
- {
- SPDLOG_LOGGER_ERROR(m_logger,"Init WebApi failed:{}, error Info:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
- return false;
- }
- // SPDLOG_LOGGER_TRACE(m_logger,"初始化WebApi成功!");
- QString serverList;
- ret = m_httpApi->DBQGetServerList(serverList);
- if(ret < 0)
- {
- SPDLOG_LOGGER_DEBUG(m_logger,"Get server list failed:{}, error info:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
- return false;
- }
- SPDLOG_LOGGER_TRACE(m_logger,"Server list:{}",serverList.toStdString());
- SPDLOG_LOGGER_DEBUG(m_logger,"WebAPI Sucess!");
- /* 登录,第二个参数是限制的服务 */
- ret = m_httpApi->DBQLogin(serverIP, serID, "SPSS", m_userToken);
- if(ret < 0)
- {
- SPDLOG_LOGGER_ERROR(m_logger,"Login failed:{}, error info:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
- return false;
- }
- SPDLOG_LOGGER_TRACE(m_logger,"Login sucess!");
- return true;
- }
- /* 写入算法信息,写入tAction表 */
- bool ToEQMDataBase::writeAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo)
- {
- if(m_httpApi == nullptr)
- {
- SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
- return false;
- }
- for(const auto& it : vecInfo)
- {
- QString retStr;
- /* 操作名称,现在一次性将设备位置和线条信息都下载下来 */
- nJson json0;
- json0["opName"] = "SPSS_InsertToAction";
- nJson json1;
- json1["actionID"] = it.ActionID; /* 算法ID */
- json1["actionName"] = it.ActionName; /* 算法名称 */
- json1["actionTaskID"] = it.ActionTaskID; /* 算法类型 */
- json0["paramList"] = json1;
- QString strCmd = QString::fromStdString(json0.dump());
- int ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, retStr);
- if(ret < 0)
- {
- SPDLOG_LOGGER_DEBUG(m_logger,"写入tAction失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
- }
- SPDLOG_LOGGER_DEBUG(m_logger,"写入一条算法 {} 到 tAction成功!", it.ActionID);
- }
- return true;
- }
- /* 删除算法信息 */
- bool ToEQMDataBase::deleteAlgorithmInfo(std::vector<AlgorithmInfo>& vecDeleteInfo)
- {
- if(m_httpApi == nullptr)
- {
- SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
- return false;
- }
- for(const auto& it : vecDeleteInfo)
- {
- nJson json0;
- json0["opName"] = "SPSS_DeleteFromAction";
- nJson json1;
- json1["actionID"] = it.ActionID;
- json0["paramList"] = json1;
- QString strCmd = QString::fromStdString(json0.dump());
- QString strRet;
- int ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet);
- if(ret < 0)
- {
- SPDLOG_LOGGER_DEBUG(m_logger,"删除tAction失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
- }
- SPDLOG_LOGGER_DEBUG(m_logger,"从tAction 删除算法 {} 成功!", it.ActionID);
- }
- return true;
- }
- /* 获取tAction数据 */
- bool ToEQMDataBase::getAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo)
- {
- if(m_httpApi == nullptr)
- {
- SPDLOG_LOGGER_ERROR(m_logger, "WebAPI is nullptr");
- return false;
- }
- /* 清空内容 */
- vecInfo.clear();
- nJson json0;
- json0["opName"] = "SPSS_SelectFromAction";
- QString strCmd = QString::fromStdString(json0.dump());
- QString strRet;
- auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
- if(ret < 0)
- {
- SPDLOG_LOGGER_DEBUG(m_logger,"获取tAction失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
- return false;
- }
- /* 解析获取到的JSON数据 */
- // SPDLOG_LOGGER_DEBUG(m_logger,"\n{}",strRet.toStdString());
- nJson json1 = nJson::parse(strRet.toStdString());
- int retCode = json1["code"].get<int>();
- if(retCode != 0)
- {
- SPDLOG_LOGGER_ERROR(m_logger,"获取tAction失败");
- return false;
- }
- nJson result = json1["result"];
- for(auto& it : result)
- {
- AlgorithmInfo info;
- info.ActionID = it["actionId"].is_null() ? "" : it["actionId"].get<std::string>();
- info.ActionName = it["actionName"].get<std::string>();
- info.ActionTaskID = it["actionTaskid"].get<int>();
- // SPDLOG_LOGGER_DEBUG(m_logger,"ActionID:{}, ActionName:{}, ActionTaskID:{}",info.ActionID,info.ActionName,info.ActionTaskID);
- vecInfo.push_back(info);
- }
- return true;
- }
|