#include "ToEQMDataBase.h" #include #include #include 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; } /** * @brief 写入算法信息,写入tAction表 * * @param vecInfo 要写入的表格数据 * @param vecNowInfo 现有的表格数据,主要是为了重复利用已经删除的自增主键而传入的 * @return true * @return false */ bool ToEQMDataBase::writeAlgorithmInfo(std::vector& vecInfo) { if(m_httpApi == nullptr) { SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr"); return false; } /* 取出可用的自增主键,从0开始计算 */ // std::list listPKID; // int nPKID = 0; // for(const auto& it : vecNowInfo) // { // while(it.ActionTaskID == nPKID) // { // if(it.PKID != nPKID) // { // listPKID.push_back(nPKID); // break; // } // nPKID ++; // } // } /* 循环写入数据 */ 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& 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& 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()); try { nJson json1 = nJson::parse(strRet.toStdString()); int retCode = json1["code"].get(); 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(); info.ActionName = it["actionName"].get(); info.ActionTaskID = it["actionTaskid"].get(); // SPDLOG_LOGGER_DEBUG(m_logger,"ActionID:{}, ActionName:{}, ActionTaskID:{}",info.ActionID,info.ActionName,info.ActionTaskID); vecInfo.push_back(info); } } catch (const nJson::parse_error& e) { SPDLOG_LOGGER_ERROR(m_logger,"解析tAction数据失败:{}",e.what()); return false; } return true; } /* 插入设备信息 */ bool ToEQMDataBase::insertDeviceInfo(std::vector& vecInfo) { if(m_httpApi == nullptr) { SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr"); return false; } if(vecInfo.empty()) { return false; } bool isSuccess = true; for(const auto& it : vecInfo) { nJson json0; json0["opName"] = "SPSS_InsertToCamerInfo"; nJson json1; /* 这里不知道是不是“?”运算符的等级是不是不够,不加括号会报错 */ json1["camerID"] = it.DeviceID; json1["camerName"] = it.DeviceName; json1["camerIP"] = it.DeviceIP; json1["camerPort"] = it.DevicePort; json1["camerUser"] = it.UserAccount; json1["camerPwd"] = it.UserPassword; json1["camerType"] = it.DeviceType; json1["camerSerial"] = it.DeviceSerial; json1["camerChannel"] = nullptr; json1["camerUrl"] = nullptr; json0["paramList"] = json1; QString strCmd = QString::fromStdString(json0.dump()); QString strRet; auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet); if(ret < 0) { SPDLOG_LOGGER_DEBUG(m_logger,"插入设备信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString()); isSuccess = false; } SPDLOG_LOGGER_DEBUG(m_logger,"插入ID {} 信息到 CamerInfo 成功!", it.DeviceID); } /* 插入信息到tActionCamer表 */ for(const auto& it0 : vecInfo) { for(const auto& it1 : it0.vecAlgorithmInfo) { nJson json0; json0["opName"] = "SPSS_InsertToActionCamer"; nJson json1; json1["actionID"] = it1.ActionID; json1["camerID"] = it0.DeviceID; json0["paramList"] = json1; QString strCmd = QString::fromStdString(json0.dump()); QString strRet; auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet); if(ret < 0) { SPDLOG_LOGGER_DEBUG(m_logger,"插入设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString()); isSuccess = false; } SPDLOG_LOGGER_DEBUG(m_logger,"插入DeviceID {} 和 ActionID {} 到 ActionCamer 成功!", it0.DeviceID, it1.ActionID); } } return isSuccess; } /* 更新设备信息 */ bool ToEQMDataBase::updateDeviceInfo(std::vector& vecUpdateInfo) { if(m_httpApi == nullptr) { SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr"); return false; } if(vecUpdateInfo.empty()) { return false; } bool isSuccess = true; for(const auto& it : vecUpdateInfo) { nJson json0; json0["opName"] = "SPSS_UpdateToCamerInfo"; nJson json1; json1["camerID"] = it.DeviceID; json1["camerName"] = it.DeviceName; json1["camerIP"] = it.DeviceIP; json1["camerPort"] = it.DevicePort; json1["camerUser"] = it.UserAccount; json1["camerPwd"] = it.UserPassword; json1["camerType"] = it.DeviceType; json1["camerSerial"] = it.DeviceSerial; json1["camerChannel"] = nullptr; json1["camerUrl"] = nullptr; json0["paramList"] = json1; QString strCmd = QString::fromStdString(json0.dump()); QString strRet; auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Update, strCmd, strRet); if(ret < 0) { SPDLOG_LOGGER_DEBUG(m_logger,"更新设备信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString()); isSuccess = false; } SPDLOG_LOGGER_DEBUG(m_logger,"更新ID {} 信息到 CamerInfo 成功!", it.DeviceID); } return isSuccess; } /* 删除设备信息 */ bool ToEQMDataBase::deleteDeviceInfo(std::vector& vecDeleteInfo) { if(m_httpApi == nullptr) { SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr"); return false; } if(vecDeleteInfo.empty()) { return false; } bool isSuccess = true; for(const auto& it : vecDeleteInfo) { nJson json0; json0["opName"] = "SPSS_DeleteFromCamerInfo"; nJson json1; json1["camerID"] = it.DeviceID; json0["paramList"] = json1; QString strCmd = QString::fromStdString(json0.dump()); QString strRet; auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet); if(ret < 0) { SPDLOG_LOGGER_DEBUG(m_logger,"删除设备信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString()); isSuccess = false; } SPDLOG_LOGGER_DEBUG(m_logger,"删除数据 {} 到 CamerInfo 成功!", it.DeviceID); } return isSuccess; } /* 从EQM获取CamerInfo信息 */ bool ToEQMDataBase::getDeviceInfo(std::vector& vecInfo) { if(m_httpApi == nullptr) { SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr"); return false; } vecInfo.clear(); nJson json0; json0["opName"] = "SPSS_SelectFromCamerInfo"; 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,"获取CamerInfo失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString()); return false; } /* 解析信息 */ try { nJson json1 = nJson::parse(strRet.toStdString()); int retCode = json1["code"].get(); if(retCode != 0) { SPDLOG_LOGGER_ERROR(m_logger,"获取CamerInfo失败"); return false; } nJson result = json1["result"]; if(result.empty()) { return false; } for(const auto& it : result) { // SPDLOG_LOGGER_DEBUG(m_logger,"camerID:{}",it["camerId"].get()); DeviceInfo info; info.DeviceID = it["camerId"].get(); info.DeviceName = it["camerName"].is_null() ? "" : it["camerName"].get(); info.DeviceIP = it["camerIp"].is_null() ? "" : it["camerIp"].get(); info.DevicePort = it["camerPort"].is_null() ? 0 : it["camerPort"].get(); info.UserAccount = it["camerUsr"].is_null() ? "" : it["camerUsr"].get(); info.UserPassword = it["camerPwd"].is_null() ? "" : it["camerPwd"].get(); info.DeviceType = it["camerType"].is_null() ? "" : it["camerType"].get(); info.DeviceSerial = it["camerSerial"].is_null() ? "" : it["camerSerial"].get(); vecInfo.push_back(info); } } catch (const nJson::parse_error& e) { SPDLOG_LOGGER_ERROR(m_logger,"解析CamerInfo数据失败:{}, 错误ID:{}",e.what(), e.id); return false; } catch (const nJson::type_error& e) { SPDLOG_LOGGER_ERROR(m_logger,"解析CamerInfo数据失败:{}, 错误ID:{}",e.what(), e.id); return false; } return true; } /* 插入设备和算法关联信息 */ bool ToEQMDataBase::insertDeviceAlgorithmInfo(std::vector& vecInfo) { if(m_httpApi == nullptr) { SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr"); return false; } /* 插入信息到tActionCamer表 */ bool isSuccess = true; for(const auto& it0 : vecInfo) { for(const auto& it1 : it0.vecAlgorithmInfo) { nJson json0; json0["opName"] = "SPSS_InsertToActionCamer"; nJson json1; json1["actionID"] = it1.ActionID; json1["camerID"] = it0.DeviceID; json0["paramList"] = json1; QString strCmd = QString::fromStdString(json0.dump()); QString strRet; auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet); if(ret < 0) { SPDLOG_LOGGER_DEBUG(m_logger,"插入设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString()); isSuccess = false; } SPDLOG_LOGGER_DEBUG(m_logger,"插入DeviceID {} 和 ActionID {} 到 ActionCamer 成功!", it0.DeviceID, it1.ActionID); } } return true; } /* 更新设备和算法关联表 */ bool ToEQMDataBase::updateDeviceAlgorithmInfo(std::vector& vecInfo) { if(m_httpApi == nullptr) { SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr"); return false; } /* 更新之前先删除相关的信息 */ for(const auto& it : vecInfo) { nJson json0; json0["opName"] = "SPSS_DeleteFromActionCamer"; nJson json1; json1["camerID"] = it.DeviceID; json0["paramList"] = json1; QString strCmd = QString::fromStdString(json0.dump()); QString strRet; auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet); if(ret < 0) { SPDLOG_LOGGER_DEBUG(m_logger,"删除设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString()); // return false; } SPDLOG_LOGGER_DEBUG(m_logger,"从 ActionCamer 删除DeviceID {} 成功!", it.DeviceID); } /* 插入信息到tActionCamer表 */ bool isSuccess = true; for(const auto& it0 : vecInfo) { for(const auto& it1 : it0.vecAlgorithmInfo) { nJson json0; json0["opName"] = "SPSS_InsertToActionCamer"; nJson json1; json1["actionID"] = it1.ActionID; json1["camerID"] = it0.DeviceID; json0["paramList"] = json1; QString strCmd = QString::fromStdString(json0.dump()); QString strRet; auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet); if(ret < 0) { SPDLOG_LOGGER_DEBUG(m_logger,"插入设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString()); isSuccess = false; } SPDLOG_LOGGER_DEBUG(m_logger,"插入DeviceID {} 和 ActionID {} 到 ActionCamer 成功!", it0.DeviceID, it1.ActionID); } } return isSuccess; } /* 删除设备和算法关联表 */ bool ToEQMDataBase::deleteDeviceAlgorithmInfo(std::vector& vecInfo) { if(m_httpApi == nullptr) { SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr"); return false; } bool isSuccess = true; /* 更新之前先删除相关的信息 */ for(const auto& it : vecInfo) { nJson json0; json0["opName"] = "SPSS_DeleteFromActionCamer"; nJson json1; json1["camerID"] = it.DeviceID; json0["paramList"] = json1; QString strCmd = QString::fromStdString(json0.dump()); QString strRet; auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet); if(ret < 0) { SPDLOG_LOGGER_DEBUG(m_logger,"删除设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString()); // return false; isSuccess = false; } SPDLOG_LOGGER_DEBUG(m_logger,"从 ActionCamer 删除DeviceID {} 成功!", it.DeviceID); } return isSuccess; } /* 获取设备和算法信息关联表,需要先从EQM数据库中获取到设备信息 */ bool ToEQMDataBase::getDeviceAlgorithmInfo(std::vector& vecInfo) { if(m_httpApi == nullptr) { SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr"); return false; } if(vecInfo.empty()) { SPDLOG_LOGGER_WARN(m_logger,"Device info is empty"); return false; } nJson json0; json0["opName"] = "SPSS_SelectFromActionCamer"; 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,"获取ActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString()); return false; } /* 解析信息 */ try { nJson json1 = nJson::parse(strRet.toStdString()); int retCode = json1["code"].get(); if(retCode != 0) { SPDLOG_LOGGER_ERROR(m_logger,"获取ActionCamer失败"); return false; } nJson result = json1["result"]; if(result.empty()) { return false; } /* 根据缓存中的设备ID解析json字符串 */ for(auto& it0 : vecInfo) { for(auto& it1 : result) { /* 对比设备ID */ if(it0.DeviceID == it1["camerId"].get()) { AlgorithmInfo info; info.ActionID = it1["actionId"].is_null() ? "" : it1["actionId"].get(); info.ActionName = it1["actionName"].is_null() ? "" : it1["actionName"].get(); info.ActionTaskID = it1["actionTaskid"].is_null() ? 0 : it1["actionTaskid"].get(); it0.vecAlgorithmInfo.push_back(info); } } } } catch (const nJson::parse_error& e) { SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}, 错误ID:{}",e.what(), e.id); return false; } catch (const nJson::type_error& e) { SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}, 错误ID:{}",e.what(), e.id); return false; } return true; }