#include "Widget_HttpSendJson.h" #include #include #include "QThreadHttpClient.h" //#pragma execution_character_set("utf-8") Widget_HttpSendJson::Widget_HttpSendJson(QWidget *parent) : QWidget(parent) { ui.setupUi(this); InitUIData(); } Widget_HttpSendJson::~Widget_HttpSendJson() { } void Widget_HttpSendJson::InitUIData() { ui.tableWidget_List->setColumnCount(3); QStringList pTableHeadList; pTableHeadList << "请求类型" << "发送请求" << "收到回复"; ui.tableWidget_List->setHorizontalHeaderLabels(pTableHeadList); //ui.tableWidget_List->setVisible(false); ui.tableWidget_List->setEditTriggers(QAbstractItemView::NoEditTriggers); ui.tableWidget_List->setSelectionBehavior(QAbstractItemView::SelectRows); ui.tableWidget_List->setSelectionMode(QAbstractItemView::SingleSelection); //ui.tableWidget->setColumnWidth(0, 120); //AddToListData(LogJson("", "", SVR_API_NULL, false)); InitConnect(); } void Widget_HttpSendJson::InitConnect() { bool bConnectError = false; bConnectError = connect(ui.tableWidget_List, SIGNAL(cellClicked(int, int)), this, SLOT(slot_ClickListUpdate(int, int))); bConnectError = connect(ui.pushButton_Clear, SIGNAL(clicked()), this, SLOT(slot_ClearResponseBtn())); bConnectError = connect(ui.pushButton_jsonTest, SIGNAL(clicked()), this, SLOT(slot_JsonTestBtn())); bConnectError = connect(ui.pushButton_sendJson, SIGNAL(clicked()), this, SLOT(slot_SendRequestBtn())); bConnectError = connect(ui.pushButton_Reload, SIGNAL(clicked()), this, SLOT(slot_ReloadListBtn())); bConnectError = connect(ui.pushButton_ShowSend, SIGNAL(clicked()), this, SLOT(slot_ShowRequestBtn())); bConnectError = connect(ui.pushButton_ShowSendEnd, SIGNAL(clicked()), this, SLOT(slot_ShowRequestAndResponseBtn())); bConnectError = connect(ui.pushButton_Test, SIGNAL(clicked()), this, SLOT(slot_ADDTestDataToList())); bConnectError = connect(ui.checkBox, SIGNAL(stateChanged(int)), this, SLOT(slot_CheckState(int))); //pTimerList = new QTimer(); //connect(pTimerList, SIGNAL(timeout()), this, SLOT(slot_TimerUpdateList())); //pTimerList->start(1000); connect(&DataManager::instance(), &DataManager::dataChanged, this, &Widget_HttpSendJson::slot_dataChange); } void Widget_HttpSendJson::UpdateSendEdit(std::string strJson) { ui.textEdit_Send->clear(); ui.textEdit_Send->setPlainText(DataManager::string_To_UTF8(strJson).c_str()); } void Widget_HttpSendJson::UpdateJSONTestEdit(std::string strJson) { ui.lineEdit_Error->clear(); ui.lineEdit_Error->setText(DataManager::string_To_UTF8(strJson).c_str()); } void Widget_HttpSendJson::UpdateHTTPAddress(std::string strAddress) { ui.lineEdit_Address->clear(); ui.lineEdit_Address->setText(DataManager::string_To_UTF8(strAddress).c_str()); } void Widget_HttpSendJson::UpdateResponseEdit(std::string strJson) { ui.textEdit_Response->clear(); ui.textEdit_Response->setPlainText(DataManager::string_To_UTF8(strJson).c_str()); } void Widget_HttpSendJson::AddToListData(LogJson& pJsonData) { int nCount = ui.tableWidget_List->rowCount(); ui.tableWidget_List->insertRow(nCount); ui.tableWidget_List->setItem(nCount, 0, new QTableWidgetItem(pJsonData.m_nAPIID.c_str())); ui.tableWidget_List->setItem(nCount, 1, new QTableWidgetItem(DataManager::string_To_UTF8(pJsonData.m_jsonRequest).c_str())); QString strShowData=""; if (pJsonData.m_bStatus) { strShowData = "发送成功"; } else{ strShowData = "发送失败"; } ui.tableWidget_List->setItem(nCount, 2, new QTableWidgetItem(strShowData)); } void Widget_HttpSendJson::slot_ClearResponseBtn() { ui.textEdit_Response->clear(); } void Widget_HttpSendJson::slot_SendRequestBtn() { QString strJson=ui.textEdit_Send->toPlainText(); QString strAddress = ui.lineEdit_Address->text(); std::string strResponse=""; Json::Reader reader; Json::Value root; if (!reader.parse(strJson.toStdString(), root)) { QString StrMessage = "json解析失败是否继续发送HTTP"; QMessageBox::StandardButton confimBtn = QMessageBox::question(this, "错误", StrMessage, QMessageBox::Ok | QMessageBox::Cancel); if (confimBtn == QMessageBox::Cancel) { return; } } bool bReturn = QThreadHttpClient::instance().HttpPost(strAddress.toStdString(),strJson.toStdString(), strResponse); std::string strError=""; if (bReturn) { strError = DataManager::UTF8_to_string("HTTP发送成功"); } else { strError = DataManager::UTF8_to_string("HTTP发送失败"); } UpdateJSONTestEdit(strError); UpdateResponseEdit(strResponse); } void Widget_HttpSendJson::slot_ReloadListBtn() { ui.tableWidget_List->clearContents(); ui.tableWidget_List->setRowCount(0); QList* LogJson = DataManager::instance().GetLogJson(); LogJson->clear(); } void Widget_HttpSendJson::slot_ClickListUpdate(int nRow, int nColumn) { m_nSelectRow = nRow; QList* LogJson = DataManager::instance().GetLogJson(); if (nRow>=0&&!LogJson->empty()) { m_pSelectData = &(*LogJson)[nRow]; } else { m_pSelectData = NULL; } } void Widget_HttpSendJson::slot_ShowRequestBtn() { QString strShow = ui.textEdit_Send->toPlainText(); if (!strShow.isEmpty()) { QString strShow = "确定清除Json条目吗?"; QMessageBox::StandardButton confimBtn = QMessageBox::question(this, "提示", strShow, QMessageBox::Ok | QMessageBox::Cancel); if (confimBtn == QMessageBox::Cancel) { return; } else { if (m_pSelectData) { UpdateSendEdit(m_pSelectData->m_jsonRequest); UpdateHTTPAddress(m_pSelectData->m_nAPIID); } } } else if (m_nSelectRow >= 0) { if (m_pSelectData) { UpdateSendEdit(m_pSelectData->m_jsonRequest); UpdateHTTPAddress(m_pSelectData->m_nAPIID); } } } void Widget_HttpSendJson::slot_ShowRequestAndResponseBtn() { QString strShow = ui.textEdit_Send->toPlainText(); if (!strShow.isEmpty()) { QString strShow = "确定清除Json条目吗?"; QMessageBox::StandardButton confimBtn = QMessageBox::question(this, "提示", strShow, QMessageBox::Ok | QMessageBox::Cancel); if (confimBtn == QMessageBox::Cancel) { return; } else { if (m_pSelectData) { UpdateSendEdit(m_pSelectData->m_jsonRequest); UpdateHTTPAddress(m_pSelectData->m_nAPIID); UpdateResponseEdit(m_pSelectData->m_jsonResponse); } } } else if (m_nSelectRow>=0) { if (m_pSelectData) { UpdateSendEdit(m_pSelectData->m_jsonRequest); UpdateHTTPAddress(m_pSelectData->m_nAPIID); UpdateResponseEdit(m_pSelectData->m_jsonResponse); } } } void Widget_HttpSendJson::slot_JsonTestBtn() { Json::Reader reader; QString strEdit = ui.textEdit_Send->toPlainText(); std::string strJson = strEdit.toStdString(); Json::Value root; std::string strError=""; if (reader.parse(strJson, root)) { strError = DataManager::UTF8_to_string("JSON结构正常,读取正常"); } else { std::string strMSG=reader.getFormattedErrorMessages(); strError = strMSG; } UpdateJSONTestEdit(strError); } void Widget_HttpSendJson::slot_ADDTestDataToList() { QString strAddress="/courserun/eqpmtstatus"; QString strJson=""; std::string strResponse=""; Json::Reader reader; Json::FastWriter wJson; Json::Value rootEqpmt; rootEqpmt["SubSys"]=1001001; rootEqpmt["CtrlType"]=0; rootEqpmt["Status"]=0; rootEqpmt["Category"]=0; rootEqpmt["LineID"]=1001; rootEqpmt["StationID"]=0; rootEqpmt["ViewID"]=0; rootEqpmt["EqpmtID"]=""; rootEqpmt["CtrlLock"]=0; rootEqpmt["FaultLock"]=0; rootEqpmt["RemoteLock"]=0; rootEqpmt["CommunicationStaus"]=0; rootEqpmt["CtrlArea"]=0; strJson=QString::fromStdString(wJson.write(rootEqpmt)); bool bReturn = QThreadHttpClient::instance().HttpPost(strAddress.toStdString(),strJson.toStdString(), strResponse); std::string strError=""; strAddress="/courserun/add_ctrl_log"; Json::Value root; root["CtrlType"]=0; root["CtrlFrom"]=0; root["CtrlUser"]=""; root["Param1"]=""; root["Param2"]=""; root["Param3"]=""; root["Param4"]=""; root["Param5"]=""; strJson =QString::fromStdString(wJson.write(root)); bReturn = QThreadHttpClient::instance().HttpPost(strAddress.toStdString(),strJson.toStdString(), strResponse); } void Widget_HttpSendJson::slot_TimerUpdateList() { ui.tableWidget_List->clearContents(); ui.tableWidget_List->setRowCount(0); //xhNetWork::InStance()->MutexLogJson.lock(); QList* LogJson = DataManager::instance().GetLogJson(); for (auto iters = LogJson->begin(); iters != LogJson->end();iters++) { AddToListData(*iters); } //xhNetWork::InStance()->MutexLogJson.unlock(); ui.tableWidget_List->selectRow(m_nSelectRow); } void Widget_HttpSendJson::slot_CheckState(int nState) { if (nState==Qt::Checked) { hideHeart = false; } else { hideHeart = true; } } void Widget_HttpSendJson::slot_dataChange(int nDataType) { slot_TimerUpdateList(); }