ToEQMDataBase.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. #include "ToEQMDataBase.h"
  2. #include <QJsonDocument>
  3. #include <QJsonObject>
  4. #include <QVector>
  5. #include <QString>
  6. ToEQMDataBase::ToEQMDataBase()
  7. {
  8. m_logger = spdlog::get("ToEQMDataBase");
  9. if(m_logger == nullptr)
  10. {
  11. SPDLOG_ERROR("ToEQMDataBase logger is nullptr");
  12. return;
  13. }
  14. }
  15. ToEQMDataBase::~ToEQMDataBase()
  16. {
  17. if(m_httpApi != nullptr)
  18. {
  19. delete m_httpApi;
  20. m_httpApi = nullptr;
  21. }
  22. }
  23. /* 初始化WebApi */
  24. bool ToEQMDataBase::initWebApi(const QString& url, const QString& serverIP, const QString& serID)
  25. {
  26. if(m_httpApi == nullptr)
  27. {
  28. m_httpApi = new lhhttpapi;
  29. }
  30. int ret = 0;
  31. ret = m_httpApi->DBQInit(url);
  32. if(ret < 0)
  33. {
  34. SPDLOG_LOGGER_ERROR(m_logger,"Init WebApi failed:{}, error Info:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  35. return false;
  36. }
  37. // SPDLOG_LOGGER_TRACE(m_logger,"初始化WebApi成功!");
  38. QString serverList;
  39. ret = m_httpApi->DBQGetServerList(serverList);
  40. if(ret < 0)
  41. {
  42. SPDLOG_LOGGER_DEBUG(m_logger,"Get server list failed:{}, error info:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  43. return false;
  44. }
  45. SPDLOG_LOGGER_TRACE(m_logger,"Server list:{}",serverList.toStdString());
  46. SPDLOG_LOGGER_DEBUG(m_logger,"WebAPI Sucess!");
  47. /* 登录,第二个参数是限制的服务 */
  48. ret = m_httpApi->DBQLogin(serverIP, serID, "SPSS", m_userToken);
  49. if(ret < 0)
  50. {
  51. SPDLOG_LOGGER_ERROR(m_logger,"Login failed:{}, error info:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  52. return false;
  53. }
  54. SPDLOG_LOGGER_TRACE(m_logger,"Login sucess!");
  55. return true;
  56. }
  57. /**
  58. * @brief 写入算法信息,写入tAction表
  59. *
  60. * @param vecInfo 要写入的表格数据
  61. * @param vecNowInfo 现有的表格数据,主要是为了重复利用已经删除的自增主键而传入的
  62. * @return true
  63. * @return false
  64. */
  65. bool ToEQMDataBase::writeAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo)
  66. {
  67. if(m_httpApi == nullptr)
  68. {
  69. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  70. return false;
  71. }
  72. /* 取出可用的自增主键,从0开始计算 */
  73. // std::list<int> listPKID;
  74. // int nPKID = 0;
  75. // for(const auto& it : vecNowInfo)
  76. // {
  77. // while(it.ActionTaskID == nPKID)
  78. // {
  79. // if(it.PKID != nPKID)
  80. // {
  81. // listPKID.push_back(nPKID);
  82. // break;
  83. // }
  84. // nPKID ++;
  85. // }
  86. // }
  87. /* 循环写入数据 */
  88. for(const auto& it : vecInfo)
  89. {
  90. QString retStr;
  91. /* 操作名称,现在一次性将设备位置和线条信息都下载下来 */
  92. nJson json0;
  93. json0["opName"] = "SPSS_InsertToAction";
  94. nJson json1;
  95. json1["actionID"] = it.ActionID; /* 算法ID */
  96. json1["actionName"] = it.ActionName; /* 算法名称 */
  97. json1["actionTaskID"] = it.ActionTaskID; /* 算法类型 */
  98. json0["paramList"] = json1;
  99. QString strCmd = QString::fromStdString(json0.dump());
  100. int ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, retStr);
  101. if(ret < 0)
  102. {
  103. SPDLOG_LOGGER_DEBUG(m_logger,"写入tAction失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  104. }
  105. SPDLOG_LOGGER_DEBUG(m_logger,"写入一条算法 {} 到 tAction成功!", it.ActionID);
  106. }
  107. return true;
  108. }
  109. /* 删除算法信息 */
  110. bool ToEQMDataBase::deleteAlgorithmInfo(std::vector<AlgorithmInfo>& vecDeleteInfo)
  111. {
  112. if(m_httpApi == nullptr)
  113. {
  114. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  115. return false;
  116. }
  117. for(const auto& it : vecDeleteInfo)
  118. {
  119. nJson json0;
  120. json0["opName"] = "SPSS_DeleteFromAction";
  121. nJson json1;
  122. json1["actionID"] = it.ActionID;
  123. json0["paramList"] = json1;
  124. QString strCmd = QString::fromStdString(json0.dump());
  125. QString strRet;
  126. int ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet);
  127. if(ret < 0)
  128. {
  129. SPDLOG_LOGGER_DEBUG(m_logger,"删除tAction失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  130. }
  131. SPDLOG_LOGGER_DEBUG(m_logger,"从tAction 删除算法 {} 成功!", it.ActionID);
  132. }
  133. return true;
  134. }
  135. /* 获取tAction数据 */
  136. bool ToEQMDataBase::getAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo)
  137. {
  138. if(m_httpApi == nullptr)
  139. {
  140. SPDLOG_LOGGER_ERROR(m_logger, "WebAPI is nullptr");
  141. return false;
  142. }
  143. /* 清空内容 */
  144. vecInfo.clear();
  145. nJson json0;
  146. json0["opName"] = "SPSS_SelectFromAction";
  147. QString strCmd = QString::fromStdString(json0.dump());
  148. QString strRet;
  149. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  150. if(ret < 0)
  151. {
  152. SPDLOG_LOGGER_DEBUG(m_logger,"获取tAction失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  153. return false;
  154. }
  155. /* 解析获取到的JSON数据 */
  156. // SPDLOG_LOGGER_DEBUG(m_logger,"\n{}",strRet.toStdString());
  157. try {
  158. nJson json1 = nJson::parse(strRet.toStdString());
  159. int retCode = json1["code"].get<int>();
  160. if(retCode != 0)
  161. {
  162. SPDLOG_LOGGER_ERROR(m_logger,"获取tAction失败");
  163. return false;
  164. }
  165. nJson result = json1["result"];
  166. for(auto& it : result)
  167. {
  168. AlgorithmInfo info;
  169. info.ActionID = it["actionId"].is_null() ? "" : it["actionId"].get<std::string>();
  170. info.ActionName = it["actionName"].get<std::string>();
  171. info.ActionTaskID = it["actionTaskid"].get<int>();
  172. // SPDLOG_LOGGER_DEBUG(m_logger,"ActionID:{}, ActionName:{}, ActionTaskID:{}",info.ActionID,info.ActionName,info.ActionTaskID);
  173. vecInfo.push_back(info);
  174. }
  175. } catch (const nJson::parse_error& e) {
  176. SPDLOG_LOGGER_ERROR(m_logger,"解析tAction数据失败:{}",e.what());
  177. return false;
  178. }
  179. return true;
  180. }
  181. /* 插入设备信息 */
  182. bool ToEQMDataBase::insertDeviceInfo(std::vector<DeviceInfo>& vecInfo)
  183. {
  184. if(m_httpApi == nullptr)
  185. {
  186. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  187. return false;
  188. }
  189. if(vecInfo.empty())
  190. {
  191. return false;
  192. }
  193. bool isSuccess = true;
  194. for(const auto& it : vecInfo)
  195. {
  196. nJson json0;
  197. json0["opName"] = "SPSS_InsertToCamerInfo";
  198. nJson json1;
  199. /* 这里不知道是不是“?”运算符的等级是不是不够,不加括号会报错 */
  200. json1["camerID"] = it.DeviceID;
  201. json1["camerName"] = it.DeviceName;
  202. json1["camerIP"] = it.DeviceIP;
  203. json1["camerPort"] = it.DevicePort;
  204. json1["camerUser"] = it.UserAccount;
  205. json1["camerPwd"] = it.UserPassword;
  206. json1["camerType"] = it.DeviceType;
  207. json1["camerSerial"] = it.DeviceSerial;
  208. json1["camerChannel"] = nullptr;
  209. json1["camerUrl"] = nullptr;
  210. json0["paramList"] = json1;
  211. QString strCmd = QString::fromStdString(json0.dump());
  212. QString strRet;
  213. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet);
  214. if(ret < 0)
  215. {
  216. SPDLOG_LOGGER_DEBUG(m_logger,"插入设备信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  217. isSuccess = false;
  218. }
  219. SPDLOG_LOGGER_DEBUG(m_logger,"插入ID {} 信息到 CamerInfo 成功!", it.DeviceID);
  220. }
  221. /* 插入信息到tActionCamer表 */
  222. for(const auto& it0 : vecInfo)
  223. {
  224. for(const auto& it1 : it0.vecAlgorithmInfo)
  225. {
  226. nJson json0;
  227. json0["opName"] = "SPSS_InsertToActionCamer";
  228. nJson json1;
  229. json1["actionID"] = it1.ActionID;
  230. json1["camerID"] = it0.DeviceID;
  231. json0["paramList"] = json1;
  232. QString strCmd = QString::fromStdString(json0.dump());
  233. QString strRet;
  234. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet);
  235. if(ret < 0)
  236. {
  237. SPDLOG_LOGGER_DEBUG(m_logger,"插入设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  238. isSuccess = false;
  239. }
  240. SPDLOG_LOGGER_DEBUG(m_logger,"插入DeviceID {} 和 ActionID {} 到 ActionCamer 成功!", it0.DeviceID, it1.ActionID);
  241. }
  242. }
  243. return isSuccess;
  244. }
  245. /* 更新设备信息 */
  246. bool ToEQMDataBase::updateDeviceInfo(std::vector<DeviceInfo>& vecUpdateInfo)
  247. {
  248. if(m_httpApi == nullptr)
  249. {
  250. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  251. return false;
  252. }
  253. if(vecUpdateInfo.empty())
  254. {
  255. return false;
  256. }
  257. bool isSuccess = true;
  258. for(const auto& it : vecUpdateInfo)
  259. {
  260. nJson json0;
  261. json0["opName"] = "SPSS_UpdateToCamerInfo";
  262. nJson json1;
  263. json1["camerID"] = it.DeviceID;
  264. json1["camerName"] = it.DeviceName;
  265. json1["camerIP"] = it.DeviceIP;
  266. json1["camerPort"] = it.DevicePort;
  267. json1["camerUser"] = it.UserAccount;
  268. json1["camerPwd"] = it.UserPassword;
  269. json1["camerType"] = it.DeviceType;
  270. json1["camerSerial"] = it.DeviceSerial;
  271. json1["camerChannel"] = nullptr;
  272. json1["camerUrl"] = nullptr;
  273. json0["paramList"] = json1;
  274. QString strCmd = QString::fromStdString(json0.dump());
  275. QString strRet;
  276. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Update, strCmd, strRet);
  277. if(ret < 0)
  278. {
  279. SPDLOG_LOGGER_DEBUG(m_logger,"更新设备信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  280. isSuccess = false;
  281. }
  282. SPDLOG_LOGGER_DEBUG(m_logger,"更新ID {} 信息到 CamerInfo 成功!", it.DeviceID);
  283. }
  284. return isSuccess;
  285. }
  286. /* 删除设备信息 */
  287. bool ToEQMDataBase::deleteDeviceInfo(std::vector<DeviceInfo>& vecDeleteInfo)
  288. {
  289. if(m_httpApi == nullptr)
  290. {
  291. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  292. return false;
  293. }
  294. if(vecDeleteInfo.empty())
  295. {
  296. return false;
  297. }
  298. bool isSuccess = true;
  299. for(const auto& it : vecDeleteInfo)
  300. {
  301. nJson json0;
  302. json0["opName"] = "SPSS_DeleteFromCamerInfo";
  303. nJson json1;
  304. json1["camerID"] = it.DeviceID;
  305. json0["paramList"] = json1;
  306. QString strCmd = QString::fromStdString(json0.dump());
  307. QString strRet;
  308. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet);
  309. if(ret < 0)
  310. {
  311. SPDLOG_LOGGER_DEBUG(m_logger,"删除设备信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  312. isSuccess = false;
  313. }
  314. SPDLOG_LOGGER_DEBUG(m_logger,"删除数据 {} 到 CamerInfo 成功!", it.DeviceID);
  315. }
  316. return isSuccess;
  317. }
  318. /* 从EQM获取CamerInfo信息 */
  319. bool ToEQMDataBase::getDeviceInfo(std::vector<DeviceInfo>& vecInfo)
  320. {
  321. if(m_httpApi == nullptr)
  322. {
  323. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  324. return false;
  325. }
  326. vecInfo.clear();
  327. nJson json0;
  328. json0["opName"] = "SPSS_SelectFromCamerInfo";
  329. QString strCmd = QString::fromStdString(json0.dump());
  330. QString strRet;
  331. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  332. if(ret < 0)
  333. {
  334. SPDLOG_LOGGER_DEBUG(m_logger,"获取CamerInfo失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  335. return false;
  336. }
  337. /* 解析信息 */
  338. try {
  339. nJson json1 = nJson::parse(strRet.toStdString());
  340. int retCode = json1["code"].get<int>();
  341. if(retCode != 0)
  342. {
  343. SPDLOG_LOGGER_ERROR(m_logger,"获取CamerInfo失败");
  344. return false;
  345. }
  346. nJson result = json1["result"];
  347. if(result.empty())
  348. {
  349. return false;
  350. }
  351. for(const auto& it : result)
  352. {
  353. // SPDLOG_LOGGER_DEBUG(m_logger,"camerID:{}",it["camerId"].get<int>());
  354. DeviceInfo info;
  355. info.DeviceID = it["camerId"].get<int>();
  356. info.DeviceName = it["camerName"].is_null() ? "" : it["camerName"].get<std::string>();
  357. info.DeviceIP = it["camerIp"].is_null() ? "" : it["camerIp"].get<std::string>();
  358. info.DevicePort = it["camerPort"].is_null() ? 0 : it["camerPort"].get<int>();
  359. info.UserAccount = it["camerUsr"].is_null() ? "" : it["camerUsr"].get<std::string>();
  360. info.UserPassword = it["camerPwd"].is_null() ? "" : it["camerPwd"].get<std::string>();
  361. info.DeviceType = it["camerType"].is_null() ? "" : it["camerType"].get<std::string>();
  362. info.DeviceSerial = it["camerSerial"].is_null() ? "" : it["camerSerial"].get<std::string>();
  363. vecInfo.push_back(info);
  364. }
  365. }
  366. catch (const nJson::parse_error& e) {
  367. SPDLOG_LOGGER_ERROR(m_logger,"解析CamerInfo数据失败:{}, 错误ID:{}",e.what(), e.id);
  368. return false;
  369. }
  370. catch (const nJson::type_error& e) {
  371. SPDLOG_LOGGER_ERROR(m_logger,"解析CamerInfo数据失败:{}, 错误ID:{}",e.what(), e.id);
  372. return false;
  373. }
  374. return true;
  375. }
  376. /* 插入设备和算法关联信息 */
  377. bool ToEQMDataBase::insertDeviceAlgorithmInfo(std::vector<DeviceInfo>& vecInfo)
  378. {
  379. if(m_httpApi == nullptr)
  380. {
  381. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  382. return false;
  383. }
  384. /* 插入信息到tActionCamer表 */
  385. bool isSuccess = true;
  386. for(const auto& it0 : vecInfo)
  387. {
  388. for(const auto& it1 : it0.vecAlgorithmInfo)
  389. {
  390. nJson json0;
  391. json0["opName"] = "SPSS_InsertToActionCamer";
  392. nJson json1;
  393. json1["actionID"] = it1.ActionID;
  394. json1["camerID"] = it0.DeviceID;
  395. json0["paramList"] = json1;
  396. QString strCmd = QString::fromStdString(json0.dump());
  397. QString strRet;
  398. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet);
  399. if(ret < 0)
  400. {
  401. SPDLOG_LOGGER_DEBUG(m_logger,"插入设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  402. isSuccess = false;
  403. }
  404. SPDLOG_LOGGER_DEBUG(m_logger,"插入DeviceID {} 和 ActionID {} 到 ActionCamer 成功!", it0.DeviceID, it1.ActionID);
  405. }
  406. }
  407. return true;
  408. }
  409. /* 更新设备和算法关联表 */
  410. bool ToEQMDataBase::updateDeviceAlgorithmInfo(std::vector<DeviceInfo>& vecInfo)
  411. {
  412. if(m_httpApi == nullptr)
  413. {
  414. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  415. return false;
  416. }
  417. /* 更新之前先删除相关的信息 */
  418. for(const auto& it : vecInfo)
  419. {
  420. nJson json0;
  421. json0["opName"] = "SPSS_DeleteFromActionCamer";
  422. nJson json1;
  423. json1["camerID"] = it.DeviceID;
  424. json0["paramList"] = json1;
  425. QString strCmd = QString::fromStdString(json0.dump());
  426. QString strRet;
  427. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet);
  428. if(ret < 0)
  429. {
  430. SPDLOG_LOGGER_DEBUG(m_logger,"删除设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  431. // return false;
  432. }
  433. SPDLOG_LOGGER_DEBUG(m_logger,"从 ActionCamer 删除DeviceID {} 成功!", it.DeviceID);
  434. }
  435. /* 插入信息到tActionCamer表 */
  436. bool isSuccess = true;
  437. for(const auto& it0 : vecInfo)
  438. {
  439. for(const auto& it1 : it0.vecAlgorithmInfo)
  440. {
  441. nJson json0;
  442. json0["opName"] = "SPSS_InsertToActionCamer";
  443. nJson json1;
  444. json1["actionID"] = it1.ActionID;
  445. json1["camerID"] = it0.DeviceID;
  446. json0["paramList"] = json1;
  447. QString strCmd = QString::fromStdString(json0.dump());
  448. QString strRet;
  449. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet);
  450. if(ret < 0)
  451. {
  452. SPDLOG_LOGGER_DEBUG(m_logger,"插入设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  453. isSuccess = false;
  454. }
  455. SPDLOG_LOGGER_DEBUG(m_logger,"插入DeviceID {} ActionID {} 到 ActionCamer 成功!", it0.DeviceID, it1.ActionID);
  456. }
  457. }
  458. return isSuccess;
  459. }
  460. /* 删除设备和算法关联表 */
  461. bool ToEQMDataBase::deleteDeviceAlgorithmInfo(std::vector<DeviceInfo>& vecInfo)
  462. {
  463. if(m_httpApi == nullptr)
  464. {
  465. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  466. return false;
  467. }
  468. bool isSuccess = true;
  469. /* 更新之前先删除相关的信息 */
  470. for(const auto& it : vecInfo)
  471. {
  472. nJson json0;
  473. json0["opName"] = "SPSS_DeleteFromActionCamer";
  474. nJson json1;
  475. json1["camerID"] = it.DeviceID;
  476. json0["paramList"] = json1;
  477. QString strCmd = QString::fromStdString(json0.dump());
  478. QString strRet;
  479. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet);
  480. if(ret < 0)
  481. {
  482. SPDLOG_LOGGER_DEBUG(m_logger,"删除设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  483. // return false;
  484. isSuccess = false;
  485. }
  486. SPDLOG_LOGGER_DEBUG(m_logger,"从 ActionCamer 删除DeviceID {} 成功!", it.DeviceID);
  487. }
  488. return isSuccess;
  489. }
  490. /* 删除设备和算法关联表 */
  491. bool ToEQMDataBase::deleteDeviceAlgorithmInfo(std::list<int>& vecID)
  492. {
  493. if(m_httpApi == nullptr)
  494. {
  495. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  496. return false;
  497. }
  498. bool isSuccess = true;
  499. /* 更新之前先删除相关的信息 */
  500. for(const auto& it : vecID)
  501. {
  502. nJson json0;
  503. json0["opName"] = "SPSS_DeleteFromActionCamer";
  504. nJson json1;
  505. json1["camerID"] = it;
  506. json0["paramList"] = json1;
  507. QString strCmd = QString::fromStdString(json0.dump());
  508. QString strRet;
  509. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet);
  510. if(ret < 0)
  511. {
  512. SPDLOG_LOGGER_DEBUG(m_logger,"删除设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  513. // return false;
  514. isSuccess = false;
  515. }
  516. SPDLOG_LOGGER_DEBUG(m_logger,"从 ActionCamer 删除DeviceID {} 成功!", it);
  517. }
  518. return isSuccess;
  519. }
  520. /* 获取设备和算法信息关联表,需要先从EQM数据库中获取到设备信息 */
  521. bool ToEQMDataBase::getDeviceAlgorithmInfo(std::vector<DeviceInfo>& vecInfo, std::list<int>& listDevIDDelete)
  522. {
  523. if(m_httpApi == nullptr)
  524. {
  525. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  526. return false;
  527. }
  528. if(vecInfo.empty())
  529. {
  530. SPDLOG_LOGGER_WARN(m_logger,"Device info is empty");
  531. return false;
  532. }
  533. listDevIDDelete.clear();
  534. nJson json0;
  535. json0["opName"] = "SPSS_SelectFromActionCamer";
  536. QString strCmd = QString::fromStdString(json0.dump());
  537. QString strRet;
  538. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  539. if(ret < 0)
  540. {
  541. SPDLOG_LOGGER_DEBUG(m_logger,"获取ActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  542. return false;
  543. }
  544. /* 设备和算法关联信息容器 */
  545. std::vector<DeviceAlgorithmInfo> vecNewInfo;
  546. /* 解析信息 */
  547. try {
  548. nJson json1 = nJson::parse(strRet.toStdString());
  549. int retCode = json1["code"].get<int>();
  550. if(retCode != 0)
  551. {
  552. SPDLOG_LOGGER_ERROR(m_logger,"获取ActionCamer失败");
  553. return false;
  554. }
  555. nJson result = json1["result"];
  556. if(result.empty())
  557. {
  558. return false;
  559. }
  560. /* 取出所有的设备算法信息 */
  561. for(const auto& it : result)
  562. {
  563. DeviceAlgorithmInfo info;
  564. info.DeviceID = it["camerId"].get<int>();
  565. info.ActionID = it["actionId"].is_null() ? "" : it["actionId"].get<std::string>();
  566. info.ActionName = it["actionName"].is_null() ? "" : it["actionName"].get<std::string>();
  567. info.ActionTaskID = it["actionTaskid"].is_null() ? 0 : it["actionTaskid"].get<int>();
  568. vecNewInfo.push_back(info);
  569. }
  570. }
  571. catch (const nJson::parse_error& e) {
  572. SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}, 错误ID:{}",e.what(), e.id);
  573. return false;
  574. }
  575. catch (const nJson::type_error& e) {
  576. SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}, 错误ID:{}",e.what(), e.id);
  577. return false;
  578. }
  579. /* 根据缓存中的设备ID取出关联的算法 */
  580. for(auto& it0 : vecInfo)
  581. {
  582. for(auto& it1 : vecNewInfo)
  583. {
  584. /* 对比设备ID */
  585. if(it0.DeviceID == it1.DeviceID)
  586. {
  587. AlgorithmInfo info;
  588. info.ActionID = it1.ActionID;
  589. info.ActionName = it1.ActionName;
  590. info.ActionTaskID = it1.ActionTaskID;
  591. it0.vecAlgorithmInfo.push_back(info);
  592. }
  593. }
  594. }
  595. /* 取出关联表中已经消失的设备ID,在本轮循环中删除 */
  596. for(const auto& it : vecNewInfo)
  597. {
  598. bool isExist = false;
  599. for(const auto& it0 : vecInfo)
  600. {
  601. if(it.DeviceID == it0.DeviceID)
  602. {
  603. isExist = true;
  604. break;
  605. }
  606. }
  607. if(!isExist)
  608. {
  609. /* 检查是否重复,一个ID只需要记录一次 */
  610. if(std::find(listDevIDDelete.begin(), listDevIDDelete.end(), it.DeviceID) == listDevIDDelete.end())
  611. {
  612. listDevIDDelete.push_back(it.DeviceID);
  613. }
  614. }
  615. }
  616. return true;
  617. }
  618. /* 获取设备和算法关联信息,包括频率信息,房间信息,重载版,只获取关联关系 */
  619. bool ToEQMDataBase::getActionInfo(ListActionInfo& listInfo)
  620. {
  621. if(m_httpApi == nullptr)
  622. {
  623. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  624. return false;
  625. }
  626. listInfo.clear();
  627. nJson json0;
  628. json0["opName"] = "SPSS_SelectFromActionInfo";
  629. QString strCmd = QString::fromStdString(json0.dump());
  630. QString strRet;
  631. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  632. if(ret < 0)
  633. {
  634. SPDLOG_LOGGER_DEBUG(m_logger,"获取ActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  635. return false;
  636. }
  637. /* 解析信息 */
  638. try
  639. {
  640. nJson json1 = nJson::parse(strRet.toStdString());
  641. int retCode = json1["code"].get<int>();
  642. if(retCode != 0)
  643. {
  644. SPDLOG_LOGGER_ERROR(m_logger,"获取ActionCamer失败");
  645. return false;
  646. }
  647. nJson result = json1["result"];
  648. if(result.empty())
  649. {
  650. return false;
  651. }
  652. /* 取出所有的设备算法信息 */
  653. for(const auto& it : result)
  654. {
  655. ActionInfo info;
  656. info.CameraID = it["camerId"].is_null() ? -1 : it["camerId"].get<int>();
  657. info.ActionID = it["actionId"].is_null() ? "" : it["actionId"].get<std::string>();
  658. info.RoomID = it["roomId"].is_null() ? -1 : it["roomId"].get<int>();
  659. info.ChannelID = it["chnid"].is_null() ? -1 : it["chnid"].get<int>();
  660. info.RoomType = it["rtype"].is_null() ? -1 : it["rtype"].get<int>();
  661. info.strRoomName = it["rname"].is_null() ? "" : it["rname"].get<std::string>();
  662. listInfo.insertActionInfo(&info);
  663. }
  664. }
  665. catch (const nJson::parse_error& e) {
  666. SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}, 错误ID:{}",e.what(), e.id);
  667. return false;
  668. }
  669. catch (const nJson::type_error& e) {
  670. SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}, 错误ID:{}",e.what(), e.id);
  671. return false;
  672. }
  673. return true;
  674. }
  675. /* 获取摄像机和房间关联信息 */
  676. bool ToEQMDataBase::getRoomCameraInfo(std::list<RoomCameraInfo>& vecInfo)
  677. {
  678. if(m_httpApi == nullptr)
  679. {
  680. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  681. return false;
  682. }
  683. vecInfo.clear();
  684. nJson json0;
  685. json0["opName"] = "SPSS_SelectRoomCamer";
  686. QString strCmd = QString::fromStdString(json0.dump());
  687. QString strRet;
  688. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  689. if(ret < 0)
  690. {
  691. SPDLOG_LOGGER_DEBUG(m_logger,"获取RoomCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  692. return false;
  693. }
  694. try
  695. {
  696. nJson json1 = nJson::parse(strRet.toStdString());
  697. int retCode = json1["code"].get<int>();
  698. if(retCode != 0)
  699. {
  700. SPDLOG_LOGGER_ERROR(m_logger,"获取RoomCamer失败");
  701. return false;
  702. }
  703. nJson result = json1["result"];
  704. if(result.empty())
  705. {
  706. return false;
  707. }
  708. for(const auto& it : result)
  709. {
  710. auto roomID = it["roomId"].get<int>();
  711. auto cameraID = it["camerId"].is_null() ? -1 : it["camerId"].get<int>();
  712. /* 先检查RoomID是否已经在列表中了 */
  713. bool isExist = false;
  714. for(auto& it0 : vecInfo)
  715. {
  716. if(it0.RoomID == roomID)
  717. {
  718. it0.listCameraID.push_back(cameraID);
  719. isExist = true;
  720. break;
  721. }
  722. }
  723. /* 没有找到RoomID,创建一个新的 */
  724. if(!isExist)
  725. {
  726. RoomCameraInfo info;
  727. info.RoomID = roomID;
  728. info.listCameraID.push_back(cameraID);
  729. }
  730. }
  731. }
  732. catch (const nJson::parse_error& e) {
  733. SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}, 错误ID:{}",e.what(), e.id);
  734. return false;
  735. }
  736. catch (const nJson::type_error& e) {
  737. SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}, 错误ID:{}",e.what(), e.id);
  738. return false;
  739. }
  740. return true;
  741. }
  742. /* 写入报警信息 */
  743. bool ToEQMDataBase::insertAlarmInfo(const AlarmInfo& alarmInfo)
  744. {
  745. if(m_httpApi == nullptr)
  746. {
  747. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  748. return false;
  749. }
  750. nJson json0;
  751. json0["opName"] = "SPSS_InsertToAlarmInfo";
  752. nJson json1;
  753. json1["AlarmID"] = alarmInfo.AlarmID;
  754. json1["StartTime"] = alarmInfo.StartTime;
  755. json1["CreateTime"] = alarmInfo.EventTime;
  756. json1["EndTime"] = alarmInfo.EndTime;
  757. json1["bBox"] = alarmInfo.BboxList;
  758. json1["PicUrl"] = alarmInfo.PicUrl;
  759. json1["AppID"] = alarmInfo.AppID;
  760. json1["ActionID"] = alarmInfo.ActionID;
  761. json1["ActionDes"] = alarmInfo.ActionDes;
  762. json1["CamerID"] = alarmInfo.DeviceID;
  763. json1["RoomID"] = alarmInfo.RoomID;
  764. json1["chnID"] = alarmInfo.ChannelID;
  765. json1["Stat"] = alarmInfo.State;
  766. json0["FaceIDList"] = alarmInfo.FaceIDList;
  767. json0["FaceNameList"] = alarmInfo.FaceNameList;
  768. json0["OnWork"] = alarmInfo.OnWork;
  769. json0["paramList"] = json1;
  770. QString strCmd = QString::fromStdString(json0.dump());
  771. QString strRet;
  772. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet);
  773. if(ret < 0)
  774. {
  775. SPDLOG_LOGGER_WARN(m_logger,"插入报警信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  776. return false;
  777. }
  778. SPDLOG_LOGGER_DEBUG(m_logger,"插入报警信息成功!");
  779. return true;
  780. }
  781. /* 更新报警结束时间 */
  782. bool ToEQMDataBase::updateAlarmEndTime(const AlarmInfo& alarmInfo)
  783. {
  784. if(m_httpApi == nullptr)
  785. {
  786. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  787. return false;
  788. }
  789. nJson json0;
  790. json0["opName"] = "SPSS_UpdateAlarmEndTime";
  791. nJson json1;
  792. json1["EndTime"] = alarmInfo.EndTime;
  793. json1["ChannelID"] = alarmInfo.ChannelID;
  794. json1["RoomID"] = alarmInfo.RoomID;
  795. json1["CamerID"] = alarmInfo.DeviceID;
  796. json1["ActionID"] = alarmInfo.ActionID;
  797. json0["paramList"] = json1;
  798. QString strCmd = QString::fromStdString(json0.dump());
  799. QString strRet;
  800. int ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Update, strCmd, strRet);
  801. if(ret < 0)
  802. {
  803. SPDLOG_LOGGER_WARN(m_logger,"更新报警结束时间失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  804. return false;
  805. }
  806. return true;
  807. }
  808. /* 获取报警规则表 */
  809. bool ToEQMDataBase::getAlarmRuleInfo(std::vector<AlarmRuleInfo>& vecInfo)
  810. {
  811. nJson json0;
  812. json0["opName"] = "SPSS_SelectFromAlarmRule";
  813. QString strCmd = QString::fromStdString(json0.dump());
  814. QString strRet;
  815. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  816. if(ret < 0)
  817. {
  818. SPDLOG_LOGGER_DEBUG(m_logger,"获取AlarmRule失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  819. return false;
  820. }
  821. try
  822. {
  823. nJson json1 = nJson::parse(strRet.toStdString());
  824. int retCode = json1["code"].get<int>();
  825. if(retCode != 0)
  826. {
  827. SPDLOG_LOGGER_ERROR(m_logger,"获取tAlarmRule失败");
  828. return false;
  829. }
  830. nJson result = json1["result"];
  831. if(result.empty())
  832. {
  833. return false;
  834. }
  835. for(const auto& it : result)
  836. {
  837. AlarmRuleInfo info;
  838. info.RuleName = it["ruleName"].get<std::string>();
  839. info.LiveMinEnable = it["liveMinEnable"].get<bool>();
  840. info.LiveMaxEnable = it["liveMaxEnable"].get<bool>();
  841. info.DicMinEnable = it["dicMinEnable"].get<bool>();
  842. info.DicMaxEnable = it["dicMaxEnable"].get<bool>();
  843. info.LiveDicMinEnable = it["liveDicMinEnable"].get<bool>();
  844. info.LiveDicMaxEnable = it["liveDicMaxEnable"].get<bool>();
  845. info.LiveMin = it["liveMin"].get<int>();
  846. info.LiveMax = it["liveMax"].get<int>();
  847. info.DicMin = it["dicMin"].get<int>();
  848. info.DicMax = it["dicMax"].get<int>();
  849. info.LiveDicMin = it["liveDicMin"].get<int>();
  850. info.LiveDicMax = it["liveDicMax"].get<int>();
  851. vecInfo.push_back(info);
  852. }
  853. }
  854. catch (const nJson::parse_error& e) {
  855. SPDLOG_LOGGER_ERROR(m_logger,"解析AlarmRule数据失败:{}, 错误ID:{}",e.what(), e.id);
  856. return false;
  857. }
  858. catch(const nJson::type_error& e)
  859. {
  860. SPDLOG_LOGGER_ERROR(m_logger,"解析AlarmRule数据失败:{}, 错误ID:{}",e.what(), e.id);
  861. return false;
  862. }
  863. return true;
  864. }
  865. /* 获取报警时段,也同时获取报警的应用信息 */
  866. bool ToEQMDataBase::getAlarmAppInfo(std::list<AppAndTimeInfo>& listInfo)
  867. {
  868. nJson json0;
  869. json0["opName"] = "SPSS_SelectPeriod";
  870. QString strCmd = QString::fromStdString(json0.dump());
  871. QString strRet;
  872. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  873. if(ret < 0)
  874. {
  875. SPDLOG_LOGGER_DEBUG(m_logger,"获取AlarmApp失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  876. return false;
  877. }
  878. try
  879. {
  880. nJson json1 = nJson::parse(strRet.toStdString());
  881. int retCode = json1["code"].get<int>();
  882. if(retCode != 0)
  883. {
  884. SPDLOG_LOGGER_ERROR(m_logger,"获取tAlarmApp失败");
  885. return false;
  886. }
  887. nJson result = json1["result"];
  888. if(result.empty())
  889. {
  890. return false;
  891. }
  892. listInfo.clear();
  893. for(const auto& it : result)
  894. {
  895. AppAndTimeInfo info;
  896. info.ChannelID = it["chnId"].get<int>();
  897. info.AppType = it["appType"].get<int>();
  898. QString strStart = QString::fromStdString(it["startTime"].get<std::string>());
  899. QString strEnd = QString::fromStdString(it["endTime"].get<std::string>());
  900. info.StartTime = QDateTime::fromString(strStart, "yyyy-MM-ss hh:mm:ss");
  901. info.EndTime = QDateTime::fromString(strEnd, "yyyy-MM-ss hh:mm:ss");
  902. listInfo.push_back(info);
  903. }
  904. }
  905. catch (const nJson::parse_error& e) {
  906. SPDLOG_LOGGER_ERROR(m_logger,"解析AlarmApp数据失败:{}, 错误ID:{}",e.what(), e.id);
  907. return false;
  908. }
  909. catch(const nJson::type_error& e)
  910. {
  911. SPDLOG_LOGGER_ERROR(m_logger,"解析AlarmApp数据失败:{}, 错误ID:{}",e.what(), e.id);
  912. return false;
  913. }
  914. return true;
  915. }
  916. /* 写入在岗信息 */
  917. bool ToEQMDataBase::insertOnWorkInfo(const RoomFaceInfo& onWorkInfo)
  918. {
  919. if(m_httpApi == nullptr)
  920. {
  921. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  922. return false;
  923. }
  924. if(onWorkInfo.listPersonInfo.empty())
  925. {
  926. return false;
  927. }
  928. /* 拼接人脸ID和人脸名称 */
  929. std::string strFaceID;
  930. std::string strFaceName;
  931. for(const auto& it : onWorkInfo.listPersonInfo)
  932. {
  933. strFaceID += it.PersonID + ";";
  934. strFaceName += it.PersonName + ";";
  935. }
  936. /* 去掉最后的“;” */
  937. strFaceID = strFaceID.substr(0, strFaceID.size() - 1);
  938. strFaceName = strFaceName.substr(0, strFaceName.size() - 1);
  939. nJson json0;
  940. json0["opName"] = "SPSS_InsertToOnWork";
  941. nJson json1;
  942. json1["nChID"] = onWorkInfo.ChannelID;
  943. json1["CamerId"] = onWorkInfo.CameraID;
  944. json1["FaceIdList"] = strFaceID;
  945. json1["FaceNameList"] = strFaceName;
  946. json1["StartTime"] = onWorkInfo.StartTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  947. json1["EndTime"] = onWorkInfo.EndTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  948. json1["MaxNumber"] = onWorkInfo.MaxNum;
  949. json1["MinNumber"] = onWorkInfo.MinNum;
  950. json0["paramList"] = json1;
  951. QString strCmd = QString::fromStdString(json0.dump());
  952. QString strRet;
  953. auto ret = m_httpApi->DBQDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet);
  954. if(ret < 0)
  955. {
  956. SPDLOG_LOGGER_WARN(m_logger,"插入在岗信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  957. return false;
  958. }
  959. SPDLOG_LOGGER_DEBUG(m_logger,"插入在岗信息成功!");
  960. return true;
  961. }