SL100_FactoryTestTool/FactoryTestTool/SourceCode/Widget/MainWidget.h

356 lines
17 KiB
C++

// MainWidget.h
#pragma once
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QWidget>
#include <QTcpServer>
#include <QJsonArray>
#include <QListWidget>
#include <QCheckBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QThreadPool>
#include <QMutex>
#include <QFileDialog>
#include <QLineEdit>
#include <QTextEdit>
#include <QTabWidget>
#include <QTimer>
#include <QGroupBox>
#include <QFormLayout>
#include <QLabel>
#include <QStackedLayout>
#include <QJsonDocument>
#include <QFile>
#include <QDebug>
#include <QHBoxLayout>
#include <QTcpSocket>
#include <QUdpSocket>
#include <QNetworkInterface>
#include <QTextStream>
#include <QScrollArea>
#include <QDateTime>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QUrl>
#include <QMap>
#include <QList>
#include <QPair>
#include <QTextCodec>
#include <QCryptographicHash>
#include <QRandomGenerator>
#include <QMessageAuthenticationCode>
#include <QCoreApplication>
#include <QSettings>
#include <QImage>
#include <QPainter>
#include <QComboBox>
#include <QVector>
#include <QSerialPortInfo>
#include <QStringList>
#include <QSerialPort>
#include <QLoggingCategory>
#include <winsock2.h>
#include <iphlpapi.h>
#include "./SerialPortHandler/crc32.h"
#include "./UI_Widget/UI_Name.h"
#include "./SerialPortHandler/SerialPortHandler.h"
#include "../Json/readJsonFile.h"
#include "../RecvDataHandler/RecvDataHandler.h"
#include "../RecvDataHandler/SerialRecvDataHandler.h"
#include "../LicenseGenerate/LicenseGenerate.h"
#include "../LicenseGenerate/LicenseConfirmWindow.h"
//#include "../Network/mdns/servicemodel.h"
#include "../Network/ClientHandler.h"
#include "../Network/httpClient.h"
#include "../Network/mdns/MdnsServiceThread.h"
#include "../Network/mdns/mdns.h"
//#include "../Network/mdns/qmdnsengine/include/server.h"
//#include "../Network/mdns/qmdnsengine/include/hostname.h"
//#include "../Network/mdns/qmdnsengine/include/provider.h"
//#include "../Network/mdns/qmdnsengine/include/service.h"
//#include "../Network/mdns/qmdnsengine/include/message.h"
//#include "../Network/mdns/qmdnsengine/include/query.h"
#define TEST_UDP_BROADCAST 0 // 用于测试 UDP 组播实现 mdns 功能 非标准 mdns
#define MANUAL_UPLOAD_LICENSE 0 // 打开手动上传 License的功能
#define START_MDNS 1
#define FOCUS_WINDOWS_BUTTON 4 // 大窗口播放视频的按键编号
//#define TCP_CONNECT_PORT 12412 // TCP监听的端口
#define GET_PIC_DATA_SIZE (480 * 640 * 1.5)
#define NEW_MAP 1
// FactoryToolSendGetUuidToHttpServer 函数还需要改
#define FACTORY_ID "TEST" // 工厂ID
#define LABEL "TEST_SL100_20240826" // 出货标签
#define BATCH 1 // 出货批次
#define MODEL "SL100" // 出货型号
class MainWidget : public QWidget
{
Q_OBJECT
public:
// 构造函数
explicit MainWidget(QWidget* parent = nullptr);
~MainWidget();
void startHttpServer(const QString& imagePath);
void sendLicenseData(int itemIndex, const QString text);
QByteArray hexStringToByteArray(const QString& hexString);
signals:
void userInputEntered(const QString&);
void openFocusWindowRequested(int itemIndex);
void serialComPortConnected();
void requestDevInfo();
void delUserDialogResult(const QString& userInput);
void openFocusWindow(int itemIndex);
private slots:
void showDelUserWindow();
void setupSerialPortHandlerConnections();
void onSerialDataReceived(const QByteArray& data);
// 发送HTTP请求
//void onSomeButtonClicked();
// 处理HTTP响应
// 启动图片共享服务
void onStartImageSharing(int width, int height, QString img_type, std::function<void(QString)> callback);
void onStartLOGOImageSharing(int width, int height, QString img_type, std::function<void(QString)> callback);
void onHttpRequestFinished(const QJsonObject& response);
// 处理HTTP错误
void onHttpRequestError(const QString& errorString);
// 处理开始服务器按钮点击事件
void onStartServerClicked();
// 处理发送获取设备信息按键点击事件
void onSendGetDevInfoClicked();
// 处理发送取图按键点击事件
void onSendGetPicClicked();
// 处理发送拉视频流按键点击事件
void onSendGetVideoClicked();
void onLicenseButtonClicked();
void onUuidButtonClicked();
// 处理一键发送按钮点击事件
void onSendAllClicked();
// 处理单独发送项按钮点击事件
void onSendFrontItemClicked();
void onSendBackItemClicked();
// 处理单独发送功能配置项按钮点击事件
void onSendFrontFuncItemClicked();
void onSendBackFuncItemClicked();
// 处理数据接收信号
//void onDataReceived(const QString& client, const QByteArray& data);
// 处理状态更新信号
void onStatusUpdated(int boardTpye, const QString& client, bool success, const QJsonArray& jsonArray, int itemJsonIndex);
// 处理所有项目处理完毕信号
void onAllItemsProcessed(int boardTpye, const QString& client, int itemsProcessedCount);
// 处理客户端断开连接
void onClientDisconnected(ClientHandler* handler);
// 启动超时定时器
void onStartTimeout(int clientId, int timeout);
// 为了使滚动条在最下面,不用手动鼠标下拉
void scrollToBottom();
// 处理点击 Save Log 按键
void onSelectFileButtonClicked();
// 处理点击 Clear Log 按键
void onclearLogButtonClicked();
// 处理复选框的状态
void onSaveCheckBoxStateChanged(int state);
// 处理打开调焦的大窗口
void onOpenFocusWindowClicked();
void printThreadPoolInfo();
void onCheckThreadStatus();
void onCheckConnectionStatus();
void startClientReadTimer(int clientId);
void stopClientReadTimer(int clientId);
void onCheckSerialRecvStatus();
void onShowDialog();
#if TEST_UDP_BROADCAST
void sendMulticastMessage();
#endif
//void onIpSelectionChanged(const QString& selectedIp);
private:
UI_config TOOL_UI;
// 读取 test JSON 配置文件
void readJsonConfig();
// 设置 UI
void setupUI();
// 切换页面
void switchToTab(int tabIndex);
#if NEW_MAP
void addDeviceAreaTab(QTabWidget* mainTabWidget, const QString& tabName,
QGroupBox* deviceInfoGroupBox, QGroupBox* licenseGroupBox,
const QJsonArray& boardTest, const QJsonArray& boardFuncConfig,
const QString& boardTestName, const QString& boardFuncConfigName,
const QString& boardTestTitle, const QString& boardConfigTitle,
QLabel* leftLensLabel, QLabel* rightLensLabel, QLabel* videoLabel,
QLineEdit* VideoResolutionEdit_type);
void addAllDeviceAreaTab(QTabWidget* mainTabWidget, const QString& tabName,
const QJsonArray& boardTest, const QJsonArray& boardFuncConfig,
const QString& boardTestName, const QString& boardFuncConfigName,
const QString& boardTestTitle, const QString& boardConfigTitle,
QLabel* leftLensLabel, QLabel* rightLensLabel, QLabel* videoLabel,
QLineEdit* VideoResolutionEdit_type);
QTabWidget* createMediaTabWidget(QLabel* leftLensLabel, QLabel* rightLensLabel, QLabel* videoLabel,
QLineEdit* VideoResolutionEdit_type, const QString& tabName);
#endif
QGroupBox* createLicenseGroupBox();
QGroupBox* createFrontDeviceInfoGroupBox();
QGroupBox* createBackDeviceInfoGroupBox();
QWidget* createFunctionTestTab(const QJsonArray& BoardFuncTest, const QString& propertyName);
QWidget* createFunctionConfigTab(const QJsonArray& BoardFuncConfig, const QString& propertyName);
QWidget* createImageDisplayTab(QLabel* leftLensLabel, QLabel* rightLensLabel);
QWidget* createVideoDisplayTab(QLabel* videoLabel, QLineEdit* VideoResolutionEdit_type, const QString& tabName);
QGroupBox* createBackConnectServerGroupBox();
// 保存日志到文件
void saveStatusListToFile(const QString& filePath);
// 用于更新按键文本的函数
void updateServerButtonText();
void updateGetVideoButtonState(int state, int connectedDevices, const QVector<QPushButton*>& buttons);
void updateGetPicButtonsState(int state, int connectedDevices);
void onHandleInvalidOperate(const QString& text);
void onDisconnectClient(int clientId);
void setupTimerForThreadPoolInfo();
void sendDataToClient(int clientId, const QByteArray& data);
// 与服务器的接口
void FactoryToolSendPostTestToHttpServer();
void FactoryToolSendPostLicenseToHttpServer(const QString& hardware_info, const QString& license_info);
void FactoryToolSendPostComfirmToHttpServer(const QString& mac_addr, const QString& uuid);
void FactoryToolSendGetUuidToHttpServer(const QString& mac_addr);
void FactoryToolSendGetLicenseToHttpServer(const QString& hw_info);
char* getSelectedIp() const;
QTcpServer* server; // TCP 服务器
HttpClient* httpClient; // HTTP 服务器
QJsonArray frontBoardOneClickTest; // 前板一键功能测试 JSON
QJsonArray frontBoardTest; // 前板单项测试 JSON
QJsonArray frontBoardFuncConfig; // 前板功能配置参数 JSON
QJsonArray frontBoardDevInfoJson; // 前板设备信息参数 JSON
QJsonArray frontBoardLicenseJson; // 前板License JSON
QJsonArray frontBoardCmdConfigJson;
QJsonArray backBoardOneClickTest; // 后板一键功能测试 JSON
QJsonArray backBoardTest; // 后板单项测试 JSON
QJsonArray backBoardFuncConfig; // 后板功能配置参数 JSON
QJsonArray backBoardDevInfoJson; // 后板设备信息参数 JSON
QJsonArray backBoardUuidJson; // 后板UUID JSON
QJsonArray backBoardCmdConfigJson;
QJsonArray factoryProductInfo; // 整机:工厂生产信息
QJsonArray allBoardTest; // 整机测试区 JSON 配置
QJsonArray allBoardFuncConfig; // 整机功能配置区 JSON 配置
QJsonArray getDevInfoJson; // 获取设备信息 JSON 配置
QJsonArray getPicJson; // 发送取图指令 JSON 配置
QJsonArray getVideoJson; // 发送拉视频指令 JSON 配置
QVBoxLayout* mainLayout; // 主布局
QListWidget* statusListWidget; // 状态列表
QPushButton* startServerButton; // 开始服务器按键
QPushButton* focusWindowsButton; // 调焦窗口按键
QPushButton* sendAllButton; // 一键发送按键
QThreadPool threadPool; // 线程池
QMutex mutex; // 互斥锁
int nextClientId; // 新增的客户端编号
int nextSerialId; // 新增的客户端编号
int currentIndex = 0; // 新增的客户端编号
int connectedClientsCount = 0; // 连接客户端的数量
int isRequestSuccessful = 0; // GET请求是否成功
int isBackBoardOrAllBoard = 0;
bool manualSend; // 判断是否是手动触发的发送
bool deviceConnected = false; // 判断是否有设备连接过
bool isSendingAll; // 一键功能测试 状态
bool checkBoxState = true;
bool isReplyOrTimeout = true;
bool isFirstSendDevInfo = true;
QJsonObject licenseDataArray; // 用于保存从服务器获取的 data 字段对象
QVector<QPushButton*> itemButtons; // 项目按键集合
QVector<QPushButton*> funcItemButtons; // 功能配置项目按键集合
QVector<QPushButton*> getPicButtons; // 保存两个取图的摄像头的按键的指针
QVector<QPushButton*> getVideoButtons; // 保存拉视频设备的按键的指针
QVector<QPushButton*> getBackVideoButtons;
QVector<QPushButton*> getAllVideoButtons;
QList<ClientHandler*> clients; // 客户端处理器集合
QList<SerialPortHandler*> serialPorts; // 客户端处理器集合
QMap<int, QTimer*> clientTimers; // 每个客户端的定时器
QMap<int, QTimer*> serialTimers; // 每个串口端的定时器
QMap<QString, QLineEdit*> devInfoLineEdits;// msg_id 和对应的 QLineEdit 的映射关系
QMap<QString, QLineEdit*> productInfoLineEdits;
QString cur_mac_addr;
QString cur_UUID;
QCheckBox* saveCheckBox; // 保存文件复选框
QPushButton* selectFileButton; // Save Log 按键
QPushButton* serialConnectButton; // 串口连接按键
QPushButton* clearLogButton; // clear Log 按键
QLineEdit* filePathLineEdit; // 文件路径显示
QLineEdit* VideoResolutionEdit; // 视频分辨率显示
QLineEdit* VideoResolutionEdit_back; // 视频分辨率显示
QLineEdit* VideoResolutionEdit_all; // 视频分辨率显示
//QLineEdit* frameRateEdit; // 视频帧率显示
QLabel* leftLens_imageLabel; // 前板左边镜头图像显示
QLabel* rightLens_imageLabel; // 前板右边镜头图像显示
QLabel* leftLens_imageLabel_back; // 后板左边镜头图像显示
QLabel* rightLens_imageLabel_back; // 后板右边镜头图像显示
QLabel* leftLens_imageLabel_all; // 整机左边镜头图像显示
QLabel* rightLens_imageLabel_all; // 整机右边镜头图像显示
QLabel* videoLabel; // 前板视频显示
QLabel* video_Label_back; // 后板视频显示
QLabel* video_Label_all; // 整机视频显示
QLineEdit* frontFuncConfigLineEdit; // 前板功能配置编辑框
QLineEdit* backFuncConfigLineEdit; // 后板功能配置编辑框
QTextEdit* licenseHwInfoEdit; // 获取license的硬件信息
QTextEdit* UuidHwInfoEdit; // Uuid的硬件信息
MainWidget* mainWidget;
QTabWidget* tabWidget; // 标签页
QTabWidget* mainTabWidget;
QWidget* functionTestArea; // 功能测试区
QWidget* functionConfigArea; // 功能配置区
QComboBox* ipComboBox;
QComboBox* serialComPortComboBox;
int lastClickedGetPicCamIndex; // 记录上一次点击取图的摄像头的按键索引
int lastClickedGetPicDevIndex; // 记录上一次点击取图的设备的按键索引
int lastClickedGetVideoCamIndex; // 记录上一次点击拉视频的摄像头的按键索引
int lastClickedGetVideoDevIndex; // 记录上一次点击拉视频的设备的按键索引
//QTimer* serialTimer;
QTimer* threadStatusTimer; // 检查线程状态的定时器
QTimer* connectionStatusCheckTimer; // 检查连接状态的定时器
QMap<int, DataHandler*> dataHandlers;
QMap<int, SerialDataHandler*> serialDataHandlers;
QMap<int, QThread*> clientThreads;
QMap<int, QTimer*> clientReadTimers;
QMap<int, ClientHandler*> clients_1;
//ServiceProvider* mServiceProvider;
QTimer* mdnsTimer;
void startMdnsService(char* select_ip);
void stopMdnsService();
void sendSerialData(const QByteArray& data, bool heartBeat);
#if TEST_UDP_BROADCAST
QUdpSocket* multicastSocket;
QTimer* multicastTimer;
#endif
QTcpServer* httpServer; // HTTP 服务器
QString imagePath; // 当前选择的图片路径
quint16 serverPort = 8080; // HTTP 服务器端口
void setupHttpServer(); // 初始化 HTTP 服务器
void handleHttpRequest(QTcpSocket* clientSocket); // 处理 HTTP 请求
MdnsServiceThread* mdnsThread; // mDNS 线程指针
SerialPortHandler* serialPortHandler;
//QTimer* portDetectionTimer;
//QThread* thread;
};
#endif // MAINWIDGET_H