203 lines
8.8 KiB
C++
203 lines
8.8 KiB
C++
// ClientHandler.h
|
|
#ifndef CLIENTHANDLER_H
|
|
#define CLIENTHANDLER_H
|
|
|
|
//#include "RingBuffer.h"
|
|
|
|
#include <QObject>
|
|
#include <QTcpSocket>
|
|
#include <QJsonArray>
|
|
#include <QMutex>
|
|
#include <QRunnable>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QTimer>
|
|
#include <QDebug>
|
|
#include <QHostAddress>
|
|
#include <QBuffer>
|
|
#include <QByteArray>
|
|
#include <QBuffer>
|
|
#include <QThread>
|
|
#include <QElapsedTimer>
|
|
#include <QStorageInfo>
|
|
#include <QMessageBox>
|
|
|
|
#include "DelUserWindows.h"
|
|
|
|
#define TEST_TCP_MOVE_TO_MAIN 0
|
|
|
|
class ClientHandler : public QObject, public QRunnable
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
#if TEST_TCP_MOVE_TO_MAIN
|
|
explicit ClientHandler(QTcpSocket* socket, QJsonArray frontBoardOneClickTest, QJsonArray frontBoardTest,
|
|
QJsonArray frontBoardFuncConfig, QJsonArray frontBoardDevInfoJson, QJsonArray frontBoardLicenseJson,
|
|
QJsonArray backBoardOneClickTest, QJsonArray backBoardTest, QJsonArray backBoardFuncConfig, QJsonArray backBoardDevInfoJson,
|
|
QJsonArray backBoardUuidJson, QJsonArray getPicJson, QJsonArray getVideoJson,
|
|
int clientId, int isBackBoardOrAllBoard, QObject* parent = nullptr);
|
|
#else
|
|
explicit ClientHandler(QJsonArray frontBoardOneClickTest, QJsonArray frontBoardTest,
|
|
QJsonArray frontBoardFuncConfig, QJsonArray frontBoardDevInfoJson, QJsonArray frontBoardLicenseJson,
|
|
QJsonArray backBoardOneClickTest, QJsonArray backBoardTest, QJsonArray backBoardFuncConfig, QJsonArray backBoardDevInfoJson,
|
|
QJsonArray backBoardUuidJson, QJsonArray getPicJson, QJsonArray getVideoJson,
|
|
int clientId, int isBackBoardOrAllBoard, QObject* parent = nullptr);
|
|
#endif
|
|
~ClientHandler();
|
|
int preVideoClientId = 0;
|
|
int controlClientId = 0;
|
|
void sendDevInfoItem();
|
|
bool isData_Stuck() const;
|
|
QTcpSocket* getSocket() const { return socket; }
|
|
|
|
#if !TEST_TCP_MOVE_TO_MAIN
|
|
void ClientHandler::initialize(QTcpSocket* socket);
|
|
#endif
|
|
|
|
// QRunnable 的虚函数,在线程池中执行
|
|
void run() override;
|
|
// 开始处理客户端
|
|
void start();
|
|
void sendJsonItem(const QJsonArray& jsonArray, int itemIndex, const QString text, const QString& itemType);
|
|
// 发送获取设备信息按键
|
|
void sendGetDevInfoItem(int itemIndex);
|
|
// 发送取图按键
|
|
void sendGetPicItem(int itemIndex, int GetPicCamIndex);
|
|
// 发送拉视频流按键
|
|
void sendGetVideoItem(int itemIndex, int GetVideoCamIndex);
|
|
// 发送License处理按键
|
|
void sendLicenseItem(int itemIndex, const QString text);
|
|
void sendUuidItem(int itemIndex, const QString text);
|
|
// 发送下一个功能配置 JSON 项目
|
|
void sendFrontFuncItem(int itemIndex, QString text);
|
|
void sendBackFuncItem(int itemIndex, QString text);
|
|
// 发送下一个 JSON 项目
|
|
void sendNextItem();
|
|
// 处理发送单独指令
|
|
void sendFrontItem(int itemIndex);
|
|
void sendBackItem(int itemIndex);
|
|
//void sendLicenseItem();
|
|
// 获取客户端的 ID
|
|
int getClientId() const { return clientId; }
|
|
// 获取当前客户端的地址
|
|
QString getClientAddress() const;
|
|
// 获取json文件中当前发送的 data 字段项
|
|
QString getCurrentItemData() const;
|
|
// 获取json文件中当前发送的 lable 字段项
|
|
QString getCurrentItemLable() const;
|
|
// 获取功能 json文件中当前发送的 data 字段项
|
|
QString getCurrentFuncItemData() const;
|
|
// 获取功能 json文件中当前发送的 lable 字段项
|
|
QString getCurrentFuncItemLable() const;
|
|
// 重置索引
|
|
void resetCurrentItemIndex();
|
|
void sendDataToClient(const QByteArray& data);
|
|
//void processRingBufferData();
|
|
void setThreadPriority(QThread::Priority priority);
|
|
|
|
signals:
|
|
// 数据接收信号
|
|
void dataReceived(const QString& client, const QByteArray& data, unsigned char msg_id, int currentRecvItemIndex,
|
|
int currentRecvFuncItemIndex, const QString& itemData, const QString& funcItemData,
|
|
const QJsonArray& jsonArray, int itemJsonIndex);
|
|
void handleData(QByteArray& data);
|
|
// 数据发送信号
|
|
void sendData(const QByteArray& data, int isBoardType);
|
|
// 状态更新信号
|
|
void statusUpdated(const QString& client, int itemIndex, int funcItemIndex,
|
|
bool success, const QString& itemData, const QString& funcItemData,
|
|
const QJsonArray& jsonArray, int itemJsonIndex);
|
|
// 客户端断开连接信号
|
|
void clientDisconnected(ClientHandler* handler);
|
|
// json文件配置项发送完毕
|
|
void allItemsProcessed(const QString& client, int itemsProcessedCount);
|
|
// 通知主线程启动定时器
|
|
void startTimeout(int timeout);
|
|
void selectClientDisconnected(int client_Id);
|
|
void startReadTimer(int client_Id);
|
|
void stopReadTimer(int client_Id);
|
|
void sendDataToSomeClient(int client_Id, const QByteArray& data);
|
|
void openFocusWindowRequested(int itemIndex);
|
|
|
|
public slots:
|
|
#if !TEST_TCP_MOVE_TO_MAIN
|
|
void onSendData(const QByteArray& data, int isBoardType);
|
|
#endif
|
|
// 处理数据接收
|
|
void onDataReceived();
|
|
// 处理超时
|
|
void onTimeout();
|
|
// 处理客户端断开连接
|
|
void onDisconnected();
|
|
//void onSocketError(QAbstractSocket::SocketError socketError);
|
|
void onTimeoutRead();
|
|
void checkConnectionStatus(); // 定期检查连接状态
|
|
void processPendingData();
|
|
void startReadVideoDataTimer(int client_Id);
|
|
void stopReadVideoDataTimer(int client_Id);
|
|
|
|
private:
|
|
QTcpSocket* socket; // 客户端 socket
|
|
QJsonArray frontBoardOneClickTest; // 前板一键功能测试 JSON
|
|
QJsonArray frontBoardTest; // 前板单项测试 JSON
|
|
QJsonArray frontBoardFuncConfig; // 前板功能配置参数 JSON
|
|
QJsonArray frontBoardDevInfoJson; // 前板设备信息参数 JSON
|
|
QJsonArray frontBoardLicenseJson; // 前板license信息 JSON
|
|
QJsonArray backBoardOneClickTest; // 后板一键功能测试 JSON
|
|
QJsonArray backBoardTest; // 后板单项测试 JSON
|
|
QJsonArray backBoardFuncConfig; // 后板功能配置参数 JSON
|
|
QJsonArray backBoardDevInfoJson; // 后板设备信息参数 JSON
|
|
QJsonArray backBoardUuidJson; // 后板UUID和SN参数 JSON
|
|
QJsonArray jsonConfig; // 测试区 JSON 配置
|
|
QJsonArray funcJsonConfig; // 功能区 JSON 配置
|
|
QJsonArray getDevInfoJson; // 获取设备信息 JSON 配置
|
|
QJsonArray getPicJson; // 发送取图指令 JSON 配置
|
|
QJsonArray getVideoJson; // 发送拉视频指令 JSON 配置
|
|
QJsonArray currentJson; // 当前处理的json
|
|
int currentJsonItem = -1;
|
|
int currentItemIndex; // 当前处理的测试 JSON 项目索引
|
|
int currentFuncItemIndex; // 当前处理的功能 JSON 项目索引
|
|
int clientId; // 新增的客户端编号
|
|
int isBackBoardOrAllBoard;
|
|
|
|
|
|
int itemsProcessedCount; // 跟踪处理项目的数量
|
|
QJsonObject currentItem; // 测试 json文件中当前发送的项
|
|
QJsonObject currentFuncItem; // 功能 json文件中当前发送的项
|
|
QMutex mutex; // 互斥锁,用于线程安全
|
|
bool isManualSend; // 判断是否是手动触发的发送
|
|
bool isSingleSend; // 单独点击按键发送的标志
|
|
bool isClickedSend; // 点击按键发送的标志,没有点击不接收数据
|
|
bool isRecvVideoData;
|
|
bool isRecvImgData = false;
|
|
bool isStartVideo = false;
|
|
bool isPowerOnSend = false; // 上电发送设备信息
|
|
//QTimer* timeoutTimer; // 超时定时器
|
|
|
|
int size;
|
|
int currentFrontBoardIndex; // 当前发送的前板设备信息项的索引
|
|
int currentBackBoardIndex; // 当前发送的后板设备信息项的索引
|
|
int currentLicenseIndex;
|
|
int currentUuidIndex;
|
|
|
|
void checkThreadStatus();
|
|
bool isFirstDataReceived;
|
|
|
|
void (ClientHandler::* processDataFunction)(QByteArray&);
|
|
bool RNDISFirstData(QByteArray& data);
|
|
void RNDISClient1Data(QByteArray& data);
|
|
void RNDISClient2Data(QByteArray& data);
|
|
void RNDISClient3Data(QByteArray& data);
|
|
void RNDISClient4Data(QByteArray& data);
|
|
|
|
void sendDevInfoJsonItem(const QJsonObject& jsonItem, int itemIndex);
|
|
//void sendDevInfoItem();
|
|
|
|
bool isDataStuck;
|
|
bool dataProcessingActive;
|
|
};
|
|
|
|
#endif // CLIENTHANDLER_H
|
|
|