180 lines
6.6 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef LAPWELDPRESENTER_H
#define LAPWELDPRESENTER_H
#include <condition_variable>
#include <thread>
#include "IVrConfig.h"
#include "IVrEyeDevice.h"
#include "IYLapWeldStatus.h"
#include "DetectPresenter.h"
#include "TCPServerProtocol.h"
#include "ConfigManager.h"
#include "VZNL_Types.h"
#include "SX_lapWeldDetection_Export.h"
#include "SG_baseDataType.h"
#include "LaserDataLoader.h"
#include "PathManager.h"
#include "CommonDialogCameraLevel.h" // 引入通用对话框的接口
#include <QImage>
#include <QPainter>
#include <QColor>
#include <memory>
class LapWeldPresenter : public IVrConfigChangeNotify,
public ICameraLevelCalculator, public ICameraLevelResultSaver
{
public:
LapWeldPresenter();
~LapWeldPresenter();
// 初始化
int Init();
// 设置状态回调
void SetStatusCallback(IYLapWeldStatus* status);
// 开始检测
int StartDetection(int cameraIndex = -1, bool isAuto = true); // cameraIndex: -1表示所有相机1/2...表示特定相机
// 停止检测
int StopDetection();
// 加载调试数据进行检测
int LoadDebugDataAndDetect(const std::string& filePath);
// 为所有相机设置状态回调
void SetCameraStatusCallback(VzNL_OnNotifyStatusCBEx fNotify, void* param);
// 设置默认相机索引
void SetDefaultCameraIndex(int cameraIndex);
// 获取当前默认相机索引
int GetDefaultCameraIndex() const { return m_currentCameraIndex; }
// 获取配置管理器
ConfigManager* GetConfigManager() { return m_pConfigManager; }
// 获取相机列表
std::vector<std::pair<std::string, IVrEyeDevice*>> GetCameraList() const { return m_vrEyeDeviceList; } // 返回相机列表包括IP地址和设备指针
// 手眼标定矩阵管理
const CalibMatrix GetClibMatrix(int index) const;
// 实现IVrConfigChangeNotify接口
virtual void OnConfigChanged(const ConfigResult& configResult) override;
// 获取检测数据缓存的副本(用于保存数据)
int GetDetectIndex() const { return m_currentCameraIndex; }
int GetDetectionDataCacheSize() const { return m_detectionDataCache.size(); }
// 保存检测数据到文件(默认实现)
int SaveDetectionDataToFile(const std::string& filePath);
// ============ 实现 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;
static void _StaticCameraNotify(EVzDeviceWorkStatus eStatus, void* pExtData, unsigned int nDataLength, void* pInfoParam);
static void _StaticDetectionCallback(EVzResultDataType eDataType, SVzLaserLineData* pLaserLinePoint, void* pParam);
private:
// 相机协议相关方法
int InitCamera(std::vector<DeviceInfo>& cameraList);
// 算法初始化接口
int InitAlgorithmParams();
// TCP服务器相关方法
int InitTCPServer();
// TCP回调函数
void OnTCPConnectionChanged(bool connected);
bool OnTCPDetectionTrigger(bool startWork, int cameraIndex, qint64 timestamp);
// TCP连接状态检查和更新
void CheckAndUpdateWorkStatus();
// 打开相机
int _OpenDevice(int cameraIndex, const char* cameraName, const char* cameraIp, ProjectType& projectType);
bool _SinglePreDetection(int cameraIndex);
int _SingleDetection(int cameraIndex, bool isStart);
// 静态回调函数,供外部使用
void _CameraNotify(EVzDeviceWorkStatus eStatus, void* pExtData, unsigned int nDataLength, void* pInfoParam);
// 检测数据回调函数
void _DetectionCallback(EVzResultDataType eDataType, SVzLaserLineData* pLaserLinePoint, void* pParam);
// 算法检测线程
void _AlgoDetectThread();
int _DetectTask();
// 释放缓存的检测数据
void _ClearDetectionDataCache();
// 发送检测结果给TCP客户端
void _SendDetectionResultToTCP(const DetectionResult& detectionResult, int cameraIndex);
// 根据相机索引获取调平参数
SSG_planeCalibPara _GetCameraCalibParam(int cameraIndex);
private:
ConfigManager* m_pConfigManager = nullptr;
std::vector<std::pair<std::string, IVrEyeDevice*>> m_vrEyeDeviceList;
IYLapWeldStatus* m_pStatus = nullptr;
DetectPresenter* m_pDetectPresenter = nullptr;
// TCP服务器相关
TCPServerProtocol* m_pTCPServer = nullptr;
// 连接状态标志
bool m_bCameraConnected = false; // 相机连接状态
bool m_bTCPConnected = false; // TCP连接状态
WorkStatus m_currentWorkStatus = WorkStatus::Error; // 当前工作状态
int m_currentCameraIndex = 0; // 当前使用的相机编号
std::atomic<bool> m_bAlgoDetectThreadRunning{false};
std::mutex m_algoDetectMutex;
std::condition_variable m_algoDetectCondition;
std::thread m_algoDetectThread; // 算法检测线程
// 检测数据缓存 - 统一存储两种类型的数据
std::mutex m_detectionDataMutex;
std::vector<std::pair<EVzResultDataType, SVzLaserLineData>> m_detectionDataCache; // 统一存储数据
// 手眼标定矩阵列表从独立文件加载暂时保留在Presenter中
std::vector<CalibMatrix> m_clibMatrixList;
ProjectType m_projectType;
// 调试数据加载器
LaserDataLoader m_dataLoader;
};
#endif // LAPWELDPRESENTER_H