FromWebAPI.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868
  1. #include "FromWebAPI.h"
  2. #include <QJsonDocument>
  3. #include <QString>
  4. #include <QUuid>
  5. #include "FuncOnAndOffJob.h"
  6. #include "FuncRegionalPerson.h"
  7. #include "UniversalFunc.h"
  8. FromWebAPI::FromWebAPI()
  9. {
  10. m_logger = spdlog::get("FromWebAPI");
  11. if(m_logger == nullptr)
  12. {
  13. fmt::print("FromWebAPI logger is nullptr");
  14. return;
  15. }
  16. }
  17. FromWebAPI::~FromWebAPI()
  18. {
  19. if(m_httpApi != nullptr)
  20. {
  21. delete m_httpApi;
  22. m_httpApi = nullptr;
  23. }
  24. }
  25. /* 初始化WebApi */
  26. bool FromWebAPI::initWebApi(const QString& url,const QString& serID, const QString appType, const QString& serverIP)
  27. {
  28. if(m_httpApi == nullptr)
  29. {
  30. m_httpApi = new lhhttpapi;
  31. }
  32. int ret = 0;
  33. if(!m_httpApi->Load())
  34. {
  35. SPDLOG_LOGGER_ERROR(m_logger,"Load WebApi failed!");
  36. return false;
  37. }
  38. m_httpApi->DBInit(url.toStdString().c_str());
  39. // if(ret != 0)
  40. // {
  41. // SPDLOG_LOGGER_ERROR(m_logger,"Init WebApi failed:{}, error Info:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  42. // return false;
  43. // }
  44. // SPDLOG_LOGGER_TRACE(m_logger,"初始化WebApi成功!");
  45. /* 获取服务器列表 */
  46. char serverList[8192];
  47. ret = m_httpApi->DBGetServerList(serverList, 8192);
  48. if(ret != 0)
  49. {
  50. SPDLOG_LOGGER_DEBUG(m_logger,"Get server list failed:{}, error info:{}",ret, m_httpApi->DoGetLastError(&ret).toStdString());
  51. return false;
  52. }
  53. // SPDLOG_LOGGER_TRACE(m_logger,"Server list:{}", serverList);
  54. SPDLOG_LOGGER_DEBUG(m_logger,"WebAPI Load Sucess!");
  55. /* 登录,第二个参数是限制的服务 */
  56. ret = m_httpApi->DBLogin(serverIP, serID, appType, m_userToken);
  57. if(ret != 0)
  58. {
  59. SPDLOG_LOGGER_ERROR(m_logger,"Login failed:{}, error info:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  60. return false;
  61. }
  62. SPDLOG_LOGGER_INFO(m_logger,"Login WebAPI sucess!");
  63. return true;
  64. }
  65. /**
  66. * @brief 写入算法信息,写入tAction表
  67. *
  68. * @param vecInfo 要写入的表格数据
  69. * @param vecNowInfo 现有的表格数据,主要是为了重复利用已经删除的自增主键而传入的
  70. * @return true
  71. * @return false
  72. */
  73. bool FromWebAPI::writeAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo)
  74. {
  75. if(m_httpApi == nullptr)
  76. {
  77. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  78. return false;
  79. }
  80. /* 取出可用的自增主键,从0开始计算 */
  81. // std::list<int> listPKID;
  82. // int nPKID = 0;
  83. // for(const auto& it : vecNowInfo)
  84. // {
  85. // while(it.ActionTaskID == nPKID)
  86. // {
  87. // if(it.PKID != nPKID)
  88. // {
  89. // listPKID.push_back(nPKID);
  90. // break;
  91. // }
  92. // nPKID ++;
  93. // }
  94. // }
  95. /* 循环写入数据 */
  96. try
  97. {
  98. nJson json0 = nJson::array();
  99. for(const auto& it : vecInfo)
  100. {
  101. /* 操作名称,现在一次性将设备位置和线条信息都下载下来 */
  102. nJson json1;
  103. json1["opName"] = "SPSS_InsertToAction";
  104. json1["Key"] = QUuid::createUuid().toString().toStdString();
  105. nJson json2;
  106. json2["actionID"] = it.ActionID; /* 算法ID */
  107. json2["actionName"] = it.ActionName; /* 算法名称 */
  108. json2["actionTaskID"] = it.ActionTaskID; /* 算法类型 */
  109. json1["paramList"] = json2;
  110. json0.push_back(json1);
  111. }
  112. QString retStr;
  113. QString strCmd = QString::fromStdString(json0.dump());
  114. int ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, retStr);
  115. if(ret != 0)
  116. {
  117. SPDLOG_LOGGER_ERROR(m_logger,"写入tAction失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  118. }
  119. }
  120. catch (const nJson::parse_error& e) {
  121. SPDLOG_LOGGER_ERROR(m_logger,"创建 算法JSON 数据失败:{}, 错误ID:{}",e.what(), e.id);
  122. return false;
  123. }
  124. catch (const nJson::type_error& e) {
  125. SPDLOG_LOGGER_ERROR(m_logger,"创建 算法JSON 数据失败:{}, 错误ID:{}",e.what(), e.id);
  126. return false;
  127. }
  128. catch(...) {
  129. SPDLOG_LOGGER_ERROR(m_logger,"创建 算法JSON 数据失败");
  130. return false;
  131. }
  132. return true;
  133. }
  134. /* 删除算法信息 */
  135. bool FromWebAPI::deleteAlgorithmInfo(std::vector<AlgorithmInfo>& vecDeleteInfo)
  136. {
  137. if(m_httpApi == nullptr)
  138. {
  139. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  140. return false;
  141. }
  142. try
  143. {
  144. nJson json0 = nJson::array();
  145. for(const auto& it : vecDeleteInfo)
  146. {
  147. nJson json1;
  148. json0["opName"] = "SPSS_DeleteFromAction";
  149. json1["Key"] = QUuid::createUuid().toString().toStdString();
  150. nJson json2;
  151. json2["actionID"] = it.ActionID;
  152. json0["paramList"] = json2;
  153. json0.push_back(json1);
  154. }
  155. QString strCmd = QString::fromStdString(json0.dump());
  156. QString strRet;
  157. int ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet);
  158. if(ret != 0)
  159. {
  160. SPDLOG_LOGGER_ERROR(m_logger,"删除tAction失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  161. return false;
  162. }
  163. }
  164. catch (const nJson::parse_error& e) {
  165. SPDLOG_LOGGER_ERROR(m_logger,"创建 删除算法的JSON 数据失败:{}, 错误ID:{}",e.what(), e.id);
  166. return false;
  167. }
  168. catch (const nJson::type_error& e) {
  169. SPDLOG_LOGGER_ERROR(m_logger,"创建 删除算法的JSON 数据失败:{}, 错误ID:{}",e.what(), e.id);
  170. return false;
  171. }
  172. catch(...) {
  173. SPDLOG_LOGGER_ERROR(m_logger,"创建 删除算法的JSON 数据失败");
  174. return false;
  175. }
  176. return true;
  177. }
  178. /* 获取tAction数据 */
  179. bool FromWebAPI::getAlgorithmInfo(std::vector<AlgorithmInfo>& vecInfo)
  180. {
  181. if(m_httpApi == nullptr)
  182. {
  183. SPDLOG_LOGGER_ERROR(m_logger, "WebAPI is nullptr");
  184. return false;
  185. }
  186. /* 清空内容 */
  187. vecInfo.clear();
  188. nJson json0;
  189. json0["opName"] = "SPSS_SelectFromAction";
  190. QString strCmd = QString::fromStdString(json0.dump());
  191. QString strRet;
  192. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  193. if(ret != 0)
  194. {
  195. SPDLOG_LOGGER_DEBUG(m_logger,"获取tAction失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  196. return false;
  197. }
  198. /* 解析获取到的JSON数据 */
  199. // SPDLOG_LOGGER_DEBUG(m_logger,"\n{}",strRet.toStdString());
  200. try {
  201. nJson json1 = nJson::parse(strRet.toStdString());
  202. int retCode = json1["code"].get<int>();
  203. if(retCode != 0)
  204. {
  205. SPDLOG_LOGGER_ERROR(m_logger,"获取tAction失败");
  206. return false;
  207. }
  208. nJson result = json1["result"];
  209. for(auto& it : result)
  210. {
  211. AlgorithmInfo info;
  212. info.ActionID = it["actionId"].is_null() ? "" : it["actionId"].get<std::string>();
  213. info.ActionName = it["actionName"].get<std::string>();
  214. info.ActionTaskID = it["actionTaskid"].get<int>();
  215. // SPDLOG_LOGGER_DEBUG(m_logger,"ActionID:{}, ActionName:{}, ActionTaskID:{}",info.ActionID,info.ActionName,info.ActionTaskID);
  216. vecInfo.push_back(info);
  217. }
  218. }
  219. catch (const nJson::parse_error& e) {
  220. SPDLOG_LOGGER_ERROR(m_logger,"创建 tAction 数据失败:{}, 错误ID:{}",e.what(), e.id);
  221. return false;
  222. }
  223. catch (const nJson::type_error& e) {
  224. SPDLOG_LOGGER_ERROR(m_logger,"创建 tAction 数据失败:{}, 错误ID:{}",e.what(), e.id);
  225. return false;
  226. }
  227. catch(...) {
  228. SPDLOG_LOGGER_ERROR(m_logger,"创建 tAction 数据失败");
  229. return false;
  230. }
  231. return true;
  232. }
  233. /* 插入设备信息 */
  234. bool FromWebAPI::insertDeviceInfo(std::vector<DeviceInfo>& vecInfo)
  235. {
  236. if(m_httpApi == nullptr)
  237. {
  238. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  239. return false;
  240. }
  241. if(vecInfo.empty())
  242. {
  243. return false;
  244. }
  245. bool isSuccess = true;
  246. try
  247. {
  248. nJson json0 = nJson::array();
  249. for(const auto& it : vecInfo)
  250. {
  251. nJson json1;
  252. json1["opName"] = "SPSS_InsertToCamerInfo";
  253. json1["Key"] = QUuid::createUuid().toString().toStdString();
  254. nJson json2;
  255. /* 这里不知道是不是“?”运算符的等级是不是不够,不加括号会报错 */
  256. json2["camerID"] = it.DeviceID;
  257. json2["camerName"] = it.DeviceName;
  258. json2["camerIP"] = it.DeviceIP;
  259. json2["camerPort"] = it.DevicePort;
  260. json2["camerUser"] = it.UserAccount;
  261. json2["camerPwd"] = it.UserPassword;
  262. json2["camerType"] = it.DeviceType;
  263. json2["camerSerial"] = it.DeviceSerial;
  264. json2["camerChannel"] = nullptr;
  265. json2["camerUrl"] = nullptr;
  266. json1["paramList"] = json2;
  267. json0.push_back(json1);
  268. }
  269. QString strCmd = QString::fromStdString(json0.dump());
  270. QString strRet;
  271. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet);
  272. if(ret != 0)
  273. {
  274. SPDLOG_LOGGER_ERROR(m_logger,"插入设备信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  275. isSuccess = false;
  276. }
  277. // /* 插入信息到tActionCamer表 */
  278. // json0.clear();
  279. // for(const auto& it0 : vecInfo)
  280. // {
  281. // for(const auto& it1 : it0.vecAlgorithmInfo)
  282. // {
  283. // nJson json1;
  284. // json1["opName"] = "SPSS_InsertToActionCamer";
  285. // json1["Key"] = QUuid::createUuid().toString().toStdString();
  286. // nJson json2;
  287. // json2["actionID"] = it1.ActionID;
  288. // json2["camerID"] = it0.DeviceID;
  289. // json1["paramList"] = json1;
  290. // json0.push_back(json1);
  291. // }
  292. // }
  293. // strCmd.clear();
  294. // strRet.clear();
  295. // ret = 0;
  296. // strCmd = QString::fromStdString(json0.dump());
  297. // ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet);
  298. // if(ret != 0)
  299. // {
  300. // SPDLOG_LOGGER_DEBUG(m_logger,"插入设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  301. // isSuccess = false;
  302. // }
  303. }
  304. catch (const nJson::parse_error& e) {
  305. SPDLOG_LOGGER_ERROR(m_logger,"创建 设备信息JSON 数据失败:{}, 错误ID:{}",e.what(), e.id);
  306. return false;
  307. }
  308. catch (const nJson::type_error& e) {
  309. SPDLOG_LOGGER_ERROR(m_logger,"创建 设备信息JSON 数据失败:{}, 错误ID:{}",e.what(), e.id);
  310. return false;
  311. }
  312. catch(...) {
  313. SPDLOG_LOGGER_ERROR(m_logger,"创建 设备信息JSON 数据失败");
  314. return false;
  315. }
  316. return isSuccess;
  317. }
  318. /* 更新设备信息 */
  319. bool FromWebAPI::updateDeviceInfo(std::vector<DeviceInfo>& vecUpdateInfo)
  320. {
  321. if(m_httpApi == nullptr)
  322. {
  323. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  324. return false;
  325. }
  326. if(vecUpdateInfo.empty())
  327. {
  328. return false;
  329. }
  330. bool isSuccess = true;
  331. try
  332. {
  333. nJson json0 = nJson::array();
  334. for(const auto& it : vecUpdateInfo)
  335. {
  336. nJson json1;
  337. json1["opName"] = "SPSS_UpdateToCamerInfo";
  338. json1["Key"] = QUuid::createUuid().toString().toStdString();
  339. nJson json2;
  340. json2["camerID"] = it.DeviceID;
  341. json2["camerName"] = it.DeviceName;
  342. json2["camerIP"] = it.DeviceIP;
  343. json2["camerPort"] = it.DevicePort;
  344. json2["camerUser"] = it.UserAccount;
  345. json2["camerPwd"] = it.UserPassword;
  346. json2["camerType"] = it.DeviceType;
  347. json2["camerSerial"] = it.DeviceSerial;
  348. json2["camerChannel"] = nullptr;
  349. json2["camerUrl"] = nullptr;
  350. json1["paramList"] = json2;
  351. json0.push_back(json1);
  352. }
  353. QString strCmd = QString::fromStdString(json0.dump());
  354. QString strRet;
  355. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet);
  356. if(ret != 0)
  357. {
  358. SPDLOG_LOGGER_ERROR(m_logger,"更新设备信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  359. isSuccess = false;
  360. }
  361. }
  362. catch (const nJson::parse_error& e) {
  363. SPDLOG_LOGGER_ERROR(m_logger,"创建 更新设备信息JSON 数据失败:{}, 错误ID:{}",e.what(), e.id);
  364. return false;
  365. }
  366. catch (const nJson::type_error& e) {
  367. SPDLOG_LOGGER_ERROR(m_logger,"创建 更新设备信息JSON 数据失败:{}, 错误ID:{}",e.what(), e.id);
  368. return false;
  369. }
  370. catch(...) {
  371. SPDLOG_LOGGER_ERROR(m_logger,"创建 更新设备信息JSON 数据失败");
  372. return false;
  373. }
  374. return isSuccess;
  375. }
  376. /* 删除设备信息 */
  377. bool FromWebAPI::deleteDeviceInfo(std::vector<DeviceInfo>& vecDeleteInfo)
  378. {
  379. if(m_httpApi == nullptr)
  380. {
  381. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  382. return false;
  383. }
  384. if(vecDeleteInfo.empty())
  385. {
  386. return true;
  387. }
  388. bool isSuccess = true;
  389. try
  390. {
  391. nJson json0 = nJson::array();
  392. for(const auto& it : vecDeleteInfo)
  393. {
  394. nJson json1;
  395. json1["opName"] = "SPSS_DeleteFromCamerInfo";
  396. json1["Key"] = QUuid::createUuid().toString().toStdString();
  397. nJson json2;
  398. json2["camerID"] = it.DeviceID;
  399. json1["paramList"] = json2;
  400. json0.push_back(json1);
  401. }
  402. QString strCmd = QString::fromStdString(json0.dump());
  403. QString strRet;
  404. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet);
  405. if(ret != 0)
  406. {
  407. SPDLOG_LOGGER_ERROR(m_logger,"删除tCamerinfo设备信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  408. isSuccess = false;
  409. }
  410. }
  411. catch (const nJson::parse_error& e) {
  412. SPDLOG_LOGGER_ERROR(m_logger,"创建 删除设备信息JSON 数据失败:{}, 错误ID:{}",e.what(), e.id);
  413. return false;
  414. }
  415. catch (const nJson::type_error& e) {
  416. SPDLOG_LOGGER_ERROR(m_logger,"创建 删除设备信息JSON 数据失败:{}, 错误ID:{}",e.what(), e.id);
  417. return false;
  418. }
  419. catch(...) {
  420. SPDLOG_LOGGER_ERROR(m_logger,"创建 删除设备信息JSON 数据失败");
  421. return false;
  422. }
  423. return isSuccess;
  424. }
  425. /* 从EQM获取CamerInfo信息 */
  426. bool FromWebAPI::getDeviceInfo(std::vector<DeviceInfo>& vecInfo)
  427. {
  428. if(m_httpApi == nullptr)
  429. {
  430. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  431. return false;
  432. }
  433. vecInfo.clear();
  434. nJson json0;
  435. json0["opName"] = "SPSS_SelectFromCamerInfo";
  436. QString strCmd = QString::fromStdString(json0.dump());
  437. QString strRet;
  438. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  439. if(ret != 0)
  440. {
  441. SPDLOG_LOGGER_DEBUG(m_logger,"获取CamerInfo失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  442. return false;
  443. }
  444. // SPDLOG_LOGGER_TRACE(m_logger, "摄像机信息:\n{}", nJson::parse(strRet.toStdString()).dump(4));
  445. /* 解析信息 */
  446. try {
  447. nJson json1 = nJson::parse(strRet.toStdString());
  448. int retCode = json1["code"].get<int>();
  449. if(retCode != 0)
  450. {
  451. SPDLOG_LOGGER_ERROR(m_logger,"获取CamerInfo失败");
  452. return false;
  453. }
  454. nJson result = json1["result"];
  455. if(result.empty())
  456. {
  457. return false;
  458. }
  459. for(const auto& it : result)
  460. {
  461. // SPDLOG_LOGGER_DEBUG(m_logger,"camerID:{}",it["camerId"].get<int>());
  462. DeviceInfo info;
  463. info.DeviceID = it["camerId"].get<int>();
  464. info.DeviceName = it["camerName"].is_null() ? "" : it["camerName"].get<std::string>();
  465. info.DeviceIP = it["camerIp"].is_null() ? "" : it["camerIp"].get<std::string>();
  466. info.DevicePort = it["camerPort"].is_null() ? 0 : it["camerPort"].get<int>();
  467. info.UserAccount = it["camerUsr"].is_null() ? "" : it["camerUsr"].get<std::string>();
  468. info.UserPassword = it["camerPwd"].is_null() ? "" : it["camerPwd"].get<std::string>();
  469. info.DeviceType = it["camerType"].is_null() ? "" : it["camerType"].get<std::string>();
  470. info.DeviceSerial = it["camerSerial"].is_null() ? "" : it["camerSerial"].get<std::string>();
  471. vecInfo.push_back(info);
  472. }
  473. }
  474. catch (const nJson::parse_error& e) {
  475. SPDLOG_LOGGER_ERROR(m_logger,"解析 CamerInfo数据失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  476. return false;
  477. }
  478. catch (const nJson::type_error& e) {
  479. SPDLOG_LOGGER_ERROR(m_logger,"解析 CamerInfo数据失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  480. return false;
  481. }
  482. catch(...) {
  483. SPDLOG_LOGGER_ERROR(m_logger,"解析 CamerInfo数据失败 数据失败");
  484. return false;
  485. }
  486. return true;
  487. }
  488. /* 获取通道信息 */
  489. bool FromWebAPI::getChannelInfo(std::map<int, std::string>& mapChannelName)
  490. {
  491. if(m_httpApi == nullptr)
  492. {
  493. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  494. return false;
  495. }
  496. nJson json0;
  497. json0["opName"] = "SPSS_SelectChannelInfo";
  498. QString strCmd = QString::fromStdString(json0.dump());
  499. QString strRet;
  500. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  501. if(ret != 0)
  502. {
  503. SPDLOG_LOGGER_DEBUG(m_logger,"获取通道信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  504. return false;
  505. }
  506. /* 解析信息 */
  507. try
  508. {
  509. nJson json1 = nJson::parse(strRet.toStdString());
  510. int retCode = json1["code"].get<int>();
  511. if(retCode != 0)
  512. {
  513. SPDLOG_LOGGER_ERROR(m_logger,"获取通道信息失败");
  514. return false;
  515. }
  516. nJson result = json1["result"];
  517. if(result.empty())
  518. {
  519. return false;
  520. }
  521. for(const auto& it : result)
  522. {
  523. int id = it["chnID"].get<int>();
  524. std::string chnName = it["chnName"].is_null() ? "" : it["chnName"].get<std::string>();
  525. mapChannelName.insert(std::make_pair(id, chnName));
  526. }
  527. }
  528. catch (const nJson::parse_error& e) {
  529. SPDLOG_LOGGER_ERROR(m_logger,"解析 tChannel数据失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  530. return false;
  531. }
  532. catch (const nJson::type_error& e) {
  533. SPDLOG_LOGGER_ERROR(m_logger,"解析 tChannel数据失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  534. return false;
  535. }
  536. catch(...) {
  537. SPDLOG_LOGGER_ERROR(m_logger,"解析 tChannel数据失败 数据失败");
  538. return false;
  539. }
  540. return true;
  541. }
  542. std::string FromWebAPI::getChannelInfo(int chnID)
  543. {
  544. if(m_httpApi == nullptr)
  545. {
  546. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  547. return "";
  548. }
  549. nJson json0;
  550. json0["opName"] = "SPSS_SelectChannelInfo";
  551. QString strCmd = QString::fromStdString(json0.dump());
  552. QString strRet;
  553. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  554. if(ret != 0)
  555. {
  556. SPDLOG_LOGGER_DEBUG(m_logger,"获取通道信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  557. return "false;";
  558. }
  559. /* 解析信息 */
  560. try
  561. {
  562. nJson json1 = nJson::parse(strRet.toStdString());
  563. int retCode = json1["code"].get<int>();
  564. if(retCode != 0)
  565. {
  566. SPDLOG_LOGGER_ERROR(m_logger,"获取通道信息失败");
  567. return "";
  568. }
  569. nJson result = json1["result"];
  570. if(result.empty())
  571. {
  572. return "";
  573. }
  574. for(const auto& it : result)
  575. {
  576. int id = it["chnID"].get<int>();
  577. if(id == chnID)
  578. {
  579. return it.dump();
  580. }
  581. }
  582. }
  583. catch (const nJson::parse_error& e) {
  584. SPDLOG_LOGGER_ERROR(m_logger,"解析 tChannel数据失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  585. return "";
  586. }
  587. catch (const nJson::type_error& e) {
  588. SPDLOG_LOGGER_ERROR(m_logger,"解析 tChannel数据失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  589. return "";
  590. }
  591. catch(...) {
  592. SPDLOG_LOGGER_ERROR(m_logger,"解析 tChannel数据失败 数据失败");
  593. return "";
  594. }
  595. return "";
  596. }
  597. /* 获取摄像机信息 */
  598. bool FromWebAPI::getCameraInfo(std::map<int, std::string>& mapCameraName)
  599. {
  600. if(m_httpApi == nullptr)
  601. {
  602. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  603. return false;
  604. }
  605. nJson json0;
  606. json0["opName"] = "SPSS_SelectCameraInfo";
  607. QString strCmd = QString::fromStdString(json0.dump());
  608. QString strRet;
  609. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  610. if(ret != 0)
  611. {
  612. SPDLOG_LOGGER_DEBUG(m_logger,"获取通道信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  613. return false;
  614. }
  615. /* 解析信息 */
  616. try
  617. {
  618. nJson json1 = nJson::parse(strRet.toStdString());
  619. int retCode = json1["code"].get<int>();
  620. if(retCode != 0)
  621. {
  622. SPDLOG_LOGGER_ERROR(m_logger,"获取通道信息失败");
  623. return false;
  624. }
  625. nJson result = json1["result"];
  626. if(result.empty())
  627. {
  628. return false;
  629. }
  630. for(const auto& it : result)
  631. {
  632. int id = it["camerId"].get<int>();
  633. std::string camerName = it["camerName"].is_null() ? "" : it["camerName"].get<std::string>();
  634. mapCameraName.insert(std::make_pair(id, camerName));
  635. }
  636. }
  637. catch (const nJson::parse_error& e) {
  638. SPDLOG_LOGGER_ERROR(m_logger,"解析 tChannel数据失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  639. return false;
  640. }
  641. catch (const nJson::type_error& e) {
  642. SPDLOG_LOGGER_ERROR(m_logger,"解析 tChannel数据失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  643. return false;
  644. }
  645. catch(...) {
  646. SPDLOG_LOGGER_ERROR(m_logger,"解析 tChannel数据失败 数据失败");
  647. return false;
  648. }
  649. return true;
  650. }
  651. /* 插入设备和算法关联信息 */
  652. bool FromWebAPI::insertDeviceAlgorithmInfo(std::vector<DeviceInfo>& vecInfo)
  653. {
  654. if(m_httpApi == nullptr)
  655. {
  656. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  657. return false;
  658. }
  659. /* 插入信息到tActionCamer表 */
  660. try
  661. {
  662. nJson json0 = nJson::array();
  663. for(const auto& it0 : vecInfo)
  664. {
  665. for(const auto& it1 : it0.vecAlgorithmInfo)
  666. {
  667. nJson json1;
  668. json1["opName"] = "SPSS_InsertToActionCamer";
  669. json1["Key"] = QUuid::createUuid().toString().toStdString();
  670. nJson json2;
  671. json2["actionID"] = it1.ActionID;
  672. json2["camerID"] = it0.DeviceID;
  673. json1["paramList"] = json1;
  674. json0.push_back(json1);
  675. }
  676. }
  677. QString strRet;
  678. QString strCmd = QString::fromStdString(json0.dump());
  679. int ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet);
  680. if(ret != 0)
  681. {
  682. SPDLOG_LOGGER_ERROR(m_logger,"插入设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  683. return false;
  684. }
  685. }
  686. catch (const nJson::parse_error& e) {
  687. SPDLOG_LOGGER_ERROR(m_logger,"创建 写入tActionCamer JSON失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  688. return false;
  689. }
  690. catch (const nJson::type_error& e) {
  691. SPDLOG_LOGGER_ERROR(m_logger,"创建 写入tActionCamer JSON失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  692. return false;
  693. }
  694. catch(...) {
  695. SPDLOG_LOGGER_ERROR(m_logger,"创建 写入tActionCamer JSON失败 数据失败");
  696. return false;
  697. }
  698. return true;
  699. }
  700. /* 更新设备和算法关联表 */
  701. bool FromWebAPI::updateDeviceAlgorithmInfo(std::vector<DeviceInfo>& vecInfo)
  702. {
  703. if(m_httpApi == nullptr)
  704. {
  705. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  706. return false;
  707. }
  708. /* 更新之前先删除相关的信息 */
  709. if(!deleteDeviceAlgorithmInfo(vecInfo))
  710. {
  711. SPDLOG_LOGGER_ERROR(m_logger,"删除 tActionCamer 关联表失败");
  712. return false;
  713. }
  714. /* 插入信息到tActionCamer表 */
  715. bool isSuccess = true;
  716. try
  717. {
  718. nJson json0 = nJson::array();
  719. for(const auto& it0 : vecInfo)
  720. {
  721. for(const auto& it1 : it0.vecAlgorithmInfo)
  722. {
  723. nJson json1;
  724. json1["opName"] = "SPSS_InsertToActionCamer";
  725. json1["Key"] = QUuid::createUuid().toString().toStdString();
  726. nJson json2;
  727. json2["actionID"] = it1.ActionID;
  728. json2["camerID"] = it0.DeviceID;
  729. json1["paramList"] = json2;
  730. json0.push_back(json1);
  731. }
  732. }
  733. QString strCmd = QString::fromStdString(json0.dump());
  734. QString strRet;
  735. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet);
  736. if(ret != 0)
  737. {
  738. SPDLOG_LOGGER_ERROR(m_logger,"插入设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  739. isSuccess = false;
  740. }
  741. // SPDLOG_LOGGER_DEBUG(m_logger,"插入数据到ActionCamer 成功! 数目: {}", json0.size());
  742. }
  743. catch (const nJson::parse_error& e) {
  744. SPDLOG_LOGGER_ERROR(m_logger,"创建 插入tActionCamer JSON失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  745. return false;
  746. }
  747. catch (const nJson::type_error& e) {
  748. SPDLOG_LOGGER_ERROR(m_logger,"创建 插入tActionCamer JSON失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  749. return false;
  750. }
  751. catch(...) {
  752. SPDLOG_LOGGER_ERROR(m_logger,"创建 插入tActionCamer JSON失败 数据失败");
  753. return false;
  754. }
  755. return isSuccess;
  756. }
  757. /* 删除设备和算法关联表 */
  758. bool FromWebAPI::deleteDeviceAlgorithmInfo(std::vector<DeviceInfo>& vecInfo)
  759. {
  760. if(m_httpApi == nullptr)
  761. {
  762. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  763. return false;
  764. }
  765. bool isSuccess = true;
  766. /* 更新之前先删除相关的信息 */
  767. try
  768. {
  769. nJson json0 = nJson::array();
  770. /* 更新之前先删除相关的信息 */
  771. for(const auto& it : vecInfo)
  772. {
  773. nJson json1;
  774. json1["opName"] = "SPSS_DeleteFromActionCamer";
  775. json1["Key"] = QUuid::createUuid().toString().toStdString();
  776. nJson json2;
  777. json2["camerID"] = it.DeviceID;
  778. json1["paramList"] = json2;
  779. json0.push_back(json1);
  780. }
  781. QString strCmd = QString::fromStdString(json0.dump());
  782. QString strRet;
  783. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet);
  784. if(ret != 0)
  785. {
  786. SPDLOG_LOGGER_ERROR(m_logger,"删除设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  787. // return false;
  788. isSuccess = false;
  789. }
  790. // SPDLOG_LOGGER_DEBUG(m_logger,"从 ActionCamer 删除成功! 数目:{}", vecInfo.size());
  791. }
  792. catch (const nJson::parse_error& e) {
  793. SPDLOG_LOGGER_ERROR(m_logger,"创建 删除tActionCamer JSON失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  794. return false;
  795. }
  796. catch (const nJson::type_error& e) {
  797. SPDLOG_LOGGER_ERROR(m_logger,"创建 删除tActionCamer JSON失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  798. return false;
  799. }
  800. catch(...) {
  801. SPDLOG_LOGGER_ERROR(m_logger,"创建 删除tActionCamer JSON失败 数据失败");
  802. return false;
  803. }
  804. return isSuccess;
  805. }
  806. /* 删除设备和算法关联表 */
  807. bool FromWebAPI::deleteDeviceAlgorithmInfo(std::list<int>& vecID)
  808. {
  809. if(m_httpApi == nullptr)
  810. {
  811. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  812. return false;
  813. }
  814. bool isSuccess = true;
  815. try
  816. {
  817. nJson json0 = nJson::array();
  818. /* 更新之前先删除相关的信息 */
  819. for(const auto& it : vecID)
  820. {
  821. nJson json1;
  822. json1["opName"] = "SPSS_DeleteFromActionCamer";
  823. json1["Key"] = QUuid::createUuid().toString().toStdString();
  824. nJson json2;
  825. json2["camerID"] = it;
  826. json1["paramList"] = json2;
  827. json0.push_back(json1);
  828. }
  829. QString strCmd = QString::fromStdString(json0.dump());
  830. QString strRet;
  831. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet);
  832. if(ret != 0)
  833. {
  834. SPDLOG_LOGGER_ERROR(m_logger,"删除设备信息到tActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  835. // return false;
  836. isSuccess = false;
  837. }
  838. // SPDLOG_LOGGER_DEBUG(m_logger,"从 ActionCamer 删除成功! 数目:{}", vecID.size());
  839. }
  840. catch (const nJson::parse_error& e) {
  841. SPDLOG_LOGGER_ERROR(m_logger,"创建 删除tActionCamer JSON失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  842. return false;
  843. }
  844. catch (const nJson::type_error& e) {
  845. SPDLOG_LOGGER_ERROR(m_logger,"创建 删除tActionCamer JSON失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  846. return false;
  847. }
  848. catch(...) {
  849. SPDLOG_LOGGER_ERROR(m_logger,"创建 删除tActionCamer JSON失败 数据失败");
  850. return false;
  851. }
  852. return isSuccess;
  853. }
  854. /* 获取设备和算法信息关联表,需要先从EQM数据库中获取到设备信息 */
  855. bool FromWebAPI::getDeviceAlgorithmInfo(std::vector<DeviceInfo>& vecInfo, std::list<int>& listDevIDDelete)
  856. {
  857. if(m_httpApi == nullptr)
  858. {
  859. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  860. return false;
  861. }
  862. if(vecInfo.empty())
  863. {
  864. SPDLOG_LOGGER_WARN(m_logger,"Device info is empty");
  865. return false;
  866. }
  867. listDevIDDelete.clear();
  868. nJson json0;
  869. json0["opName"] = "SPSS_SelectFromActionCamer";
  870. QString strCmd = QString::fromStdString(json0.dump());
  871. QString strRet;
  872. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  873. if(ret != 0)
  874. {
  875. SPDLOG_LOGGER_DEBUG(m_logger,"获取ActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  876. return false;
  877. }
  878. /* 设备和算法关联信息容器 */
  879. std::vector<DeviceAlgorithmInfo> vecNewInfo;
  880. /* 解析信息 */
  881. try {
  882. nJson json1 = nJson::parse(strRet.toStdString());
  883. int retCode = json1["code"].get<int>();
  884. if(retCode != 0)
  885. {
  886. SPDLOG_LOGGER_ERROR(m_logger,"获取ActionCamer失败");
  887. return false;
  888. }
  889. nJson result = json1["result"];
  890. if(result.empty())
  891. {
  892. return false;
  893. }
  894. /* 取出所有的设备算法信息 */
  895. for(const auto& it : result)
  896. {
  897. DeviceAlgorithmInfo info;
  898. info.DeviceID = it["camerId"].get<int>();
  899. info.ActionID = it["actionId"].is_null() ? "" : it["actionId"].get<std::string>();
  900. info.ActionName = it["actionName"].is_null() ? "" : it["actionName"].get<std::string>();
  901. info.ActionTaskID = it["actionTaskid"].is_null() ? 0 : it["actionTaskid"].get<int>();
  902. vecNewInfo.push_back(info);
  903. }
  904. }
  905. catch (const nJson::parse_error& e) {
  906. SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}, 错误ID:{}",e.what(), e.id);
  907. return false;
  908. }
  909. catch (const nJson::type_error& e) {
  910. SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}, 错误ID:{}",e.what(), e.id);
  911. return false;
  912. }
  913. /* 根据缓存中的设备ID取出关联的算法 */
  914. for(auto& it0 : vecInfo)
  915. {
  916. for(auto& it1 : vecNewInfo)
  917. {
  918. /* 对比设备ID */
  919. if(it0.DeviceID == it1.DeviceID)
  920. {
  921. AlgorithmInfo info;
  922. info.ActionID = it1.ActionID;
  923. info.ActionName = it1.ActionName;
  924. info.ActionTaskID = it1.ActionTaskID;
  925. it0.vecAlgorithmInfo.push_back(info);
  926. }
  927. }
  928. }
  929. /* 取出关联表中已经消失的设备ID,在本轮循环中删除 */
  930. for(const auto& it : vecNewInfo)
  931. {
  932. bool isExist = false;
  933. for(const auto& it0 : vecInfo)
  934. {
  935. if(it.DeviceID == it0.DeviceID)
  936. {
  937. isExist = true;
  938. break;
  939. }
  940. }
  941. if(!isExist)
  942. {
  943. /* 检查是否重复,一个ID只需要记录一次 */
  944. if(std::find(listDevIDDelete.begin(), listDevIDDelete.end(), it.DeviceID) == listDevIDDelete.end())
  945. {
  946. listDevIDDelete.push_back(it.DeviceID);
  947. }
  948. }
  949. }
  950. return true;
  951. }
  952. /* 获取设备和算法关联信息,包括频率信息,房间信息,重载版,只获取关联关系 */
  953. bool FromWebAPI::getActionInfo(ListActionInfo& listInfo)
  954. {
  955. if(m_httpApi == nullptr)
  956. {
  957. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  958. return false;
  959. }
  960. listInfo.clear();
  961. nJson json0;
  962. json0["opName"] = "SPSS_SelectFromActionInfo";
  963. QString strCmd = QString::fromStdString(json0.dump());
  964. QString strRet;
  965. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  966. if(ret != 0)
  967. {
  968. SPDLOG_LOGGER_DEBUG(m_logger,"获取ActionCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  969. return false;
  970. }
  971. /* 解析信息 */
  972. try
  973. {
  974. nJson json1 = nJson::parse(strRet.toStdString());
  975. int retCode = json1["code"].get<int>();
  976. if(retCode != 0)
  977. {
  978. SPDLOG_LOGGER_ERROR(m_logger,"获取ActionCamer失败");
  979. return false;
  980. }
  981. nJson result = json1["result"];
  982. if(result.empty())
  983. {
  984. return false;
  985. }
  986. /* 取出所有的设备算法信息 */
  987. for(const auto& it : result)
  988. {
  989. ActionInfo info;
  990. info.CameraID = it["camerId"].is_null() ? -1 : it["camerId"].get<int>();
  991. info.ActionID = it["actionId"].is_null() ? "" : it["actionId"].get<std::string>();
  992. info.RoomID = it["roomId"].is_null() ? -1 : it["roomId"].get<int>();
  993. info.ChannelID = it["chnId"].is_null() ? -1 : it["chnId"].get<int>();
  994. int roomType = it["rtype"].is_null() ? 0 : it["rtype"].get<int>();
  995. info.RoomType = static_cast<Enum_RoomType>(roomType);
  996. info.strRoomName = it["rname"].is_null() ? "" : it["rname"].get<std::string>();
  997. listInfo.insertActionInfo(&info);
  998. }
  999. }
  1000. catch (const nJson::parse_error& e) {
  1001. SPDLOG_LOGGER_ERROR(m_logger,"解析 ActionCamer 数据失败:{}, 错误ID:{}",e.what(), e.id);
  1002. return false;
  1003. }
  1004. catch (const nJson::type_error& e) {
  1005. SPDLOG_LOGGER_ERROR(m_logger,"解析 ActionCamer 数据失败:{}, 错误ID:{}",e.what(), e.id);
  1006. return false;
  1007. }
  1008. catch(...)
  1009. {
  1010. SPDLOG_LOGGER_ERROR(m_logger,"解析 ActionCamer 数据失败");
  1011. return false;
  1012. }
  1013. return true;
  1014. }
  1015. /* 获取摄像机和房间关联信息 */
  1016. bool FromWebAPI::getRoomCameraInfo(std::list<RoomCameraInfo>& vecInfo)
  1017. {
  1018. if(m_httpApi == nullptr)
  1019. {
  1020. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  1021. return false;
  1022. }
  1023. vecInfo.clear();
  1024. nJson json0;
  1025. json0["opName"] = "SPSS_SelectRoomCamer";
  1026. QString strCmd = QString::fromStdString(json0.dump());
  1027. QString strRet;
  1028. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  1029. if(ret != 0)
  1030. {
  1031. SPDLOG_LOGGER_DEBUG(m_logger,"获取RoomCamer失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  1032. return false;
  1033. }
  1034. try
  1035. {
  1036. nJson json1 = nJson::parse(strRet.toStdString());
  1037. int retCode = json1["code"].get<int>();
  1038. if(retCode != 0)
  1039. {
  1040. SPDLOG_LOGGER_ERROR(m_logger,"获取RoomCamer失败");
  1041. return false;
  1042. }
  1043. nJson result = json1["result"];
  1044. if(result.empty())
  1045. {
  1046. return false;
  1047. }
  1048. for(const auto& it : result)
  1049. {
  1050. auto roomID = it["roomId"].get<int>();
  1051. auto cameraID = it["camerId"].is_null() ? -1 : it["camerId"].get<int>();
  1052. /* 先检查RoomID是否已经在列表中了 */
  1053. bool isExist = false;
  1054. for(auto& it0 : vecInfo)
  1055. {
  1056. if(it0.RoomID == roomID)
  1057. {
  1058. it0.listCameraID.push_back(cameraID);
  1059. isExist = true;
  1060. break;
  1061. }
  1062. }
  1063. /* 没有找到RoomID,创建一个新的 */
  1064. if(!isExist)
  1065. {
  1066. RoomCameraInfo info;
  1067. info.RoomID = roomID;
  1068. info.listCameraID.push_back(cameraID);
  1069. }
  1070. }
  1071. }
  1072. catch (const nJson::parse_error& e) {
  1073. SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}, 错误ID:{}",e.what(), e.id);
  1074. return false;
  1075. }
  1076. catch (const nJson::type_error& e) {
  1077. SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}, 错误ID:{}",e.what(), e.id);
  1078. return false;
  1079. }
  1080. catch(const std::exception& e)
  1081. {
  1082. SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败:{}",e.what());
  1083. return false;
  1084. }
  1085. catch(...)
  1086. {
  1087. SPDLOG_LOGGER_ERROR(m_logger,"解析ActionCamer数据失败");
  1088. return false;
  1089. }
  1090. return true;
  1091. }
  1092. /* 根据获取功能检测时间区间 */
  1093. bool FromWebAPI::getFuncDetectTime(int ChannelID, AppFunction app, DetectPeriodInfo& periodInfo)
  1094. {
  1095. if(m_httpApi == nullptr)
  1096. {
  1097. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  1098. return false;
  1099. }
  1100. try
  1101. {
  1102. /* 生成发送的字符串 */
  1103. nJson json0;
  1104. json0["opName"] = "SPSS_SelectPeriodByChannel";
  1105. nJson json1;
  1106. json1["channelID"] = ChannelID;
  1107. json1["appType"] = getAppFunctionID(app);
  1108. json0["paramList"] = json1;
  1109. QString strCmd = QString::fromStdString(json0.dump());
  1110. QString strRet;
  1111. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  1112. if(ret != 0)
  1113. {
  1114. SPDLOG_LOGGER_WARN(m_logger,"获取Period信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  1115. return false;
  1116. }
  1117. /* 解析返回的数据 */
  1118. nJson retJson = nJson::parse(strRet.toStdString());
  1119. // SPDLOG_LOGGER_DEBUG(m_logger,"获取tPeriod信息成功:\n{}", retJson.dump(4));
  1120. int retCode = retJson["code"].get<int>();
  1121. if(retCode != 0)
  1122. {
  1123. SPDLOG_LOGGER_ERROR(m_logger,"获取Period信息失败");
  1124. return false;
  1125. }
  1126. nJson resultJson = retJson["result"];
  1127. if(resultJson.empty())
  1128. {
  1129. SPDLOG_LOGGER_WARN(m_logger,"获取Period信息失败,结果为空");
  1130. SPDLOG_LOGGER_DEBUG(m_logger,"发送的字符串:{}\n", json0.dump(4));
  1131. return false;
  1132. }
  1133. periodInfo.listWeekTime.clear();
  1134. for(const auto& it : resultJson)
  1135. {
  1136. periodInfo.ChannelID = it["chnId"].get<int>();
  1137. periodInfo.PeriodName = it["periodName"].is_null() ? "" : it["periodName"].get<std::string>();
  1138. int PeriodType = it["type"].is_null() ? 0 : it["type"].get<int>();
  1139. periodInfo.PeriodType = static_cast<Enum_PeriodType>(PeriodType);
  1140. int appType = it["appType"].is_null() ? 0 : it["appType"].get<int>();
  1141. periodInfo.appFunction = getAppFunctionID(appType);
  1142. WeekTime wt;
  1143. wt.WeekDay = it["week"].is_null() ? 0 : it["week"].get<int>();
  1144. std::string st = it["startTime"].is_null() ? "" : it["startTime"].get<std::string>();
  1145. wt.StartTime = QDateTime::fromString(QString::fromStdString(st), "yyyy-MM-dd hh:mm:ss");
  1146. std::string et = it["endTime"].is_null() ? "" : it["endTime"].get<std::string>();
  1147. wt.EndTime = QDateTime::fromString(QString::fromStdString(et), "yyyy-MM-dd hh:mm:ss");
  1148. periodInfo.listWeekTime.push_back(wt);
  1149. }
  1150. }
  1151. catch (const nJson::parse_error& e) {
  1152. SPDLOG_LOGGER_ERROR(m_logger,"解析 tPeriod 数据失败:{}, 错误ID:{}",e.what(), e.id);
  1153. return false;
  1154. }
  1155. catch (const nJson::type_error& e) {
  1156. SPDLOG_LOGGER_ERROR(m_logger,"解析 tPeriod 数据失败:{}, 错误ID:{}",e.what(), e.id);
  1157. return false;
  1158. }
  1159. catch(...)
  1160. {
  1161. SPDLOG_LOGGER_ERROR(m_logger,"解析 tPeriod 数据失败");
  1162. return false;
  1163. }
  1164. return true;
  1165. }
  1166. /* 写入报警信息 */
  1167. bool FromWebAPI::insertAlarmInfo(const AlarmInfo& alarmInfo)
  1168. {
  1169. int PKID = 0;
  1170. return insertAlarmInfo(alarmInfo, PKID);
  1171. }
  1172. bool FromWebAPI::insertAlarmInfo(const AlarmInfo& alarmInfo, int& PKID)
  1173. {
  1174. if(m_httpApi == nullptr)
  1175. {
  1176. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  1177. return false;
  1178. }
  1179. /* 判断是否有图片,没有图片就取消报警 */
  1180. if(alarmInfo.PicUrl.empty())
  1181. {
  1182. SPDLOG_LOGGER_WARN(m_logger, "× 取消新增报警信息,因为没有图片,频道[{}],房间[{}],摄像头[{}],{},{},有{}个区域,有{}个人脸",
  1183. alarmInfo.ChannelID, alarmInfo.RoomID, alarmInfo.DeviceID, alarmInfo.ActionDes, alarmInfo.ImageInfo, alarmInfo.listBbox.size(), alarmInfo.listPersonInfo.size());
  1184. return false;
  1185. }
  1186. /* 拼接Bbox */
  1187. std::string strBbox;
  1188. for(const auto& it : alarmInfo.listBbox)
  1189. {
  1190. strBbox += it;
  1191. strBbox += "|";
  1192. }
  1193. /* 去掉最后一个“|” */
  1194. strBbox.pop_back();
  1195. // printAlarmInfo(alarmInfo, strBbox);
  1196. try
  1197. {
  1198. nJson json0;
  1199. json0["opName"] = "SPSS_InsertToAlarm";
  1200. nJson json1;
  1201. json1["alarmID"] = alarmInfo.AlarmID;
  1202. json1["startTime"] = alarmInfo.StartTime.empty() ? "" : alarmInfo.StartTime;
  1203. json1["createTime"] = alarmInfo.EventTime.empty() ? "" : alarmInfo.EventTime;
  1204. json1["endTime"] = alarmInfo.EndTime;
  1205. json1["bbox"] = strBbox;
  1206. json1["picUrl"] = alarmInfo.PicUrl;
  1207. json1["appID"] = getAppFunctionID(alarmInfo.appFunction);
  1208. json1["actionID"] = alarmInfo.ActionID;
  1209. json1["actionDes"] = alarmInfo.ActionDes;
  1210. json1["camerID"] = alarmInfo.DeviceID;
  1211. json1["roomID"] = alarmInfo.RoomID;
  1212. json1["chnid"] = alarmInfo.ChannelID;
  1213. json1["state"] = alarmInfo.State;
  1214. json1["faceIDList"] = alarmInfo.FaceIDList;
  1215. json1["faceNameList"] = alarmInfo.FaceNameList;
  1216. json1["onWork"] = alarmInfo.OnWork;
  1217. json0["paramList"] = json1;
  1218. // SPDLOG_LOGGER_DEBUG(m_logger, "写入报警信息的字符串:{}\n", json0.dump(4));
  1219. QString strCmd = QString::fromStdString(json0.dump());
  1220. QString strRet;
  1221. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet);
  1222. if(ret != 0)
  1223. {
  1224. SPDLOG_LOGGER_WARN(m_logger,"插入报警信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  1225. return false;
  1226. }
  1227. // SPDLOG_LOGGER_DEBUG(m_logger,"插入报警信息成功!");
  1228. // SPDLOG_LOGGER_DEBUG(m_logger, "返回的数据:{}", strRet.toStdString());
  1229. nJson retJson = nJson::parse(strRet.toStdString());
  1230. int retCode = retJson["code"].get<int>();
  1231. if(retCode != 0)
  1232. {
  1233. SPDLOG_LOGGER_ERROR(m_logger,"获取写入报警信息的PKID失败, 错误信息:{}", retJson["msg"].get<std::string>());
  1234. return false;
  1235. }
  1236. /* 解析返回的报警ID */
  1237. nJson result = retJson["result"];
  1238. if(result.empty())
  1239. {
  1240. SPDLOG_LOGGER_ERROR(m_logger,"插入报警信息失败, 返回的结果为空");
  1241. return false;
  1242. }
  1243. for(const auto& it : result)
  1244. {
  1245. PKID = it["pkid"].is_null() ? 0 : it["pkid"].get<int>();
  1246. }
  1247. }
  1248. catch (const nJson::parse_error& e) {
  1249. SPDLOG_LOGGER_ERROR(m_logger,"创建 写入报警信息 JSON失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  1250. return false;
  1251. }
  1252. catch (const nJson::type_error& e) {
  1253. SPDLOG_LOGGER_ERROR(m_logger,"创建 写入报警信息 JSON失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  1254. return false;
  1255. }
  1256. catch(...) {
  1257. SPDLOG_LOGGER_ERROR(m_logger,"创建 写入报警信息 JSON失败 数据失败");
  1258. return false;
  1259. }
  1260. return true;
  1261. }
  1262. /* 打印报警数据信息 */
  1263. void FromWebAPI::printAlarmInfo(const AlarmInfo& alarmInfo, std::string strBBox)
  1264. {
  1265. std::string strLog;
  1266. strLog += "写入报警信息:\n";
  1267. strLog += fmt::format("alarmID: {}\n", alarmInfo.AlarmID);
  1268. strLog += fmt::format("startTime: {}\n", alarmInfo.StartTime);
  1269. strLog += fmt::format("createTime: {}\n", alarmInfo.EventTime);
  1270. strLog += fmt::format("endTime: {}\n", alarmInfo.EndTime);
  1271. strLog += fmt::format("bbox: {}\n", strBBox);
  1272. strLog += fmt::format("picUrl: {}\n", alarmInfo.PicUrl);
  1273. strLog += fmt::format("appID: {}\n", getAppFunctionID(alarmInfo.appFunction));
  1274. strLog += fmt::format("actionID: {}\n", alarmInfo.ActionID);
  1275. strLog += fmt::format("actionDes: {}\n", alarmInfo.ActionDes);
  1276. strLog += fmt::format("camerID: {}\n", alarmInfo.DeviceID);
  1277. strLog += fmt::format("roomID: {}\n", alarmInfo.RoomID);
  1278. strLog += fmt::format("chnid: {}\n", alarmInfo.ChannelID);
  1279. strLog += fmt::format("state: {}\n", alarmInfo.State);
  1280. strLog += fmt::format("faceIDList: {}\n", alarmInfo.FaceIDList);
  1281. strLog += fmt::format("faceNameList: {}\n", alarmInfo.FaceNameList);
  1282. strLog += fmt::format("onWork: {}", alarmInfo.OnWork);
  1283. SPDLOG_LOGGER_DEBUG(m_logger, strLog);
  1284. }
  1285. /* 更新报警结束时间 */
  1286. bool FromWebAPI::updateAlarmEndTime(const AlarmInfo& alarmInfo)
  1287. {
  1288. if(m_httpApi == nullptr)
  1289. {
  1290. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  1291. return false;
  1292. }
  1293. try
  1294. {
  1295. nJson json0;
  1296. json0["opName"] = "SPSS_UpdateAlarmEndTime";
  1297. nJson json1;
  1298. json1["EndTime"] = alarmInfo.EndTime;
  1299. json1["ChannelID"] = alarmInfo.ChannelID;
  1300. json1["RoomID"] = alarmInfo.RoomID;
  1301. json1["CamerID"] = alarmInfo.DeviceID;
  1302. json1["ActionID"] = alarmInfo.ActionID;
  1303. json0["paramList"] = json1;
  1304. QString strCmd = QString::fromStdString(json0.dump());
  1305. QString strRet;
  1306. int ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Update, strCmd, strRet);
  1307. if(ret != 0)
  1308. {
  1309. SPDLOG_LOGGER_WARN(m_logger,"更新报警结束时间失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  1310. return false;
  1311. }
  1312. }
  1313. catch (const nJson::parse_error& e) {
  1314. SPDLOG_LOGGER_ERROR(m_logger,"创建 更新报警结束时间 JSON失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  1315. return false;
  1316. }
  1317. catch (const nJson::type_error& e) {
  1318. SPDLOG_LOGGER_ERROR(m_logger,"创建 更新报警结束时间 JSON失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  1319. return false;
  1320. }
  1321. catch(...) {
  1322. SPDLOG_LOGGER_ERROR(m_logger,"创建 更新报警结束时间 JSON失败 数据失败");
  1323. return false;
  1324. }
  1325. return true;
  1326. }
  1327. /**
  1328. * @brief 根据PKID更新报警记录,用来结束报警,这个只需要PKID和结束时间
  1329. *
  1330. * @param alarmInfo
  1331. * @return true
  1332. * @return false
  1333. */
  1334. bool FromWebAPI::endAlarmInfoByPKID(const int PKID, const QDateTime dateTime)
  1335. {
  1336. if(m_httpApi == nullptr)
  1337. {
  1338. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  1339. return false;
  1340. }
  1341. try
  1342. {
  1343. nJson json0;
  1344. json0["opName"] = "SPSS_EndAlarmByPKID";
  1345. nJson json1;
  1346. json1["PKID"] = PKID;
  1347. json1["endTime"] = dateTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  1348. json0["paramList"] = json1;
  1349. QString strCmd = QString::fromStdString(json0.dump());
  1350. QString strRet;
  1351. int ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Update, strCmd, strRet);
  1352. if(ret != 0)
  1353. {
  1354. SPDLOG_LOGGER_WARN(m_logger,"更新报警结束时间失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  1355. return false;
  1356. }
  1357. // SPDLOG_LOGGER_DEBUG(m_logger,"更新报警结束时间成功!");
  1358. }
  1359. catch (const nJson::parse_error& e) {
  1360. SPDLOG_LOGGER_ERROR(m_logger,"创建 更新报警结束时间 JSON失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  1361. return false;
  1362. }
  1363. catch (const nJson::type_error& e) {
  1364. SPDLOG_LOGGER_ERROR(m_logger,"创建 更新报警结束时间 JSON失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  1365. return false;
  1366. }
  1367. catch(...) {
  1368. SPDLOG_LOGGER_ERROR(m_logger,"创建 更新报警结束时间 JSON失败 数据失败");
  1369. return false;
  1370. }
  1371. return true;
  1372. }
  1373. /* 更新人员信息 */
  1374. bool FromWebAPI::updatePersonInfo(RoomOnWorkInfo& roomInfo)
  1375. {
  1376. if(m_httpApi == nullptr)
  1377. {
  1378. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  1379. return false;
  1380. }
  1381. try
  1382. {
  1383. std::string strDes = fmt::format("人员在岗[{}][{}]", roomInfo.getFaceIDListString(), roomInfo.getFaceNameListString());
  1384. nJson json0;
  1385. json0["opName"] = "SPSS_UpdateFaceInfoByPKID";
  1386. nJson json1;
  1387. json1["pkid"] = roomInfo.PKID;
  1388. json1["faceIDList"] = roomInfo.getFaceIDListString();
  1389. json1["faceNameList"] = roomInfo.getFaceNameListString();
  1390. json1["ActionDes"] = strDes;
  1391. json0["paramList"] = json1;
  1392. QString strCmd = QString::fromStdString(json0.dump());
  1393. QString strRet;
  1394. int ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Update, strCmd, strRet);
  1395. if(ret != 0)
  1396. {
  1397. SPDLOG_LOGGER_WARN(m_logger,"更新报警结束时间失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  1398. return false;
  1399. }
  1400. // SPDLOG_LOGGER_DEBUG(m_logger,"更新报警结束时间成功!");
  1401. }
  1402. catch (const nJson::parse_error& e) {
  1403. SPDLOG_LOGGER_ERROR(m_logger,"创建 更新人员信息 JSON失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  1404. return false;
  1405. }
  1406. catch (const nJson::type_error& e) {
  1407. SPDLOG_LOGGER_ERROR(m_logger,"创建 更新人员信息 JSON失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  1408. return false;
  1409. }
  1410. catch(...) {
  1411. SPDLOG_LOGGER_ERROR(m_logger,"创建 更新人员信息 JSON失败 数据失败");
  1412. return false;
  1413. }
  1414. return true;
  1415. }
  1416. /* 通过频率ID和应用ID获取最大的PKID报警 */
  1417. bool FromWebAPI::getMaxAlarmPKID(const int ChannelID, const AppFunction AppID, int& PKID)
  1418. {
  1419. if(m_httpApi == nullptr)
  1420. {
  1421. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  1422. return false;
  1423. }
  1424. PKID = 0;
  1425. nJson json0;
  1426. json0["opName"] = "SPSS_GetChnAlarmMaxPKID";
  1427. nJson json1;
  1428. json1["ChannelID"] = ChannelID;
  1429. json1["AppID"] = static_cast<int>(AppID);
  1430. json0["paramList"] = json1;
  1431. QString strCmd = QString::fromStdString(json0.dump());
  1432. QString strRet;
  1433. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  1434. if(ret != 0)
  1435. {
  1436. SPDLOG_LOGGER_DEBUG(m_logger,"获取Alarma max PKID失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  1437. return false;
  1438. }
  1439. try
  1440. {
  1441. nJson json1 = nJson::parse(strRet.toStdString());
  1442. int retCode = json1["code"].get<int>();
  1443. if(retCode != 0)
  1444. {
  1445. SPDLOG_LOGGER_ERROR(m_logger,"获取Alarma max PKID失败");
  1446. return false;
  1447. }
  1448. nJson result = json1["result"];
  1449. if(result.empty())
  1450. {
  1451. return false;
  1452. }
  1453. for(const auto& it : result)
  1454. {
  1455. PKID = it["maxPKID"].is_null() ? 0 : it["maxPKID"].get<int>();
  1456. }
  1457. }
  1458. catch (const nJson::parse_error& e) {
  1459. SPDLOG_LOGGER_ERROR(m_logger,"解析 Alarma max PKID 失败:{}, 错误ID:{}",e.what(), e.id);
  1460. return false;
  1461. }
  1462. catch(const nJson::type_error& e)
  1463. {
  1464. SPDLOG_LOGGER_ERROR(m_logger,"解析 Alarma max PKID 失败:{}, 错误ID:{}",e.what(), e.id);
  1465. return false;
  1466. }
  1467. catch(...)
  1468. {
  1469. SPDLOG_LOGGER_ERROR(m_logger,"解析 Alarma max PKID 失败");
  1470. return false;
  1471. }
  1472. return true;
  1473. }
  1474. /* 清空在岗离岗报警的在岗人员信息 */
  1475. bool FromWebAPI::clearOnWorkAlarmInfo(const int PKID, const bool onWork, const std::string actionDes)
  1476. {
  1477. if(m_httpApi == nullptr)
  1478. {
  1479. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  1480. return false;
  1481. }
  1482. nJson json0;
  1483. json0["opName"] = "SPSS_ClearAlarmOnWorkPersonInfo";
  1484. nJson json1;
  1485. json1["pkid"] = PKID;
  1486. json1["onWork"] = onWork;
  1487. json1["actionDes"] = actionDes;
  1488. json0["paramList"] = json1;
  1489. QString strCmd = QString::fromStdString(json0.dump());
  1490. QString strRet;
  1491. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Update, strCmd, strRet);
  1492. if(ret != 0)
  1493. {
  1494. SPDLOG_LOGGER_DEBUG(m_logger,"清空在岗离岗报警的在岗人员信息失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  1495. return false;
  1496. }
  1497. return true;
  1498. }
  1499. /* 获取报警规则表 */
  1500. bool FromWebAPI::getPersonCountRuleInfo(std::list<PersonCountRuleInfo>& list)
  1501. {
  1502. if(m_httpApi == nullptr)
  1503. {
  1504. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  1505. return false;
  1506. }
  1507. nJson json0;
  1508. json0["opName"] = "SPSS_GetPersonCountRule";
  1509. QString strCmd = QString::fromStdString(json0.dump());
  1510. QString strRet;
  1511. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  1512. if(ret != 0)
  1513. {
  1514. SPDLOG_LOGGER_DEBUG(m_logger,"获取AlarmRule失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  1515. return false;
  1516. }
  1517. try
  1518. {
  1519. nJson json1 = nJson::parse(strRet.toStdString());
  1520. int retCode = json1["code"].get<int>();
  1521. if(retCode != 0)
  1522. {
  1523. SPDLOG_LOGGER_ERROR(m_logger,"获取tAlarmRule失败");
  1524. return false;
  1525. }
  1526. nJson result = json1["result"];
  1527. if(result.empty())
  1528. {
  1529. return false;
  1530. }
  1531. list.clear();
  1532. for(const auto& it : result)
  1533. {
  1534. PersonCountRuleInfo info;
  1535. info.ChannelID = it["chnId"].is_null() ? -1 : it["chnId"].get<int>();
  1536. info.RuleName = it["periodName"].is_null() ? "" : it["periodName"].get<std::string>();
  1537. info.RuleName = it["ruleName"].get<std::string>();
  1538. info.LiveMinEnable = it["liveMinEnable"].get<int>() == 1 ? true : false;
  1539. info.LiveMaxEnable = it["liveMaxEnable"].get<int>() == 1 ? true : false;
  1540. info.DicMinEnable = it["dicMinEnable"].get<int>() == 1 ? true : false;
  1541. info.DicMaxEnable = it["dicMaxEnable"].get<int>() == 1 ? true : false;
  1542. info.LiveDicMinEnable = it["liveDicMinEnable"].get<int>() == 1 ? true : false;
  1543. info.LiveDicMaxEnable = it["liveDicMaxEnable"].get<int>() == 1 ? true : false;
  1544. info.LiveMin = it["liveMin"].is_null() ? 0 : it["liveMin"].get<int>();
  1545. info.LiveMax = it["liveMax"].is_null() ? 0 : it["liveMax"].get<int>();
  1546. info.DicMin = it["dicMin"].is_null() ? 0 : it["dicMin"].get<int>();
  1547. info.DicMax = it["dicMax"].is_null() ? 0 : it["dicMax"].get<int>();
  1548. info.LiveDicMin = it["liveDicMin"].is_null() ? 0 : it["liveDicMin"].get<int>();
  1549. info.LiveDicMax = it["liveDicMax"].is_null() ? 0 : it["liveDicMax"].get<int>();
  1550. list.push_back(info);
  1551. }
  1552. }
  1553. catch (const nJson::parse_error& e) {
  1554. SPDLOG_LOGGER_ERROR(m_logger,"解析 tRuleInfo 数据失败:{}, 错误ID:{}",e.what(), e.id);
  1555. return false;
  1556. }
  1557. catch(const nJson::type_error& e)
  1558. {
  1559. SPDLOG_LOGGER_ERROR(m_logger,"解析 tRuleInfo 数据失败:{}, 错误ID:{}",e.what(), e.id);
  1560. return false;
  1561. }
  1562. catch(...)
  1563. {
  1564. SPDLOG_LOGGER_ERROR(m_logger,"解析 tRuleInfo 数据失败");
  1565. return false;
  1566. }
  1567. return true;
  1568. }
  1569. /* 获取报警时段,也同时获取报警的应用信息,tPeriod表 */
  1570. bool FromWebAPI::getAlarmAppInfo(std::list<AppAndTimeInfo>& listInfo)
  1571. {
  1572. nJson json0;
  1573. json0["opName"] = "SPSS_SelectPeriod";
  1574. QString strCmd = QString::fromStdString(json0.dump());
  1575. QString strRet;
  1576. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet);
  1577. if(ret != 0)
  1578. {
  1579. SPDLOG_LOGGER_DEBUG(m_logger,"获取AlarmApp失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  1580. return false;
  1581. }
  1582. try
  1583. {
  1584. nJson json1 = nJson::parse(strRet.toStdString());
  1585. int retCode = json1["code"].get<int>();
  1586. if(retCode != 0)
  1587. {
  1588. SPDLOG_LOGGER_ERROR(m_logger,"获取tAlarmApp失败");
  1589. return false;
  1590. }
  1591. nJson result = json1["result"];
  1592. if(result.empty())
  1593. {
  1594. return false;
  1595. }
  1596. listInfo.clear();
  1597. for(const auto& it : result)
  1598. {
  1599. AppAndTimeInfo info;
  1600. info.ChannelID = it["chnId"].get<int>();
  1601. info.AppType = it["appType"].get<int>();
  1602. QString strStart = QString::fromStdString(it["startTime"].get<std::string>());
  1603. QString strEnd = QString::fromStdString(it["endTime"].get<std::string>());
  1604. info.StartTime = QDateTime::fromString(strStart, "yyyy-MM-ss hh:mm:ss");
  1605. info.EndTime = QDateTime::fromString(strEnd, "yyyy-MM-ss hh:mm:ss");
  1606. listInfo.push_back(info);
  1607. }
  1608. }
  1609. catch (const nJson::parse_error& e) {
  1610. SPDLOG_LOGGER_ERROR(m_logger,"解析tPeriod数据失败:{}, 错误ID:{}",e.what(), e.id);
  1611. return false;
  1612. }
  1613. catch(const nJson::type_error& e)
  1614. {
  1615. SPDLOG_LOGGER_ERROR(m_logger,"解析tPeriod数据失败:{}, 错误ID:{}",e.what(), e.id);
  1616. return false;
  1617. }
  1618. catch(...)
  1619. {
  1620. SPDLOG_LOGGER_ERROR(m_logger,"解析tPeriod数据失败");
  1621. return false;
  1622. }
  1623. return true;
  1624. }
  1625. /* 结束人员计数报警,通过频率ID和应用ID */
  1626. bool FromWebAPI::endPersonCountAlarm(int chnID, AppFunction appID)
  1627. {
  1628. if(m_httpApi == nullptr)
  1629. {
  1630. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  1631. return false;
  1632. }
  1633. try
  1634. {
  1635. nJson json0;
  1636. json0["opName"] = "SPSS_EndPersonCountAlarm";
  1637. nJson json1;
  1638. json1["channelID"] = chnID;
  1639. json1["appID"] = getAppFunctionID(appID);
  1640. json0["paramList"] = json1;
  1641. QString strCmd = QString::fromStdString(json0.dump());
  1642. QString strRet;
  1643. int ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Update, strCmd, strRet);
  1644. if(ret != 0)
  1645. {
  1646. SPDLOG_LOGGER_WARN(m_logger,"结束人员计数报警失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  1647. return false;
  1648. }
  1649. }
  1650. catch (const nJson::parse_error& e) {
  1651. SPDLOG_LOGGER_ERROR(m_logger,"创建 结束人员计数报警 JSON失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  1652. return false;
  1653. }
  1654. catch (const nJson::type_error& e) {
  1655. SPDLOG_LOGGER_ERROR(m_logger,"创建 结束人员计数报警 JSON失败 数据失败:{}, 错误ID:{}",e.what(), e.id);
  1656. return false;
  1657. }
  1658. catch(...) {
  1659. SPDLOG_LOGGER_ERROR(m_logger,"创建 结束人员计数报警 JSON失败 数据失败");
  1660. return false;
  1661. }
  1662. return true;
  1663. }
  1664. /* 写入在岗信息 tWorkOnInfo */
  1665. bool FromWebAPI::insertOnWorkInfo(const RoomFaceInfo& onWorkInfo)
  1666. {
  1667. if(m_httpApi == nullptr)
  1668. {
  1669. SPDLOG_LOGGER_ERROR(m_logger,"WebApi is nullptr");
  1670. return false;
  1671. }
  1672. if(onWorkInfo.listPersonInfo.empty())
  1673. {
  1674. return false;
  1675. }
  1676. /* 拼接人脸ID和人脸名称 */
  1677. std::string strFaceID;
  1678. std::string strFaceName;
  1679. for(const auto& it : onWorkInfo.listPersonInfo)
  1680. {
  1681. strFaceID += it.PersonID + ";";
  1682. strFaceName += it.PersonName + ";";
  1683. }
  1684. /* 去掉最后的“;” */
  1685. strFaceID = strFaceID.substr(0, strFaceID.size() - 1);
  1686. strFaceName = strFaceName.substr(0, strFaceName.size() - 1);
  1687. try
  1688. {
  1689. nJson json0;
  1690. json0["opName"] = "SPSS_InsertToOnWork";
  1691. nJson json1;
  1692. json1["nChID"] = onWorkInfo.ChannelID;
  1693. json1["CamerId"] = onWorkInfo.CameraID;
  1694. json1["FaceIdList"] = strFaceID;
  1695. json1["FaceNameList"] = strFaceName;
  1696. json1["StartTime"] = onWorkInfo.StartTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  1697. json1["EndTime"] = onWorkInfo.EndTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  1698. json1["MaxNumber"] = onWorkInfo.MaxNum;
  1699. json1["MinNumber"] = onWorkInfo.MinNum;
  1700. json0["paramList"] = json1;
  1701. QString strCmd = QString::fromStdString(json0.dump());
  1702. QString strRet;
  1703. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Insert, strCmd, strRet);
  1704. if(ret != 0)
  1705. {
  1706. SPDLOG_LOGGER_WARN(m_logger,"插入 tWorkOnInfo 失败:{}, 错误信息:{}",ret,m_httpApi->DoGetLastError(&ret).toStdString());
  1707. return false;
  1708. }
  1709. // SPDLOG_LOGGER_DEBUG(m_logger,"插入在岗信息成功!");
  1710. }
  1711. catch (const nJson::parse_error& e) {
  1712. SPDLOG_LOGGER_ERROR(m_logger,"生成 tWorkOnInfo JSON 数据失败:{}, 错误ID:{}",e.what(), e.id);
  1713. return false;
  1714. }
  1715. catch(const nJson::type_error& e)
  1716. {
  1717. SPDLOG_LOGGER_ERROR(m_logger,"生成 tWorkOnInfo JSON 数据失败:{}, 错误ID:{}",e.what(), e.id);
  1718. return false;
  1719. }
  1720. catch(...)
  1721. {
  1722. SPDLOG_LOGGER_ERROR(m_logger,"生成 tWorkOnInfo JSON 数据失败");
  1723. return false;
  1724. }
  1725. return true;
  1726. }