ToEQMDataBase.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. try {
  157. nJson json1 = nJson::parse(strRet.toStdString());
  158. int retCode = json1["code"].get<int>();
  159. if(retCode != 0)
  160. {
  161. SPDLOG_LOGGER_ERROR(m_logger,"获取tAction失败");
  162. return false;
  163. }
  164. nJson result = json1["result"];
  165. for(auto& it : result)
  166. {
  167. AlgorithmInfo info;
  168. info.ActionID = it["actionId"].is_null() ? "" : it["actionId"].get<std::string>();
  169. info.ActionName = it["actionName"].get<std::string>();
  170. info.ActionTaskID = it["actionTaskid"].get<int>();
  171. // SPDLOG_LOGGER_DEBUG(m_logger,"ActionID:{}, ActionName:{}, ActionTaskID:{}",info.ActionID,info.ActionName,info.ActionTaskID);
  172. vecInfo.push_back(info);
  173. }
  174. } catch (const nJson::parse_error& e) {
  175. SPDLOG_LOGGER_ERROR(m_logger,"解析tAction数据失败:{}",e.what());
  176. return false;
  177. }
  178. return true;
  179. }
  180. /* 插入设备信息 */
  181. bool ToEQMDataBase::insertDeviceInfo(std::vector<DeviceInfo>& vecInfo)
  182. {
  183. if(m_httpApi == nullptr)
  184. {
  185. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  186. return false;
  187. }
  188. if(vecInfo.empty())
  189. {
  190. return false;
  191. }
  192. bool isSuccess = true;
  193. for(const auto& it : vecInfo)
  194. {
  195. nJson json0;
  196. json0["opName"] = "SPSS_InsertToCamerInfo";
  197. nJson json1;
  198. /* 这里不知道是不是“?”运算符的等级是不是不够,不加括号会报错 */
  199. json1["camerID"] = it.DeviceID;
  200. json1["camerName"] = it.DeviceName;
  201. json1["camerIP"] = it.DeviceIP;
  202. json1["camerPort"] = it.DevicePort;
  203. json1["camerUser"] = it.UserAccount;
  204. json1["camerPwd"] = it.UserPassword;
  205. json1["camerType"] = it.DeviceType;
  206. json1["camerSerial"] = it.DeviceSerial;
  207. json1["camerChannel"] = nullptr;
  208. json1["camerUrl"] = nullptr;
  209. json0["paramList"] = json1;
  210. QString strCmd = QString::fromStdString(json0.dump());
  211. QString strRet;
  212. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet);
  213. if(ret < 0)
  214. {
  215. SPDLOG_LOGGER_DEBUG(m_logger,"插入设备信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  216. isSuccess = false;
  217. }
  218. SPDLOG_LOGGER_DEBUG(m_logger,"插入ID {} 信息到 CamerInfo 成功!", it.DeviceID);
  219. }
  220. /* 插入信息到tActionCamer表 */
  221. for(const auto& it0 : vecInfo)
  222. {
  223. for(const auto& it1 : it0.vecAlgorithmInfo)
  224. {
  225. nJson json0;
  226. json0["opName"] = "SPSS_InsertToActionCamer";
  227. nJson json1;
  228. json1["actionID"] = it1.ActionID;
  229. json1["camerID"] = it0.DeviceID;
  230. json0["paramList"] = json1;
  231. QString strCmd = QString::fromStdString(json0.dump());
  232. QString strRet;
  233. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet);
  234. if(ret < 0)
  235. {
  236. SPDLOG_LOGGER_DEBUG(m_logger,"插入设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  237. isSuccess = false;
  238. }
  239. SPDLOG_LOGGER_DEBUG(m_logger,"插入DeviceID {} 和 ActionID {} 到 ActionCamer 成功!", it0.DeviceID, it1.ActionID);
  240. }
  241. }
  242. return isSuccess;
  243. }
  244. /* 更新设备信息 */
  245. bool ToEQMDataBase::updateDeviceInfo(std::vector<DeviceInfo>& vecUpdateInfo)
  246. {
  247. if(m_httpApi == nullptr)
  248. {
  249. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  250. return false;
  251. }
  252. if(vecUpdateInfo.empty())
  253. {
  254. return false;
  255. }
  256. bool isSuccess = true;
  257. for(const auto& it : vecUpdateInfo)
  258. {
  259. nJson json0;
  260. json0["opName"] = "SPSS_UpdateToCamerInfo";
  261. nJson json1;
  262. json1["camerID"] = it.DeviceID;
  263. json1["camerName"] = it.DeviceName;
  264. json1["camerIP"] = it.DeviceIP;
  265. json1["camerPort"] = it.DevicePort;
  266. json1["camerUser"] = it.UserAccount;
  267. json1["camerPwd"] = it.UserPassword;
  268. json1["camerType"] = it.DeviceType;
  269. json1["camerSerial"] = it.DeviceSerial;
  270. json1["camerChannel"] = nullptr;
  271. json1["camerUrl"] = nullptr;
  272. json0["paramList"] = json1;
  273. QString strCmd = QString::fromStdString(json0.dump());
  274. QString strRet;
  275. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Update, strCmd, strRet);
  276. if(ret < 0)
  277. {
  278. SPDLOG_LOGGER_DEBUG(m_logger,"更新设备信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  279. isSuccess = false;
  280. }
  281. SPDLOG_LOGGER_DEBUG(m_logger,"更新ID {} 信息到 CamerInfo 成功!", it.DeviceID);
  282. }
  283. return isSuccess;
  284. }
  285. /* 删除设备信息 */
  286. bool ToEQMDataBase::deleteDeviceInfo(std::vector<DeviceInfo>& vecDeleteInfo)
  287. {
  288. if(m_httpApi == nullptr)
  289. {
  290. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  291. return false;
  292. }
  293. if(vecDeleteInfo.empty())
  294. {
  295. return false;
  296. }
  297. bool isSuccess = true;
  298. for(const auto& it : vecDeleteInfo)
  299. {
  300. nJson json0;
  301. json0["opName"] = "SPSS_DeleteFromCamerInfo";
  302. nJson json1;
  303. json1["camerID"] = it.DeviceID;
  304. json0["paramList"] = json1;
  305. QString strCmd = QString::fromStdString(json0.dump());
  306. QString strRet;
  307. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet);
  308. if(ret < 0)
  309. {
  310. SPDLOG_LOGGER_DEBUG(m_logger,"删除设备信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  311. isSuccess = false;
  312. }
  313. SPDLOG_LOGGER_DEBUG(m_logger,"删除数据 {} 到 CamerInfo 成功!", it.DeviceID);
  314. }
  315. return isSuccess;
  316. }
  317. /* 从EQM获取CamerInfo信息 */
  318. bool ToEQMDataBase::getDeviceInfo(std::vector<DeviceInfo>& vecInfo)
  319. {
  320. if(m_httpApi == nullptr)
  321. {
  322. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  323. return false;
  324. }
  325. vecInfo.clear();
  326. nJson json0;
  327. json0["opName"] = "SPSS_SelectFromCamerInfo";
  328. QString strCmd = QString::fromStdString(json0.dump());
  329. QString strRet;
  330. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  331. if(ret < 0)
  332. {
  333. SPDLOG_LOGGER_DEBUG(m_logger,"获取CamerInfo失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  334. return false;
  335. }
  336. /* 解析信息 */
  337. try {
  338. nJson json1 = nJson::parse(strRet.toStdString());
  339. int retCode = json1["code"].get<int>();
  340. if(retCode != 0)
  341. {
  342. SPDLOG_LOGGER_ERROR(m_logger,"获取CamerInfo失败");
  343. return false;
  344. }
  345. nJson result = json1["result"];
  346. if(result.empty())
  347. {
  348. return false;
  349. }
  350. for(const auto& it : result)
  351. {
  352. // SPDLOG_LOGGER_DEBUG(m_logger,"camerID:{}",it["camerId"].get<int>());
  353. DeviceInfo info;
  354. info.DeviceID = it["camerId"].get<int>();
  355. info.DeviceName = it["camerName"].is_null() ? "" : it["camerName"].get<std::string>();
  356. info.DeviceIP = it["camerIp"].is_null() ? "" : it["camerIp"].get<std::string>();
  357. info.DevicePort = it["camerPort"].is_null() ? 0 : it["camerPort"].get<int>();
  358. info.UserAccount = it["camerUsr"].is_null() ? "" : it["camerUsr"].get<std::string>();
  359. info.UserPassword = it["camerPwd"].is_null() ? "" : it["camerPwd"].get<std::string>();
  360. info.DeviceType = it["camerType"].is_null() ? "" : it["camerType"].get<std::string>();
  361. info.DeviceSerial = it["camerSerial"].is_null() ? "" : it["camerSerial"].get<std::string>();
  362. vecInfo.push_back(info);
  363. }
  364. }
  365. catch (const nJson::parse_error& e) {
  366. SPDLOG_LOGGER_ERROR(m_logger,"解析CamerInfo数据失败:{}, 错误ID:{}",e.what(), e.id);
  367. return false;
  368. }
  369. catch (const nJson::type_error& e) {
  370. SPDLOG_LOGGER_ERROR(m_logger,"解析CamerInfo数据失败:{}, 错误ID:{}",e.what(), e.id);
  371. return false;
  372. }
  373. return true;
  374. }
  375. /* 插入设备和算法关联信息 */
  376. bool ToEQMDataBase::insertDeviceAlgorithmInfo(std::vector<DeviceInfo>& vecInfo)
  377. {
  378. if(m_httpApi == nullptr)
  379. {
  380. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  381. return false;
  382. }
  383. /* 插入信息到tActionCamer表 */
  384. bool isSuccess = true;
  385. for(const auto& it0 : vecInfo)
  386. {
  387. for(const auto& it1 : it0.vecAlgorithmInfo)
  388. {
  389. nJson json0;
  390. json0["opName"] = "SPSS_InsertToActionCamer";
  391. nJson json1;
  392. json1["actionID"] = it1.ActionID;
  393. json1["camerID"] = it0.DeviceID;
  394. json0["paramList"] = json1;
  395. QString strCmd = QString::fromStdString(json0.dump());
  396. QString strRet;
  397. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet);
  398. if(ret < 0)
  399. {
  400. SPDLOG_LOGGER_DEBUG(m_logger,"插入设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  401. isSuccess = false;
  402. }
  403. SPDLOG_LOGGER_DEBUG(m_logger,"插入DeviceID {} 和 ActionID {} 到 ActionCamer 成功!", it0.DeviceID, it1.ActionID);
  404. }
  405. }
  406. return true;
  407. }
  408. /* 更新设备和算法关联表 */
  409. bool ToEQMDataBase::updateDeviceAlgorithmInfo(std::vector<DeviceInfo>& vecInfo)
  410. {
  411. if(m_httpApi == nullptr)
  412. {
  413. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  414. return false;
  415. }
  416. /* 更新之前先删除相关的信息 */
  417. for(const auto& it : vecInfo)
  418. {
  419. nJson json0;
  420. json0["opName"] = "SPSS_DeleteFromActionCamer";
  421. nJson json1;
  422. json1["camerID"] = it.DeviceID;
  423. json0["paramList"] = json1;
  424. QString strCmd = QString::fromStdString(json0.dump());
  425. QString strRet;
  426. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet);
  427. if(ret < 0)
  428. {
  429. SPDLOG_LOGGER_DEBUG(m_logger,"删除设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  430. // return false;
  431. }
  432. SPDLOG_LOGGER_DEBUG(m_logger,"从 ActionCamer 删除DeviceID {} 成功!", it.DeviceID);
  433. }
  434. /* 插入信息到tActionCamer表 */
  435. bool isSuccess = true;
  436. for(const auto& it0 : vecInfo)
  437. {
  438. for(const auto& it1 : it0.vecAlgorithmInfo)
  439. {
  440. nJson json0;
  441. json0["opName"] = "SPSS_InsertToActionCamer";
  442. nJson json1;
  443. json1["actionID"] = it1.ActionID;
  444. json1["camerID"] = it0.DeviceID;
  445. json0["paramList"] = json1;
  446. QString strCmd = QString::fromStdString(json0.dump());
  447. QString strRet;
  448. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet);
  449. if(ret < 0)
  450. {
  451. SPDLOG_LOGGER_DEBUG(m_logger,"插入设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  452. isSuccess = false;
  453. }
  454. SPDLOG_LOGGER_DEBUG(m_logger,"插入DeviceID {} ActionID {} 到 ActionCamer 成功!", it0.DeviceID, it1.ActionID);
  455. }
  456. }
  457. return isSuccess;
  458. }
  459. /* 删除设备和算法关联表 */
  460. bool ToEQMDataBase::deleteDeviceAlgorithmInfo(std::vector<DeviceInfo>& vecInfo)
  461. {
  462. if(m_httpApi == nullptr)
  463. {
  464. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  465. return false;
  466. }
  467. bool isSuccess = true;
  468. /* 更新之前先删除相关的信息 */
  469. for(const auto& it : vecInfo)
  470. {
  471. nJson json0;
  472. json0["opName"] = "SPSS_DeleteFromActionCamer";
  473. nJson json1;
  474. json1["camerID"] = it.DeviceID;
  475. json0["paramList"] = json1;
  476. QString strCmd = QString::fromStdString(json0.dump());
  477. QString strRet;
  478. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet);
  479. if(ret < 0)
  480. {
  481. SPDLOG_LOGGER_DEBUG(m_logger,"删除设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  482. // return false;
  483. isSuccess = false;
  484. }
  485. SPDLOG_LOGGER_DEBUG(m_logger,"从 ActionCamer 删除DeviceID {} 成功!", it.DeviceID);
  486. }
  487. return isSuccess;
  488. }
  489. /* 删除设备和算法关联表 */
  490. bool ToEQMDataBase::deleteDeviceAlgorithmInfo(std::list<int>& vecID)
  491. {
  492. if(m_httpApi == nullptr)
  493. {
  494. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  495. return false;
  496. }
  497. bool isSuccess = true;
  498. /* 更新之前先删除相关的信息 */
  499. for(const auto& it : vecID)
  500. {
  501. nJson json0;
  502. json0["opName"] = "SPSS_DeleteFromActionCamer";
  503. nJson json1;
  504. json1["camerID"] = it;
  505. json0["paramList"] = json1;
  506. QString strCmd = QString::fromStdString(json0.dump());
  507. QString strRet;
  508. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet);
  509. if(ret < 0)
  510. {
  511. SPDLOG_LOGGER_DEBUG(m_logger,"删除设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  512. // return false;
  513. isSuccess = false;
  514. }
  515. SPDLOG_LOGGER_DEBUG(m_logger,"从 ActionCamer 删除DeviceID {} 成功!", it);
  516. }
  517. return isSuccess;
  518. }
  519. /* 获取设备和算法信息关联表,需要先从EQM数据库中获取到设备信息 */
  520. bool ToEQMDataBase::getDeviceAlgorithmInfo(std::vector<DeviceInfo>& vecInfo, std::list<int>& listDevIDDelete)
  521. {
  522. if(m_httpApi == nullptr)
  523. {
  524. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  525. return false;
  526. }
  527. if(vecInfo.empty())
  528. {
  529. SPDLOG_LOGGER_WARN(m_logger,"Device info is empty");
  530. return false;
  531. }
  532. listDevIDDelete.clear();
  533. nJson json0;
  534. json0["opName"] = "SPSS_SelectFromActionCamer";
  535. QString strCmd = QString::fromStdString(json0.dump());
  536. QString strRet;
  537. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  538. if(ret < 0)
  539. {
  540. SPDLOG_LOGGER_DEBUG(m_logger,"获取ActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  541. return false;
  542. }
  543. /* 设备和算法关联信息容器 */
  544. std::vector<DeviceAlgorithmInfo> vecNewInfo;
  545. /* 解析信息 */
  546. try {
  547. nJson json1 = nJson::parse(strRet.toStdString());
  548. int retCode = json1["code"].get<int>();
  549. if(retCode != 0)
  550. {
  551. SPDLOG_LOGGER_ERROR(m_logger,"获取ActionCamer失败");
  552. return false;
  553. }
  554. nJson result = json1["result"];
  555. if(result.empty())
  556. {
  557. return false;
  558. }
  559. /* 取出所有的设备算法信息 */
  560. for(const auto& it : result)
  561. {
  562. DeviceAlgorithmInfo info;
  563. info.DeviceID = it["camerId"].get<int>();
  564. info.ActionID = it["actionId"].is_null() ? "" : it["actionId"].get<std::string>();
  565. info.ActionName = it["actionName"].is_null() ? "" : it["actionName"].get<std::string>();
  566. info.ActionTaskID = it["actionTaskid"].is_null() ? 0 : it["actionTaskid"].get<int>();
  567. vecNewInfo.push_back(info);
  568. }
  569. }
  570. catch (const nJson::parse_error& e) {
  571. SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}, 错误ID:{}",e.what(), e.id);
  572. return false;
  573. }
  574. catch (const nJson::type_error& e) {
  575. SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}, 错误ID:{}",e.what(), e.id);
  576. return false;
  577. }
  578. /* 根据缓存中的设备ID取出关联的算法 */
  579. for(auto& it0 : vecInfo)
  580. {
  581. for(auto& it1 : vecNewInfo)
  582. {
  583. /* 对比设备ID */
  584. if(it0.DeviceID == it1.DeviceID)
  585. {
  586. AlgorithmInfo info;
  587. info.ActionID = it1.ActionID;
  588. info.ActionName = it1.ActionName;
  589. info.ActionTaskID = it1.ActionTaskID;
  590. it0.vecAlgorithmInfo.push_back(info);
  591. }
  592. }
  593. }
  594. /* 取出关联表中已经消失的设备ID,在本轮循环中删除 */
  595. for(const auto& it : vecNewInfo)
  596. {
  597. bool isExist = false;
  598. for(const auto& it0 : vecInfo)
  599. {
  600. if(it.DeviceID == it0.DeviceID)
  601. {
  602. isExist = true;
  603. break;
  604. }
  605. }
  606. if(!isExist)
  607. {
  608. /* 检查是否重复,一个ID只需要记录一次 */
  609. if(std::find(listDevIDDelete.begin(), listDevIDDelete.end(), it.DeviceID) == listDevIDDelete.end())
  610. {
  611. listDevIDDelete.push_back(it.DeviceID);
  612. }
  613. }
  614. }
  615. return true;
  616. }
  617. /* 获取设备和算法关联信息,包括频率信息,房间信息,重载版,只获取关联关系 */
  618. bool ToEQMDataBase::getActionInfo(ListActionInfo& listInfo)
  619. {
  620. if(m_httpApi == nullptr)
  621. {
  622. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  623. return false;
  624. }
  625. listInfo.clear();
  626. nJson json0;
  627. json0["opName"] = "SPSS_SelectFromActionInfo";
  628. QString strCmd = QString::fromStdString(json0.dump());
  629. QString strRet;
  630. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  631. if(ret < 0)
  632. {
  633. SPDLOG_LOGGER_DEBUG(m_logger,"获取ActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  634. return false;
  635. }
  636. /* 解析信息 */
  637. try
  638. {
  639. nJson json1 = nJson::parse(strRet.toStdString());
  640. int retCode = json1["code"].get<int>();
  641. if(retCode != 0)
  642. {
  643. SPDLOG_LOGGER_ERROR(m_logger,"获取ActionCamer失败");
  644. return false;
  645. }
  646. nJson result = json1["result"];
  647. if(result.empty())
  648. {
  649. return false;
  650. }
  651. /* 取出所有的设备算法信息 */
  652. for(const auto& it : result)
  653. {
  654. ActionInfo info;
  655. info.CameraID = it["camerId"].is_null() ? -1 : it["camerId"].get<int>();
  656. info.ActionID = it["actionId"].is_null() ? "" : it["actionId"].get<std::string>();
  657. info.RoomID = it["roomId"].is_null() ? -1 : it["roomId"].get<int>();
  658. info.ChannelID = it["chnid"].is_null() ? -1 : it["chnid"].get<int>();
  659. info.RoomType = it["rtype"].is_null() ? -1 : it["rtype"].get<int>();
  660. info.strRoomName = it["rname"].is_null() ? "" : it["rname"].get<std::string>();
  661. listInfo.insertActionInfo(&info);
  662. }
  663. }
  664. catch (const nJson::parse_error& e) {
  665. SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}, 错误ID:{}",e.what(), e.id);
  666. return false;
  667. }
  668. catch (const nJson::type_error& e) {
  669. SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}, 错误ID:{}",e.what(), e.id);
  670. return false;
  671. }
  672. return true;
  673. }
  674. /* 获取摄像机和房间关联信息 */
  675. bool ToEQMDataBase::getRoomCameraInfo(std::list<RoomCameraInfo>& vecInfo)
  676. {
  677. if(m_httpApi == nullptr)
  678. {
  679. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  680. return false;
  681. }
  682. vecInfo.clear();
  683. nJson json0;
  684. json0["opName"] = "SPSS_SelectRoomCamer";
  685. QString strCmd = QString::fromStdString(json0.dump());
  686. QString strRet;
  687. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  688. if(ret < 0)
  689. {
  690. SPDLOG_LOGGER_DEBUG(m_logger,"获取RoomCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  691. return false;
  692. }
  693. try
  694. {
  695. nJson json1 = nJson::parse(strRet.toStdString());
  696. int retCode = json1["code"].get<int>();
  697. if(retCode != 0)
  698. {
  699. SPDLOG_LOGGER_ERROR(m_logger,"获取RoomCamer失败");
  700. return false;
  701. }
  702. nJson result = json1["result"];
  703. if(result.empty())
  704. {
  705. return false;
  706. }
  707. for(const auto& it : result)
  708. {
  709. auto roomID = it["roomId"].get<int>();
  710. auto cameraID = it["camerId"].is_null() ? -1 : it["camerId"].get<int>();
  711. /* 先检查RoomID是否已经在列表中了 */
  712. bool isExist = false;
  713. for(auto& it0 : vecInfo)
  714. {
  715. if(it0.RoomID == roomID)
  716. {
  717. it0.listCameraID.push_back(cameraID);
  718. isExist = true;
  719. break;
  720. }
  721. }
  722. /* 没有找到RoomID,创建一个新的 */
  723. if(!isExist)
  724. {
  725. RoomCameraInfo info;
  726. info.RoomID = roomID;
  727. info.listCameraID.push_back(cameraID);
  728. }
  729. }
  730. }
  731. catch (const nJson::parse_error& e) {
  732. SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}, 错误ID:{}",e.what(), e.id);
  733. return false;
  734. }
  735. catch (const nJson::type_error& e) {
  736. SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}, 错误ID:{}",e.what(), e.id);
  737. return false;
  738. }
  739. return true;
  740. }
  741. /* 写入报警信息 */
  742. bool ToEQMDataBase::insertAlarmInfo(const AlarmInfo& alarmInfo)
  743. {
  744. if(m_httpApi == nullptr)
  745. {
  746. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  747. return false;
  748. }
  749. nJson json0;
  750. json0["opName"] = "SPSS_InsertToAlarmInfo";
  751. nJson json1;
  752. json1["AlarmID"] = alarmInfo.AlarmID;
  753. json1["StartTime"] = alarmInfo.StartTime;
  754. json1["CreateTime"] = alarmInfo.EventTime;
  755. json1["EndTime"] = alarmInfo.EndTime;
  756. json1["bBox"] = alarmInfo.BboxList;
  757. json1["PicUrl"] = alarmInfo.PicUrl;
  758. json1["AppID"] = alarmInfo.AppID;
  759. json1["ActionID"] = alarmInfo.ActionID;
  760. json1["ActionDes"] = alarmInfo.ActionDes;
  761. json1["CamerID"] = alarmInfo.DeviceID;
  762. json1["RoomID"] = alarmInfo.RoomID;
  763. json1["chnID"] = alarmInfo.ChannelID;
  764. json1["Stat"] = alarmInfo.State;
  765. json0["FaceIDList"] = alarmInfo.FaceIDList;
  766. json0["FaceNameList"] = alarmInfo.FaceNameList;
  767. json0["OnWork"] = alarmInfo.OnWork;
  768. json0["paramList"] = json1;
  769. QString strCmd = QString::fromStdString(json0.dump());
  770. QString strRet;
  771. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet);
  772. if(ret < 0)
  773. {
  774. SPDLOG_LOGGER_WARN(m_logger,"插入报警信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  775. return false;
  776. }
  777. SPDLOG_LOGGER_DEBUG(m_logger,"插入报警信息成功!");
  778. return true;
  779. }
  780. /* 更新报警结束时间 */
  781. bool ToEQMDataBase::updateAlarmEndTime(const AlarmInfo& alarmInfo)
  782. {
  783. if(m_httpApi == nullptr)
  784. {
  785. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  786. return false;
  787. }
  788. nJson json0;
  789. json0["opName"] = "SPSS_UpdateAlarmEndTime";
  790. nJson json1;
  791. json1["EndTime"] = alarmInfo.EndTime;
  792. json1["ChannelID"] = alarmInfo.ChannelID;
  793. json1["RoomID"] = alarmInfo.RoomID;
  794. json1["CamerID"] = alarmInfo.DeviceID;
  795. json1["ActionID"] = alarmInfo.ActionID;
  796. json0["paramList"] = json1;
  797. QString strCmd = QString::fromStdString(json0.dump());
  798. QString strRet;
  799. int ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Update, strCmd, strRet);
  800. if(ret < 0)
  801. {
  802. SPDLOG_LOGGER_WARN(m_logger,"更新报警结束时间失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  803. return false;
  804. }
  805. return true;
  806. }
  807. /* 获取报警规则表 */
  808. bool ToEQMDataBase::getAlarmRuleInfo(std::vector<AlarmRuleInfo>& vecInfo)
  809. {
  810. nJson json0;
  811. json0["opName"] = "SPSS_SelectFromAlarmRule";
  812. QString strCmd = QString::fromStdString(json0.dump());
  813. QString strRet;
  814. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  815. if(ret < 0)
  816. {
  817. SPDLOG_LOGGER_DEBUG(m_logger,"获取AlarmRule失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  818. return false;
  819. }
  820. try
  821. {
  822. nJson json1 = nJson::parse(strRet.toStdString());
  823. int retCode = json1["code"].get<int>();
  824. if(retCode != 0)
  825. {
  826. SPDLOG_LOGGER_ERROR(m_logger,"获取tAlarmRule失败");
  827. return false;
  828. }
  829. nJson result = json1["result"];
  830. if(result.empty())
  831. {
  832. return false;
  833. }
  834. for(const auto& it : result)
  835. {
  836. AlarmRuleInfo info;
  837. info.RuleName = it["ruleName"].get<std::string>();
  838. info.LiveMinEnable = it["liveMinEnable"].get<bool>();
  839. info.LiveMaxEnable = it["liveMaxEnable"].get<bool>();
  840. info.DicMinEnable = it["dicMinEnable"].get<bool>();
  841. info.DicMaxEnable = it["dicMaxEnable"].get<bool>();
  842. info.LiveDicMinEnable = it["liveDicMinEnable"].get<bool>();
  843. info.LiveDicMaxEnable = it["liveDicMaxEnable"].get<bool>();
  844. info.LiveMin = it["liveMin"].get<int>();
  845. info.LiveMax = it["liveMax"].get<int>();
  846. info.DicMin = it["dicMin"].get<int>();
  847. info.DicMax = it["dicMax"].get<int>();
  848. info.LiveDicMin = it["liveDicMin"].get<int>();
  849. info.LiveDicMax = it["liveDicMax"].get<int>();
  850. vecInfo.push_back(info);
  851. }
  852. }
  853. catch (const nJson::parse_error& e) {
  854. SPDLOG_LOGGER_ERROR(m_logger,"解析AlarmRule数据失败:{}, 错误ID:{}",e.what(), e.id);
  855. return false;
  856. }
  857. catch(const nJson::type_error& e)
  858. {
  859. SPDLOG_LOGGER_ERROR(m_logger,"解析AlarmRule数据失败:{}, 错误ID:{}",e.what(), e.id);
  860. return false;
  861. }
  862. return true;
  863. }