FromWebAPI.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  354. int retCode = lowerKeyJson["code"].get<int>();
  355. if(retCode != 0)
  356. {
  357. LH_WRITE_ERROR("获取tExecPlan表格数据失败");
  358. return false;
  359. }
  360. nJson result = lowerKeyJson["result"];
  361. for(auto& it : result)
  362. {
  363. OnePlanItemInfo info;
  364. info.ChannelID = chnID;
  365. info.ChannelName = QString::fromStdString(it["channelName"].get<std::string>());
  366. info.onWeekDay = static_cast<enum_WeekDay>(it["onWeekday"].get<int>());
  367. info.onDateTime.setDate(QDate::fromString(QString::fromStdString(it["ondate"].get<std::string>()), "yyyy-MM-dd"));
  368. info.onDateTime.setTime(QTime::fromString(QString::fromStdString(it["ontime"].get<std::string>()), "hh:mm:ss"));
  369. info.offWeekDay = static_cast<enum_WeekDay>(it["offWeekday"].get<int>());
  370. info.offDateTime.setDate(QDate::fromString(QString::fromStdString(it["offdate"].get<std::string>()), "yyyy-MM-dd"));
  371. info.offDateTime.setTime(QTime::fromString(QString::fromStdString(it["offtime"].get<std::string>()), "hh:mm:ss"));
  372. list.append(info);
  373. }
  374. } catch (const nJson::parse_error& e) {
  375. LH_WRITE_ERROR(QString("解析 tExecPlan 表格数据失败:%1").arg(e.what()));
  376. return false;
  377. }
  378. catch (const nJson::exception& e)
  379. {
  380. LH_WRITE_ERROR(QString("解析 tExecPlan 表格数据失败:%1").arg(e.what()));
  381. return false;
  382. }
  383. catch(...)
  384. {
  385. LH_WRITE_ERROR("解析 tExecPlan 表格数据失败");
  386. return false;
  387. }
  388. LH_WRITE_LOG_DEBUG(QString("从 tExecPlan 表格获取数据成功,计划数目:%1").arg(list.size()));
  389. return true;
  390. }
  391. bool FromWebAPI::getExecPlanData(QMap<int, QList<OnePlanItemInfo>>& mapPlan)
  392. {
  393. if(m_httpApi == nullptr)
  394. {
  395. LH_WRITE_ERROR("WebAPI is nullptr");
  396. return false;
  397. }
  398. nJson json0;
  399. json0["opName"] = "ESM8C_GetExecPlan";
  400. nJson json1;
  401. json0["paramList"] = json1;
  402. QString strCmd = QString::fromStdString(json0.dump());
  403. QString strRet;
  404. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  405. if(ret != 0)
  406. {
  407. LH_WRITE_ERROR(QString("从EQM获取tExecPlan表格数据失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  408. return false;
  409. }
  410. /* 解析获取到的JSON数据 */
  411. // SPDLOG_LOGGER_DEBUG(m_logger,"获取执行计划:\n{}",strRet.toStdString());
  412. try
  413. {
  414. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  415. int retCode = lowerKeyJson["code"].get<int>();
  416. if(retCode != 0)
  417. {
  418. LH_WRITE_ERROR("获取tExecPlan表格数据失败");
  419. return false;
  420. }
  421. nJson result = lowerKeyJson["result"];
  422. for(auto& it : result)
  423. {
  424. OnePlanItemInfo info;
  425. info.ChannelID = it["channelid"].is_null() ? -1 : it["channelid"].get<int>();
  426. info.ChannelName = QString::fromStdString(it["channelname"].get<std::string>());
  427. info.onWeekDay = static_cast<enum_WeekDay>(it["onweekday"].get<int>());
  428. info.onDateTime.setDate(QDate::fromString(QString::fromStdString(it["ondate"].get<std::string>()), "yyyy-MM-dd"));
  429. info.onDateTime.setTime(QTime::fromString(QString::fromStdString(it["ontime"].get<std::string>()), "hh:mm:ss"));
  430. info.offWeekDay = static_cast<enum_WeekDay>(it["offweekday"].get<int>());
  431. info.offDateTime.setDate(QDate::fromString(QString::fromStdString(it["offdate"].get<std::string>()), "yyyy-MM-dd"));
  432. info.offDateTime.setTime(QTime::fromString(QString::fromStdString(it["offtime"].get<std::string>()), "hh:mm:ss"));
  433. /* 查找对应的设备列表 */
  434. auto list = mapPlan.find(info.ChannelID);
  435. if(list == mapPlan.end())
  436. {
  437. /* 创建一个新的列表 */
  438. QList<OnePlanItemInfo> listPlan;
  439. listPlan.append(info);
  440. mapPlan.insert(info.ChannelID, listPlan);
  441. }else {
  442. /* 找到对应的列表 */
  443. list->append(info);
  444. }
  445. }
  446. } catch (const nJson::parse_error& e) {
  447. LH_WRITE_ERROR(QString("解析 tExecPlan 表格数据失败:%1").arg(e.what()));
  448. return false;
  449. }
  450. catch (const nJson::exception& e)
  451. {
  452. LH_WRITE_ERROR(QString("解析 tExecPlan 表格数据失败:%1").arg(e.what()));
  453. return false;
  454. }
  455. catch(...)
  456. {
  457. LH_WRITE_ERROR("解析 tExecPlan 表格数据失败");
  458. return false;
  459. }
  460. return true;
  461. }
  462. /* 删除所有行 */
  463. bool FromWebAPI::deleteAllRow()
  464. {
  465. if(m_httpApi == nullptr)
  466. {
  467. LH_WRITE_ERROR("WebAPI is nullptr");
  468. return false;
  469. }
  470. nJson json0;
  471. json0["opName"] = "ESM8C_DeleteAllRow";
  472. QString strCmd = QString::fromStdString(json0.dump());
  473. QString strRet;
  474. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet, true);
  475. if(ret != 0)
  476. {
  477. LH_WRITE_ERROR(QString("删除所有行失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  478. return false;
  479. }
  480. return true;
  481. }
  482. /* 删除一个发射机计划 */
  483. bool FromWebAPI::deleteOneChnPlan(int chnID)
  484. {
  485. if(m_httpApi == nullptr)
  486. {
  487. LH_WRITE_ERROR("WebAPI is nullptr");
  488. return false;
  489. }
  490. /* 查找出有多少发射机类型 */
  491. QList<int> listPttypeCodeID;
  492. for(auto& it : DevTypeContainer.getMapDevType())
  493. {
  494. listPttypeCodeID.append(it.PTTypeCode);
  495. }
  496. nJson json0;
  497. json0["opName"] = "ESM8C_DeleteOneChnPlan";
  498. json0["paramList"].clear();
  499. nJson json1;
  500. json1["channelID"] = chnID;
  501. json0["paramList"].push_back(json1);
  502. QString strCmd = QString::fromStdString(json0.dump());
  503. QString strRet;
  504. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet, true);
  505. if(ret != 0)
  506. {
  507. LH_WRITE_ERROR(QString("删除频率: %3 计划失败: %1, 错误信息: %2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)).arg(chnID));
  508. }else {
  509. // LH_WRITE_LOG(QString("删除频率: %1 计划成功!").arg(chnID));
  510. }
  511. return true;
  512. }
  513. /**
  514. * @brief 将页面的配置写入数据库,主要是默认计划和执行模式
  515. *
  516. * @param mapConfig
  517. * @return true
  518. * @return false
  519. */
  520. bool FromWebAPI::insertConfigData(QMap<int, ConfigData>& list)
  521. {
  522. if(m_httpApi == nullptr)
  523. {
  524. LH_WRITE_ERROR("WebAPI is nullptr");
  525. return false;
  526. }
  527. deleteConfigData(list);
  528. nJson json0 = nJson::array();
  529. try{
  530. for(auto begin = list.begin(), end = list.end(); begin != end; begin++)
  531. {
  532. nJson json1;
  533. json1["opName"] = "ESM8C_InsertExecPlanConfig";
  534. json1["Key"] = QUuid::createUuid().toString().toStdString();
  535. nJson json2;
  536. json2["keyWord"] = begin->key;
  537. json2["value1"] = begin->value;
  538. json2["updateTime"] = begin->updateTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  539. json2["notes"] = begin->notes.toStdString();
  540. json1["paramList"] = json2;
  541. json0.push_back(json1);
  542. }
  543. }catch (const nJson::parse_error& e) {
  544. SPDLOG_ERROR("生成json数据失败:{}", e.what());
  545. return false;
  546. }
  547. catch (const nJson::exception& e)
  548. {
  549. SPDLOG_ERROR("生成json数据失败:{}", e.what());
  550. return false;
  551. }
  552. catch (...)
  553. {
  554. SPDLOG_ERROR("生成json数据失败");
  555. return false;
  556. }
  557. /* 写入EQM数据库 */
  558. QString strCmd = QString::fromStdString(json0.dump());
  559. QString strRet;
  560. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet, true);
  561. if(ret != 0)
  562. {
  563. LH_WRITE_ERROR(QString("写入tExecPlanConfig失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  564. return false;
  565. }
  566. LH_WRITE_LOG_DEBUG(QString("页面配置写入tExecPlanConfig成功, 数目:%1").arg(QString::number(list.size())));
  567. return true;
  568. }
  569. /* 删除配置数据 */
  570. bool FromWebAPI::deleteConfigData(QMap<int, ConfigData>& list)
  571. {
  572. if(m_httpApi == nullptr)
  573. {
  574. LH_WRITE_ERROR("WebAPI is nullptr");
  575. return false;
  576. }
  577. nJson json0 = nJson::array();
  578. try{
  579. for(auto begin = list.begin(), end = list.end(); begin != end; begin++)
  580. {
  581. nJson json1;
  582. json1["opName"] = "ESM8C_DeleteExecPlanConfig";
  583. json1["Key"] = QUuid::createUuid().toString().toStdString();
  584. nJson json2;
  585. json2["keyWord"] = begin->key;
  586. json1["paramList"] = json2;
  587. json0.push_back(json1);
  588. }
  589. }catch (const nJson::parse_error& e) {
  590. SPDLOG_ERROR("生成json数据失败:{}", e.what());
  591. return false;
  592. }
  593. catch (const nJson::exception& e)
  594. {
  595. SPDLOG_ERROR("生成json数据失败:{}", e.what());
  596. return false;
  597. }
  598. catch (...)
  599. {
  600. SPDLOG_ERROR("生成json数据失败");
  601. return false;
  602. }
  603. /* 写入EQM数据库 */
  604. QString strCmd = QString::fromStdString(json0.dump());
  605. QString strRet;
  606. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet, true);
  607. if(ret != 0)
  608. {
  609. LH_WRITE_ERROR(QString("删除tExecPlanConfig表格数据失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  610. return false;
  611. }
  612. LH_WRITE_LOG_DEBUG(QString("删除tExecPlanConfig表格数据成功, 计划数目:%1").arg(QString::number(list.size())));
  613. return true;
  614. }
  615. /* 获取配置数据 */
  616. bool FromWebAPI::getConfigData(QMap<int, ConfigData>& list)
  617. {
  618. if(m_httpApi == nullptr)
  619. {
  620. LH_WRITE_ERROR("WebAPI is nullptr");
  621. return false;
  622. }
  623. nJson json0;
  624. json0["opName"] = "ESM8C_GetExecPlanConfig";
  625. QString strCmd = QString::fromStdString(json0.dump());
  626. QString strRet;
  627. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  628. if(ret != 0)
  629. {
  630. LH_WRITE_ERROR(QString("获取tExecPlanConfig表格数据失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  631. return false;
  632. }
  633. /* 解析获取到的JSON数据 */
  634. try{
  635. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  636. // LH_WRITE_LOG_DEBUG(QString::fromStdString(lowerKeyJson.dump(4)));
  637. int retCode = lowerKeyJson["code"].get<int>();
  638. if(retCode != 0)
  639. {
  640. LH_WRITE_ERROR("获取tExecPlanConfig失败");
  641. return false;
  642. }
  643. nJson result = lowerKeyJson["result"];
  644. for(auto& it : result)
  645. {
  646. ConfigData data;
  647. data.key = it["keyword"].get<int>();
  648. data.value = it["value1"].get<int>();
  649. data.updateTime = QDateTime::fromString(QString::fromStdString(it["updatetime"].get<std::string>()),"yyyy-MM-dd hh:mm:ss");
  650. data.notes = QString::fromStdString(it["notes"].get<std::string>());
  651. list.insert(data.key, data);
  652. }
  653. } catch (const nJson::parse_error& e) {
  654. LH_WRITE_ERROR(QString("解析tExecPlanConfig数据失败:%1").arg(e.what()));
  655. return false;
  656. }
  657. catch (const nJson::exception& e)
  658. {
  659. LH_WRITE_ERROR(QString("解析tExecPlanConfig数据失败:%1").arg(e.what()));
  660. return false;
  661. }
  662. catch(...)
  663. {
  664. LH_WRITE_ERROR("解析tExecPlanConfig数据失败");
  665. return false;
  666. }
  667. LH_WRITE_LOG_DEBUG(QString("获取tExecPlanConfig表格数据成功,数据数目:%1").arg(list.size()));
  668. return true;
  669. }
  670. /* 单独获取某个关键字的数据 */
  671. bool FromWebAPI::getConfigData(int key, ConfigData& data)
  672. {
  673. if(m_httpApi == nullptr)
  674. {
  675. LH_WRITE_ERROR("WebAPI is nullptr");
  676. return false;
  677. }
  678. nJson json0;
  679. json0["opName"] = "ESM8C_GetExecPlanConfigOfKeyWord";
  680. nJson json1;
  681. json1["keyWord"] = key;
  682. json0["paramList"] = json1;
  683. QString strCmd = QString::fromStdString(json0.dump());
  684. QString strRet;
  685. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  686. if(ret != 0)
  687. {
  688. LH_WRITE_ERROR(QString("获取tExecPlanConfig表格数据失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  689. return false;
  690. }
  691. /* 解析获取到的JSON数据 */
  692. LH_WRITE_LOG_DEBUG(strRet);
  693. try{
  694. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  695. int retCode = lowerKeyJson["code"].get<int>();
  696. if(retCode != 0)
  697. {
  698. LH_WRITE_ERROR("获取tExecPlanConfig失败");
  699. return false;
  700. }
  701. nJson result = lowerKeyJson["result"];
  702. for(auto& it : result)
  703. {
  704. data.key = it["keyword"].get<int>();
  705. if(data.key != key)
  706. {
  707. LH_WRITE_ERROR("获取到的数据和传入的关键字不匹配");
  708. continue;
  709. }
  710. data.value = it["value1"].get<int>();
  711. data.updateTime = QDateTime::fromString(QString::fromStdString(it["updatetime"].get<std::string>()),"yyyy-MM-dd hh:mm:ss");
  712. data.notes = QString::fromStdString(it["notes"].get<std::string>());
  713. break;
  714. }
  715. } catch (const nJson::parse_error& e) {
  716. LH_WRITE_ERROR(QString("解析tExecPlanConfig数据失败:%1").arg(e.what()));
  717. return false;
  718. }
  719. catch (const nJson::exception& e)
  720. {
  721. LH_WRITE_ERROR(QString("解析tExecPlanConfig数据失败:%1").arg(e.what()));
  722. return false;
  723. }
  724. catch(...)
  725. {
  726. LH_WRITE_ERROR("解析tExecPlanConfig数据失败");
  727. return false;
  728. }
  729. if(data.key != key)
  730. {
  731. LH_WRITE_ERROR("获取到的数据和传入的关键字不匹配");
  732. return false;
  733. }
  734. return true;
  735. }
  736. /* 获取EQM数据库中的模版编号 */
  737. bool FromWebAPI::getEQMTemplateID(QList<int>& listId)
  738. {
  739. if(m_httpApi == nullptr)
  740. {
  741. LH_WRITE_ERROR("WebAPI is nullptr");
  742. return false;
  743. }
  744. nJson json0;
  745. json0["opName"] = "ESMC_GetTemplateNumFromExecPlan";
  746. QString strCmd = QString::fromStdString(json0.dump());
  747. QString strRet;
  748. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  749. if(ret != 0)
  750. {
  751. LH_WRITE_ERROR(QString("从EQM获取模版编号失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  752. return false;
  753. }
  754. /* 解析获取到的JSON数据 */
  755. LH_WRITE_LOG_DEBUG(strRet);
  756. try
  757. {
  758. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  759. int retCode = lowerKeyJson["code"].get<int>();
  760. if(retCode != 0)
  761. {
  762. LH_WRITE_ERROR("获取模版编号失败");
  763. return false;
  764. }
  765. nJson result = lowerKeyJson["result"];
  766. for(auto& it : result)
  767. {
  768. if(it["templateNum"].is_null())
  769. {
  770. LH_WRITE_ERROR("模版编号为空");
  771. continue;
  772. }
  773. auto id = it["templatenum"].get<int>();
  774. if(listId.contains(id))
  775. {
  776. continue;
  777. }
  778. listId.append(id);
  779. }
  780. } catch (const nJson::parse_error& e) {
  781. LH_WRITE_ERROR(QString("解析模版编号失败:%1").arg(e.what()));
  782. return false;
  783. }
  784. catch (const nJson::exception& e)
  785. {
  786. LH_WRITE_ERROR(QString("解析模版编号失败:%1").arg(e.what()));
  787. return false;
  788. }
  789. catch(...)
  790. {
  791. LH_WRITE_ERROR("解析模版编号失败");
  792. return false;
  793. }
  794. return true;
  795. }
  796. /* 保存到模板 */
  797. bool FromWebAPI::saveTemplate(const QString& templateName, QList<OnePlanItemInfo>& list)
  798. {
  799. if(m_httpApi == nullptr)
  800. {
  801. LH_WRITE_ERROR("WebAPI is nullptr");
  802. return false;
  803. }
  804. nJson json0 = nJson::array();
  805. for(const auto& it : list)
  806. {
  807. nJson json1;
  808. json1["opName"] = "TMS_InsertToExecPlanTemplate";
  809. json1["Key"] = QUuid::createUuid().toString().toStdString();
  810. nJson json2;
  811. json2["templateName"] = templateName.toStdString();
  812. json2["channelID"] = it.ChannelID;
  813. json2["channelName"] = it.ChannelName.toStdString();
  814. json2["onWeekDay"] = it.onWeekDay;
  815. json2["onDate"] = it.onDateTime.date().toString("yyyy-MM-dd").toStdString();
  816. json2["onTime"] = it.onDateTime.time().toString("hh:mm:ss").toStdString();
  817. json2["offWeekDay"] = it.offWeekDay;
  818. json2["offDate"] = it.offDateTime.date().toString("yyyy-MM-dd").toStdString();
  819. json2["offTime"] = it.offDateTime.time().toString("hh:mm:ss").toStdString();
  820. json1["paramList"] = json2;
  821. json0.push_back(json1);
  822. }
  823. /* 将json写入本地文件 */
  824. #ifdef C_DEBUG
  825. // QString fileJson(QString::fromStdString(json0.dump(4)));
  826. // LH_WRITE_LOG_DEBUG("\n" + fileJson);
  827. #endif
  828. /* 写入EQM数据库 */
  829. QString strCmd = QString::fromStdString(json0.dump());
  830. QString strRet;
  831. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet, true);
  832. if(ret != 0)
  833. {
  834. LH_WRITE_ERROR(QString("保存到模板失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  835. return false;
  836. }
  837. // LH_WRITE_LOG("保存到模板成功,模板名称:" + templateName + ", 计划数目:" + QString::number(list.size()));
  838. return true;
  839. }
  840. /* 批量写入模版数据 */
  841. bool FromWebAPI::saveTemplateList(const QMap<OneTemplateInfo, QList<OnePlanItemInfo>>& mapTemplate)
  842. {
  843. if(m_httpApi == nullptr)
  844. {
  845. LH_WRITE_ERROR("WebAPI is nullptr");
  846. return false;
  847. }
  848. nJson json0 = nJson::array();
  849. try
  850. {
  851. for(auto list = mapTemplate.begin(), end = mapTemplate.end(); list != end; list++)
  852. {
  853. for(auto& item : *list)
  854. {
  855. nJson json1;
  856. json1["opName"] = "TMS_InsertToExecPlanTemplate";
  857. json1["Key"] = QUuid::createUuid().toString().toStdString();
  858. nJson json2;
  859. json2["templateName"] = list.key().templateName.toStdString();
  860. json2["channelID"] = item.ChannelID;
  861. json2["channelName"] = item.ChannelName.toStdString();
  862. json2["onWeekDay"] = item.onWeekDay;
  863. json2["onDate"] = item.onDateTime.date().toString("yyyy-MM-dd").toStdString();
  864. json2["onTime"] = item.onDateTime.time().toString("hh:mm:ss").toStdString();
  865. json2["offWeekDay"] = item.offWeekDay;
  866. json2["offDate"] = item.offDateTime.date().toString("yyyy-MM-dd").toStdString();
  867. json2["offTime"] = item.offDateTime.time().toString("hh:mm:ss").toStdString();
  868. json1["paramList"] = json2;
  869. json0.push_back(json1);
  870. }
  871. }
  872. }
  873. catch(...)
  874. {
  875. LH_WRITE_ERROR("生成json数据失败");
  876. return false;
  877. }
  878. /* 写入EQM数据库 */
  879. QString strCmd = QString::fromStdString(json0.dump());
  880. QString strRet;
  881. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet, true);
  882. if(ret != 0)
  883. {
  884. LH_WRITE_ERROR(QString("保存到模板失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  885. return false;
  886. }
  887. // LH_WRITE_LOG("保存到模板成功,模板名称:" + templateName + ", 计划数目:" + QString::number(list.size()));
  888. return true;
  889. }
  890. /* 获取模板内容 */
  891. bool FromWebAPI::getOneTemplateData(OneTemplateInfo tmpInfo, QList<OnePlanItemInfo>& list)
  892. {
  893. if(m_httpApi == nullptr)
  894. {
  895. LH_WRITE_ERROR("WebAPI is nullptr");
  896. return false;
  897. }
  898. nJson json0;
  899. json0["opName"] = "TMS_GetExecPlanTemplateData";
  900. nJson json1;
  901. json1["templateName"] = tmpInfo.templateName.toStdString();
  902. json1["channelID"] = tmpInfo.channelInfo.ChannelID;
  903. json0["paramList"] = json1;
  904. QString strCmd = QString::fromStdString(json0.dump());
  905. QString strRet;
  906. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  907. if(ret != 0)
  908. {
  909. LH_WRITE_ERROR(QString("获取模板失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  910. return false;
  911. }
  912. /* 解析获取到的JSON数据 */
  913. try
  914. {
  915. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  916. int retCode = lowerKeyJson["code"].get<int>();
  917. if(retCode != 0)
  918. {
  919. LH_WRITE_ERROR("获取模板失败");
  920. return false;
  921. }
  922. nJson result = lowerKeyJson["result"];
  923. for(auto& it : result)
  924. {
  925. OnePlanItemInfo info;
  926. info.ChannelID = it["channelid"].is_null() ? -1 : it["channelid"].get<int>();
  927. info.ChannelName = QString::fromStdString(it["channelname"].get<std::string>());
  928. info.onWeekDay = static_cast<enum_WeekDay>(it["onweekday"].get<int>());
  929. info.onDateTime.setDate(QDate::fromString(QString::fromStdString(it["ondate"].get<std::string>()), "yyyy-MM-dd"));
  930. info.onDateTime.setTime(QTime::fromString(QString::fromStdString(it["ontime"].get<std::string>()), "hh:mm:ss"));
  931. info.offWeekDay = static_cast<enum_WeekDay>(it["offweekday"].get<int>());
  932. info.offDateTime.setDate(QDate::fromString(QString::fromStdString(it["offdate"].get<std::string>()), "yyyy-MM-dd"));
  933. info.offDateTime.setTime(QTime::fromString(QString::fromStdString(it["offtime"].get<std::string>()), "hh:mm:ss"));
  934. list.append(info);
  935. }
  936. LH_WRITE_LOG_DEBUG(QString("获取模板数据成功,数据数目:%1").arg(list.size()));
  937. }
  938. catch (const nJson::parse_error& e)
  939. {
  940. LH_WRITE_ERROR(QString("解析模板失败:%1").arg(e.what()));
  941. return false;
  942. }
  943. catch (const nJson::exception& e)
  944. {
  945. LH_WRITE_ERROR(QString("解析模板失败:%1").arg(e.what()));
  946. return false;
  947. }
  948. catch(...)
  949. {
  950. LH_WRITE_ERROR("解析模板失败");
  951. return false;
  952. }
  953. return true;
  954. }
  955. /* 获取多个模板的内容 */
  956. bool FromWebAPI::getTemplateData(QList<OneTemplateInfo>& listTemplate, QMap<int, QList<OnePlanItemInfo>>& mapPlan)
  957. {
  958. if(m_httpApi == nullptr)
  959. {
  960. LH_WRITE_ERROR("WebAPI is nullptr");
  961. return false;
  962. }
  963. nJson json0 = nJson::array();
  964. for(auto& it : listTemplate)
  965. {
  966. nJson json1;
  967. json1["opName"] = "TMS_GetExecPlanTemplateData";
  968. /* 这里的Key使用频率ID,方便后面区分是哪个频率的数据 */
  969. json1["Key"] = it.channelInfo.ChannelID;
  970. nJson json2;
  971. json2["templateName"] = it.templateName.toStdString();
  972. json2["channelID"] = it.channelInfo.ChannelID;
  973. json1["paramList"] = json2;
  974. json0.push_back(json1);
  975. }
  976. /* 打印生成的JSON */
  977. // LH_WRITE_LOG_DEBUG(QString::fromStdString(json0.dump(4)));
  978. QString strCmd = QString::fromStdString(json0.dump());
  979. QString strRet;
  980. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet, true);
  981. if(ret != 0)
  982. {
  983. LH_WRITE_ERROR(QString("获取模板失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  984. return false;
  985. }
  986. LH_WRITE_LOG_DEBUG(QString("获取到的json数据的大小:%1").arg(strRet.size()));
  987. /* 解析获取到的JSON数据 */
  988. try
  989. {
  990. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  991. // LH_WRITE_LOG_DEBUG(QString("\n: %1").arg(QString::fromStdString(jsonRet.dump(4))));
  992. int retCode = lowerKeyJson["code"].get<int>();
  993. if(retCode != 0)
  994. {
  995. LH_WRITE_ERROR("获取模板数据失败");
  996. return false;
  997. }
  998. nJson data = lowerKeyJson["data"];
  999. for(auto& it : data)
  1000. {
  1001. /* 逐步获取对应频率的计划信息 */
  1002. int retChnID = QString::fromStdString(it["key"].get<std::string>()).toInt();
  1003. if(retChnID == 0)
  1004. {
  1005. LH_WRITE_ERROR("获取到的频率ID为0");
  1006. continue;
  1007. }
  1008. nJson value = it["value"];
  1009. if(value.is_null())
  1010. {
  1011. LH_WRITE_ERROR("获取到的值为空");
  1012. continue;
  1013. }
  1014. int retCode1 = value["code"].get<int>();
  1015. if(retCode1 != 0)
  1016. {
  1017. LH_WRITE_ERROR(QString("获取模板:%1数据失败").arg(retChnID));
  1018. continue;
  1019. }
  1020. nJson result = value["result"];
  1021. QList<OnePlanItemInfo> listPlan;
  1022. for(auto& plan : result)
  1023. {
  1024. OnePlanItemInfo info;
  1025. info.ChannelID = plan["channelid"].is_null() ? -1 : plan["channelid"].get<int>();
  1026. info.ChannelName = plan["channelname"].is_null() ? "" : QString::fromStdString(plan["channelname"].get<std::string>());
  1027. info.onWeekDay = static_cast<enum_WeekDay>(plan["onweekday"].get<int>());
  1028. info.onDateTime.setDate(QDate::fromString(QString::fromStdString(plan["ondate"].get<std::string>()), "yyyy-MM-dd"));
  1029. info.onDateTime.setTime(QTime::fromString(QString::fromStdString(plan["ontime"].get<std::string>()), "hh:mm:ss"));
  1030. info.offWeekDay = static_cast<enum_WeekDay>(plan["offweekday"].get<int>());
  1031. info.offDateTime.setDate(QDate::fromString(QString::fromStdString(plan["offdate"].get<std::string>()), "yyyy-MM-dd"));
  1032. info.offDateTime.setTime(QTime::fromString(QString::fromStdString(plan["offtime"].get<std::string>()), "hh:mm:ss"));
  1033. listPlan.append(info);
  1034. }
  1035. mapPlan.insert(retChnID, listPlan);
  1036. }
  1037. }
  1038. catch (const nJson::parse_error& e)
  1039. {
  1040. LH_WRITE_ERROR(QString("解析模板失败:%1").arg(e.what()));
  1041. return false;
  1042. }
  1043. catch (const nJson::exception& e)
  1044. {
  1045. LH_WRITE_ERROR(QString("解析模板失败:%1").arg(e.what()));
  1046. return false;
  1047. }
  1048. catch(...)
  1049. {
  1050. LH_WRITE_ERROR("解析模板失败");
  1051. return false;
  1052. }
  1053. LH_WRITE_LOG_DEBUG(QString("获取多个频率模板成功,频率数目:%1").arg(mapPlan.size()));
  1054. return true;
  1055. }
  1056. /* 获取模板列表 */
  1057. bool FromWebAPI::getTemplateList(QMultiMap<int, OneTemplateInfo>& mapTemplate)
  1058. {
  1059. if(m_httpApi == nullptr)
  1060. {
  1061. LH_WRITE_ERROR("WebAPI is nullptr");
  1062. return false;
  1063. }
  1064. nJson json0;
  1065. json0["opName"] = "TMS_GetExecPlanTemplateList";
  1066. QString strCmd = QString::fromStdString(json0.dump());
  1067. QString strRet;
  1068. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  1069. if(ret != 0)
  1070. {
  1071. LH_WRITE_ERROR(QString("获取模板列表失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  1072. return false;
  1073. }
  1074. /* 解析获取到的JSON数据 */
  1075. // LH_WRITE_LOG_DEBUG(strRet);
  1076. try
  1077. {
  1078. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  1079. int retCode = lowerKeyJson["code"].get<int>();
  1080. if(retCode != 0)
  1081. {
  1082. LH_WRITE_ERROR("获取模板列表失败");
  1083. return false;
  1084. }
  1085. nJson result = lowerKeyJson["result"];
  1086. for(auto& it : result)
  1087. {
  1088. OneTemplateInfo info;
  1089. info.channelInfo.ChannelID = it["channelid"].get<int>();
  1090. info.templateName = QString::fromStdString(it["templatename"].get<std::string>());
  1091. mapTemplate.insert(info.channelInfo.ChannelID, info);
  1092. }
  1093. } catch (const nJson::parse_error& e) {
  1094. LH_WRITE_ERROR(QString("解析模板列表失败:%1").arg(e.what()));
  1095. return false;
  1096. }
  1097. catch (const nJson::exception& e)
  1098. {
  1099. LH_WRITE_ERROR(QString("解析模板列表失败:%1").arg(e.what()));
  1100. return false;
  1101. }
  1102. catch(...)
  1103. {
  1104. LH_WRITE_ERROR("解析模板列表失败");
  1105. return false;
  1106. }
  1107. // for(auto it = list.begin(); it != list.end(); it++)
  1108. // {
  1109. // LH_WRITE_LOG_DEBUG(QString("模板名称:%1, 类型:%2").arg(it.key()).arg(it.value()));
  1110. // }
  1111. return true;
  1112. }
  1113. /* 获取某个频率的模版列表 */
  1114. bool FromWebAPI::getTemplateList(int chnID, QList<OneTemplateInfo>& listTemplate)
  1115. {
  1116. if(m_httpApi == nullptr)
  1117. {
  1118. LH_WRITE_ERROR("WebAPI is nullptr");
  1119. return false;
  1120. }
  1121. nJson json0;
  1122. json0["opName"] = "TMS_GetExecPlanOneTemplateList";
  1123. nJson jsonParam;
  1124. jsonParam["channelID"] = chnID;
  1125. json0["paramList"] = jsonParam;
  1126. /* 发送请求 */
  1127. QString strCmd = QString::fromStdString(json0.dump());
  1128. QString strRet;
  1129. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Select, strCmd, strRet, true);
  1130. if(ret != 0)
  1131. {
  1132. LH_WRITE_ERROR(QString("获取模板列表失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  1133. return false;
  1134. }
  1135. /* 解析获取到的JSON数据 */
  1136. // LH_WRITE_LOG_DEBUG(strRet);
  1137. try
  1138. {
  1139. nJson lowerKeyJson = convertJsonKeyToLower(nJson::parse(strRet.toStdString()));
  1140. int retCode = lowerKeyJson["code"].get<int>();
  1141. if(retCode != 0)
  1142. {
  1143. LH_WRITE_ERROR("获取模板列表失败");
  1144. return false;
  1145. }
  1146. nJson result = lowerKeyJson["result"];
  1147. for(auto& it : result)
  1148. {
  1149. OneTemplateInfo info;
  1150. info.channelInfo.ChannelID = it["channelid"].get<int>();
  1151. info.templateName = QString::fromStdString(it["templatename"].get<std::string>());
  1152. listTemplate.append(info);
  1153. }
  1154. } catch (const nJson::parse_error& e) {
  1155. LH_WRITE_ERROR(QString("解析模板列表失败:%1").arg(e.what()));
  1156. return false;
  1157. }
  1158. catch (const nJson::exception& e)
  1159. {
  1160. LH_WRITE_ERROR(QString("解析模板列表失败:%1").arg(e.what()));
  1161. return false;
  1162. }
  1163. catch(...)
  1164. {
  1165. LH_WRITE_ERROR("解析模板列表失败");
  1166. return false;
  1167. }
  1168. return true;
  1169. }
  1170. /* 删除一个模板 */
  1171. bool FromWebAPI::deleteTemplate(const OneTemplateInfo& info)
  1172. {
  1173. if(m_httpApi == nullptr)
  1174. {
  1175. LH_WRITE_ERROR("WebAPI is nullptr");
  1176. return false;
  1177. }
  1178. nJson json0;
  1179. json0["opName"] = "TMS_DeleteExecPlanTemplate";
  1180. nJson json1;
  1181. json1["channelID"] = info.channelInfo.ChannelID;
  1182. json1["templateName"] = info.templateName.toStdString();
  1183. json0["paramList"] = json1;
  1184. QString strCmd = QString::fromStdString(json0.dump());
  1185. QString strRet;
  1186. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_Delete, strCmd, strRet, true);
  1187. if(ret != 0)
  1188. {
  1189. LH_WRITE_ERROR(QString("删除模板失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  1190. return false;
  1191. }
  1192. return true;
  1193. }
  1194. /* 一次删除多个模版 */
  1195. bool FromWebAPI::deleteTemplateList(const QList<OneTemplateInfo>& listTemplate)
  1196. {
  1197. if(m_httpApi == nullptr)
  1198. {
  1199. LH_WRITE_ERROR("WebAPI is nullptr");
  1200. return false;
  1201. }
  1202. nJson json0 = nJson::array();
  1203. for(auto& it : listTemplate)
  1204. {
  1205. nJson json1;
  1206. json1["opName"] = "TMS_DeleteExecPlanTemplate";
  1207. json1["Key"] = QUuid::createUuid().toString().toStdString();
  1208. nJson json2;
  1209. json2["channelID"] = it.channelInfo.ChannelID;
  1210. json2["templateName"] = it.templateName.toStdString();
  1211. json1["paramList"] = json2;
  1212. json0.push_back(json1);
  1213. }
  1214. /* 打印生成的JSON */
  1215. // LH_WRITE_LOG_DEBUG(QString::fromStdString(json0.dump(4)));
  1216. QString strCmd = QString::fromStdString(json0.dump());
  1217. QString strRet;
  1218. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet, true);
  1219. if(ret != 0)
  1220. {
  1221. LH_WRITE_ERROR(QString("获取模板失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  1222. return false;
  1223. }
  1224. // LH_WRITE_LOG_DEBUG(strRet);
  1225. return true;
  1226. }
  1227. /**
  1228. * @brief 修改模版,先删除该频率相同名称的模版,再插入新的模版
  1229. *
  1230. * @param info
  1231. * @param list 新的模版计划
  1232. * @return true
  1233. * @return false
  1234. */
  1235. bool FromWebAPI::modifyTemplate(const OneTemplateInfo& info, QList<OnePlanItemInfo>& list)
  1236. {
  1237. if(deleteTemplate(info) == false)
  1238. {
  1239. LH_WRITE_ERROR("删除模版失败");
  1240. return false;
  1241. }
  1242. if(saveTemplate(info.templateName, list) == false)
  1243. {
  1244. LH_WRITE_ERROR("保存模版失败");
  1245. return false;
  1246. }
  1247. return true;
  1248. }
  1249. /* 批量修改模版 */
  1250. bool FromWebAPI::modifyTemplateList(const QMap<OneTemplateInfo, QList<OnePlanItemInfo>>& mapTemplate)
  1251. {
  1252. /* 先批量删除模版 */
  1253. if(deleteTemplateList(mapTemplate.keys()) == false)
  1254. {
  1255. LH_WRITE_ERROR("批量删除模版失败");
  1256. return false;
  1257. }
  1258. /* 再批量插入模版 */
  1259. if(!saveTemplateList(mapTemplate))
  1260. {
  1261. LH_WRITE_ERROR("批量插入模版失败");
  1262. return false;
  1263. }
  1264. return true;
  1265. }
  1266. /**
  1267. * @brief 更新模板名称
  1268. *
  1269. * @param mapTemplateName
  1270. * @return true
  1271. * @return false
  1272. */
  1273. bool FromWebAPI::updateTemplateName(const QMap<OneTemplateInfo, QString>& mapTemplateName)
  1274. {
  1275. if(m_httpApi == nullptr)
  1276. {
  1277. LH_WRITE_ERROR("WebAPI is nullptr");
  1278. return false;
  1279. }
  1280. nJson json0 = nJson::array();
  1281. for(auto it = mapTemplateName.begin(), end = mapTemplateName.end(); it != end; it++)
  1282. {
  1283. nJson json1;
  1284. json1["opName"] = "TMS_UpdateExecPlanTemplateName";
  1285. json1["Key"] = QUuid::createUuid().toString().toStdString();
  1286. nJson json2;
  1287. json2["channelID"] = it.key().channelInfo.ChannelID;
  1288. json2["oldTemplateName"] = it.key().templateName.toStdString();
  1289. json2["newTemplateName"] = it.value().toStdString();
  1290. json1["paramList"] = json2;
  1291. json0.push_back(json1);
  1292. }
  1293. /* 打印生成的JSON */
  1294. // LH_WRITE_LOG_DEBUG(QString::fromStdString(json0.dump(4)));
  1295. QString strCmd = QString::fromStdString(json0.dump());
  1296. QString strRet;
  1297. auto ret = m_httpApi->DBDoInterface(enDBOperatorType::EDBOT_BatchTransAction, strCmd, strRet, true);
  1298. if(ret != 0)
  1299. {
  1300. LH_WRITE_ERROR(QString("更新模板名称失败:%1, 错误信息:%2").arg(ret).arg(m_httpApi->DoGetLastError(&ret)));
  1301. return false;
  1302. }
  1303. // LH_WRITE_LOG_DEBUG(strRet);
  1304. return true;
  1305. }
  1306. /* 将json的key转换成小写 */
  1307. nJson FromWebAPI::convertJsonKeyToLower(const nJson& json)
  1308. {
  1309. nJson result;
  1310. for(auto& it : json.items())
  1311. {
  1312. std::string key = it.key();
  1313. std::transform(key.begin(), key.end(), key.begin(), ::tolower); // 转换成小写
  1314. /* 是object */
  1315. if(it.value().is_object())
  1316. {
  1317. result[key] = convertJsonKeyToLower(it.value()); // 递归处理嵌套的对象
  1318. }
  1319. /* 是array */
  1320. else if(it.value().is_array())
  1321. {
  1322. nJson arrayResult = nJson::array();
  1323. for(auto& item : it.value())
  1324. {
  1325. if(item.is_object())
  1326. {
  1327. arrayResult.push_back(convertJsonKeyToLower(item));
  1328. }
  1329. else
  1330. {
  1331. arrayResult.push_back(item);
  1332. }
  1333. }
  1334. result[key] = arrayResult;
  1335. }
  1336. else
  1337. {
  1338. /* 直接赋值非对象和非数组类型 */
  1339. result[key] = it.value();
  1340. }
  1341. }
  1342. return result;
  1343. }
  1344. // #include <regex>
  1345. // #include <string>
  1346. // #include <algorithm>
  1347. // std::string FromWebAPI::convertJsonKeyToLower(const std::string& str)
  1348. // {
  1349. // std::regex reg(R"(\"([a-zA-Z0-9_]+)\":)");
  1350. // std::string strStd = str;
  1351. // std::string result = std::regex_replace(strStd, reg, [](const std::smatch& match) {
  1352. // std::string key = match[1].str();
  1353. // std::transform(key.begin(), key.end(), key.begin(), ::tolower); // 转换成小写
  1354. // return "\"" + key + "\":";
  1355. // });
  1356. // return result;
  1357. // }