FromWebAPI.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. #include "FromWebAPI.h"
  2. #include <QVector>
  3. #include <QString>
  4. #include <QUuid>
  5. #include <QApplication>
  6. #include "LHQLogAPI.h"
  7. #include "TransmitterSwitchInfo.h"
  8. #include "lhhttpapi.h"
  9. FromWebAPI::FromWebAPI()
  10. {
  11. }
  12. FromWebAPI::~FromWebAPI()
  13. {
  14. if(m_httpApi != nullptr)
  15. {
  16. delete m_httpApi;
  17. m_httpApi = nullptr;
  18. }
  19. }
  20. /* 初始化WebApi */
  21. bool FromWebAPI::initWebApi(const QString& url, const QString& serverID, const QString& serverKey)
  22. {
  23. if(m_httpApi == nullptr)
  24. {
  25. m_httpApi = new lhhttpapi;
  26. }
  27. #if defined(Q_OS_WIN)
  28. #ifdef QT_DEBUG
  29. QString libFile = QString("%1/LHSqlWebInterfaced.dll").arg(QApplication::applicationDirPath());
  30. #else
  31. QString libFile = QString("%1/LHSqlWebInterface.dll").arg(QApplication::applicationDirPath());
  32. #endif
  33. #elif defined(Q_OS_LINUX)
  34. #ifdef QT_DEBUG
  35. QString libFile = QString("%1/libLHSqlWebInterface.so").arg(QApplication::applicationDirPath());
  36. #else
  37. QString libFile = QString("%1/libLHSqlWebInterface.so").arg(QApplication::applicationDirPath());
  38. #endif
  39. #endif
  40. if(!m_httpApi->Load(libFile))
  41. {
  42. LH_WRITE_ERROR("Load WebAPI failed");
  43. return false;
  44. }
  45. LH_WRITE_LOG(QString("URL:%1").arg(url));
  46. LH_WRITE_LOG_DEBUG(QString("ServerID:%1").arg(serverID));
  47. // LH_WRITE_LOG_DEBUG(QString("ServerKey:%1").arg(serverKey));
  48. void* pHttp = nullptr;
  49. int i = 0;
  50. for(i = 0; i < 3; i++)
  51. {
  52. pHttp = m_httpApi->DBInit(url.toStdString().c_str(), true);
  53. if(pHttp != nullptr)
  54. {
  55. break;
  56. }
  57. LH_WRITE_ERROR(QString("设置WebAPI地址失败:%1").arg(m_httpApi->DoGetLastError(&i)));
  58. }
  59. if(i >= 3)
  60. {
  61. LH_WRITE_ERROR("WebAPI设置地址错误!");
  62. return false;
  63. }
  64. int ret = 0;
  65. #ifdef QT_DEBUG
  66. /* 获取服务器列表 */
  67. char serverList[8192]={0};
  68. ret = m_httpApi->DBGetServerList(serverList, 8192-1);
  69. if(ret != 0)
  70. {
  71. LH_WRITE_ERROR(QString("Get server list failed:%1, error info:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  72. return false;
  73. }
  74. // LH_WRITE_LOG(QString("\nGet server list success:%1").arg(serverList));
  75. #endif
  76. /* 登录,第二个参数是限制的服务 */
  77. for(i = 0; i < 3; i++)
  78. {
  79. ret = m_httpApi->DBLogin("", serverID, "ESM8C", m_userToken, true, pHttp);
  80. if(ret == 0)
  81. {
  82. break;
  83. }
  84. /* 登录失败,等待一段时间 */
  85. QThread::msleep(200);
  86. LH_WRITE_ERROR(QString("Login failed:%1, error info:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  87. }
  88. if(i >= 3)
  89. {
  90. LH_WRITE_ERROR(QString("登陆WebAPI失败!"));
  91. return false;
  92. }
  93. LH_WRITE_LOG("\n");
  94. LH_WRITE_LOG("WebAPI Login success!");
  95. return true;
  96. }
  97. /* 获取设备信息 */
  98. bool FromWebAPI::getDeviceInfo(QMap<QString, DeviceInfo>& mapDevice)
  99. {
  100. if(m_httpApi == nullptr)
  101. {
  102. LH_WRITE_ERROR("WebAPI is nullptr");
  103. return false;
  104. }
  105. nJson json0;
  106. json0["opName"] = "ESM8C_GetDevice";
  107. QString strCmd = QString::fromStdString(json0.dump());
  108. QString strRet;
  109. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  110. if(ret != 0)
  111. {
  112. LH_WRITE_ERROR(QString("从数据库获取设备失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  113. return false;
  114. }
  115. /* 解析获取到的JSON数据 */
  116. // LH_WRITE_LOG_DEBUG(strRet);
  117. try
  118. {
  119. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  120. int retCode = lowerKeyJson["code"].get<int>();
  121. if(retCode != 0)
  122. {
  123. LH_WRITE_ERROR("获取设备数据失败");
  124. return false;
  125. }
  126. nJson result = lowerKeyJson["result"];
  127. for(auto& it : result)
  128. {
  129. DeviceInfo DevInfo;
  130. QString devName = QString::fromStdString(it["dname"].get<std::string>());
  131. DevInfo.devName = devName;
  132. DevInfo.DID = it["did"].is_null() ? -1 : it["did"].get<int>();
  133. DevInfo.DTID = it["dtid"].is_null() ? -1 : it["dtid"].get<int>();
  134. DevInfo.MPID = it["mpid"].is_null() ? -1 : it["mpid"].get<int>();
  135. DevInfo.PTTypeCode = it["pttypecode"].is_null() ? -1 : it["pttypecode"].get<int>();
  136. DevInfo.ChannelID = it["chnid"].is_null() ? -1 : it["chnid"].get<int>();
  137. if(DevInfo.ChannelID <= 0)
  138. {
  139. // LH_WRITE_LOG_DEBUG(QString("通道ID错误:%1").arg(DevInfo.ChannelID));
  140. continue;
  141. }
  142. /* 查找对应的设备类型,根据PTTypeCode来查找,这个是唯一的 */
  143. auto devType = DevTypeContainer.getDevType(DevInfo.PTTypeCode);
  144. if(devType.PTTypeCode < 0)
  145. {
  146. // LH_WRITE_LOG_DEBUG(QString("未找到设备类型:%1").arg(DevInfo.PTTypeCode));
  147. continue;
  148. }
  149. DevInfo.DevType = devType;
  150. /* 检查有没有设置主备 */
  151. int devMB = it["dClass"].is_null() ? -1 : it["dClass"].get<int>();
  152. if(devMB < 0)
  153. {
  154. LH_WRITE_ERROR(QString("设备类型:%1《主/备》 类型为空").arg(DevInfo.DevType.PTTypeCode));
  155. continue;
  156. }
  157. mapDevice.insert(DevInfo.devName, DevInfo);
  158. }
  159. LH_WRITE_LOG(QString("获取设备数据成功,设备数目:%1").arg(mapDevice.size()));
  160. } catch (const nJson::parse_error& e) {
  161. LH_WRITE_ERROR("解析ESM-8C设备数据失败");
  162. return false;
  163. }
  164. catch (const nJson::exception& e)
  165. {
  166. LH_WRITE_ERROR("解析ESM-8C设备数据失败");
  167. return false;
  168. }
  169. catch(...)
  170. {
  171. LH_WRITE_ERROR("解析ESM-8C设备数据失败");
  172. return false;
  173. }
  174. return true;
  175. }
  176. /* 获取频率信息 */
  177. bool FromWebAPI::getChannelInfo(QMap<int, ChannelInfo>& mapFreq)
  178. {
  179. /* 测试,创建几个频率 */
  180. // for(int i = 0; i < 12; i++)
  181. // {
  182. // ChannelInfo freqInfo;
  183. // freqInfo.ChannelID = i + 1;
  184. // freqInfo.ChannelName = QString("频率%1").arg(freqInfo.ChannelID);
  185. // mapFreq.insert(freqInfo.ChannelID, freqInfo);
  186. // }
  187. if(m_httpApi == nullptr)
  188. {
  189. LH_WRITE_ERROR("WebAPI is nullptr");
  190. return false;
  191. }
  192. nJson json0;
  193. json0["opName"] = "ESM8C_GetChannel";
  194. QString strCmd = QString::fromStdString(json0.dump());
  195. QString strRet;
  196. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  197. if(ret != 0)
  198. {
  199. LH_WRITE_ERROR(QString("从数据库获取频率信息失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  200. return false;
  201. }
  202. try
  203. {
  204. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  205. int retCode = lowerKeyJson["code"].get<int>();
  206. if(retCode != 0)
  207. {
  208. LH_WRITE_ERROR("获取频率数据失败");
  209. return false;
  210. }
  211. nJson result = lowerKeyJson["result"];
  212. for(auto& it : result)
  213. {
  214. ChannelInfo freqInfo;
  215. freqInfo.ChannelID = it["chnid"].is_null() ? -1 : it["chnid"].get<int>();
  216. if(freqInfo.ChannelID <= 0)
  217. {
  218. LH_WRITE_LOG_DEBUG(QString("通道ID错误:%1").arg(freqInfo.ChannelID));
  219. continue;
  220. }
  221. freqInfo.ChannelName = QString::fromStdString(it["chnname"].get<std::string>());
  222. mapFreq.insert(freqInfo.ChannelID, freqInfo);
  223. }
  224. }
  225. catch (const nJson::parse_error& e) {
  226. LH_WRITE_ERROR("解析频率信息数据失败");
  227. return false;
  228. }
  229. catch (const nJson::exception& e)
  230. {
  231. LH_WRITE_ERROR("解析频率信息数据失败");
  232. return false;
  233. }
  234. catch(...)
  235. {
  236. LH_WRITE_ERROR("解析频率信息数据失败");
  237. return false;
  238. }
  239. LH_WRITE_LOG_DEBUG(QString("获取频率数据成功,频率数目:%1").arg(mapFreq.size()));
  240. return true;
  241. }
  242. /**
  243. * @brief 将数据写入EQM数据库
  244. *
  245. * @param chnID 频率ID
  246. * @param list
  247. * @return true
  248. * @return false
  249. */
  250. bool FromWebAPI::insertData(int chnID, QList<OnePlanItemInfo>& list)
  251. {
  252. if(m_httpApi == nullptr)
  253. {
  254. LH_WRITE_ERROR("WebAPI is nullptr");
  255. return false;
  256. }
  257. /* 先删除表格中的数据 */
  258. deleteOneChnPlan(chnID);
  259. QDate zeroDate(1970,1,1);
  260. /* 写入数据 */
  261. nJson json0 = nJson::array();
  262. try{
  263. for(auto& it : list)
  264. {
  265. nJson json1;
  266. json1["opName"] = "ESM8C_InsertDataToExecPlan";
  267. json1["Key"] = QUuid::createUuid().toString().toStdString();
  268. nJson json2;
  269. json2["channelID"] = it.ChannelID;
  270. json2["channelName"] = it.ChannelName.toStdString();
  271. json2["onWeekDay"] = it.onWeekDay;
  272. json2["onDate"] = it.onDateTime.date().toString("yyyy-MM-dd").toStdString();
  273. json2["onTime"] = it.onDateTime.time().toString("hh:mm:ss").toStdString();
  274. json2["offWeekDay"] = it.offWeekDay;
  275. json2["offDate"] = it.offDateTime.date().toString("yyyy-MM-dd").toStdString();
  276. json2["offTime"] = it.offDateTime.time().toString("hh:mm:ss").toStdString();
  277. json1["paramList"] = json2;
  278. json0.push_back(json1);
  279. }
  280. }catch (const nJson::parse_error& e) {
  281. SPDLOG_ERROR("生成json数据失败:{}", e.what());
  282. return false;
  283. }
  284. catch (const nJson::exception& e)
  285. {
  286. SPDLOG_ERROR("生成json数据失败:{}", e.what());
  287. return false;
  288. }
  289. catch (...)
  290. {
  291. SPDLOG_ERROR("生成json数据失败");
  292. return false;
  293. }
  294. /* 将json写入本地文件 */
  295. #ifdef C_DEBUG
  296. QString fileJson(QString::fromStdString(json0.dump(4)));
  297. // LH_WRITE_LOG_DEBUG("\n" + fileJson);
  298. // QString filePath = QApplication::applicationDirPath() + QString("ExecPlanData.json");
  299. // SPDLOG_LOGGER_DEBUG(m_logger,"To EQM Json写入文件:{}", filePath.toStdString());
  300. // QFile file(filePath);
  301. // if(file.open(QIODevice::WriteOnly | QIODevice::Truncate))
  302. // {
  303. // file.write(fileJson.toUtf8());
  304. // file.close();
  305. // }else {
  306. // SPDLOG_LOGGER_ERROR(m_logger,"打开文件失败:{}", filePath.toStdString());
  307. // }
  308. #endif
  309. /* 写入EQM数据库 */
  310. QString strCmd = QString::fromStdString(json0.dump());
  311. QString strRet;
  312. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet, true);
  313. if(ret != 0)
  314. {
  315. LH_WRITE_ERROR(QString("写入EQM数据库失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  316. return false;
  317. }
  318. // LH_WRITE_LOG(QString("计划写入EQM数据库成功, 计划数目:%1").arg(QString::number(list.size())));
  319. return true;
  320. }
  321. /**
  322. * @brief 获取tExecPlan表格数据,获取到的数据会根据设备名查找对应的设备信息,如果没有查找到,就抛弃这个计划
  323. *
  324. * @param chnID 频率ID
  325. * @param mapPlan
  326. * @return true
  327. * @return false
  328. */
  329. bool FromWebAPI::getExecPlanData(int chnID, QList<OnePlanItemInfo>& list)
  330. {
  331. if(m_httpApi == nullptr)
  332. {
  333. LH_WRITE_ERROR("WebAPI is nullptr");
  334. return false;
  335. }
  336. nJson json0;
  337. json0["opName"] = "ESM8C_GetOneChnPlan";
  338. nJson json1;
  339. json1["channelID"] = chnID;
  340. json0["paramList"] = json1;
  341. QString strCmd = QString::fromStdString(json0.dump());
  342. QString strRet;
  343. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  344. if(ret != 0)
  345. {
  346. LH_WRITE_ERROR(QString("从EQM获取tExecPlan表格数据失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  347. return false;
  348. }
  349. /* 解析获取到的JSON数据 */
  350. // SPDLOG_LOGGER_DEBUG(m_logger,"获取执行计划:\n{}",strRet.toStdString());
  351. try
  352. {
  353. // LH_WRITE_LOG_DEBUG(QString::fromStdString(nJson::parse(strRet.toStdString()).dump(4)));
  354. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  355. int retCode = lowerKeyJson["code"].get<int>();
  356. if(retCode != 0)
  357. {
  358. LH_WRITE_ERROR("获取tExecPlan表格数据失败");
  359. return false;
  360. }
  361. nJson result = lowerKeyJson["result"];
  362. for(auto& it : result)
  363. {
  364. OnePlanItemInfo info;
  365. info.ChannelID = chnID;
  366. std::string strChnName = it["channelName"].is_null() ? "" : it["channelName"].get<std::string>();
  367. info.ChannelName = QString::fromStdString(strChnName);
  368. info.onWeekDay = static_cast<enum_WeekDay>(it["onweekday"].get<int>());
  369. info.onDateTime.setDate(QDate::fromString(QString::fromStdString(it["ondate"].get<std::string>()), "yyyy-MM-dd"));
  370. info.onDateTime.setTime(QTime::fromString(QString::fromStdString(it["ontime"].get<std::string>()), "hh:mm:ss"));
  371. info.offWeekDay = static_cast<enum_WeekDay>(it["offweekday"].get<int>());
  372. info.offDateTime.setDate(QDate::fromString(QString::fromStdString(it["offdate"].get<std::string>()), "yyyy-MM-dd"));
  373. info.offDateTime.setTime(QTime::fromString(QString::fromStdString(it["offtime"].get<std::string>()), "hh:mm:ss"));
  374. list.append(info);
  375. }
  376. } catch (const nJson::parse_error& e) {
  377. LH_WRITE_ERROR(QString("解析 tExecPlan 表格数据失败:%1").arg(e.what()));
  378. return false;
  379. }
  380. catch (const nJson::exception& e)
  381. {
  382. LH_WRITE_ERROR(QString("解析 tExecPlan 表格数据失败:%1").arg(e.what()));
  383. return false;
  384. }
  385. catch(...)
  386. {
  387. LH_WRITE_ERROR("解析 tExecPlan 表格数据失败");
  388. return false;
  389. }
  390. LH_WRITE_LOG_DEBUG(QString("从 tExecPlan 表格获取数据成功,计划数目:%1").arg(list.size()));
  391. return true;
  392. }
  393. bool FromWebAPI::getExecPlanData(QMap<int, QList<OnePlanItemInfo>>& mapPlan)
  394. {
  395. if(m_httpApi == nullptr)
  396. {
  397. LH_WRITE_ERROR("WebAPI is nullptr");
  398. return false;
  399. }
  400. nJson json0;
  401. json0["opName"] = "ESM8C_GetExecPlan";
  402. nJson json1;
  403. json0["paramList"] = json1;
  404. QString strCmd = QString::fromStdString(json0.dump());
  405. QString strRet;
  406. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  407. if(ret != 0)
  408. {
  409. LH_WRITE_ERROR(QString("从EQM获取tExecPlan表格数据失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  410. return false;
  411. }
  412. /* 解析获取到的JSON数据 */
  413. // SPDLOG_LOGGER_DEBUG(m_logger,"获取执行计划:\n{}",strRet.toStdString());
  414. try
  415. {
  416. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  417. int retCode = lowerKeyJson["code"].get<int>();
  418. if(retCode != 0)
  419. {
  420. LH_WRITE_ERROR("获取tExecPlan表格数据失败");
  421. return false;
  422. }
  423. nJson result = lowerKeyJson["result"];
  424. for(auto& it : result)
  425. {
  426. OnePlanItemInfo info;
  427. info.ChannelID = it["channelid"].is_null() ? -1 : it["channelid"].get<int>();
  428. info.ChannelName = QString::fromStdString(it["channelname"].get<std::string>());
  429. info.onWeekDay = static_cast<enum_WeekDay>(it["onweekday"].get<int>());
  430. info.onDateTime.setDate(QDate::fromString(QString::fromStdString(it["ondate"].get<std::string>()), "yyyy-MM-dd"));
  431. info.onDateTime.setTime(QTime::fromString(QString::fromStdString(it["ontime"].get<std::string>()), "hh:mm:ss"));
  432. info.offWeekDay = static_cast<enum_WeekDay>(it["offweekday"].get<int>());
  433. info.offDateTime.setDate(QDate::fromString(QString::fromStdString(it["offdate"].get<std::string>()), "yyyy-MM-dd"));
  434. info.offDateTime.setTime(QTime::fromString(QString::fromStdString(it["offtime"].get<std::string>()), "hh:mm:ss"));
  435. /* 查找对应的设备列表 */
  436. auto list = mapPlan.find(info.ChannelID);
  437. if(list == mapPlan.end())
  438. {
  439. /* 创建一个新的列表 */
  440. QList<OnePlanItemInfo> listPlan;
  441. listPlan.append(info);
  442. mapPlan.insert(info.ChannelID, listPlan);
  443. }else {
  444. /* 找到对应的列表 */
  445. list->append(info);
  446. }
  447. }
  448. } catch (const nJson::parse_error& e) {
  449. LH_WRITE_ERROR(QString("解析 tExecPlan 表格数据失败:%1").arg(e.what()));
  450. return false;
  451. }
  452. catch (const nJson::exception& e)
  453. {
  454. LH_WRITE_ERROR(QString("解析 tExecPlan 表格数据失败:%1").arg(e.what()));
  455. return false;
  456. }
  457. catch(...)
  458. {
  459. LH_WRITE_ERROR("解析 tExecPlan 表格数据失败");
  460. return false;
  461. }
  462. return true;
  463. }
  464. /* 删除所有行 */
  465. bool FromWebAPI::deleteAllRow()
  466. {
  467. if(m_httpApi == nullptr)
  468. {
  469. LH_WRITE_ERROR("WebAPI is nullptr");
  470. return false;
  471. }
  472. nJson json0;
  473. json0["opName"] = "ESM8C_DeleteAllRow";
  474. QString strCmd = QString::fromStdString(json0.dump());
  475. QString strRet;
  476. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet, true);
  477. if(ret != 0)
  478. {
  479. LH_WRITE_ERROR(QString("删除所有行失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  480. return false;
  481. }
  482. return true;
  483. }
  484. /* 删除一个发射机计划 */
  485. bool FromWebAPI::deleteOneChnPlan(int chnID)
  486. {
  487. if(m_httpApi == nullptr)
  488. {
  489. LH_WRITE_ERROR("WebAPI is nullptr");
  490. return false;
  491. }
  492. /* 查找出有多少发射机类型 */
  493. QList<int> listPttypeCodeID;
  494. for(auto& it : DevTypeContainer.getMapDevType())
  495. {
  496. listPttypeCodeID.append(it.PTTypeCode);
  497. }
  498. nJson json0;
  499. json0["opName"] = "ESM8C_DeleteOneChnPlan";
  500. json0["paramList"].clear();
  501. nJson json1;
  502. json1["channelID"] = chnID;
  503. json0["paramList"].push_back(json1);
  504. QString strCmd = QString::fromStdString(json0.dump());
  505. QString strRet;
  506. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet, true);
  507. if(ret != 0)
  508. {
  509. LH_WRITE_ERROR(QString("删除频率: %3 计划失败: %1, 错误信息: %2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)).arg(chnID));
  510. }else {
  511. // LH_WRITE_LOG(QString("删除频率: %1 计划成功!").arg(chnID));
  512. }
  513. return true;
  514. }
  515. /**
  516. * @brief 将页面的配置写入数据库,主要是默认计划和执行模式
  517. *
  518. * @param mapConfig
  519. * @return true
  520. * @return false
  521. */
  522. bool FromWebAPI::insertConfigData(QMap<int, ConfigData>& list)
  523. {
  524. if(m_httpApi == nullptr)
  525. {
  526. LH_WRITE_ERROR("WebAPI is nullptr");
  527. return false;
  528. }
  529. deleteConfigData(list);
  530. nJson json0 = nJson::array();
  531. try{
  532. for(auto begin = list.begin(), end = list.end(); begin != end; begin++)
  533. {
  534. nJson json1;
  535. json1["opName"] = "ESM8C_InsertExecPlanConfig";
  536. json1["Key"] = QUuid::createUuid().toString().toStdString();
  537. nJson json2;
  538. json2["keyWord"] = begin->key;
  539. json2["value1"] = begin->value;
  540. json2["updateTime"] = begin->updateTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  541. json2["notes"] = begin->notes.toStdString();
  542. json1["paramList"] = json2;
  543. json0.push_back(json1);
  544. }
  545. }catch (const nJson::parse_error& e) {
  546. SPDLOG_ERROR("生成json数据失败:{}", e.what());
  547. return false;
  548. }
  549. catch (const nJson::exception& e)
  550. {
  551. SPDLOG_ERROR("生成json数据失败:{}", e.what());
  552. return false;
  553. }
  554. catch (...)
  555. {
  556. SPDLOG_ERROR("生成json数据失败");
  557. return false;
  558. }
  559. /* 写入EQM数据库 */
  560. QString strCmd = QString::fromStdString(json0.dump());
  561. QString strRet;
  562. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet, true);
  563. if(ret != 0)
  564. {
  565. LH_WRITE_ERROR(QString("写入tExecPlanConfig失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  566. return false;
  567. }
  568. LH_WRITE_LOG_DEBUG(QString("页面配置写入tExecPlanConfig成功, 数目:%1").arg(QString::number(list.size())));
  569. return true;
  570. }
  571. /* 删除配置数据 */
  572. bool FromWebAPI::deleteConfigData(QMap<int, ConfigData>& list)
  573. {
  574. if(m_httpApi == nullptr)
  575. {
  576. LH_WRITE_ERROR("WebAPI is nullptr");
  577. return false;
  578. }
  579. nJson json0 = nJson::array();
  580. try{
  581. for(auto begin = list.begin(), end = list.end(); begin != end; begin++)
  582. {
  583. nJson json1;
  584. json1["opName"] = "ESM8C_DeleteExecPlanConfig";
  585. json1["Key"] = QUuid::createUuid().toString().toStdString();
  586. nJson json2;
  587. json2["keyWord"] = begin->key;
  588. json1["paramList"] = json2;
  589. json0.push_back(json1);
  590. }
  591. }catch (const nJson::parse_error& e) {
  592. SPDLOG_ERROR("生成json数据失败:{}", e.what());
  593. return false;
  594. }
  595. catch (const nJson::exception& e)
  596. {
  597. SPDLOG_ERROR("生成json数据失败:{}", e.what());
  598. return false;
  599. }
  600. catch (...)
  601. {
  602. SPDLOG_ERROR("生成json数据失败");
  603. return false;
  604. }
  605. /* 写入EQM数据库 */
  606. QString strCmd = QString::fromStdString(json0.dump());
  607. QString strRet;
  608. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet, true);
  609. if(ret != 0)
  610. {
  611. LH_WRITE_ERROR(QString("删除tExecPlanConfig表格数据失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  612. return false;
  613. }
  614. LH_WRITE_LOG_DEBUG(QString("删除tExecPlanConfig表格数据成功, 计划数目:%1").arg(QString::number(list.size())));
  615. return true;
  616. }
  617. /* 获取配置数据 */
  618. bool FromWebAPI::getConfigData(QMap<int, ConfigData>& list)
  619. {
  620. if(m_httpApi == nullptr)
  621. {
  622. LH_WRITE_ERROR("WebAPI is nullptr");
  623. return false;
  624. }
  625. nJson json0;
  626. json0["opName"] = "ESM8C_GetExecPlanConfig";
  627. QString strCmd = QString::fromStdString(json0.dump());
  628. QString strRet;
  629. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  630. if(ret != 0)
  631. {
  632. LH_WRITE_ERROR(QString("获取tExecPlanConfig表格数据失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  633. return false;
  634. }
  635. /* 解析获取到的JSON数据 */
  636. try{
  637. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  638. // LH_WRITE_LOG_DEBUG(QString::fromStdString(lowerKeyJson.dump(4)));
  639. int retCode = lowerKeyJson["code"].get<int>();
  640. if(retCode != 0)
  641. {
  642. LH_WRITE_ERROR("获取tExecPlanConfig失败");
  643. return false;
  644. }
  645. nJson result = lowerKeyJson["result"];
  646. for(auto& it : result)
  647. {
  648. ConfigData data;
  649. data.key = it["keyword"].get<int>();
  650. data.value = it["value1"].get<int>();
  651. data.updateTime = QDateTime::fromString(QString::fromStdString(it["updatetime"].get<std::string>()),"yyyy-MM-dd hh:mm:ss");
  652. data.notes = QString::fromStdString(it["notes"].get<std::string>());
  653. list.insert(data.key, data);
  654. }
  655. } catch (const nJson::parse_error& e) {
  656. LH_WRITE_ERROR(QString("解析tExecPlanConfig数据失败:%1").arg(e.what()));
  657. return false;
  658. }
  659. catch (const nJson::exception& e)
  660. {
  661. LH_WRITE_ERROR(QString("解析tExecPlanConfig数据失败:%1").arg(e.what()));
  662. return false;
  663. }
  664. catch(...)
  665. {
  666. LH_WRITE_ERROR("解析tExecPlanConfig数据失败");
  667. return false;
  668. }
  669. LH_WRITE_LOG_DEBUG(QString("获取tExecPlanConfig表格数据成功,数据数目:%1").arg(list.size()));
  670. return true;
  671. }
  672. /* 单独获取某个关键字的数据 */
  673. bool FromWebAPI::getConfigData(int key, ConfigData& data)
  674. {
  675. if(m_httpApi == nullptr)
  676. {
  677. LH_WRITE_ERROR("WebAPI is nullptr");
  678. return false;
  679. }
  680. nJson json0;
  681. json0["opName"] = "ESM8C_GetExecPlanConfigOfKeyWord";
  682. nJson json1;
  683. json1["keyWord"] = key;
  684. json0["paramList"] = json1;
  685. QString strCmd = QString::fromStdString(json0.dump());
  686. QString strRet;
  687. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  688. if(ret != 0)
  689. {
  690. LH_WRITE_ERROR(QString("获取tExecPlanConfig表格数据失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  691. return false;
  692. }
  693. /* 解析获取到的JSON数据 */
  694. LH_WRITE_LOG_DEBUG(strRet);
  695. try{
  696. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  697. int retCode = lowerKeyJson["code"].get<int>();
  698. if(retCode != 0)
  699. {
  700. LH_WRITE_ERROR("获取tExecPlanConfig失败");
  701. return false;
  702. }
  703. nJson result = lowerKeyJson["result"];
  704. for(auto& it : result)
  705. {
  706. data.key = it["keyword"].get<int>();
  707. if(data.key != key)
  708. {
  709. LH_WRITE_ERROR("获取到的数据和传入的关键字不匹配");
  710. continue;
  711. }
  712. data.value = it["value1"].get<int>();
  713. data.updateTime = QDateTime::fromString(QString::fromStdString(it["updatetime"].get<std::string>()),"yyyy-MM-dd hh:mm:ss");
  714. data.notes = QString::fromStdString(it["notes"].get<std::string>());
  715. break;
  716. }
  717. } catch (const nJson::parse_error& e) {
  718. LH_WRITE_ERROR(QString("解析tExecPlanConfig数据失败:%1").arg(e.what()));
  719. return false;
  720. }
  721. catch (const nJson::exception& e)
  722. {
  723. LH_WRITE_ERROR(QString("解析tExecPlanConfig数据失败:%1").arg(e.what()));
  724. return false;
  725. }
  726. catch(...)
  727. {
  728. LH_WRITE_ERROR("解析tExecPlanConfig数据失败");
  729. return false;
  730. }
  731. if(data.key != key)
  732. {
  733. LH_WRITE_ERROR("获取到的数据和传入的关键字不匹配");
  734. return false;
  735. }
  736. return true;
  737. }
  738. /* 获取EQM数据库中的模版编号 */
  739. bool FromWebAPI::getEQMTemplateID(QList<int>& listId)
  740. {
  741. if(m_httpApi == nullptr)
  742. {
  743. LH_WRITE_ERROR("WebAPI is nullptr");
  744. return false;
  745. }
  746. nJson json0;
  747. json0["opName"] = "ESMC_GetTemplateNumFromExecPlan";
  748. QString strCmd = QString::fromStdString(json0.dump());
  749. QString strRet;
  750. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  751. if(ret != 0)
  752. {
  753. LH_WRITE_ERROR(QString("从EQM获取模版编号失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  754. return false;
  755. }
  756. /* 解析获取到的JSON数据 */
  757. LH_WRITE_LOG_DEBUG(strRet);
  758. try
  759. {
  760. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  761. int retCode = lowerKeyJson["code"].get<int>();
  762. if(retCode != 0)
  763. {
  764. LH_WRITE_ERROR("获取模版编号失败");
  765. return false;
  766. }
  767. nJson result = lowerKeyJson["result"];
  768. for(auto& it : result)
  769. {
  770. if(it["templateNum"].is_null())
  771. {
  772. LH_WRITE_ERROR("模版编号为空");
  773. continue;
  774. }
  775. auto id = it["templatenum"].get<int>();
  776. if(listId.contains(id))
  777. {
  778. continue;
  779. }
  780. listId.append(id);
  781. }
  782. } catch (const nJson::parse_error& e) {
  783. LH_WRITE_ERROR(QString("解析模版编号失败:%1").arg(e.what()));
  784. return false;
  785. }
  786. catch (const nJson::exception& e)
  787. {
  788. LH_WRITE_ERROR(QString("解析模版编号失败:%1").arg(e.what()));
  789. return false;
  790. }
  791. catch(...)
  792. {
  793. LH_WRITE_ERROR("解析模版编号失败");
  794. return false;
  795. }
  796. return true;
  797. }
  798. /* 保存到模板 */
  799. bool FromWebAPI::saveTemplate(const QString& templateName, QList<OnePlanItemInfo>& list)
  800. {
  801. if(m_httpApi == nullptr)
  802. {
  803. LH_WRITE_ERROR("WebAPI is nullptr");
  804. return false;
  805. }
  806. nJson json0 = nJson::array();
  807. for(const auto& it : list)
  808. {
  809. nJson json1;
  810. json1["opName"] = "TMS_InsertToExecPlanTemplate";
  811. json1["Key"] = QUuid::createUuid().toString().toStdString();
  812. nJson json2;
  813. json2["templateName"] = templateName.toStdString();
  814. json2["channelID"] = it.ChannelID;
  815. json2["channelName"] = it.ChannelName.toStdString();
  816. json2["onWeekDay"] = it.onWeekDay;
  817. json2["onDate"] = it.onDateTime.date().toString("yyyy-MM-dd").toStdString();
  818. json2["onTime"] = it.onDateTime.time().toString("hh:mm:ss").toStdString();
  819. json2["offWeekDay"] = it.offWeekDay;
  820. json2["offDate"] = it.offDateTime.date().toString("yyyy-MM-dd").toStdString();
  821. json2["offTime"] = it.offDateTime.time().toString("hh:mm:ss").toStdString();
  822. json1["paramList"] = json2;
  823. json0.push_back(json1);
  824. }
  825. /* 将json写入本地文件 */
  826. #ifdef C_DEBUG
  827. // QString fileJson(QString::fromStdString(json0.dump(4)));
  828. // LH_WRITE_LOG_DEBUG("\n" + fileJson);
  829. #endif
  830. /* 写入EQM数据库 */
  831. QString strCmd = QString::fromStdString(json0.dump());
  832. QString strRet;
  833. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet, true);
  834. if(ret != 0)
  835. {
  836. LH_WRITE_ERROR(QString("保存到模板失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  837. return false;
  838. }
  839. // LH_WRITE_LOG("保存到模板成功,模板名称:" + templateName + ", 计划数目:" + QString::number(list.size()));
  840. return true;
  841. }
  842. /* 批量写入模版数据 */
  843. bool FromWebAPI::saveTemplateList(const QMap<OneTemplateInfo, QList<OnePlanItemInfo>>& mapTemplate)
  844. {
  845. if(m_httpApi == nullptr)
  846. {
  847. LH_WRITE_ERROR("WebAPI is nullptr");
  848. return false;
  849. }
  850. nJson json0 = nJson::array();
  851. try
  852. {
  853. for(auto list = mapTemplate.begin(), end = mapTemplate.end(); list != end; list++)
  854. {
  855. for(auto& item : *list)
  856. {
  857. nJson json1;
  858. json1["opName"] = "TMS_InsertToExecPlanTemplate";
  859. json1["Key"] = QUuid::createUuid().toString().toStdString();
  860. nJson json2;
  861. json2["templateName"] = list.key().templateName.toStdString();
  862. json2["channelID"] = item.ChannelID;
  863. json2["channelName"] = item.ChannelName.toStdString();
  864. json2["onWeekDay"] = item.onWeekDay;
  865. json2["onDate"] = item.onDateTime.date().toString("yyyy-MM-dd").toStdString();
  866. json2["onTime"] = item.onDateTime.time().toString("hh:mm:ss").toStdString();
  867. json2["offWeekDay"] = item.offWeekDay;
  868. json2["offDate"] = item.offDateTime.date().toString("yyyy-MM-dd").toStdString();
  869. json2["offTime"] = item.offDateTime.time().toString("hh:mm:ss").toStdString();
  870. json1["paramList"] = json2;
  871. json0.push_back(json1);
  872. }
  873. }
  874. }
  875. catch(...)
  876. {
  877. LH_WRITE_ERROR("生成json数据失败");
  878. return false;
  879. }
  880. /* 写入EQM数据库 */
  881. QString strCmd = QString::fromStdString(json0.dump());
  882. QString strRet;
  883. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet, true);
  884. if(ret != 0)
  885. {
  886. LH_WRITE_ERROR(QString("保存到模板失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  887. return false;
  888. }
  889. // LH_WRITE_LOG("保存到模板成功,模板名称:" + templateName + ", 计划数目:" + QString::number(list.size()));
  890. return true;
  891. }
  892. /* 获取模板内容 */
  893. bool FromWebAPI::getOneTemplateData(OneTemplateInfo tmpInfo, QList<OnePlanItemInfo>& list)
  894. {
  895. if(m_httpApi == nullptr)
  896. {
  897. LH_WRITE_ERROR("WebAPI is nullptr");
  898. return false;
  899. }
  900. nJson json0;
  901. json0["opName"] = "TMS_GetExecPlanTemplateData";
  902. nJson json1;
  903. json1["templateName"] = tmpInfo.templateName.toStdString();
  904. json1["channelID"] = tmpInfo.channelInfo.ChannelID;
  905. json0["paramList"] = json1;
  906. QString strCmd = QString::fromStdString(json0.dump());
  907. QString strRet;
  908. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  909. if(ret != 0)
  910. {
  911. LH_WRITE_ERROR(QString("获取模板失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  912. return false;
  913. }
  914. /* 解析获取到的JSON数据 */
  915. try
  916. {
  917. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  918. int retCode = lowerKeyJson["code"].get<int>();
  919. if(retCode != 0)
  920. {
  921. LH_WRITE_ERROR("获取模板失败");
  922. return false;
  923. }
  924. nJson result = lowerKeyJson["result"];
  925. for(auto& it : result)
  926. {
  927. OnePlanItemInfo info;
  928. info.ChannelID = it["channelid"].is_null() ? -1 : it["channelid"].get<int>();
  929. info.ChannelName = QString::fromStdString(it["channelname"].get<std::string>());
  930. info.onWeekDay = static_cast<enum_WeekDay>(it["onweekday"].get<int>());
  931. info.onDateTime.setDate(QDate::fromString(QString::fromStdString(it["ondate"].get<std::string>()), "yyyy-MM-dd"));
  932. info.onDateTime.setTime(QTime::fromString(QString::fromStdString(it["ontime"].get<std::string>()), "hh:mm:ss"));
  933. info.offWeekDay = static_cast<enum_WeekDay>(it["offweekday"].get<int>());
  934. info.offDateTime.setDate(QDate::fromString(QString::fromStdString(it["offdate"].get<std::string>()), "yyyy-MM-dd"));
  935. info.offDateTime.setTime(QTime::fromString(QString::fromStdString(it["offtime"].get<std::string>()), "hh:mm:ss"));
  936. list.append(info);
  937. }
  938. LH_WRITE_LOG_DEBUG(QString("获取模板数据成功,数据数目:%1").arg(list.size()));
  939. }
  940. catch (const nJson::parse_error& e)
  941. {
  942. LH_WRITE_ERROR(QString("解析模板失败:%1").arg(e.what()));
  943. return false;
  944. }
  945. catch (const nJson::exception& e)
  946. {
  947. LH_WRITE_ERROR(QString("解析模板失败:%1").arg(e.what()));
  948. return false;
  949. }
  950. catch(...)
  951. {
  952. LH_WRITE_ERROR("解析模板失败");
  953. return false;
  954. }
  955. return true;
  956. }
  957. /* 获取多个模板的内容 */
  958. bool FromWebAPI::getTemplateData(QList<OneTemplateInfo>& listTemplate, QMap<int, QList<OnePlanItemInfo>>& mapPlan)
  959. {
  960. if(m_httpApi == nullptr)
  961. {
  962. LH_WRITE_ERROR("WebAPI is nullptr");
  963. return false;
  964. }
  965. nJson json0 = nJson::array();
  966. for(auto& it : listTemplate)
  967. {
  968. nJson json1;
  969. json1["opName"] = "TMS_GetExecPlanTemplateData";
  970. /* 这里的Key使用频率ID,方便后面区分是哪个频率的数据 */
  971. json1["Key"] = it.channelInfo.ChannelID;
  972. nJson json2;
  973. json2["templateName"] = it.templateName.toStdString();
  974. json2["channelID"] = it.channelInfo.ChannelID;
  975. json1["paramList"] = json2;
  976. json0.push_back(json1);
  977. }
  978. /* 打印生成的JSON */
  979. // LH_WRITE_LOG_DEBUG(QString::fromStdString(json0.dump(4)));
  980. QString strCmd = QString::fromStdString(json0.dump());
  981. QString strRet;
  982. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet, true);
  983. if(ret != 0)
  984. {
  985. LH_WRITE_ERROR(QString("获取模板失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  986. return false;
  987. }
  988. LH_WRITE_LOG_DEBUG(QString("获取到的json数据的大小:%1").arg(strRet.size()));
  989. /* 解析获取到的JSON数据 */
  990. try
  991. {
  992. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  993. // LH_WRITE_LOG_DEBUG(QString("\n: %1").arg(QString::fromStdString(jsonRet.dump(4))));
  994. int retCode = lowerKeyJson["code"].get<int>();
  995. if(retCode != 0)
  996. {
  997. LH_WRITE_ERROR("获取模板数据失败");
  998. return false;
  999. }
  1000. nJson data = lowerKeyJson["data"];
  1001. for(auto& it : data)
  1002. {
  1003. /* 逐步获取对应频率的计划信息 */
  1004. int retChnID = QString::fromStdString(it["key"].get<std::string>()).toInt();
  1005. if(retChnID == 0)
  1006. {
  1007. LH_WRITE_ERROR("获取到的频率ID为0");
  1008. continue;
  1009. }
  1010. nJson value = it["value"];
  1011. if(value.is_null())
  1012. {
  1013. LH_WRITE_ERROR("获取到的值为空");
  1014. continue;
  1015. }
  1016. int retCode1 = value["code"].get<int>();
  1017. if(retCode1 != 0)
  1018. {
  1019. LH_WRITE_ERROR(QString("获取模板:%1数据失败").arg(retChnID));
  1020. continue;
  1021. }
  1022. nJson result = value["result"];
  1023. QList<OnePlanItemInfo> listPlan;
  1024. for(auto& plan : result)
  1025. {
  1026. OnePlanItemInfo info;
  1027. info.ChannelID = plan["channelid"].is_null() ? -1 : plan["channelid"].get<int>();
  1028. info.ChannelName = plan["channelname"].is_null() ? "" : QString::fromStdString(plan["channelname"].get<std::string>());
  1029. info.onWeekDay = static_cast<enum_WeekDay>(plan["onweekday"].get<int>());
  1030. info.onDateTime.setDate(QDate::fromString(QString::fromStdString(plan["ondate"].get<std::string>()), "yyyy-MM-dd"));
  1031. info.onDateTime.setTime(QTime::fromString(QString::fromStdString(plan["ontime"].get<std::string>()), "hh:mm:ss"));
  1032. info.offWeekDay = static_cast<enum_WeekDay>(plan["offweekday"].get<int>());
  1033. info.offDateTime.setDate(QDate::fromString(QString::fromStdString(plan["offdate"].get<std::string>()), "yyyy-MM-dd"));
  1034. info.offDateTime.setTime(QTime::fromString(QString::fromStdString(plan["offtime"].get<std::string>()), "hh:mm:ss"));
  1035. listPlan.append(info);
  1036. }
  1037. mapPlan.insert(retChnID, listPlan);
  1038. }
  1039. }
  1040. catch (const nJson::parse_error& e)
  1041. {
  1042. LH_WRITE_ERROR(QString("解析模板失败:%1").arg(e.what()));
  1043. return false;
  1044. }
  1045. catch (const nJson::exception& e)
  1046. {
  1047. LH_WRITE_ERROR(QString("解析模板失败:%1").arg(e.what()));
  1048. return false;
  1049. }
  1050. catch(...)
  1051. {
  1052. LH_WRITE_ERROR("解析模板失败");
  1053. return false;
  1054. }
  1055. LH_WRITE_LOG_DEBUG(QString("获取多个频率模板成功,频率数目:%1").arg(mapPlan.size()));
  1056. return true;
  1057. }
  1058. /* 获取模板列表 */
  1059. bool FromWebAPI::getTemplateList(QMultiMap<int, OneTemplateInfo>& mapTemplate)
  1060. {
  1061. if(m_httpApi == nullptr)
  1062. {
  1063. LH_WRITE_ERROR("WebAPI is nullptr");
  1064. return false;
  1065. }
  1066. nJson json0;
  1067. json0["opName"] = "TMS_GetExecPlanTemplateList";
  1068. QString strCmd = QString::fromStdString(json0.dump());
  1069. QString strRet;
  1070. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  1071. if(ret != 0)
  1072. {
  1073. LH_WRITE_ERROR(QString("获取模板列表失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  1074. return false;
  1075. }
  1076. /* 解析获取到的JSON数据 */
  1077. // LH_WRITE_LOG_DEBUG(strRet);
  1078. try
  1079. {
  1080. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  1081. int retCode = lowerKeyJson["code"].get<int>();
  1082. if(retCode != 0)
  1083. {
  1084. LH_WRITE_ERROR("获取模板列表失败");
  1085. return false;
  1086. }
  1087. nJson result = lowerKeyJson["result"];
  1088. for(auto& it : result)
  1089. {
  1090. OneTemplateInfo info;
  1091. info.channelInfo.ChannelID = it["channelid"].get<int>();
  1092. info.templateName = QString::fromStdString(it["templatename"].get<std::string>());
  1093. mapTemplate.insert(info.channelInfo.ChannelID, info);
  1094. }
  1095. } catch (const nJson::parse_error& e) {
  1096. LH_WRITE_ERROR(QString("解析模板列表失败:%1").arg(e.what()));
  1097. return false;
  1098. }
  1099. catch (const nJson::exception& e)
  1100. {
  1101. LH_WRITE_ERROR(QString("解析模板列表失败:%1").arg(e.what()));
  1102. return false;
  1103. }
  1104. catch(...)
  1105. {
  1106. LH_WRITE_ERROR("解析模板列表失败");
  1107. return false;
  1108. }
  1109. // for(auto it = list.begin(); it != list.end(); it++)
  1110. // {
  1111. // LH_WRITE_LOG_DEBUG(QString("模板名称:%1, 类型:%2").arg(it.key()).arg(it.value()));
  1112. // }
  1113. return true;
  1114. }
  1115. /* 获取某个频率的模版列表 */
  1116. bool FromWebAPI::getTemplateList(int chnID, QList<OneTemplateInfo>& listTemplate)
  1117. {
  1118. if(m_httpApi == nullptr)
  1119. {
  1120. LH_WRITE_ERROR("WebAPI is nullptr");
  1121. return false;
  1122. }
  1123. nJson json0;
  1124. json0["opName"] = "TMS_GetExecPlanOneTemplateList";
  1125. nJson jsonParam;
  1126. jsonParam["channelID"] = chnID;
  1127. json0["paramList"] = jsonParam;
  1128. /* 发送请求 */
  1129. QString strCmd = QString::fromStdString(json0.dump());
  1130. QString strRet;
  1131. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  1132. if(ret != 0)
  1133. {
  1134. LH_WRITE_ERROR(QString("获取模板列表失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  1135. return false;
  1136. }
  1137. /* 解析获取到的JSON数据 */
  1138. // LH_WRITE_LOG_DEBUG(strRet);
  1139. try
  1140. {
  1141. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  1142. int retCode = lowerKeyJson["code"].get<int>();
  1143. if(retCode != 0)
  1144. {
  1145. LH_WRITE_ERROR("获取模板列表失败");
  1146. return false;
  1147. }
  1148. nJson result = lowerKeyJson["result"];
  1149. for(auto& it : result)
  1150. {
  1151. OneTemplateInfo info;
  1152. info.channelInfo.ChannelID = it["channelid"].get<int>();
  1153. info.templateName = QString::fromStdString(it["templatename"].get<std::string>());
  1154. listTemplate.append(info);
  1155. }
  1156. } catch (const nJson::parse_error& e) {
  1157. LH_WRITE_ERROR(QString("解析模板列表失败:%1").arg(e.what()));
  1158. return false;
  1159. }
  1160. catch (const nJson::exception& e)
  1161. {
  1162. LH_WRITE_ERROR(QString("解析模板列表失败:%1").arg(e.what()));
  1163. return false;
  1164. }
  1165. catch(...)
  1166. {
  1167. LH_WRITE_ERROR("解析模板列表失败");
  1168. return false;
  1169. }
  1170. return true;
  1171. }
  1172. /* 删除一个模板 */
  1173. bool FromWebAPI::deleteTemplate(const OneTemplateInfo& info)
  1174. {
  1175. if(m_httpApi == nullptr)
  1176. {
  1177. LH_WRITE_ERROR("WebAPI is nullptr");
  1178. return false;
  1179. }
  1180. nJson json0;
  1181. json0["opName"] = "TMS_DeleteExecPlanTemplate";
  1182. nJson json1;
  1183. json1["channelID"] = info.channelInfo.ChannelID;
  1184. json1["templateName"] = info.templateName.toStdString();
  1185. json0["paramList"] = json1;
  1186. QString strCmd = QString::fromStdString(json0.dump());
  1187. QString strRet;
  1188. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet, true);
  1189. if(ret != 0)
  1190. {
  1191. LH_WRITE_ERROR(QString("删除模板失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  1192. return false;
  1193. }
  1194. return true;
  1195. }
  1196. /* 一次删除多个模版 */
  1197. bool FromWebAPI::deleteTemplateList(const QList<OneTemplateInfo>& listTemplate)
  1198. {
  1199. if(m_httpApi == nullptr)
  1200. {
  1201. LH_WRITE_ERROR("WebAPI is nullptr");
  1202. return false;
  1203. }
  1204. nJson json0 = nJson::array();
  1205. for(auto& it : listTemplate)
  1206. {
  1207. nJson json1;
  1208. json1["opName"] = "TMS_DeleteExecPlanTemplate";
  1209. json1["Key"] = QUuid::createUuid().toString().toStdString();
  1210. nJson json2;
  1211. json2["channelID"] = it.channelInfo.ChannelID;
  1212. json2["templateName"] = it.templateName.toStdString();
  1213. json1["paramList"] = json2;
  1214. json0.push_back(json1);
  1215. }
  1216. /* 打印生成的JSON */
  1217. // LH_WRITE_LOG_DEBUG(QString::fromStdString(json0.dump(4)));
  1218. QString strCmd = QString::fromStdString(json0.dump());
  1219. QString strRet;
  1220. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet, true);
  1221. if(ret != 0)
  1222. {
  1223. LH_WRITE_ERROR(QString("获取模板失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  1224. return false;
  1225. }
  1226. // LH_WRITE_LOG_DEBUG(strRet);
  1227. return true;
  1228. }
  1229. /**
  1230. * @brief 修改模版,先删除该频率相同名称的模版,再插入新的模版
  1231. *
  1232. * @param info
  1233. * @param list 新的模版计划
  1234. * @return true
  1235. * @return false
  1236. */
  1237. bool FromWebAPI::modifyTemplate(const OneTemplateInfo& info, QList<OnePlanItemInfo>& list)
  1238. {
  1239. if(deleteTemplate(info) == false)
  1240. {
  1241. LH_WRITE_ERROR("删除模版失败");
  1242. return false;
  1243. }
  1244. if(saveTemplate(info.templateName, list) == false)
  1245. {
  1246. LH_WRITE_ERROR("保存模版失败");
  1247. return false;
  1248. }
  1249. return true;
  1250. }
  1251. /* 批量修改模版 */
  1252. bool FromWebAPI::modifyTemplateList(const QMap<OneTemplateInfo, QList<OnePlanItemInfo>>& mapTemplate)
  1253. {
  1254. /* 先批量删除模版 */
  1255. if(deleteTemplateList(mapTemplate.keys()) == false)
  1256. {
  1257. LH_WRITE_ERROR("批量删除模版失败");
  1258. return false;
  1259. }
  1260. /* 再批量插入模版 */
  1261. if(!saveTemplateList(mapTemplate))
  1262. {
  1263. LH_WRITE_ERROR("批量插入模版失败");
  1264. return false;
  1265. }
  1266. return true;
  1267. }
  1268. /**
  1269. * @brief 更新模板名称
  1270. *
  1271. * @param mapTemplateName
  1272. * @return true
  1273. * @return false
  1274. */
  1275. bool FromWebAPI::updateTemplateName(const QMap<OneTemplateInfo, QString>& mapTemplateName)
  1276. {
  1277. if(m_httpApi == nullptr)
  1278. {
  1279. LH_WRITE_ERROR("WebAPI is nullptr");
  1280. return false;
  1281. }
  1282. nJson json0 = nJson::array();
  1283. for(auto it = mapTemplateName.begin(), end = mapTemplateName.end(); it != end; it++)
  1284. {
  1285. nJson json1;
  1286. json1["opName"] = "TMS_UpdateExecPlanTemplateName";
  1287. json1["Key"] = QUuid::createUuid().toString().toStdString();
  1288. nJson json2;
  1289. json2["channelID"] = it.key().channelInfo.ChannelID;
  1290. json2["oldTemplateName"] = it.key().templateName.toStdString();
  1291. json2["newTemplateName"] = it.value().toStdString();
  1292. json1["paramList"] = json2;
  1293. json0.push_back(json1);
  1294. }
  1295. /* 打印生成的JSON */
  1296. // LH_WRITE_LOG_DEBUG(QString::fromStdString(json0.dump(4)));
  1297. QString strCmd = QString::fromStdString(json0.dump());
  1298. QString strRet;
  1299. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet, true);
  1300. if(ret != 0)
  1301. {
  1302. LH_WRITE_ERROR(QString("更新模板名称失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  1303. return false;
  1304. }
  1305. // LH_WRITE_LOG_DEBUG(strRet);
  1306. return true;
  1307. }
  1308. /* 将json的key转换成小写 */
  1309. nJson FromWebAPI::convertJsonKeyToLower(const nJson& json)
  1310. {
  1311. nJson result;
  1312. for(auto& it : json.items())
  1313. {
  1314. std::string key = it.key();
  1315. std::transform(key.begin(), key.end(), key.begin(), ::tolower); // 转换成小写
  1316. /* 是object */
  1317. if(it.value().is_object())
  1318. {
  1319. result[key] = convertJsonKeyToLower(it.value()); // 递归处理嵌套的对象
  1320. }
  1321. /* 是array */
  1322. else if(it.value().is_array())
  1323. {
  1324. nJson arrayResult = nJson::array();
  1325. for(auto& item : it.value())
  1326. {
  1327. if(item.is_object())
  1328. {
  1329. arrayResult.push_back(convertJsonKeyToLower(item));
  1330. }
  1331. else
  1332. {
  1333. arrayResult.push_back(item);
  1334. }
  1335. }
  1336. result[key] = arrayResult;
  1337. }
  1338. else
  1339. {
  1340. /* 直接赋值非对象和非数组类型 */
  1341. result[key] = it.value();
  1342. }
  1343. }
  1344. return result;
  1345. }
  1346. // #include <regex>
  1347. // #include <string>
  1348. // #include <algorithm>
  1349. // std::string FromWebAPI::convertJsonKeyToLower(const std::string& str)
  1350. // {
  1351. // std::regex reg(R"(\"([a-zA-Z0-9_]+)\":)");
  1352. // std::string strStd = str;
  1353. // std::string result = std::regex_replace(strStd, reg, [](const std::smatch& match) {
  1354. // std::string key = match[1].str();
  1355. // std::transform(key.begin(), key.end(), key.begin(), ::tolower); // 转换成小写
  1356. // return "\"" + key + "\":";
  1357. // });
  1358. // return result;
  1359. // }