Widget_HttpSendJson.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. #include "Widget_HttpSendJson.h"
  2. #include <QTime>
  3. #include <QTimer>
  4. #include "QThreadHttpClient.h"
  5. #include "DataManager.h"
  6. #include <QApplication>
  7. #include <QCloseEvent>
  8. #include <QDebug>
  9. #ifdef Q_OS_WIN
  10. //#pragma execution_character_set("utf-8")
  11. #endif
  12. Widget_HttpSendJson::Widget_HttpSendJson(QWidget *parent)
  13. : QWidget(parent)
  14. {
  15. ui.setupUi(this);
  16. InitUIData();
  17. // 创建托盘图标和菜单
  18. createTrayIcon();
  19. }
  20. Widget_HttpSendJson::~Widget_HttpSendJson()
  21. {
  22. // 清理资源
  23. if (trayIcon) {
  24. trayIcon->hide();
  25. }
  26. }
  27. void Widget_HttpSendJson::InitUIData()
  28. {
  29. ui.tableWidget_List->setColumnCount(3);
  30. QStringList pTableHeadList;
  31. pTableHeadList << G2U("请求类型") << G2U("发送请求") << G2U("收到回复");
  32. ui.tableWidget_List->setHorizontalHeaderLabels(pTableHeadList);
  33. //ui.tableWidget_List->setVisible(false);
  34. ui.tableWidget_List->setEditTriggers(QAbstractItemView::NoEditTriggers);
  35. ui.tableWidget_List->setSelectionBehavior(QAbstractItemView::SelectRows);
  36. ui.tableWidget_List->setSelectionMode(QAbstractItemView::SingleSelection);
  37. ui.tableWidget_List->setColumnWidth(0, 120);
  38. //AddToListData(LogJson("", "", SVR_API_NULL, false));
  39. InitConnect();
  40. }
  41. void Widget_HttpSendJson::InitConnect()
  42. {
  43. bool bConnectError = false;
  44. bConnectError = connect(ui.tableWidget_List, SIGNAL(cellClicked(int, int)), this, SLOT(slot_ClickListUpdate(int, int)));
  45. bConnectError = connect(ui.pushButton_Clear, SIGNAL(clicked()), this, SLOT(slot_ClearResponseBtn()));
  46. bConnectError = connect(ui.pushButton_jsonTest, SIGNAL(clicked()), this, SLOT(slot_JsonTestBtn()));
  47. bConnectError = connect(ui.pushButton_sendJson, SIGNAL(clicked()), this, SLOT(slot_SendRequestBtn()));
  48. bConnectError = connect(ui.pushButton_Reload, SIGNAL(clicked()), this, SLOT(slot_ReloadListBtn()));
  49. bConnectError = connect(ui.pushButton_ShowSend, SIGNAL(clicked()), this, SLOT(slot_ShowRequestBtn()));
  50. bConnectError = connect(ui.pushButton_ShowSendEnd, SIGNAL(clicked()), this, SLOT(slot_ShowRequestAndResponseBtn()));
  51. bConnectError = connect(ui.pushButton_Test, SIGNAL(clicked()), this, SLOT(slot_ADDTestDataToList()));
  52. bConnectError = connect(ui.checkBox, SIGNAL(stateChanged(int)), this, SLOT(slot_CheckState(int)));
  53. //pTimerList = new QTimer();
  54. //connect(pTimerList, SIGNAL(timeout()), this, SLOT(slot_TimerUpdateList()));
  55. //pTimerList->start(1000);
  56. connect(&DataManager::instance(), &DataManager::dataChanged, this, &Widget_HttpSendJson::slot_dataChange);
  57. bConnectError = connect(ui.pb_Start, SIGNAL(clicked()), this, SLOT(slot_StartThread()));
  58. bConnectError = connect(ui.pb_End, SIGNAL(clicked()), this, SLOT(slot_EndThread()));
  59. bConnectError = connect(DataManager::instance().pRunThread, SIGNAL(signal_RunSuccess()), this, SLOT(slot_RunSuccess()));
  60. }
  61. void Widget_HttpSendJson::UpdateSendEdit(std::string strJson)
  62. {
  63. ui.textEdit_Send->clear();
  64. ui.textEdit_Send->setPlainText(DataManager::string_To_UTF8(strJson).c_str());
  65. }
  66. void Widget_HttpSendJson::UpdateJSONTestEdit(std::string strJson)
  67. {
  68. ui.lineEdit_Error->clear();
  69. ui.lineEdit_Error->setText(DataManager::string_To_UTF8(strJson).c_str());
  70. }
  71. void Widget_HttpSendJson::UpdateHTTPAddress(std::string strAddress)
  72. {
  73. ui.lineEdit_Address->clear();
  74. ui.lineEdit_Address->setText(DataManager::string_To_UTF8(strAddress).c_str());
  75. }
  76. void Widget_HttpSendJson::UpdateResponseEdit(std::string strJson)
  77. {
  78. ui.textEdit_Response->clear();
  79. ui.textEdit_Response->setPlainText(DataManager::string_To_UTF8(strJson).c_str());
  80. }
  81. void Widget_HttpSendJson::AddToListData(LogJson& pJsonData)
  82. {
  83. int nCount = ui.tableWidget_List->rowCount();
  84. ui.tableWidget_List->insertRow(nCount);
  85. ui.tableWidget_List->setItem(nCount, 0, new QTableWidgetItem(pJsonData.m_nAPIID.c_str()));
  86. ui.tableWidget_List->setItem(nCount, 1, new QTableWidgetItem(DataManager::string_To_UTF8(pJsonData.m_jsonRequest).c_str()));
  87. QString strShowData="";
  88. if (pJsonData.m_bStatus)
  89. {
  90. strShowData = G2U("发送成功");
  91. }
  92. else{
  93. strShowData = G2U("发送失败");
  94. }
  95. ui.tableWidget_List->setItem(nCount, 2, new QTableWidgetItem(strShowData));
  96. }
  97. void Widget_HttpSendJson::slot_StartThread()
  98. {
  99. if (!DataManager::instance().pRunThread->isRunning())
  100. {
  101. DataManager::instance().pRunThread->SetRunData(1010001,OCC_SYS_DD_SVR,121111221,556);
  102. DataManager::instance().pRunThread->SetCMDType(TaskRunThread::server_Run);
  103. DataManager::instance().pRunThread->start();
  104. }
  105. }
  106. void Widget_HttpSendJson::slot_EndThread()
  107. {
  108. if (!DataManager::instance().pRunThread->isRunning())
  109. {
  110. DataManager::instance().pRunThread->SetRunData(1010001, OCC_SYS_DD_SVR, 121111221, 556);
  111. DataManager::instance().pRunThread->SetCMDType(TaskRunThread::server_Stop);
  112. DataManager::instance().pRunThread->start();
  113. }
  114. }
  115. void Widget_HttpSendJson::slot_ClearResponseBtn()
  116. {
  117. ui.textEdit_Response->clear();
  118. }
  119. void Widget_HttpSendJson::slot_SendRequestBtn()
  120. {
  121. QString strJson=ui.textEdit_Send->toPlainText();
  122. QString strAddress = ui.lineEdit_Address->text();
  123. std::string strResponse="";
  124. Json::Reader reader;
  125. Json::Value root;
  126. if (!reader.parse(strJson.toStdString(), root))
  127. {
  128. QString StrMessage = G2U("json解析失败是否继续发送HTTP");
  129. QMessageBox::StandardButton confimBtn = QMessageBox::question(this, "错误", StrMessage, QMessageBox::Ok | QMessageBox::Cancel);
  130. if (confimBtn == QMessageBox::Cancel) {
  131. return;
  132. }
  133. }
  134. bool bReturn = QThreadHttpClient::instance().HttpPost(strAddress.toStdString(),strJson.toStdString(), strResponse);
  135. std::string strError="";
  136. if (bReturn)
  137. {
  138. strError = DataManager::UTF8_to_string("HTTP发送成功");
  139. }
  140. else
  141. {
  142. strError = DataManager::UTF8_to_string("HTTP发送失败");
  143. }
  144. UpdateJSONTestEdit(strError);
  145. UpdateResponseEdit(strResponse);
  146. }
  147. void Widget_HttpSendJson::slot_ReloadListBtn()
  148. {
  149. ui.tableWidget_List->clearContents();
  150. ui.tableWidget_List->setRowCount(0);
  151. QList<LogJson>* LogJson = DataManager::instance().GetLogJson();
  152. LogJson->clear();
  153. }
  154. void Widget_HttpSendJson::slot_ClickListUpdate(int nRow, int nColumn)
  155. {
  156. m_nSelectRow = nRow;
  157. QList<LogJson>* LogJson = DataManager::instance().GetLogJson();
  158. if (nRow>=0&&!LogJson->empty())
  159. {
  160. m_pSelectData = &(*LogJson)[nRow];
  161. }
  162. else
  163. {
  164. m_pSelectData = NULL;
  165. }
  166. }
  167. void Widget_HttpSendJson::slot_ShowRequestBtn()
  168. {
  169. QString strShow = ui.textEdit_Send->toPlainText();
  170. if (!strShow.isEmpty())
  171. {
  172. QString strShow = G2U("确定清除Json条目吗?");
  173. QMessageBox::StandardButton confimBtn = QMessageBox::question(this, G2U("提示"), strShow, QMessageBox::Ok | QMessageBox::Cancel);
  174. if (confimBtn == QMessageBox::Cancel) {
  175. return;
  176. }
  177. else
  178. {
  179. if (m_pSelectData)
  180. {
  181. UpdateSendEdit(m_pSelectData->m_jsonRequest);
  182. UpdateHTTPAddress(m_pSelectData->m_nAPIID);
  183. }
  184. }
  185. }
  186. else if (m_nSelectRow >= 0)
  187. {
  188. if (m_pSelectData)
  189. {
  190. UpdateSendEdit(m_pSelectData->m_jsonRequest);
  191. UpdateHTTPAddress(m_pSelectData->m_nAPIID);
  192. }
  193. }
  194. }
  195. void Widget_HttpSendJson::slot_ShowRequestAndResponseBtn()
  196. {
  197. QString strShow = ui.textEdit_Send->toPlainText();
  198. if (!strShow.isEmpty())
  199. {
  200. QString strShow = G2U("确定清除Json条目吗?");
  201. QMessageBox::StandardButton confimBtn = QMessageBox::question(this, G2U("提示"), strShow, QMessageBox::Ok | QMessageBox::Cancel);
  202. if (confimBtn == QMessageBox::Cancel) {
  203. return;
  204. }
  205. else
  206. {
  207. if (m_pSelectData)
  208. {
  209. UpdateSendEdit(m_pSelectData->m_jsonRequest);
  210. UpdateHTTPAddress(m_pSelectData->m_nAPIID);
  211. UpdateResponseEdit(m_pSelectData->m_jsonResponse);
  212. }
  213. }
  214. }
  215. else if (m_nSelectRow>=0)
  216. {
  217. if (m_pSelectData)
  218. {
  219. UpdateSendEdit(m_pSelectData->m_jsonRequest);
  220. UpdateHTTPAddress(m_pSelectData->m_nAPIID);
  221. UpdateResponseEdit(m_pSelectData->m_jsonResponse);
  222. }
  223. }
  224. }
  225. void Widget_HttpSendJson::slot_JsonTestBtn()
  226. {
  227. Json::Reader reader;
  228. QString strEdit = ui.textEdit_Send->toPlainText();
  229. std::string strJson = strEdit.toStdString();
  230. Json::Value root;
  231. std::string strError="";
  232. if (reader.parse(strJson, root))
  233. {
  234. strError = DataManager::UTF8_to_string("JSON结构正常,读取正常");
  235. }
  236. else
  237. {
  238. std::string strMSG=reader.getFormattedErrorMessages();
  239. strError = strMSG;
  240. }
  241. UpdateJSONTestEdit(strError);
  242. }
  243. void Widget_HttpSendJson::slot_ADDTestDataToList()
  244. {
  245. QString strAddress="/courserun/eqpmtstatus";
  246. QString strJson="";
  247. std::string strResponse="";
  248. Json::Reader reader;
  249. Json::FastWriter wJson;
  250. Json::Value rootEqpmt;
  251. rootEqpmt["SubSys"]=1001001;
  252. rootEqpmt["CtrlType"]=0;
  253. rootEqpmt["Status"]=0;
  254. rootEqpmt["Category"]=0;
  255. rootEqpmt["LineID"]=1001;
  256. rootEqpmt["StationID"]=0;
  257. rootEqpmt["ViewID"]=0;
  258. rootEqpmt["EqpmtID"]="";
  259. rootEqpmt["CtrlLock"]=0;
  260. rootEqpmt["FaultLock"]=0;
  261. rootEqpmt["RemoteLock"]=0;
  262. rootEqpmt["CommunicationStaus"]=0;
  263. rootEqpmt["CtrlArea"]=0;
  264. strJson=QString::fromStdString(wJson.write(rootEqpmt));
  265. bool bReturn = QThreadHttpClient::instance().HttpPost(strAddress.toStdString(),strJson.toStdString(), strResponse);
  266. std::string strError="";
  267. strAddress="/courserun/add_ctrl_log";
  268. Json::Value root;
  269. root["CtrlType"]=0;
  270. root["CtrlFrom"]=0;
  271. root["CtrlUser"]="";
  272. root["Param1"]="";
  273. root["Param2"]="";
  274. root["Param3"]="";
  275. root["Param4"]="";
  276. root["Param5"]="";
  277. strJson =QString::fromStdString(wJson.write(root));
  278. bReturn = QThreadHttpClient::instance().HttpPost(strAddress.toStdString(),strJson.toStdString(), strResponse);
  279. }
  280. void Widget_HttpSendJson::slot_TimerUpdateList()
  281. {
  282. ui.tableWidget_List->clearContents();
  283. ui.tableWidget_List->setRowCount(0);
  284. //xhNetWork::InStance()->MutexLogJson.lock();
  285. QList<LogJson>* LogJson = DataManager::instance().GetLogJson();
  286. for (auto iters = LogJson->begin(); iters != LogJson->end();iters++)
  287. {
  288. AddToListData(*iters);
  289. }
  290. //xhNetWork::InStance()->MutexLogJson.unlock();
  291. ui.tableWidget_List->selectRow(m_nSelectRow);
  292. }
  293. void Widget_HttpSendJson::slot_CheckState(int nState)
  294. {
  295. if (nState==Qt::Checked)
  296. {
  297. hideHeart = false;
  298. }
  299. else
  300. {
  301. hideHeart = true;
  302. }
  303. }
  304. void Widget_HttpSendJson::slot_dataChange(int nDataType)
  305. {
  306. slot_TimerUpdateList();
  307. }
  308. void Widget_HttpSendJson::slot_RunSuccess()
  309. {
  310. //QMessageBox::information(this, "提示", "执行成功");
  311. string strData = "";
  312. switch (TaskMNG::instance()->getTaskStatus())
  313. {
  314. case TaskMNG::TaskStatus::Status_Run:
  315. {
  316. strData = G2U("课程开启").toStdString();
  317. DataManager::instance().setEqpmtStatus(1001,2);
  318. }
  319. break;
  320. case TaskMNG::TaskStatus::Status_Null:
  321. {
  322. strData = G2U("课程结束").toStdString();
  323. DataManager::instance().setEqpmtStatus(1001, 1);
  324. }
  325. break;
  326. default:
  327. break;
  328. };
  329. strData.append(G2U("执行成功").toStdString());
  330. LogJson pJsonData(G2U("执行成功").toStdString(),"--", strData,true);
  331. AddToListData(pJsonData);
  332. }
  333. void Widget_HttpSendJson::closeEvent(QCloseEvent* event)
  334. {
  335. // 当用户点击窗口的关闭按钮时,询问用户确认退出
  336. QMessageBox::StandardButton reply;
  337. reply = QMessageBox::question(this, G2U("确认退出"),
  338. G2U("确定要退出程序吗?"),
  339. QMessageBox::Yes | QMessageBox::No);
  340. if (reply == QMessageBox::Yes) {
  341. // 用户确认退出,隐藏托盘图标并退出程序
  342. if (trayIcon) {
  343. trayIcon->hide();
  344. }
  345. event->accept();
  346. }
  347. else {
  348. // 用户取消退出,隐藏窗口到托盘而不是退出
  349. this->hide();
  350. event->ignore();
  351. }
  352. }
  353. void Widget_HttpSendJson::onTrayIconActivated(QSystemTrayIcon::ActivationReason reason)
  354. {
  355. switch (reason) {
  356. case QSystemTrayIcon::DoubleClick: // 双击托盘图标
  357. onShowWindowTriggered();
  358. break;
  359. case QSystemTrayIcon::Trigger: // 单击托盘图标(某些系统)
  360. // 可以根据需要处理单击事件
  361. break;
  362. case QSystemTrayIcon::MiddleClick: // 中键点击
  363. // 可以添加中键点击的功能
  364. break;
  365. default:
  366. break;
  367. }
  368. }
  369. void Widget_HttpSendJson::onShowWindowTriggered()
  370. {
  371. // 显示窗口
  372. this->show();
  373. // 恢复正常状态(如果是最小化状态)
  374. this->showNormal();
  375. // 激活窗口并将其置于前台
  376. this->activateWindow();
  377. this->raise();
  378. }
  379. void Widget_HttpSendJson::onHideWindowTriggered()
  380. {
  381. // 隐藏窗口到托盘
  382. this->hide();
  383. // 可选:显示提示消息
  384. // trayIcon->showMessage(tr("程序已隐藏"),
  385. // tr("程序已隐藏到系统托盘继续运行"),
  386. // QSystemTrayIcon::Information, 1500);
  387. }
  388. void Widget_HttpSendJson::onQuitTriggered()
  389. {
  390. // 退出程序
  391. QMessageBox::StandardButton reply;
  392. reply = QMessageBox::question(this, G2U("确认退出"),
  393. G2U("确定要退出程序吗?"),
  394. QMessageBox::Yes | QMessageBox::No);
  395. if (reply == QMessageBox::Yes) {
  396. // 隐藏托盘图标
  397. if (trayIcon) {
  398. trayIcon->hide();
  399. }
  400. // 退出应用程序
  401. qApp->quit();
  402. }
  403. }
  404. void Widget_HttpSendJson::createTrayIcon()
  405. {
  406. // 创建系统托盘图标
  407. trayIcon = new QSystemTrayIcon(this);
  408. // 设置托盘图标 - 请替换为你的图标路径
  409. // 可以使用系统图标作为备选
  410. QIcon icon = QIcon(QPixmap(QStringLiteral(":/new/prefix1/ElcIcon.png")));
  411. if (icon.isNull()) {
  412. // 如果自定义图标加载失败,使用系统标准图标
  413. icon = QApplication::style()->standardIcon(QStyle::SP_ComputerIcon);
  414. }
  415. trayIcon->setIcon(icon);
  416. // 设置鼠标悬停提示
  417. trayIcon->setToolTip(G2U("电调服务"));
  418. // 设置托盘菜单
  419. setupTrayMenu();
  420. // 连接托盘图标的激活信号(单击、双击等)
  421. connect(trayIcon, &QSystemTrayIcon::activated,
  422. this, &Widget_HttpSendJson::onTrayIconActivated);
  423. // 显示托盘图标
  424. trayIcon->show();
  425. }
  426. void Widget_HttpSendJson::setupTrayMenu()
  427. {
  428. // 创建托盘菜单
  429. trayMenu = new QMenu(this);
  430. // 创建菜单动作
  431. showWindowAction = new QAction(G2U("显示窗口"), this);
  432. hideWindowAction = new QAction(G2U("隐藏窗口"), this);
  433. quitAction = new QAction(G2U("退出程序"), this);
  434. // 设置动作图标(可选)
  435. showWindowAction->setIcon(QApplication::style()->standardIcon(QStyle::SP_TitleBarNormalButton));
  436. hideWindowAction->setIcon(QApplication::style()->standardIcon(QStyle::SP_TitleBarMinButton));
  437. quitAction->setIcon(QApplication::style()->standardIcon(QStyle::SP_TitleBarCloseButton));
  438. // 连接动作信号到槽函数
  439. connect(showWindowAction, &QAction::triggered, this, &Widget_HttpSendJson::onShowWindowTriggered);
  440. connect(hideWindowAction, &QAction::triggered, this, &Widget_HttpSendJson::onHideWindowTriggered);
  441. connect(quitAction, &QAction::triggered, this, &Widget_HttpSendJson::onQuitTriggered);
  442. // 将动作添加到菜单
  443. trayMenu->addAction(showWindowAction);
  444. trayMenu->addAction(hideWindowAction);
  445. trayMenu->addSeparator(); // 添加分隔线
  446. trayMenu->addAction(quitAction);
  447. // 设置托盘图标的上下文菜单
  448. trayIcon->setContextMenu(trayMenu);
  449. }