ToEQMDataBase.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. /**
  57. * @brief 写入算法信息,写入tAction表
  58. *
  59. * @param vecInfo 要写入的表格数据
  60. * @param vecNowInfo 现有的表格数据,主要是为了重复利用已经删除的自增主键而传入的
  61. * @return true
  62. * @return false
  63. */
  64. bool ToEQMDataBase::writeAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo)
  65. {
  66. if(m_httpApi == nullptr)
  67. {
  68. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  69. return false;
  70. }
  71. /* 取出可用的自增主键,从0开始计算 */
  72. // std::list<int> listPKID;
  73. // int nPKID = 0;
  74. // for(const auto& it : vecNowInfo)
  75. // {
  76. // while(it.ActionTaskID == nPKID)
  77. // {
  78. // if(it.PKID != nPKID)
  79. // {
  80. // listPKID.push_back(nPKID);
  81. // break;
  82. // }
  83. // nPKID ++;
  84. // }
  85. // }
  86. /* 循环写入数据 */
  87. for(const auto& it : vecInfo)
  88. {
  89. QString retStr;
  90. /* 操作名称,现在一次性将设备位置和线条信息都下载下来 */
  91. nJson json0;
  92. json0["opName"] = "SPSS_InsertToAction";
  93. nJson json1;
  94. json1["actionID"] = it.ActionID; /* 算法ID */
  95. json1["actionName"] = it.ActionName; /* 算法名称 */
  96. json1["actionTaskID"] = it.ActionTaskID; /* 算法类型 */
  97. json0["paramList"] = json1;
  98. QString strCmd = QString::fromStdString(json0.dump());
  99. int ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, retStr);
  100. if(ret < 0)
  101. {
  102. SPDLOG_LOGGER_DEBUG(m_logger,"写入tAction失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  103. }
  104. SPDLOG_LOGGER_DEBUG(m_logger,"写入一条算法 {} 到 tAction成功!", it.ActionID);
  105. }
  106. return true;
  107. }
  108. /* 删除算法信息 */
  109. bool ToEQMDataBase::deleteAlgorithmInfo(std::vector<AlgorithmInfo>& vecDeleteInfo)
  110. {
  111. if(m_httpApi == nullptr)
  112. {
  113. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  114. return false;
  115. }
  116. for(const auto& it : vecDeleteInfo)
  117. {
  118. nJson json0;
  119. json0["opName"] = "SPSS_DeleteFromAction";
  120. nJson json1;
  121. json1["actionID"] = it.ActionID;
  122. json0["paramList"] = json1;
  123. QString strCmd = QString::fromStdString(json0.dump());
  124. QString strRet;
  125. int ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet);
  126. if(ret < 0)
  127. {
  128. SPDLOG_LOGGER_DEBUG(m_logger,"删除tAction失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  129. }
  130. SPDLOG_LOGGER_DEBUG(m_logger,"从tAction 删除算法 {} 成功!", it.ActionID);
  131. }
  132. return true;
  133. }
  134. /* 获取tAction数据 */
  135. bool ToEQMDataBase::getAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo)
  136. {
  137. if(m_httpApi == nullptr)
  138. {
  139. SPDLOG_LOGGER_ERROR(m_logger, "WebAPI is nullptr");
  140. return false;
  141. }
  142. /* 清空内容 */
  143. vecInfo.clear();
  144. nJson json0;
  145. json0["opName"] = "SPSS_SelectFromAction";
  146. QString strCmd = QString::fromStdString(json0.dump());
  147. QString strRet;
  148. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  149. if(ret < 0)
  150. {
  151. SPDLOG_LOGGER_DEBUG(m_logger,"获取tAction失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  152. return false;
  153. }
  154. /* 解析获取到的JSON数据 */
  155. // SPDLOG_LOGGER_DEBUG(m_logger,"\n{}",strRet.toStdString());
  156. nJson json1 = nJson::parse(strRet.toStdString());
  157. int retCode = json1["code"].get<int>();
  158. if(retCode != 0)
  159. {
  160. SPDLOG_LOGGER_ERROR(m_logger,"获取tAction失败");
  161. return false;
  162. }
  163. nJson result = json1["result"];
  164. for(auto& it : result)
  165. {
  166. AlgorithmInfo info;
  167. info.ActionID = it["actionId"].is_null() ? "" : it["actionId"].get<std::string>();
  168. info.ActionName = it["actionName"].get<std::string>();
  169. info.ActionTaskID = it["actionTaskid"].get<int>();
  170. // SPDLOG_LOGGER_DEBUG(m_logger,"ActionID:{}, ActionName:{}, ActionTaskID:{}",info.ActionID,info.ActionName,info.ActionTaskID);
  171. vecInfo.push_back(info);
  172. }
  173. return true;
  174. }
  175. /* 插入设备信息 */
  176. bool ToEQMDataBase::insertDeviceInfo(std::vector<DeviceInfo>& vecInfo)
  177. {
  178. if(m_httpApi == nullptr)
  179. {
  180. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  181. return false;
  182. }
  183. if(vecInfo.empty())
  184. {
  185. return false;
  186. }
  187. bool isSuccess = true;
  188. for(const auto& it : vecInfo)
  189. {
  190. nJson json0;
  191. json0["opName"] = "SPSS_InsertToCamerInfo";
  192. nJson json1;
  193. json1["camerID"] = it.DeviceID;
  194. json1["camerName"] = it.DeviceName.empty() ? nullptr : it.DeviceName;
  195. json1["camerIP"] = it.DeviceIP.empty() ? nullptr : it.DeviceIP;
  196. json1["camerPort"] = it.DevicePort;
  197. json1["camerUser"] = it.UserAccount.empty() ? nullptr : it.UserAccount;
  198. json1["camerPwd"] = it.UserPassword.empty() ? nullptr : it.UserPassword;
  199. json1["camerType"] = it.DeviceType.empty() ? nullptr : it.DeviceType;
  200. json1["camerSerial"] = it.DeviceSerial.empty() ? nullptr : it.DeviceSerial;
  201. json1["camerChannel"] = nullptr;
  202. json1["camerUrl"] = nullptr;
  203. json0["paramList"] = json1;
  204. QString strCmd = QString::fromStdString(json0.dump());
  205. QString strRet;
  206. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet);
  207. if(ret < 0)
  208. {
  209. SPDLOG_LOGGER_DEBUG(m_logger,"插入设备信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  210. isSuccess = false;
  211. }
  212. SPDLOG_LOGGER_DEBUG(m_logger,"插入数据 {} 到 CamerInfo 成功!", it.DeviceID);
  213. }
  214. return isSuccess;
  215. }
  216. /* 更新设备信息 */
  217. bool ToEQMDataBase::updateDeviceInfo(std::vector<DeviceInfo>& vecUpdateInfo)
  218. {
  219. if(m_httpApi == nullptr)
  220. {
  221. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  222. return false;
  223. }
  224. if(vecUpdateInfo.empty())
  225. {
  226. return false;
  227. }
  228. bool isSuccess = true;
  229. for(const auto& it : vecUpdateInfo)
  230. {
  231. nJson json0;
  232. json0["opName"] = "SPSS_UpdateToCamerInfo";
  233. nJson json1;
  234. json1["camerID"] = it.DeviceID;
  235. json1["camerName"] = it.DeviceName.empty() ? nullptr : it.DeviceName;
  236. json1["camerIP"] = it.DeviceIP.empty() ? nullptr : it.DeviceIP;
  237. json1["camerPort"] = it.DevicePort;
  238. json1["camerUser"] = it.UserAccount.empty() ? nullptr : it.UserAccount;
  239. json1["camerPwd"] = it.UserPassword.empty() ? nullptr : it.UserPassword;
  240. json1["camerType"] = it.DeviceType.empty() ? nullptr : it.DeviceType;
  241. json1["camerSerial"] = it.DeviceSerial.empty() ? nullptr : it.DeviceSerial;
  242. json1["camerChannel"] = nullptr;
  243. json1["camerUrl"] = nullptr;
  244. json0["paramList"] = json1;
  245. QString strCmd = QString::fromStdString(json0.dump());
  246. QString strRet;
  247. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Update, strCmd, strRet);
  248. if(ret < 0)
  249. {
  250. SPDLOG_LOGGER_DEBUG(m_logger,"更新设备信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  251. isSuccess = false;
  252. }
  253. SPDLOG_LOGGER_DEBUG(m_logger,"更新数据 {} 到 CamerInfo 成功!", it.DeviceID);
  254. }
  255. return isSuccess;
  256. }
  257. /* 删除设备信息 */
  258. bool ToEQMDataBase::deleteDeviceInfo(std::vector<DeviceInfo>& vecDeleteInfo)
  259. {
  260. if(m_httpApi == nullptr)
  261. {
  262. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  263. return false;
  264. }
  265. if(vecDeleteInfo.empty())
  266. {
  267. return false;
  268. }
  269. bool isSuccess = true;
  270. for(const auto& it : vecDeleteInfo)
  271. {
  272. nJson json0;
  273. json0["opName"] = "SPSS_DeleteFromCamerInfo";
  274. nJson json1;
  275. json1["camerID"] = it.DeviceID;
  276. json0["paramList"] = json1;
  277. QString strCmd = QString::fromStdString(json0.dump());
  278. QString strRet;
  279. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet);
  280. if(ret < 0)
  281. {
  282. SPDLOG_LOGGER_DEBUG(m_logger,"删除设备信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  283. isSuccess = false;
  284. }
  285. SPDLOG_LOGGER_DEBUG(m_logger,"删除数据 {} 到 CamerInfo 成功!", it.DeviceID);
  286. }
  287. return isSuccess;
  288. }
  289. /* 从EQM获取CamerInfo信息 */
  290. bool ToEQMDataBase::getDeviceInfo(std::vector<DeviceInfo>& vecInfo)
  291. {
  292. if(m_httpApi == nullptr)
  293. {
  294. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  295. return false;
  296. }
  297. vecInfo.clear();
  298. nJson json0;
  299. json0["opName"] = "SPSS_SelectFromCamerInfo";
  300. QString strCmd = QString::fromStdString(json0.dump());
  301. QString strRet;
  302. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  303. if(ret < 0)
  304. {
  305. SPDLOG_LOGGER_DEBUG(m_logger,"获取CamerInfo失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  306. return false;
  307. }
  308. /* 解析信息 */
  309. nJson json1 = nJson::parse(strRet.toStdString());
  310. int retCode = json1["code"].get<int>();
  311. if(retCode != 0)
  312. {
  313. SPDLOG_LOGGER_ERROR(m_logger,"获取CamerInfo失败");
  314. return false;
  315. }
  316. nJson result = json1["result"];
  317. if(result.empty())
  318. {
  319. return false;
  320. }
  321. for(const auto& it : result)
  322. {
  323. // SPDLOG_LOGGER_DEBUG(m_logger,"camerID:{}",it["camerId"].get<int>());
  324. DeviceInfo info;
  325. info.DeviceID = it["camerId"].get<int>();
  326. info.DeviceName = it["camerName"].is_null() ? "" : it["camerName"].get<std::string>();
  327. info.DeviceIP = it["camerIp"].is_null() ? "" : it["camerIp"].get<std::string>();
  328. info.DevicePort = it["camerPort"].is_null() ? 0 : it["camerPort"].get<int>();
  329. info.UserAccount = it["camerUsr"].is_null() ? "" : it["camerUsr"].get<std::string>();
  330. info.UserPassword = it["camerPwd"].is_null() ? "" : it["camerPwd"].get<std::string>();
  331. info.DeviceType = it["camerType"].is_null() ? "" : it["camerType"].get<std::string>();
  332. info.DeviceSerial = it["camerSerial"].is_null() ? "" : it["camerSerial"].get<std::string>();
  333. vecInfo.push_back(info);
  334. }
  335. return true;
  336. }