Widget_HttpSendJson.cpp.autosave 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #include "Widget_HttpSendJson.h"
  2. #include <QTime>
  3. #include <QTimer>
  4. #include "QThreadHttpClient.h"
  5. //#pragma execution_character_set("utf-8")
  6. Widget_HttpSendJson::Widget_HttpSendJson(QWidget *parent)
  7. : QWidget(parent)
  8. {
  9. ui.setupUi(this);
  10. InitUIData();
  11. }
  12. Widget_HttpSendJson::~Widget_HttpSendJson()
  13. {
  14. }
  15. void Widget_HttpSendJson::InitUIData()
  16. {
  17. ui.tableWidget_List->setColumnCount(3);
  18. QStringList pTableHeadList;
  19. pTableHeadList << "请求类型" << "发送请求" << "收到回复";
  20. ui.tableWidget_List->setHorizontalHeaderLabels(pTableHeadList);
  21. //ui.tableWidget_List->setVisible(false);
  22. ui.tableWidget_List->setEditTriggers(QAbstractItemView::NoEditTriggers);
  23. ui.tableWidget_List->setSelectionBehavior(QAbstractItemView::SelectRows);
  24. ui.tableWidget_List->setSelectionMode(QAbstractItemView::SingleSelection);
  25. //ui.tableWidget->setColumnWidth(0, 120);
  26. //AddToListData(LogJson("", "", SVR_API_NULL, false));
  27. InitConnect();
  28. }
  29. void Widget_HttpSendJson::InitConnect()
  30. {
  31. bool bConnectError = false;
  32. bConnectError = connect(ui.tableWidget_List, SIGNAL(cellClicked(int, int)), this, SLOT(slot_ClickListUpdate(int, int)));
  33. bConnectError = connect(ui.pushButton_Clear, SIGNAL(clicked()), this, SLOT(slot_ClearResponseBtn()));
  34. bConnectError = connect(ui.pushButton_jsonTest, SIGNAL(clicked()), this, SLOT(slot_JsonTestBtn()));
  35. bConnectError = connect(ui.pushButton_sendJson, SIGNAL(clicked()), this, SLOT(slot_SendRequestBtn()));
  36. bConnectError = connect(ui.pushButton_Reload, SIGNAL(clicked()), this, SLOT(slot_ReloadListBtn()));
  37. bConnectError = connect(ui.pushButton_ShowSend, SIGNAL(clicked()), this, SLOT(slot_ShowRequestBtn()));
  38. bConnectError = connect(ui.pushButton_ShowSendEnd, SIGNAL(clicked()), this, SLOT(slot_ShowRequestAndResponseBtn()));
  39. bConnectError = connect(ui.pushButton_Test, SIGNAL(clicked()), this, SLOT(slot_ADDTestDataToList()));
  40. bConnectError = connect(ui.checkBox, SIGNAL(stateChanged(int)), this, SLOT(slot_CheckState(int)));
  41. //pTimerList = new QTimer();
  42. //connect(pTimerList, SIGNAL(timeout()), this, SLOT(slot_TimerUpdateList()));
  43. //pTimerList->start(1000);
  44. connect(&DataManager::instance(), &DataManager::dataChanged, this, &Widget_HttpSendJson::slot_dataChange);
  45. }
  46. void Widget_HttpSendJson::UpdateSendEdit(std::string strJson)
  47. {
  48. ui.textEdit_Send->clear();
  49. ui.textEdit_Send->setPlainText(DataManager::string_To_UTF8(strJson).c_str());
  50. }
  51. void Widget_HttpSendJson::UpdateJSONTestEdit(std::string strJson)
  52. {
  53. ui.lineEdit_Error->clear();
  54. ui.lineEdit_Error->setText(DataManager::string_To_UTF8(strJson).c_str());
  55. }
  56. void Widget_HttpSendJson::UpdateHTTPAddress(std::string strAddress)
  57. {
  58. ui.lineEdit_Address->clear();
  59. ui.lineEdit_Address->setText(DataManager::string_To_UTF8(strAddress).c_str());
  60. }
  61. void Widget_HttpSendJson::UpdateResponseEdit(std::string strJson)
  62. {
  63. ui.textEdit_Response->clear();
  64. ui.textEdit_Response->setPlainText(DataManager::string_To_UTF8(strJson).c_str());
  65. }
  66. void Widget_HttpSendJson::AddToListData(LogJson& pJsonData)
  67. {
  68. int nCount = ui.tableWidget_List->rowCount();
  69. ui.tableWidget_List->insertRow(nCount);
  70. ui.tableWidget_List->setItem(nCount, 0, new QTableWidgetItem(pJsonData.m_nAPIID.c_str()));
  71. ui.tableWidget_List->setItem(nCount, 1, new QTableWidgetItem(DataManager::string_To_UTF8(pJsonData.m_jsonRequest).c_str()));
  72. QString strShowData="";
  73. if (pJsonData.m_bStatus)
  74. {
  75. strShowData = "发送成功";
  76. }
  77. else{
  78. strShowData = "发送失败";
  79. }
  80. ui.tableWidget_List->setItem(nCount, 2, new QTableWidgetItem(strShowData));
  81. }
  82. void Widget_HttpSendJson::slot_ClearResponseBtn()
  83. {
  84. ui.textEdit_Response->clear();
  85. }
  86. void Widget_HttpSendJson::slot_SendRequestBtn()
  87. {
  88. QString strJson=ui.textEdit_Send->toPlainText();
  89. QString strAddress = ui.lineEdit_Address->text();
  90. std::string strResponse="";
  91. Json::Reader reader;
  92. Json::Value root;
  93. if (!reader.parse(strJson.toStdString(), root))
  94. {
  95. QString StrMessage = "json解析失败是否继续发送HTTP";
  96. QMessageBox::StandardButton confimBtn = QMessageBox::question(this, "错误", StrMessage, QMessageBox::Ok | QMessageBox::Cancel);
  97. if (confimBtn == QMessageBox::Cancel) {
  98. return;
  99. }
  100. }
  101. bool bReturn = QThreadHttpClient::instance().HttpPost(strAddress.toStdString(),strJson.toStdString(), strResponse);
  102. std::string strError="";
  103. if (bReturn)
  104. {
  105. strError = DataManager::UTF8_to_string("HTTP发送成功");
  106. }
  107. else
  108. {
  109. strError = DataManager::UTF8_to_string("HTTP发送失败");
  110. }
  111. UpdateJSONTestEdit(strError);
  112. UpdateResponseEdit(strResponse);
  113. }
  114. void Widget_HttpSendJson::slot_ReloadListBtn()
  115. {
  116. ui.tableWidget_List->clearContents();
  117. ui.tableWidget_List->setRowCount(0);
  118. QList<LogJson>* LogJson = DataManager::instance().GetLogJson();
  119. LogJson->clear();
  120. }
  121. void Widget_HttpSendJson::slot_ClickListUpdate(int nRow, int nColumn)
  122. {
  123. m_nSelectRow = nRow;
  124. QList<LogJson>* LogJson = DataManager::instance().GetLogJson();
  125. if (nRow>=0&&!LogJson->empty())
  126. {
  127. m_pSelectData = &(*LogJson)[nRow];
  128. }
  129. else
  130. {
  131. m_pSelectData = NULL;
  132. }
  133. }
  134. void Widget_HttpSendJson::slot_ShowRequestBtn()
  135. {
  136. QString strShow = ui.textEdit_Send->toPlainText();
  137. if (!strShow.isEmpty())
  138. {
  139. QString strShow = "确定清除Json条目吗?";
  140. QMessageBox::StandardButton confimBtn = QMessageBox::question(this, "提示", strShow, QMessageBox::Ok | QMessageBox::Cancel);
  141. if (confimBtn == QMessageBox::Cancel) {
  142. return;
  143. }
  144. else
  145. {
  146. if (m_pSelectData)
  147. {
  148. UpdateSendEdit(m_pSelectData->m_jsonRequest);
  149. UpdateHTTPAddress(m_pSelectData->m_nAPIID);
  150. }
  151. }
  152. }
  153. else if (m_nSelectRow >= 0)
  154. {
  155. if (m_pSelectData)
  156. {
  157. UpdateSendEdit(m_pSelectData->m_jsonRequest);
  158. UpdateHTTPAddress(m_pSelectData->m_nAPIID);
  159. }
  160. }
  161. }
  162. void Widget_HttpSendJson::slot_ShowRequestAndResponseBtn()
  163. {
  164. QString strShow = ui.textEdit_Send->toPlainText();
  165. if (!strShow.isEmpty())
  166. {
  167. QString strShow = "确定清除Json条目吗?";
  168. QMessageBox::StandardButton confimBtn = QMessageBox::question(this, "提示", strShow, QMessageBox::Ok | QMessageBox::Cancel);
  169. if (confimBtn == QMessageBox::Cancel) {
  170. return;
  171. }
  172. else
  173. {
  174. if (m_pSelectData)
  175. {
  176. UpdateSendEdit(m_pSelectData->m_jsonRequest);
  177. UpdateHTTPAddress(m_pSelectData->m_nAPIID);
  178. UpdateResponseEdit(m_pSelectData->m_jsonResponse);
  179. }
  180. }
  181. }
  182. else if (m_nSelectRow>=0)
  183. {
  184. if (m_pSelectData)
  185. {
  186. UpdateSendEdit(m_pSelectData->m_jsonRequest);
  187. UpdateHTTPAddress(m_pSelectData->m_nAPIID);
  188. UpdateResponseEdit(m_pSelectData->m_jsonResponse);
  189. }
  190. }
  191. }
  192. void Widget_HttpSendJson::slot_JsonTestBtn()
  193. {
  194. Json::Reader reader;
  195. QString strEdit = ui.textEdit_Send->toPlainText();
  196. std::string strJson = strEdit.toStdString();
  197. Json::Value root;
  198. std::string strError="";
  199. if (reader.parse(strJson, root))
  200. {
  201. strError = DataManager::UTF8_to_string("JSON结构正常,读取正常");
  202. }
  203. else
  204. {
  205. std::string strMSG=reader.getFormattedErrorMessages();
  206. strError = strMSG;
  207. }
  208. UpdateJSONTestEdit(strError);
  209. }
  210. void Widget_HttpSendJson::slot_ADDTestDataToList()
  211. {
  212. QString strAddress="/courserun/eqpmtstatus";
  213. QString strJson="";
  214. std::string strResponse="";
  215. Json::Reader reader;
  216. Json::FastWriter wJson;
  217. Json::Value rootEqpmt;
  218. rootEqpmt["SubSys"]=1001001;
  219. rootEqpmt["CtrlType"]=0;
  220. rootEqpmt["Status"]=0;
  221. rootEqpmt["Category"]=0;
  222. rootEqpmt["LineID"]=1001;
  223. rootEqpmt["StationID"]=0;
  224. rootEqpmt["ViewID"]=0;
  225. rootEqpmt["EqpmtID"]="";
  226. rootEqpmt["CtrlLock"]=0;
  227. rootEqpmt["FaultLock"]=0;
  228. rootEqpmt["RemoteLock"]=0;
  229. rootEqpmt["CommunicationStaus"]=0;
  230. rootEqpmt["CtrlArea"]=0;
  231. strJson=QString::fromStdString(wJson.write(rootEqpmt));
  232. bool bReturn = QThreadHttpClient::instance().HttpPost(strAddress.toStdString(),strJson.toStdString(), strResponse);
  233. std::string strError="";
  234. strAddress="/courserun/add_ctrl_log";
  235. Json::Value root;
  236. root["CtrlType"]=0;
  237. root["CtrlFrom"]=0;
  238. root["CtrlUser"]="";
  239. root["Param1"]="";
  240. root["Param2"]="";
  241. root["Param3"]="";
  242. root["Param4"]="";
  243. root["Param5"]="";
  244. strJson =QString::fromStdString(wJson.write(root));
  245. bReturn = QThreadHttpClient::instance().HttpPost(strAddress.toStdString(),strJson.toStdString(), strResponse);
  246. }
  247. void Widget_HttpSendJson::slot_TimerUpdateList()
  248. {
  249. ui.tableWidget_List->clearContents();
  250. ui.tableWidget_List->setRowCount(0);
  251. //xhNetWork::InStance()->MutexLogJson.lock();
  252. QList<LogJson>* LogJson = DataManager::instance().GetLogJson();
  253. for (auto iters = LogJson->begin(); iters != LogJson->end();iters++)
  254. {
  255. AddToListData(*iters);
  256. }
  257. //xhNetWork::InStance()->MutexLogJson.unlock();
  258. ui.tableWidget_List->selectRow(m_nSelectRow);
  259. }
  260. void Widget_HttpSendJson::slot_CheckState(int nState)
  261. {
  262. if (nState==Qt::Checked)
  263. {
  264. hideHeart = false;
  265. }
  266. else
  267. {
  268. hideHeart = true;
  269. }
  270. }
  271. void Widget_HttpSendJson::slot_dataChange(int nDataType)
  272. {
  273. slot_TimerUpdateList();
  274. }