新增产测与服务器的HTTP连接及交互功能
This commit is contained in:
@@ -4,19 +4,24 @@
|
||||
#include "ImageEnrollWindow.h"
|
||||
#include "PasswordEnrollWindow.h"
|
||||
|
||||
ClientHandler::ClientHandler(QTcpSocket* socket, QJsonArray frontBoardOneClickTest, QJsonArray frontBoardTest, QJsonArray frontBoardFuncConfig,
|
||||
QJsonArray frontBoardDevInfoJson, QJsonArray frontBoardLicenseJson, QJsonArray backBoardDevInfoJson,
|
||||
ClientHandler::ClientHandler(QTcpSocket* socket,
|
||||
QJsonArray frontBoardOneClickTest, QJsonArray frontBoardTest, QJsonArray frontBoardFuncConfig,
|
||||
QJsonArray frontBoardDevInfoJson, QJsonArray frontBoardLicenseJson,
|
||||
QJsonArray backBoardOneClickTest, QJsonArray backBoardTest, QJsonArray backBoardFuncConfig,
|
||||
QJsonArray backBoardDevInfoJson,
|
||||
QJsonArray getPicJson, QJsonArray getVideoJson, int clientId, QObject* parent)
|
||||
: QObject(parent), socket(socket),
|
||||
frontBoardOneClickTest(frontBoardOneClickTest), frontBoardTest(frontBoardTest),
|
||||
frontBoardFuncConfig(frontBoardFuncConfig), frontBoardDevInfoJson(frontBoardDevInfoJson),
|
||||
frontBoardLicenseJson(frontBoardLicenseJson),
|
||||
backBoardDevInfoJson(backBoardDevInfoJson), getPicJson(getPicJson),
|
||||
getVideoJson(getVideoJson), currentItemIndex(0), clientId(clientId),
|
||||
backBoardOneClickTest(backBoardOneClickTest), backBoardTest(backBoardTest),
|
||||
backBoardFuncConfig(backBoardFuncConfig), backBoardDevInfoJson(backBoardDevInfoJson),
|
||||
getPicJson(getPicJson), getVideoJson(getVideoJson),
|
||||
currentItemIndex(0), clientId(clientId),
|
||||
isManualSend(false), isSingleSend(false), isClickedSend(false), size(0),
|
||||
isFirstDataReceived(true), processDataFunction(nullptr),
|
||||
isDataStuck(false), dataProcessingActive(false), isRecvVideoData(false),
|
||||
currentFrontBoardIndex(0),
|
||||
isDataStuck(false), dataProcessingActive(false), isRecvVideoData(true),
|
||||
currentFrontBoardIndex(0), currentLicenseIndex(0), currentUuidIndex(0),
|
||||
currentBackBoardIndex(0)
|
||||
{
|
||||
connect(socket, &QTcpSocket::readyRead, this, &ClientHandler::onDataReceived);
|
||||
@@ -24,30 +29,12 @@ ClientHandler::ClientHandler(QTcpSocket* socket, QJsonArray frontBoardOneClickTe
|
||||
|
||||
qint64 bufferSize = socket->socketOption(QAbstractSocket::ReceiveBufferSizeSocketOption).toLongLong();
|
||||
|
||||
/*connect(socket, QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::error),
|
||||
this, &ClientHandler::onSocketError);*/
|
||||
}
|
||||
|
||||
ClientHandler::~ClientHandler() {
|
||||
qDebug() << "ClientHandler destroyed for clientId:" << clientId;
|
||||
if (readTimer) {
|
||||
readTimer->stop();
|
||||
delete readTimer;
|
||||
}
|
||||
if (threadStatusTimer) {
|
||||
threadStatusTimer->stop();
|
||||
delete threadStatusTimer;
|
||||
}
|
||||
if (connectionCheckTimer) {
|
||||
connectionCheckTimer->stop();
|
||||
delete connectionCheckTimer;
|
||||
}
|
||||
}
|
||||
|
||||
//void ClientHandler::onSocketError(QAbstractSocket::SocketError socketError) {
|
||||
// qWarning() << "-------------------------------Socket error:" << socketError << "clientId:" << clientId;
|
||||
//}
|
||||
|
||||
void ClientHandler::startReadVideoDataTimer()
|
||||
{
|
||||
emit startReadTimer(clientId);
|
||||
@@ -127,9 +114,17 @@ void ClientHandler::start()
|
||||
sendNextItem();
|
||||
}
|
||||
|
||||
void ClientHandler::sendDataToClient(const QByteArray& data)
|
||||
{
|
||||
emit sendData(data);
|
||||
}
|
||||
|
||||
void ClientHandler::sendJsonItem(const QJsonArray& jsonArray, int itemIndex, const QString text, const QString& itemType)
|
||||
{
|
||||
QMutexLocker locker(&mutex);
|
||||
QMutexLocker locker(&mutex);
|
||||
if (itemType == "License") {
|
||||
|
||||
}
|
||||
isSingleSend = true;
|
||||
isClickedSend = true;
|
||||
if (itemType == "handleVideo") {
|
||||
@@ -147,6 +142,7 @@ void ClientHandler::sendJsonItem(const QJsonArray& jsonArray, int itemIndex, con
|
||||
QJsonObject currentItem = jsonArray[itemIndex].toObject();
|
||||
if (!text.isEmpty() && currentItem.contains("val")) {
|
||||
QJsonValue originalValue = currentItem["val"];
|
||||
qDebug() << "text:" << text;
|
||||
if (originalValue.isDouble()) {
|
||||
bool ok;
|
||||
double doubleValue = text.toDouble(&ok);
|
||||
@@ -162,7 +158,8 @@ void ClientHandler::sendJsonItem(const QJsonArray& jsonArray, int itemIndex, con
|
||||
}
|
||||
}
|
||||
QString itemData = QJsonDocument(currentItem).toJson();
|
||||
emit sendData(itemData.toUtf8());
|
||||
//emit sendData(itemData.toUtf8());
|
||||
sendDataToSomeClient(controlClientId, itemData.toUtf8());
|
||||
|
||||
// 设置超时处理
|
||||
if (currentItem.contains("timeout")) {
|
||||
@@ -194,7 +191,7 @@ void ClientHandler::sendGetVideoItem(int itemIndex, int video_flag)
|
||||
}
|
||||
|
||||
// 发送License处理按键
|
||||
void ClientHandler::sendLicenseItem(int itemIndex)
|
||||
void ClientHandler::sendLicenseItem(int itemIndex, const QString text)
|
||||
{
|
||||
if (itemIndex < 0 || itemIndex >= frontBoardLicenseJson.size()) {
|
||||
qDebug() << "Invalid itemIndex";
|
||||
@@ -205,23 +202,34 @@ void ClientHandler::sendLicenseItem(int itemIndex)
|
||||
if (label == "write_license") {
|
||||
LicenseConfirmWindow dialog("你确定要发送此授权项吗?");
|
||||
if (dialog.exec() == QDialog::Accepted) {
|
||||
sendJsonItem(frontBoardLicenseJson, itemIndex, "", "License");
|
||||
sendJsonItem(frontBoardLicenseJson, itemIndex, text, "License");
|
||||
}
|
||||
}
|
||||
else {
|
||||
sendJsonItem(frontBoardLicenseJson, itemIndex, "", "License");
|
||||
sendJsonItem(frontBoardLicenseJson, itemIndex, text, "License");
|
||||
}
|
||||
}
|
||||
|
||||
void ClientHandler::sendUuidItem(int itemIndex, const QString text)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// 发送单独一个功能配置 JSON 项目
|
||||
void ClientHandler::sendFuncItem(int itemIndex, const QString text)
|
||||
void ClientHandler::sendFrontFuncItem(int itemIndex, const QString text)
|
||||
{
|
||||
//qDebug() << "sendFuncItem Text:" << text;
|
||||
sendJsonItem(frontBoardFuncConfig, itemIndex, text, "func");
|
||||
}
|
||||
|
||||
void ClientHandler::sendBackFuncItem(int itemIndex, const QString text)
|
||||
{
|
||||
//qDebug() << "sendFuncItem Text:" << text;
|
||||
sendJsonItem(backBoardFuncConfig, itemIndex, text, "func");
|
||||
}
|
||||
|
||||
// 发送单独一个测试配置 JSON 项目
|
||||
void ClientHandler::sendItem(int itemIndex)
|
||||
void ClientHandler::sendFrontItem(int itemIndex)
|
||||
{
|
||||
QString text = "";
|
||||
QJsonObject currentItem = frontBoardTest[itemIndex].toObject();
|
||||
@@ -262,6 +270,47 @@ void ClientHandler::sendItem(int itemIndex)
|
||||
sendJsonItem(frontBoardTest, itemIndex, text, "test");
|
||||
}
|
||||
|
||||
void ClientHandler::sendBackItem(int itemIndex)
|
||||
{
|
||||
QString text = "";
|
||||
QJsonObject currentItem = backBoardTest[itemIndex].toObject();
|
||||
if (currentItem.contains("cmd") && currentItem["cmd"].toString() == "IMG_ENROLL") {
|
||||
ImageEnrollWindow dialog;
|
||||
if (dialog.exec() == QDialog::Accepted) {
|
||||
text = dialog.getFilePath();
|
||||
QByteArray imageData = dialog.getImageData();
|
||||
if (!imageData.isEmpty()) {
|
||||
text = QString::fromUtf8(imageData);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (currentItem.contains("cmd") && currentItem["cmd"].toString() == "DEL_USER") {
|
||||
DelUserWindow dialog;
|
||||
if (dialog.exec() == QDialog::Accepted) {
|
||||
QString userInput = dialog.getUserInput();
|
||||
if (!userInput.isEmpty() && currentItem.contains("val")) {
|
||||
text = userInput;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (currentItem.contains("cmd") && currentItem["cmd"].toString() == "PASSWD_ENROLL") {
|
||||
PasswordEnrollWindow dialog;
|
||||
if (dialog.exec() == QDialog::Accepted) {
|
||||
text = dialog.getPassword();
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
sendJsonItem(backBoardTest, itemIndex, text, "test");
|
||||
}
|
||||
|
||||
void ClientHandler::sendDevInfoJsonItem(const QJsonObject& jsonItem, int itemIndex)
|
||||
{
|
||||
QMutexLocker locker(&mutex);
|
||||
@@ -270,7 +319,8 @@ void ClientHandler::sendDevInfoJsonItem(const QJsonObject& jsonItem, int itemInd
|
||||
qDebug() << "jsonItem.size():" << jsonItem.size();
|
||||
|
||||
QString itemData = QJsonDocument(jsonItem).toJson();
|
||||
emit sendData(itemData.toUtf8());
|
||||
//emit sendData(itemData.toUtf8());
|
||||
sendDataToSomeClient(controlClientId, itemData.toUtf8());
|
||||
|
||||
if (jsonItem.contains("timeout")) {
|
||||
int timeout = jsonItem.value("timeout").toInt();
|
||||
@@ -281,9 +331,16 @@ void ClientHandler::sendDevInfoJsonItem(const QJsonObject& jsonItem, int itemInd
|
||||
else {
|
||||
// 如果没有 timeout 字段,则不设置超时处理,一直等待数据接收
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//void ClientHandler::sendLicenseItem()
|
||||
//{
|
||||
// if (currentLicenseIndex < frontBoardLicenseJson.size()) {
|
||||
//
|
||||
// currentLicenseIndex ++;
|
||||
// }
|
||||
//}
|
||||
|
||||
void ClientHandler::sendDevInfoItem()
|
||||
{
|
||||
// 前板
|
||||
@@ -328,7 +385,8 @@ void ClientHandler::sendNextItem()
|
||||
}
|
||||
}
|
||||
}
|
||||
emit sendData(itemData.toUtf8());
|
||||
//emit sendData(itemData.toUtf8());
|
||||
sendDataToSomeClient(controlClientId, itemData.toUtf8());
|
||||
|
||||
if (currentItem.contains("timeout")) {
|
||||
int timeout = currentItem.value("timeout").toInt();
|
||||
@@ -388,6 +446,7 @@ bool ClientHandler::RNDISFirstData(QByteArray& data) {
|
||||
data = data.mid(11);
|
||||
(this->*processDataFunction)(data);
|
||||
setThreadPriority(QThread::HighPriority);
|
||||
preVideoClientId = clientId;
|
||||
break;
|
||||
case 0x03:
|
||||
processDataFunction = &ClientHandler::RNDISClient3Data;
|
||||
@@ -396,8 +455,9 @@ bool ClientHandler::RNDISFirstData(QByteArray& data) {
|
||||
break;
|
||||
case 0x04:
|
||||
processDataFunction = &ClientHandler::RNDISClient4Data;
|
||||
setThreadPriority(QThread::LowestPriority);
|
||||
setThreadPriority(QThread::LowPriority);
|
||||
socket->setReadBufferSize(20 * 1024);
|
||||
controlClientId = clientId;
|
||||
break;
|
||||
default:
|
||||
qWarning() << "Unhandled client type:" << clientType;
|
||||
@@ -479,9 +539,13 @@ void ClientHandler::onDataReceived()
|
||||
isSingleSend = false; // 重置标志
|
||||
isClickedSend = false;
|
||||
}
|
||||
|
||||
else if (isPowerOnSend && currentFrontBoardIndex < frontBoardDevInfoJson.size()) {
|
||||
sendDevInfoItem();
|
||||
}
|
||||
else if (isPowerOnSend && currentBackBoardIndex < backBoardDevInfoJson.size()) {
|
||||
sendDevInfoItem();
|
||||
}
|
||||
/*else if (isPowerOnSend) {
|
||||
qDebug() << "All items processed in onDataReceived.";
|
||||
emit allItemsProcessed(getClientAddress(), currentFrontBoardIndex);
|
||||
@@ -502,6 +566,7 @@ void ClientHandler::onDataReceived()
|
||||
// 接收视频流数据 isRecvVideoData 置 0
|
||||
else if (isRecvVideoData && (!dataProcessingActive)) {
|
||||
dataProcessingActive = true;
|
||||
isRecvVideoData = false;
|
||||
QTimer::singleShot(0, this, &ClientHandler::processPendingData);
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -31,7 +31,8 @@ class ClientHandler : public QObject, public QRunnable
|
||||
public:
|
||||
explicit ClientHandler(QTcpSocket* socket, QJsonArray frontBoardOneClickTest, QJsonArray frontBoardTest,
|
||||
QJsonArray frontBoardFuncConfig, QJsonArray frontBoardDevInfoJson, QJsonArray frontBoardLicenseJson,
|
||||
QJsonArray backBoardDevInfoJson, QJsonArray getPicJson, QJsonArray getVideoJson,
|
||||
QJsonArray backBoardOneClickTest, QJsonArray backBoardTest, QJsonArray backBoardFuncConfig, QJsonArray backBoardDevInfoJson,
|
||||
QJsonArray getPicJson, QJsonArray getVideoJson,
|
||||
int clientId, QObject* parent = nullptr);
|
||||
|
||||
~ClientHandler();
|
||||
@@ -54,13 +55,17 @@ public:
|
||||
// 发送拉视频流按键
|
||||
void sendGetVideoItem(int itemIndex, int video_flag);
|
||||
// 发送License处理按键
|
||||
void sendLicenseItem(int itemIndex);
|
||||
void sendLicenseItem(int itemIndex, const QString text);
|
||||
void sendUuidItem(int itemIndex, const QString text);
|
||||
// 发送下一个功能配置 JSON 项目
|
||||
void sendFuncItem(int itemIndex, QString text);
|
||||
void sendFrontFuncItem(int itemIndex, QString text);
|
||||
void sendBackFuncItem(int itemIndex, QString text);
|
||||
// 发送下一个 JSON 项目
|
||||
void sendNextItem();
|
||||
// 处理发送单独指令
|
||||
void sendItem(int itemIndex);
|
||||
void sendFrontItem(int itemIndex);
|
||||
void sendBackItem(int itemIndex);
|
||||
//void sendLicenseItem();
|
||||
// 获取客户端的 ID
|
||||
int getClientId() const { return clientId; }
|
||||
// 获取当前客户端的地址
|
||||
@@ -75,7 +80,7 @@ public:
|
||||
QString getCurrentFuncItemLable() const;
|
||||
// 重置索引
|
||||
void resetCurrentItemIndex();
|
||||
// 处理 RingBuffer 中的数据
|
||||
void sendDataToClient(const QByteArray& data);
|
||||
//void processRingBufferData();
|
||||
void setThreadPriority(QThread::Priority priority);
|
||||
|
||||
@@ -98,6 +103,7 @@ signals:
|
||||
void selectClientDisconnected(int clientId);
|
||||
void startReadTimer(int clientId);
|
||||
void stopReadTimer(int clientId);
|
||||
void sendDataToSomeClient(int clientId, const QByteArray& data);
|
||||
|
||||
public slots:
|
||||
// 处理数据接收
|
||||
@@ -120,6 +126,9 @@ private:
|
||||
QJsonArray frontBoardFuncConfig; // 前板功能配置参数 JSON
|
||||
QJsonArray frontBoardDevInfoJson; // 前板设备信息参数 JSON
|
||||
QJsonArray frontBoardLicenseJson; // 前板license信息 JSON
|
||||
QJsonArray backBoardOneClickTest; // 后板一键功能测试 JSON
|
||||
QJsonArray backBoardTest; // 后板单项测试 JSON
|
||||
QJsonArray backBoardFuncConfig; // 后板功能配置参数 JSON
|
||||
QJsonArray backBoardDevInfoJson; // 后板设备信息参数 JSON
|
||||
QJsonArray jsonConfig; // 测试区 JSON 配置
|
||||
QJsonArray funcJsonConfig; // 功能区 JSON 配置
|
||||
@@ -129,6 +138,8 @@ private:
|
||||
int currentItemIndex; // 当前处理的测试 JSON 项目索引
|
||||
int currentFuncItemIndex; // 当前处理的功能 JSON 项目索引
|
||||
int clientId; // 新增的客户端编号
|
||||
int preVideoClientId = 0;
|
||||
int controlClientId = 1;
|
||||
int itemsProcessedCount; // 跟踪处理项目的数量
|
||||
QJsonObject currentItem; // 测试 json文件中当前发送的项
|
||||
QJsonObject currentFuncItem; // 功能 json文件中当前发送的项
|
||||
@@ -143,6 +154,8 @@ private:
|
||||
int size;
|
||||
int currentFrontBoardIndex; // 当前发送的前板设备信息项的索引
|
||||
int currentBackBoardIndex; // 当前发送的后板设备信息项的索引
|
||||
int currentLicenseIndex;
|
||||
int currentUuidIndex;
|
||||
|
||||
void checkThreadStatus();
|
||||
bool isFirstDataReceived;
|
||||
@@ -157,10 +170,6 @@ private:
|
||||
void sendDevInfoJsonItem(const QJsonObject& jsonItem, int itemIndex);
|
||||
//void sendDevInfoItem();
|
||||
|
||||
QTimer* readTimer;
|
||||
QTimer* threadStatusTimer;
|
||||
QTimer* connectionCheckTimer;
|
||||
|
||||
bool isDataStuck;
|
||||
bool dataProcessingActive;
|
||||
};
|
||||
|
||||
38
FactoryTestTool/SourceCode/Network/httpClient.cpp
Normal file
38
FactoryTestTool/SourceCode/Network/httpClient.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
// httpClient.cpp
|
||||
#include <QDebug>
|
||||
#include "httpClient.h"
|
||||
|
||||
HttpClient::HttpClient(QObject* parent)
|
||||
: QObject(parent),
|
||||
networkManager(new QNetworkAccessManager(this))
|
||||
{
|
||||
connect(networkManager, &QNetworkAccessManager::finished, this, &HttpClient::onNetworkReplyFinished);
|
||||
}
|
||||
|
||||
void HttpClient::sendPostRequest(const QNetworkRequest& request, const QByteArray& jsonData)
|
||||
{
|
||||
qDebug() << "jsonData" << jsonData.data();
|
||||
networkManager->post(request, jsonData);
|
||||
}
|
||||
|
||||
void HttpClient::sendGetRequest(const QNetworkRequest& request)
|
||||
{
|
||||
networkManager->get(request);
|
||||
}
|
||||
|
||||
void HttpClient::onNetworkReplyFinished(QNetworkReply* reply)
|
||||
{
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
QByteArray responseData = reply->readAll();
|
||||
QJsonDocument jsonResponse = QJsonDocument::fromJson(responseData);
|
||||
QJsonObject jsonObject = jsonResponse.object();
|
||||
qDebug() << "jsonObject: " << jsonObject;
|
||||
emit requestFinished(jsonObject);
|
||||
}
|
||||
else {
|
||||
qDebug() << "reply->error(): " << reply->error();
|
||||
qDebug() << "errorString: " << reply->errorString();
|
||||
emit requestError(reply->errorString());
|
||||
}
|
||||
reply->deleteLater();
|
||||
}
|
||||
34
FactoryTestTool/SourceCode/Network/httpClient.h
Normal file
34
FactoryTestTool/SourceCode/Network/httpClient.h
Normal file
@@ -0,0 +1,34 @@
|
||||
// httpClient.h
|
||||
#ifndef HTTPCLIENT_H
|
||||
#define HTTPCLIENT_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QUrlQuery>
|
||||
|
||||
|
||||
class HttpClient : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit HttpClient(QObject* parent = nullptr);
|
||||
void sendPostRequest(const QNetworkRequest& request, const QByteArray& jsonData);
|
||||
void sendGetRequest(const QNetworkRequest& request);
|
||||
|
||||
signals:
|
||||
void requestFinished(const QJsonObject& response);
|
||||
void requestError(const QString& errorString);
|
||||
|
||||
private slots:
|
||||
void onNetworkReplyFinished(QNetworkReply* reply);
|
||||
|
||||
private:
|
||||
QNetworkAccessManager* networkManager;
|
||||
};
|
||||
|
||||
#endif // HTTPCLIENT_H
|
||||
Reference in New Issue
Block a user