191 lines
7.3 KiB
C++
191 lines
7.3 KiB
C++
#ifndef BELTTEARINGPRESENTER_H
|
||
#define BELTTEARINGPRESENTER_H
|
||
|
||
#include <QObject>
|
||
#include <QTimer>
|
||
#include <QMap>
|
||
#include <QJsonObject>
|
||
#include <QJsonArray>
|
||
#include <QJsonDocument>
|
||
#include <QDataStream>
|
||
#include <deque>
|
||
#include <mutex>
|
||
#include <thread>
|
||
#include <condition_variable>
|
||
#include <atomic>
|
||
#include <opencv2/opencv.hpp>
|
||
#include "IVrEyeDevice.h"
|
||
#include "beltTearingDetection_Export.h"
|
||
#include "IVrBeltTearingConfig.h"
|
||
#include "VZNL_Common.h"
|
||
#include "VZNL_Types.h"
|
||
#include "VrLog.h"
|
||
#include "IYTCPServer.h"
|
||
#include "RobotProtocol.h" // 添加RobotProtocol头文件
|
||
#include "ModbusRTUMaster.h" // 添加ModbusRTUMaster头文件
|
||
#include "ProtocolCommon.h" // 添加ProtocolCommon头文件
|
||
#include "TearingTcpProtocol.h" // 添加TearingTcpProtocol头文件
|
||
|
||
|
||
class BeltTearingPresenter : public QObject, public IVrBeltTearingConfigChangeNotify
|
||
{
|
||
Q_OBJECT
|
||
|
||
public:
|
||
explicit BeltTearingPresenter(QObject *parent = nullptr);
|
||
~BeltTearingPresenter();
|
||
|
||
// 配置相关方法
|
||
bool loadConfiguration(const QString& configFilePath);
|
||
void applyAlgorithmParameters(const BeltTearingAlgorithmParams& params);
|
||
|
||
// 初始化TCP服务器
|
||
bool startServer(quint16 port = 0); // port = 0 表示使用配置文件中的端口
|
||
void stopServer();
|
||
|
||
// 相机相关方法
|
||
bool initializeCamera();
|
||
void startCamera();
|
||
void stopCamera();
|
||
|
||
void ResetDetect();
|
||
|
||
// 系统控制方法(供ModbusTCP调用)
|
||
void StopWork(); // 停止工作
|
||
void StartWork(); // 开始工作
|
||
|
||
// 获取配置信息
|
||
quint16 getServerPort() const { return m_configResult.serverPort; }
|
||
const BeltTearingConfigResult& getConfig() const { return m_configResult; }
|
||
|
||
// IVrBeltTearingConfigChangeNotify接口实现
|
||
void OnConfigChanged(const BeltTearingConfigResult& configResult) override;
|
||
|
||
// 发送模拟数据
|
||
void sendTestData(std::string fileName);
|
||
void sendSimulationData();
|
||
|
||
private slots:
|
||
void onCameraInitTimer();
|
||
|
||
private:
|
||
// TCP服务器回调函数
|
||
static void OnServerRecv(const TCPClient* pClient, const char* pData, const unsigned int nLen);
|
||
static void OnServerEvent(const TCPClient* pClient, TCPServerEventType eventType);
|
||
|
||
// TCP服务器相关
|
||
IYTCPServer *m_tcpServer; // 旧协议服务器(ReadConfig/WriteConfig)
|
||
IYTCPServer *m_tcpProtocolServer; // 新协议服务器(TearingTcpProtocol)
|
||
TearingTcpProtocol *m_tearingProtocol; // 撕裂TCP通信协议处理器
|
||
QMap<QString, const TCPClient*> m_clients;
|
||
quint16 m_port;
|
||
quint16 m_tcpPort; // 新协议端口
|
||
|
||
// 配置相关
|
||
IVrBeltTearingConfig *m_config;
|
||
BeltTearingConfigResult m_configResult;
|
||
QString m_configFilePath;
|
||
|
||
// 相机相关
|
||
IVrEyeDevice *m_eyeDevice = nullptr;
|
||
QTimer *m_cameraInitTimer;
|
||
QString m_cameraIP;
|
||
bool m_cameraInitialized;
|
||
bool m_cameraDetecting;
|
||
|
||
// SDK算法相关
|
||
std::atomic<bool> m_bInitAlgo{false};
|
||
std::vector<SSG_hLineProInfo> m_hLineWorkers;
|
||
|
||
SSG_beltTearingParam m_algorithmParam;
|
||
std::vector<SSG_beltTearingInfo> m_beltTearings_new;
|
||
std::vector<SSG_beltTearingInfo> m_beltTearings_growing;
|
||
std::vector<SSG_beltTearingInfo> m_beltTearings_ended;
|
||
std::vector<SSG_beltTearingInfo> m_beltTearings_unknown;
|
||
|
||
// 激光线队列管理
|
||
std::deque<SVzLaserLineData> m_laserLineQueue; // 激光线数据队列
|
||
std::mutex m_queueMutex; // 队列访问互斥锁
|
||
unsigned long long m_lineCounter; // 新增线条计数器
|
||
int m_maxQueueSize; // 最大队列大小(从配置读取)
|
||
int m_generationInterval; // 图像生成间隔(从配置读取)
|
||
|
||
// 检测数据缓存和算法检测线程
|
||
std::mutex m_detectionDataMutex; // 检测数据缓存访问互斥锁
|
||
std::atomic<bool> m_bAlgoDetectThreadRunning{false}; // 算法检测线程运行标志
|
||
|
||
std::mutex m_algoDetectMutex; // 算法检测互斥锁
|
||
std::condition_variable m_algoDetectCondition; // 算法检测条件变量
|
||
std::thread m_algoDetectThread; // 算法检测线程
|
||
|
||
// 相机状态回调函数
|
||
static void OnStatusCallback(EVzDeviceWorkStatus eStatus, void* pExtData, unsigned int nDataLength, void* pInfoParam);
|
||
|
||
// 点云数据回调函数
|
||
static void OnPointCloudCallback(EVzResultDataType eDataType, SVzLaserLineData* pLaserLinePoint, void* pParam);
|
||
|
||
// 发送算法结果
|
||
void sendTearingResults(const std::vector<SSG_beltTearingInfo>& results);
|
||
|
||
// 激光线队列管理方法
|
||
void addLaserLineToQueue(const SVzLaserLineData* laserLine);
|
||
|
||
// 算法检测线程方法
|
||
void _AlgoDetectThread();
|
||
int _DetectTask();
|
||
// 合并撕裂结果的独立函数
|
||
void _MergeAndReplace(std::vector<SSG_beltTearingInfo>& allTearings, const std::vector<SSG_beltTearingInfo>& source);
|
||
|
||
// 图像生成和发送
|
||
void sendImageToClients(const QImage& image);
|
||
|
||
// 工具方法
|
||
QString generateClientId(const TCPClient* client);
|
||
|
||
// 实例方法(非静态)
|
||
void handleServerRecv(const TCPClient* pClient, const char* pData, const unsigned int nLen);
|
||
void handleServerEvent(const TCPClient* pClient, TCPServerEventType eventType);
|
||
|
||
// 处理算法参数更新
|
||
void handleAlgorithmParameterUpdate(const QByteArray& paramData);
|
||
void handleSetAlgorithmParams(const QJsonObject& paramObj);
|
||
void handleGetServerInfo(const QJsonObject& requestObj);
|
||
void handleReadConfig(const TCPClient* pClient); // 添加ReadConfig处理函数声明
|
||
void handleWriteConfig(const QByteArray& paramData); // 添加WriteConfig处理函数声明
|
||
|
||
// 获取单例实例(用于静态回调)
|
||
static BeltTearingPresenter* s_instance;
|
||
|
||
// 配置与SDK参数转换函数
|
||
SSG_beltTearingParam configToSDKParam(const BeltTearingConfigResult& config) const;
|
||
BeltTearingConfigResult sdkToConfigParam(const SSG_beltTearingParam& sdkParam) const;
|
||
|
||
// 添加RobotProtocol相关成员变量和方法
|
||
RobotProtocol* m_pRobotProtocol = nullptr; // 机械臂协议实例
|
||
bool m_bRobotConnected = false; // 机械臂连接状态
|
||
|
||
// 添加ModbusRTUMaster相关成员变量和方法
|
||
ModbusRTUMaster* m_pModbusRTUMaster = nullptr; // Modbus-RTU主端实例
|
||
bool m_bModbusRTUConnected = false; // Modbus-RTU连接状态
|
||
|
||
// 初始化机械臂协议
|
||
int InitRobotProtocol();
|
||
|
||
// 初始化Modbus-RTU主端
|
||
int InitModbusRTUMaster();
|
||
|
||
// 机械臂协议回调函数
|
||
void OnRobotConnectionChanged(bool connected);
|
||
bool OnRobotWorkSignal(bool startWork, int cameraIndex);
|
||
|
||
// 发送检测结果给机械臂
|
||
void SendDetectionResultToRobot(const std::vector<SSG_beltTearingInfo>& detectionResults);
|
||
|
||
// 发送温度数据给所有客户端
|
||
void SendTemperatureData(float temperature);
|
||
|
||
|
||
};
|
||
|
||
#endif // BELTTEARINGPRESENTER_H
|