新增产测与服务器的HTTP连接及交互功能

This commit is contained in:
2024-08-26 18:33:55 +08:00
parent 0a5b0db9a5
commit 07ab6b9608
24 changed files with 2825 additions and 1654 deletions

View File

@@ -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 {