diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 98422c1..dd69711 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -89,6 +89,8 @@ // 6. 绘制一个设备的多个通道数据面板 this->generateWidgetForDevice(); + + HttpServer::instance().run(QHostAddress::Any, SettingConfig::getInstance().SERVER_PORT); } PhaseWindow::~PhaseWindow() @@ -232,6 +234,11 @@ void PhaseWindow::on_devSelect_currentIndexChanged(int index) { // 清空显示的数据 + if (ui->scrollContents->children().size() < 1) + { + return ; + } + for (int i = 0; i < 16; i++) { // 获取对应的通道BOX diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 98422c1..dd69711 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -89,6 +89,8 @@ // 6. 绘制一个设备的多个通道数据面板 this->generateWidgetForDevice(); + + HttpServer::instance().run(QHostAddress::Any, SettingConfig::getInstance().SERVER_PORT); } PhaseWindow::~PhaseWindow() @@ -232,6 +234,11 @@ void PhaseWindow::on_devSelect_currentIndexChanged(int index) { // 清空显示的数据 + if (ui->scrollContents->children().size() < 1) + { + return ; + } + for (int i = 0; i < 16; i++) { // 获取对应的通道BOX diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 52a8d88..bf1cb57 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -9,6 +9,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" #include "common/HttpRequestController.h" +#include "common/HttpServer.h" namespace Ui { class PhaseWindow; diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 98422c1..dd69711 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -89,6 +89,8 @@ // 6. 绘制一个设备的多个通道数据面板 this->generateWidgetForDevice(); + + HttpServer::instance().run(QHostAddress::Any, SettingConfig::getInstance().SERVER_PORT); } PhaseWindow::~PhaseWindow() @@ -232,6 +234,11 @@ void PhaseWindow::on_devSelect_currentIndexChanged(int index) { // 清空显示的数据 + if (ui->scrollContents->children().size() < 1) + { + return ; + } + for (int i = 0; i < 16; i++) { // 获取对应的通道BOX diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 52a8d88..bf1cb57 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -9,6 +9,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" #include "common/HttpRequestController.h" +#include "common/HttpServer.h" namespace Ui { class PhaseWindow; diff --git a/PhaseCompAcq/common/HttpServer.cpp b/PhaseCompAcq/common/HttpServer.cpp new file mode 100644 index 0000000..c1a1b48 --- /dev/null +++ b/PhaseCompAcq/common/HttpServer.cpp @@ -0,0 +1,68 @@ +#include "HttpServer.h" + +HttpServer::HttpServer(QObject *parent) : QObject(parent) +{ + httpServer = new QTcpServer(this); + httpServer->setMaxPendingConnections(1024); + QObject::connect(httpServer, &QTcpServer::newConnection, this, &HttpServer::newConnection); +} + +HttpServer::~HttpServer() +{ + +} + +HttpServer &HttpServer::instance() +{ + static HttpServer obj; + return obj; +} + +void HttpServer::run(const QHostAddress &address, const quint16 &port) +{ + httpServer->listen(address, port); +} + +void HttpServer::newConnection() +{ + QTcpSocket * m_socket = httpServer->nextPendingConnection(); + QObject::connect(m_socket, &QTcpSocket::readyRead, this, &HttpServer::readyRead); +} + +void HttpServer::readyRead() +{ + QTcpSocket * socket = qobject_cast(sender()); + if(socket) { + QByteArray request = socket->readAll(); + + qDebug() << "Request Data:" << request; + + QByteArray response = this->buildHeartBeatResponse().toUtf8(); + + QString http = "HTTP/1.1 200 OK\r\n"; + http += "Server: qt\r\n"; + http += "Content-Type: application/json;charset=utf-8\r\n"; + http += "Connection: keep-alive\r\n"; + http += QString("Content-Length: %1\r\n\r\n").arg(QString::number(response.size())); + + socket->write(http.toUtf8()); + socket->write(response); + socket->flush(); + socket->waitForBytesWritten(http.size() + response.size()); + socket->close(); + } +} + +QString HttpServer::buildHeartBeatResponse() +{ + QJsonObject resp; + resp.insert("code", 200); + resp.insert("success", true); + + QJsonObject data; + data.insert("status", "1"); + data.insert("remarks", ""); + resp.insert("data", data); + + return QString(QJsonDocument(resp).toJson(QJsonDocument::Compact)); +} diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 98422c1..dd69711 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -89,6 +89,8 @@ // 6. 绘制一个设备的多个通道数据面板 this->generateWidgetForDevice(); + + HttpServer::instance().run(QHostAddress::Any, SettingConfig::getInstance().SERVER_PORT); } PhaseWindow::~PhaseWindow() @@ -232,6 +234,11 @@ void PhaseWindow::on_devSelect_currentIndexChanged(int index) { // 清空显示的数据 + if (ui->scrollContents->children().size() < 1) + { + return ; + } + for (int i = 0; i < 16; i++) { // 获取对应的通道BOX diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 52a8d88..bf1cb57 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -9,6 +9,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" #include "common/HttpRequestController.h" +#include "common/HttpServer.h" namespace Ui { class PhaseWindow; diff --git a/PhaseCompAcq/common/HttpServer.cpp b/PhaseCompAcq/common/HttpServer.cpp new file mode 100644 index 0000000..c1a1b48 --- /dev/null +++ b/PhaseCompAcq/common/HttpServer.cpp @@ -0,0 +1,68 @@ +#include "HttpServer.h" + +HttpServer::HttpServer(QObject *parent) : QObject(parent) +{ + httpServer = new QTcpServer(this); + httpServer->setMaxPendingConnections(1024); + QObject::connect(httpServer, &QTcpServer::newConnection, this, &HttpServer::newConnection); +} + +HttpServer::~HttpServer() +{ + +} + +HttpServer &HttpServer::instance() +{ + static HttpServer obj; + return obj; +} + +void HttpServer::run(const QHostAddress &address, const quint16 &port) +{ + httpServer->listen(address, port); +} + +void HttpServer::newConnection() +{ + QTcpSocket * m_socket = httpServer->nextPendingConnection(); + QObject::connect(m_socket, &QTcpSocket::readyRead, this, &HttpServer::readyRead); +} + +void HttpServer::readyRead() +{ + QTcpSocket * socket = qobject_cast(sender()); + if(socket) { + QByteArray request = socket->readAll(); + + qDebug() << "Request Data:" << request; + + QByteArray response = this->buildHeartBeatResponse().toUtf8(); + + QString http = "HTTP/1.1 200 OK\r\n"; + http += "Server: qt\r\n"; + http += "Content-Type: application/json;charset=utf-8\r\n"; + http += "Connection: keep-alive\r\n"; + http += QString("Content-Length: %1\r\n\r\n").arg(QString::number(response.size())); + + socket->write(http.toUtf8()); + socket->write(response); + socket->flush(); + socket->waitForBytesWritten(http.size() + response.size()); + socket->close(); + } +} + +QString HttpServer::buildHeartBeatResponse() +{ + QJsonObject resp; + resp.insert("code", 200); + resp.insert("success", true); + + QJsonObject data; + data.insert("status", "1"); + data.insert("remarks", ""); + resp.insert("data", data); + + return QString(QJsonDocument(resp).toJson(QJsonDocument::Compact)); +} diff --git a/PhaseCompAcq/common/HttpServer.h b/PhaseCompAcq/common/HttpServer.h new file mode 100644 index 0000000..7499987 --- /dev/null +++ b/PhaseCompAcq/common/HttpServer.h @@ -0,0 +1,33 @@ +#ifndef HTTPSERVER_H +#define HTTPSERVER_H + +#include +#include +#include +#include + +class HttpServer : public QObject +{ + Q_OBJECT +public: + static HttpServer &instance(); + void run(const QHostAddress &address = QHostAddress::Any, const quint16 &port = 5905); + +private: + explicit HttpServer(QObject *parent = nullptr); + ~HttpServer(); + Q_DISABLE_COPY(HttpServer) + + QTcpServer * httpServer; + + QString buildHeartBeatResponse(); + +signals: + +public slots: +private slots: + void newConnection(); + void readyRead(); +}; + +#endif // HTTPSERVER_H diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 98422c1..dd69711 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -89,6 +89,8 @@ // 6. 绘制一个设备的多个通道数据面板 this->generateWidgetForDevice(); + + HttpServer::instance().run(QHostAddress::Any, SettingConfig::getInstance().SERVER_PORT); } PhaseWindow::~PhaseWindow() @@ -232,6 +234,11 @@ void PhaseWindow::on_devSelect_currentIndexChanged(int index) { // 清空显示的数据 + if (ui->scrollContents->children().size() < 1) + { + return ; + } + for (int i = 0; i < 16; i++) { // 获取对应的通道BOX diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 52a8d88..bf1cb57 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -9,6 +9,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" #include "common/HttpRequestController.h" +#include "common/HttpServer.h" namespace Ui { class PhaseWindow; diff --git a/PhaseCompAcq/common/HttpServer.cpp b/PhaseCompAcq/common/HttpServer.cpp new file mode 100644 index 0000000..c1a1b48 --- /dev/null +++ b/PhaseCompAcq/common/HttpServer.cpp @@ -0,0 +1,68 @@ +#include "HttpServer.h" + +HttpServer::HttpServer(QObject *parent) : QObject(parent) +{ + httpServer = new QTcpServer(this); + httpServer->setMaxPendingConnections(1024); + QObject::connect(httpServer, &QTcpServer::newConnection, this, &HttpServer::newConnection); +} + +HttpServer::~HttpServer() +{ + +} + +HttpServer &HttpServer::instance() +{ + static HttpServer obj; + return obj; +} + +void HttpServer::run(const QHostAddress &address, const quint16 &port) +{ + httpServer->listen(address, port); +} + +void HttpServer::newConnection() +{ + QTcpSocket * m_socket = httpServer->nextPendingConnection(); + QObject::connect(m_socket, &QTcpSocket::readyRead, this, &HttpServer::readyRead); +} + +void HttpServer::readyRead() +{ + QTcpSocket * socket = qobject_cast(sender()); + if(socket) { + QByteArray request = socket->readAll(); + + qDebug() << "Request Data:" << request; + + QByteArray response = this->buildHeartBeatResponse().toUtf8(); + + QString http = "HTTP/1.1 200 OK\r\n"; + http += "Server: qt\r\n"; + http += "Content-Type: application/json;charset=utf-8\r\n"; + http += "Connection: keep-alive\r\n"; + http += QString("Content-Length: %1\r\n\r\n").arg(QString::number(response.size())); + + socket->write(http.toUtf8()); + socket->write(response); + socket->flush(); + socket->waitForBytesWritten(http.size() + response.size()); + socket->close(); + } +} + +QString HttpServer::buildHeartBeatResponse() +{ + QJsonObject resp; + resp.insert("code", 200); + resp.insert("success", true); + + QJsonObject data; + data.insert("status", "1"); + data.insert("remarks", ""); + resp.insert("data", data); + + return QString(QJsonDocument(resp).toJson(QJsonDocument::Compact)); +} diff --git a/PhaseCompAcq/common/HttpServer.h b/PhaseCompAcq/common/HttpServer.h new file mode 100644 index 0000000..7499987 --- /dev/null +++ b/PhaseCompAcq/common/HttpServer.h @@ -0,0 +1,33 @@ +#ifndef HTTPSERVER_H +#define HTTPSERVER_H + +#include +#include +#include +#include + +class HttpServer : public QObject +{ + Q_OBJECT +public: + static HttpServer &instance(); + void run(const QHostAddress &address = QHostAddress::Any, const quint16 &port = 5905); + +private: + explicit HttpServer(QObject *parent = nullptr); + ~HttpServer(); + Q_DISABLE_COPY(HttpServer) + + QTcpServer * httpServer; + + QString buildHeartBeatResponse(); + +signals: + +public slots: +private slots: + void newConnection(); + void readyRead(); +}; + +#endif // HTTPSERVER_H diff --git a/PhaseCompAcq/common/common.pri b/PhaseCompAcq/common/common.pri index 6280f64..b80a640 100644 --- a/PhaseCompAcq/common/common.pri +++ b/PhaseCompAcq/common/common.pri @@ -7,6 +7,7 @@ SOURCES += $$PWD/utils/HttpRequestUtil.cpp SOURCES += $$PWD/utils/MD5.cpp SOURCES += $$PWD/HttpRequestController.cpp +SOURCES += $$PWD/HttpServer.cpp HEADERS += $$PWD/utils/SettingConfig.h HEADERS += $$PWD/utils/QByteUtil.h @@ -17,4 +18,5 @@ HEADERS += $$PWD/utils/DefHead.h HEADERS += $$PWD/utils/MD5.h HEADERS += $$PWD/HttpRequestController.h +HEADERS += $$PWD/HttpServer.h HEADERS += $$PWD/ConstCache.h diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 98422c1..dd69711 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -89,6 +89,8 @@ // 6. 绘制一个设备的多个通道数据面板 this->generateWidgetForDevice(); + + HttpServer::instance().run(QHostAddress::Any, SettingConfig::getInstance().SERVER_PORT); } PhaseWindow::~PhaseWindow() @@ -232,6 +234,11 @@ void PhaseWindow::on_devSelect_currentIndexChanged(int index) { // 清空显示的数据 + if (ui->scrollContents->children().size() < 1) + { + return ; + } + for (int i = 0; i < 16; i++) { // 获取对应的通道BOX diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 52a8d88..bf1cb57 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -9,6 +9,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" #include "common/HttpRequestController.h" +#include "common/HttpServer.h" namespace Ui { class PhaseWindow; diff --git a/PhaseCompAcq/common/HttpServer.cpp b/PhaseCompAcq/common/HttpServer.cpp new file mode 100644 index 0000000..c1a1b48 --- /dev/null +++ b/PhaseCompAcq/common/HttpServer.cpp @@ -0,0 +1,68 @@ +#include "HttpServer.h" + +HttpServer::HttpServer(QObject *parent) : QObject(parent) +{ + httpServer = new QTcpServer(this); + httpServer->setMaxPendingConnections(1024); + QObject::connect(httpServer, &QTcpServer::newConnection, this, &HttpServer::newConnection); +} + +HttpServer::~HttpServer() +{ + +} + +HttpServer &HttpServer::instance() +{ + static HttpServer obj; + return obj; +} + +void HttpServer::run(const QHostAddress &address, const quint16 &port) +{ + httpServer->listen(address, port); +} + +void HttpServer::newConnection() +{ + QTcpSocket * m_socket = httpServer->nextPendingConnection(); + QObject::connect(m_socket, &QTcpSocket::readyRead, this, &HttpServer::readyRead); +} + +void HttpServer::readyRead() +{ + QTcpSocket * socket = qobject_cast(sender()); + if(socket) { + QByteArray request = socket->readAll(); + + qDebug() << "Request Data:" << request; + + QByteArray response = this->buildHeartBeatResponse().toUtf8(); + + QString http = "HTTP/1.1 200 OK\r\n"; + http += "Server: qt\r\n"; + http += "Content-Type: application/json;charset=utf-8\r\n"; + http += "Connection: keep-alive\r\n"; + http += QString("Content-Length: %1\r\n\r\n").arg(QString::number(response.size())); + + socket->write(http.toUtf8()); + socket->write(response); + socket->flush(); + socket->waitForBytesWritten(http.size() + response.size()); + socket->close(); + } +} + +QString HttpServer::buildHeartBeatResponse() +{ + QJsonObject resp; + resp.insert("code", 200); + resp.insert("success", true); + + QJsonObject data; + data.insert("status", "1"); + data.insert("remarks", ""); + resp.insert("data", data); + + return QString(QJsonDocument(resp).toJson(QJsonDocument::Compact)); +} diff --git a/PhaseCompAcq/common/HttpServer.h b/PhaseCompAcq/common/HttpServer.h new file mode 100644 index 0000000..7499987 --- /dev/null +++ b/PhaseCompAcq/common/HttpServer.h @@ -0,0 +1,33 @@ +#ifndef HTTPSERVER_H +#define HTTPSERVER_H + +#include +#include +#include +#include + +class HttpServer : public QObject +{ + Q_OBJECT +public: + static HttpServer &instance(); + void run(const QHostAddress &address = QHostAddress::Any, const quint16 &port = 5905); + +private: + explicit HttpServer(QObject *parent = nullptr); + ~HttpServer(); + Q_DISABLE_COPY(HttpServer) + + QTcpServer * httpServer; + + QString buildHeartBeatResponse(); + +signals: + +public slots: +private slots: + void newConnection(); + void readyRead(); +}; + +#endif // HTTPSERVER_H diff --git a/PhaseCompAcq/common/common.pri b/PhaseCompAcq/common/common.pri index 6280f64..b80a640 100644 --- a/PhaseCompAcq/common/common.pri +++ b/PhaseCompAcq/common/common.pri @@ -7,6 +7,7 @@ SOURCES += $$PWD/utils/HttpRequestUtil.cpp SOURCES += $$PWD/utils/MD5.cpp SOURCES += $$PWD/HttpRequestController.cpp +SOURCES += $$PWD/HttpServer.cpp HEADERS += $$PWD/utils/SettingConfig.h HEADERS += $$PWD/utils/QByteUtil.h @@ -17,4 +18,5 @@ HEADERS += $$PWD/utils/DefHead.h HEADERS += $$PWD/utils/MD5.h HEADERS += $$PWD/HttpRequestController.h +HEADERS += $$PWD/HttpServer.h HEADERS += $$PWD/ConstCache.h diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp index 035040f..dbe949b 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp @@ -42,9 +42,9 @@ void QSerialPortUtil::sendData(QByteArray data) { + std::cout << data.toStdString() << std::endl; if (this->open == true) { - std::cout << data.toStdString() << std::endl; serial.write(data); } } diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 98422c1..dd69711 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -89,6 +89,8 @@ // 6. 绘制一个设备的多个通道数据面板 this->generateWidgetForDevice(); + + HttpServer::instance().run(QHostAddress::Any, SettingConfig::getInstance().SERVER_PORT); } PhaseWindow::~PhaseWindow() @@ -232,6 +234,11 @@ void PhaseWindow::on_devSelect_currentIndexChanged(int index) { // 清空显示的数据 + if (ui->scrollContents->children().size() < 1) + { + return ; + } + for (int i = 0; i < 16; i++) { // 获取对应的通道BOX diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 52a8d88..bf1cb57 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -9,6 +9,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" #include "common/HttpRequestController.h" +#include "common/HttpServer.h" namespace Ui { class PhaseWindow; diff --git a/PhaseCompAcq/common/HttpServer.cpp b/PhaseCompAcq/common/HttpServer.cpp new file mode 100644 index 0000000..c1a1b48 --- /dev/null +++ b/PhaseCompAcq/common/HttpServer.cpp @@ -0,0 +1,68 @@ +#include "HttpServer.h" + +HttpServer::HttpServer(QObject *parent) : QObject(parent) +{ + httpServer = new QTcpServer(this); + httpServer->setMaxPendingConnections(1024); + QObject::connect(httpServer, &QTcpServer::newConnection, this, &HttpServer::newConnection); +} + +HttpServer::~HttpServer() +{ + +} + +HttpServer &HttpServer::instance() +{ + static HttpServer obj; + return obj; +} + +void HttpServer::run(const QHostAddress &address, const quint16 &port) +{ + httpServer->listen(address, port); +} + +void HttpServer::newConnection() +{ + QTcpSocket * m_socket = httpServer->nextPendingConnection(); + QObject::connect(m_socket, &QTcpSocket::readyRead, this, &HttpServer::readyRead); +} + +void HttpServer::readyRead() +{ + QTcpSocket * socket = qobject_cast(sender()); + if(socket) { + QByteArray request = socket->readAll(); + + qDebug() << "Request Data:" << request; + + QByteArray response = this->buildHeartBeatResponse().toUtf8(); + + QString http = "HTTP/1.1 200 OK\r\n"; + http += "Server: qt\r\n"; + http += "Content-Type: application/json;charset=utf-8\r\n"; + http += "Connection: keep-alive\r\n"; + http += QString("Content-Length: %1\r\n\r\n").arg(QString::number(response.size())); + + socket->write(http.toUtf8()); + socket->write(response); + socket->flush(); + socket->waitForBytesWritten(http.size() + response.size()); + socket->close(); + } +} + +QString HttpServer::buildHeartBeatResponse() +{ + QJsonObject resp; + resp.insert("code", 200); + resp.insert("success", true); + + QJsonObject data; + data.insert("status", "1"); + data.insert("remarks", ""); + resp.insert("data", data); + + return QString(QJsonDocument(resp).toJson(QJsonDocument::Compact)); +} diff --git a/PhaseCompAcq/common/HttpServer.h b/PhaseCompAcq/common/HttpServer.h new file mode 100644 index 0000000..7499987 --- /dev/null +++ b/PhaseCompAcq/common/HttpServer.h @@ -0,0 +1,33 @@ +#ifndef HTTPSERVER_H +#define HTTPSERVER_H + +#include +#include +#include +#include + +class HttpServer : public QObject +{ + Q_OBJECT +public: + static HttpServer &instance(); + void run(const QHostAddress &address = QHostAddress::Any, const quint16 &port = 5905); + +private: + explicit HttpServer(QObject *parent = nullptr); + ~HttpServer(); + Q_DISABLE_COPY(HttpServer) + + QTcpServer * httpServer; + + QString buildHeartBeatResponse(); + +signals: + +public slots: +private slots: + void newConnection(); + void readyRead(); +}; + +#endif // HTTPSERVER_H diff --git a/PhaseCompAcq/common/common.pri b/PhaseCompAcq/common/common.pri index 6280f64..b80a640 100644 --- a/PhaseCompAcq/common/common.pri +++ b/PhaseCompAcq/common/common.pri @@ -7,6 +7,7 @@ SOURCES += $$PWD/utils/HttpRequestUtil.cpp SOURCES += $$PWD/utils/MD5.cpp SOURCES += $$PWD/HttpRequestController.cpp +SOURCES += $$PWD/HttpServer.cpp HEADERS += $$PWD/utils/SettingConfig.h HEADERS += $$PWD/utils/QByteUtil.h @@ -17,4 +18,5 @@ HEADERS += $$PWD/utils/DefHead.h HEADERS += $$PWD/utils/MD5.h HEADERS += $$PWD/HttpRequestController.h +HEADERS += $$PWD/HttpServer.h HEADERS += $$PWD/ConstCache.h diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp index 035040f..dbe949b 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp @@ -42,9 +42,9 @@ void QSerialPortUtil::sendData(QByteArray data) { + std::cout << data.toStdString() << std::endl; if (this->open == true) { - std::cout << data.toStdString() << std::endl; serial.write(data); } } diff --git a/PhaseCompAcq/common/utils/SettingConfig.cpp b/PhaseCompAcq/common/utils/SettingConfig.cpp index 6273b6d..e984099 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.cpp +++ b/PhaseCompAcq/common/utils/SettingConfig.cpp @@ -20,6 +20,7 @@ DEV_TYPES = getProperty("client", "devTypes").toString(); SYSTEM = getProperty("client", "system").toString(); WORK_TYPE = getProperty("client", "workMode").toString(); + SERVER_PORT = getProperty("client", "serverPort").toInt(); BASE_URL = getProperty("http", "baseUrl").toString(); diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 98422c1..dd69711 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -89,6 +89,8 @@ // 6. 绘制一个设备的多个通道数据面板 this->generateWidgetForDevice(); + + HttpServer::instance().run(QHostAddress::Any, SettingConfig::getInstance().SERVER_PORT); } PhaseWindow::~PhaseWindow() @@ -232,6 +234,11 @@ void PhaseWindow::on_devSelect_currentIndexChanged(int index) { // 清空显示的数据 + if (ui->scrollContents->children().size() < 1) + { + return ; + } + for (int i = 0; i < 16; i++) { // 获取对应的通道BOX diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 52a8d88..bf1cb57 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -9,6 +9,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" #include "common/HttpRequestController.h" +#include "common/HttpServer.h" namespace Ui { class PhaseWindow; diff --git a/PhaseCompAcq/common/HttpServer.cpp b/PhaseCompAcq/common/HttpServer.cpp new file mode 100644 index 0000000..c1a1b48 --- /dev/null +++ b/PhaseCompAcq/common/HttpServer.cpp @@ -0,0 +1,68 @@ +#include "HttpServer.h" + +HttpServer::HttpServer(QObject *parent) : QObject(parent) +{ + httpServer = new QTcpServer(this); + httpServer->setMaxPendingConnections(1024); + QObject::connect(httpServer, &QTcpServer::newConnection, this, &HttpServer::newConnection); +} + +HttpServer::~HttpServer() +{ + +} + +HttpServer &HttpServer::instance() +{ + static HttpServer obj; + return obj; +} + +void HttpServer::run(const QHostAddress &address, const quint16 &port) +{ + httpServer->listen(address, port); +} + +void HttpServer::newConnection() +{ + QTcpSocket * m_socket = httpServer->nextPendingConnection(); + QObject::connect(m_socket, &QTcpSocket::readyRead, this, &HttpServer::readyRead); +} + +void HttpServer::readyRead() +{ + QTcpSocket * socket = qobject_cast(sender()); + if(socket) { + QByteArray request = socket->readAll(); + + qDebug() << "Request Data:" << request; + + QByteArray response = this->buildHeartBeatResponse().toUtf8(); + + QString http = "HTTP/1.1 200 OK\r\n"; + http += "Server: qt\r\n"; + http += "Content-Type: application/json;charset=utf-8\r\n"; + http += "Connection: keep-alive\r\n"; + http += QString("Content-Length: %1\r\n\r\n").arg(QString::number(response.size())); + + socket->write(http.toUtf8()); + socket->write(response); + socket->flush(); + socket->waitForBytesWritten(http.size() + response.size()); + socket->close(); + } +} + +QString HttpServer::buildHeartBeatResponse() +{ + QJsonObject resp; + resp.insert("code", 200); + resp.insert("success", true); + + QJsonObject data; + data.insert("status", "1"); + data.insert("remarks", ""); + resp.insert("data", data); + + return QString(QJsonDocument(resp).toJson(QJsonDocument::Compact)); +} diff --git a/PhaseCompAcq/common/HttpServer.h b/PhaseCompAcq/common/HttpServer.h new file mode 100644 index 0000000..7499987 --- /dev/null +++ b/PhaseCompAcq/common/HttpServer.h @@ -0,0 +1,33 @@ +#ifndef HTTPSERVER_H +#define HTTPSERVER_H + +#include +#include +#include +#include + +class HttpServer : public QObject +{ + Q_OBJECT +public: + static HttpServer &instance(); + void run(const QHostAddress &address = QHostAddress::Any, const quint16 &port = 5905); + +private: + explicit HttpServer(QObject *parent = nullptr); + ~HttpServer(); + Q_DISABLE_COPY(HttpServer) + + QTcpServer * httpServer; + + QString buildHeartBeatResponse(); + +signals: + +public slots: +private slots: + void newConnection(); + void readyRead(); +}; + +#endif // HTTPSERVER_H diff --git a/PhaseCompAcq/common/common.pri b/PhaseCompAcq/common/common.pri index 6280f64..b80a640 100644 --- a/PhaseCompAcq/common/common.pri +++ b/PhaseCompAcq/common/common.pri @@ -7,6 +7,7 @@ SOURCES += $$PWD/utils/HttpRequestUtil.cpp SOURCES += $$PWD/utils/MD5.cpp SOURCES += $$PWD/HttpRequestController.cpp +SOURCES += $$PWD/HttpServer.cpp HEADERS += $$PWD/utils/SettingConfig.h HEADERS += $$PWD/utils/QByteUtil.h @@ -17,4 +18,5 @@ HEADERS += $$PWD/utils/DefHead.h HEADERS += $$PWD/utils/MD5.h HEADERS += $$PWD/HttpRequestController.h +HEADERS += $$PWD/HttpServer.h HEADERS += $$PWD/ConstCache.h diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp index 035040f..dbe949b 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp @@ -42,9 +42,9 @@ void QSerialPortUtil::sendData(QByteArray data) { + std::cout << data.toStdString() << std::endl; if (this->open == true) { - std::cout << data.toStdString() << std::endl; serial.write(data); } } diff --git a/PhaseCompAcq/common/utils/SettingConfig.cpp b/PhaseCompAcq/common/utils/SettingConfig.cpp index 6273b6d..e984099 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.cpp +++ b/PhaseCompAcq/common/utils/SettingConfig.cpp @@ -20,6 +20,7 @@ DEV_TYPES = getProperty("client", "devTypes").toString(); SYSTEM = getProperty("client", "system").toString(); WORK_TYPE = getProperty("client", "workMode").toString(); + SERVER_PORT = getProperty("client", "serverPort").toInt(); BASE_URL = getProperty("http", "baseUrl").toString(); diff --git a/PhaseCompAcq/common/utils/SettingConfig.h b/PhaseCompAcq/common/utils/SettingConfig.h index 6add4de..3423d16 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.h +++ b/PhaseCompAcq/common/utils/SettingConfig.h @@ -42,6 +42,7 @@ QString DEV_TYPES; QString SYSTEM; QString WORK_TYPE; + quint16 SERVER_PORT; QString BASE_URL; diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 98422c1..dd69711 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -89,6 +89,8 @@ // 6. 绘制一个设备的多个通道数据面板 this->generateWidgetForDevice(); + + HttpServer::instance().run(QHostAddress::Any, SettingConfig::getInstance().SERVER_PORT); } PhaseWindow::~PhaseWindow() @@ -232,6 +234,11 @@ void PhaseWindow::on_devSelect_currentIndexChanged(int index) { // 清空显示的数据 + if (ui->scrollContents->children().size() < 1) + { + return ; + } + for (int i = 0; i < 16; i++) { // 获取对应的通道BOX diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 52a8d88..bf1cb57 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -9,6 +9,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" #include "common/HttpRequestController.h" +#include "common/HttpServer.h" namespace Ui { class PhaseWindow; diff --git a/PhaseCompAcq/common/HttpServer.cpp b/PhaseCompAcq/common/HttpServer.cpp new file mode 100644 index 0000000..c1a1b48 --- /dev/null +++ b/PhaseCompAcq/common/HttpServer.cpp @@ -0,0 +1,68 @@ +#include "HttpServer.h" + +HttpServer::HttpServer(QObject *parent) : QObject(parent) +{ + httpServer = new QTcpServer(this); + httpServer->setMaxPendingConnections(1024); + QObject::connect(httpServer, &QTcpServer::newConnection, this, &HttpServer::newConnection); +} + +HttpServer::~HttpServer() +{ + +} + +HttpServer &HttpServer::instance() +{ + static HttpServer obj; + return obj; +} + +void HttpServer::run(const QHostAddress &address, const quint16 &port) +{ + httpServer->listen(address, port); +} + +void HttpServer::newConnection() +{ + QTcpSocket * m_socket = httpServer->nextPendingConnection(); + QObject::connect(m_socket, &QTcpSocket::readyRead, this, &HttpServer::readyRead); +} + +void HttpServer::readyRead() +{ + QTcpSocket * socket = qobject_cast(sender()); + if(socket) { + QByteArray request = socket->readAll(); + + qDebug() << "Request Data:" << request; + + QByteArray response = this->buildHeartBeatResponse().toUtf8(); + + QString http = "HTTP/1.1 200 OK\r\n"; + http += "Server: qt\r\n"; + http += "Content-Type: application/json;charset=utf-8\r\n"; + http += "Connection: keep-alive\r\n"; + http += QString("Content-Length: %1\r\n\r\n").arg(QString::number(response.size())); + + socket->write(http.toUtf8()); + socket->write(response); + socket->flush(); + socket->waitForBytesWritten(http.size() + response.size()); + socket->close(); + } +} + +QString HttpServer::buildHeartBeatResponse() +{ + QJsonObject resp; + resp.insert("code", 200); + resp.insert("success", true); + + QJsonObject data; + data.insert("status", "1"); + data.insert("remarks", ""); + resp.insert("data", data); + + return QString(QJsonDocument(resp).toJson(QJsonDocument::Compact)); +} diff --git a/PhaseCompAcq/common/HttpServer.h b/PhaseCompAcq/common/HttpServer.h new file mode 100644 index 0000000..7499987 --- /dev/null +++ b/PhaseCompAcq/common/HttpServer.h @@ -0,0 +1,33 @@ +#ifndef HTTPSERVER_H +#define HTTPSERVER_H + +#include +#include +#include +#include + +class HttpServer : public QObject +{ + Q_OBJECT +public: + static HttpServer &instance(); + void run(const QHostAddress &address = QHostAddress::Any, const quint16 &port = 5905); + +private: + explicit HttpServer(QObject *parent = nullptr); + ~HttpServer(); + Q_DISABLE_COPY(HttpServer) + + QTcpServer * httpServer; + + QString buildHeartBeatResponse(); + +signals: + +public slots: +private slots: + void newConnection(); + void readyRead(); +}; + +#endif // HTTPSERVER_H diff --git a/PhaseCompAcq/common/common.pri b/PhaseCompAcq/common/common.pri index 6280f64..b80a640 100644 --- a/PhaseCompAcq/common/common.pri +++ b/PhaseCompAcq/common/common.pri @@ -7,6 +7,7 @@ SOURCES += $$PWD/utils/HttpRequestUtil.cpp SOURCES += $$PWD/utils/MD5.cpp SOURCES += $$PWD/HttpRequestController.cpp +SOURCES += $$PWD/HttpServer.cpp HEADERS += $$PWD/utils/SettingConfig.h HEADERS += $$PWD/utils/QByteUtil.h @@ -17,4 +18,5 @@ HEADERS += $$PWD/utils/DefHead.h HEADERS += $$PWD/utils/MD5.h HEADERS += $$PWD/HttpRequestController.h +HEADERS += $$PWD/HttpServer.h HEADERS += $$PWD/ConstCache.h diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp index 035040f..dbe949b 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp @@ -42,9 +42,9 @@ void QSerialPortUtil::sendData(QByteArray data) { + std::cout << data.toStdString() << std::endl; if (this->open == true) { - std::cout << data.toStdString() << std::endl; serial.write(data); } } diff --git a/PhaseCompAcq/common/utils/SettingConfig.cpp b/PhaseCompAcq/common/utils/SettingConfig.cpp index 6273b6d..e984099 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.cpp +++ b/PhaseCompAcq/common/utils/SettingConfig.cpp @@ -20,6 +20,7 @@ DEV_TYPES = getProperty("client", "devTypes").toString(); SYSTEM = getProperty("client", "system").toString(); WORK_TYPE = getProperty("client", "workMode").toString(); + SERVER_PORT = getProperty("client", "serverPort").toInt(); BASE_URL = getProperty("http", "baseUrl").toString(); diff --git a/PhaseCompAcq/common/utils/SettingConfig.h b/PhaseCompAcq/common/utils/SettingConfig.h index 6add4de..3423d16 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.h +++ b/PhaseCompAcq/common/utils/SettingConfig.h @@ -42,6 +42,7 @@ QString DEV_TYPES; QString SYSTEM; QString WORK_TYPE; + quint16 SERVER_PORT; QString BASE_URL; diff --git a/PhaseCompAcq/conf/config.ini b/PhaseCompAcq/conf/config.ini index 5aed46c..05addaa 100644 --- a/PhaseCompAcq/conf/config.ini +++ b/PhaseCompAcq/conf/config.ini @@ -16,6 +16,7 @@ devTypes="02" system="phase" workMode="mock" +serverPort=5905 [http] baseUrl="http://111.198.10.15:11410"