171 lines
5.7 KiB
C++
171 lines
5.7 KiB
C++
#ifndef WORKPIECEHOLEPRESENTER_H
|
||
#define WORKPIECEHOLEPRESENTER_H
|
||
|
||
#include <condition_variable>
|
||
#include <thread>
|
||
#include <map>
|
||
#include <mutex>
|
||
#include <memory>
|
||
|
||
#include "BasePresenter.h"
|
||
#include "IVrEyeDevice.h"
|
||
#include "ConfigManager.h"
|
||
#include "TCPServerProtocol.h"
|
||
#include "IYWorkpieceHoleStatus.h"
|
||
#include "SG_baseDataType.h"
|
||
#include "VrConvert.h"
|
||
#include "LaserDataLoader.h"
|
||
#include "CommonDialogCameraLevel.h" // 引入通用对话框的接口
|
||
#include <QImage>
|
||
#include <QPainter>
|
||
#include <QColor>
|
||
#include <QObject>
|
||
#include <QTimer>
|
||
#include <memory>
|
||
|
||
// Forward declarations
|
||
class DetectPresenter;
|
||
|
||
class WorkpieceHolePresenter : public BasePresenter, public IVrConfigChangeNotify,
|
||
public ICameraLevelCalculator, public ICameraLevelResultSaver
|
||
{
|
||
Q_OBJECT
|
||
|
||
public:
|
||
explicit WorkpieceHolePresenter(QObject *parent = nullptr);
|
||
~WorkpieceHolePresenter();
|
||
|
||
// 初始化
|
||
int InitApp() override;
|
||
|
||
// 获取配置管理器
|
||
ConfigManager* GetConfigManager() { return m_pConfigManager; }
|
||
|
||
// 手眼标定矩阵管理
|
||
CalibMatrix GetClibMatrix(int index) const;
|
||
|
||
// 实现IVrConfigChangeNotify接口
|
||
virtual void OnConfigChanged(const ConfigResult& configResult) override;
|
||
|
||
// ============ 实现 ICameraLevelCalculator 接口 ============
|
||
|
||
/**
|
||
* @brief 计算平面调平参数
|
||
*/
|
||
bool CalculatePlaneCalibration(
|
||
const std::vector<std::pair<EVzResultDataType, SVzLaserLineData>>& scanData,
|
||
double planeCalib[9],
|
||
double& planeHeight,
|
||
double invRMatrix[9]) override;
|
||
|
||
// ============ 实现 ICameraLevelResultSaver 接口 ============
|
||
|
||
/**
|
||
* @brief 保存相机调平结果到配置文件
|
||
*/
|
||
bool SaveLevelingResults(double planeCalib[9], double planeHeight, double invRMatrix[9],
|
||
int cameraIndex, const QString& cameraName) override;
|
||
|
||
/**
|
||
* @brief 从配置文件加载相机调平结果
|
||
*/
|
||
bool LoadLevelingResults(int cameraIndex, const QString& cameraName,
|
||
double planeCalib[9], double& planeHeight, double invRMatrix[9]) override;
|
||
|
||
protected:
|
||
// ============ 实现 BasePresenter 纯虚函数 ============
|
||
|
||
/**
|
||
* @brief 初始化算法参数(实现纯虚函数)
|
||
*/
|
||
int InitAlgoParams() override;
|
||
|
||
/**
|
||
* @brief 执行算法检测(实现纯虚函数)
|
||
* @param detectionDataCache 检测数据缓存的引用
|
||
*/
|
||
int ProcessAlgoDetection(std::vector<std::pair<EVzResultDataType, SVzLaserLineData>>& detectionDataCache) override;
|
||
|
||
/**
|
||
* @brief 获取检测数据类型(实现纯虚函数)
|
||
* 工件孔定位项目只使用Position点云数据
|
||
*/
|
||
EVzResultDataType GetDetectionDataType() override {
|
||
return keResultDataType_Position;
|
||
}
|
||
|
||
/**
|
||
* @brief 相机状态变化通知(实现纯虚函数)
|
||
*/
|
||
void OnCameraStatusChanged(int cameraIndex, bool isConnected) override;
|
||
|
||
/**
|
||
* @brief 工作状态变化通知(重写虚函数)
|
||
*
|
||
* 当BasePresenter的工作状态改变时,此方法会被调用
|
||
* 在此调用UI回调接口通知状态变化
|
||
*/
|
||
void OnWorkStatusChanged(WorkStatus status) override;
|
||
|
||
/**
|
||
* @brief 相机数量变化通知(重写虚函数)
|
||
*
|
||
* 当相机初始化时,此方法会被调用
|
||
* 在此调用UI回调接口通知相机数量
|
||
*/
|
||
void OnCameraCountChanged(int count) override;
|
||
|
||
/**
|
||
* @brief 状态文字更新通知(重写虚函数)
|
||
*
|
||
* 当需要更新状态文字时,此方法会被调用
|
||
* 在此调用UI回调接口通知状态消息
|
||
*/
|
||
void OnStatusUpdate(const std::string& statusMessage) override;
|
||
|
||
private:
|
||
// TCP服务器相关方法
|
||
int InitTcpServer(int nPort);
|
||
int InitTCPServer(); // 初始化TCP服务器协议(使用配置端口)
|
||
bool startServer(quint16 port = 0); // port = 0 表示使用配置文件中的端口
|
||
void stopServer();
|
||
|
||
// TCP服务器回调处理方法
|
||
void onTcpDataReceivedFromCallback(const TCPClient* pClient, const char* pData, const unsigned int nLen);
|
||
void onTcpClientEventFromCallback(const TCPClient* pClient, TCPServerEventType eventType);
|
||
|
||
// TCP连接状态改变回调
|
||
void OnTCPConnectionChanged(bool connected);
|
||
|
||
// TCP检测触发回调
|
||
bool OnTCPDetectionTrigger(bool startWork, int cameraIndex, qint64 timestamp);
|
||
|
||
// 发送检测结果给TCP客户端
|
||
void _SendDetectionResultToTCP(const WorkpieceHoleDetectionResult& detectionResult, int cameraIndex);
|
||
|
||
// 连接状态检查和更新
|
||
void CheckAndUpdateWorkStatus();
|
||
|
||
// 根据相机索引获取调平参数
|
||
SSG_planeCalibPara _GetCameraCalibParam(int cameraIndex);
|
||
|
||
private:
|
||
// WorkpieceHolePresenter 特有的成员变量
|
||
ConfigManager* m_pConfigManager = nullptr;
|
||
|
||
// TCP服务器相关
|
||
IYTCPServer* m_pTcpServer = nullptr;
|
||
TCPServerProtocol* m_pTCPServer = nullptr; // TCP服务器协议实例
|
||
quint16 m_port = 0;
|
||
bool m_bTcpClientConnected = false; // TCP客户端连接状态
|
||
bool m_bTCPConnected = false; // TCP客户端连接状态(新协议)
|
||
|
||
// 检测处理器
|
||
DetectPresenter* m_pDetectPresenter = nullptr;
|
||
|
||
// 手眼标定矩阵列表(从独立文件加载,暂时保留在Presenter中)
|
||
std::vector<CalibMatrix> m_clibMatrixList;
|
||
};
|
||
|
||
#endif // WORKPIECEHOLEPRESENTER_H
|