GrabBag/AppUtils/AppCommon/Inc/BasePresenter.h

196 lines
4.8 KiB
C++
Raw 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 BASEPRESENTER_H
#define BASEPRESENTER_H
#include <atomic>
#include <condition_variable>
#include <thread>
#include <mutex>
#include <vector>
#include <memory>
#include <string>
#include <QObject>
#include <QTimer>
#include "IVrEyeDevice.h"
#include "VZNL_Types.h"
/**
* @brief 设备信息结构体
*/
struct DeviceInfo
{
std::string name; // 设备名称
std::string ip; // 设备IP地址
};
/**
* @brief 基础Presenter类
*
* 提取各应用Presenter的共同功能
* - 相机设备管理
* - 检测线程管理
* - 检测数据缓存
* - 相机重连机制
*
* 子类需要实现的纯虚函数:
* - AlgoDetectThreadFunc(): 算法检测线程的主循环
* - ProcessDetectionData(): 处理检测数据的具体算法
*
* 用法示例:
* @code
* class GrabBagPresenter : public BasePresenter {
* protected:
* void AlgoDetectThreadFunc() override {
* // 实现具体的检测算法
* }
* void ProcessDetectionData(...) override {
* // 实现数据处理逻辑
* }
* };
* @endcode
*/
class BasePresenter : public QObject
{
Q_OBJECT
public:
explicit BasePresenter(QObject *parent = nullptr);
virtual ~BasePresenter();
/**
* @brief 初始化Presenter子类应该重写此方法
* @return 0: 成功, 其他: 错误码
*/
virtual int Init();
/**
* @brief 开始检测
* @param cameraIndex 相机索引(-1表示所有相机
* @param isAuto 是否自动模式
* @return 0: 成功, 其他: 错误码
*/
virtual int StartDetection(int cameraIndex = -1, bool isAuto = true);
/**
* @brief 停止检测
* @return 0: 成功, 其他: 错误码
*/
virtual int StopDetection();
/**
* @brief 获取相机列表
* @return 相机列表IP地址和设备指针的pair
*/
std::vector<std::pair<std::string, IVrEyeDevice*>> GetCameraList() const {
return m_vrEyeDeviceList;
}
/**
* @brief 设置默认相机索引
* @param cameraIndex 相机索引
*/
void SetDefaultCameraIndex(int cameraIndex) {
m_currentCameraIndex = cameraIndex;
}
/**
* @brief 获取默认相机索引
* @return 相机索引
*/
int GetDefaultCameraIndex() const {
return m_currentCameraIndex;
}
/**
* @brief 获取检测数据缓存大小
* @return 缓存中的数据个数
*/
int GetDetectionDataCacheSize() const;
/**
* @brief 保存检测数据到文件
* @param filePath 文件路径
* @return 0: 成功, 其他: 错误码
*/
virtual int SaveDetectionDataToFile(const std::string& filePath);
protected:
/**
* @brief 算法检测线程函数(纯虚函数,子类必须实现)
*
* 子类应该在此方法中实现具体的检测算法循环
*/
virtual void AlgoDetectThreadFunc() = 0;
/**
* @brief 启动算法检测线程
*/
void StartAlgoDetectThread();
/**
* @brief 停止算法检测线程
*/
void StopAlgoDetectThread();
/**
* @brief 清空检测数据缓存
*/
void ClearDetectionDataCache();
/**
* @brief 添加检测数据到缓存
* @param dataType 数据类型
* @param laserData 激光数据
*/
void AddDetectionDataToCache(EVzResultDataType dataType, const SVzLaserLineData& laserData);
/**
* @brief 初始化相机设备
* @param cameraList 相机配置列表
* @return 0: 成功, 其他: 错误码
*/
virtual int InitCamera(std::vector<DeviceInfo>& cameraList);
/**
* @brief 启动相机重连定时器
*/
void StartCameraReconnectTimer();
/**
* @brief 停止相机重连定时器
*/
void StopCameraReconnectTimer();
private slots:
/**
* @brief 相机重连定时器触发
*/
void OnCameraReconnectTimer();
protected:
// 相机设备列表
std::vector<std::pair<std::string, IVrEyeDevice*>> m_vrEyeDeviceList;
// 当前相机索引
int m_currentCameraIndex = 0;
// 连接状态标志
bool m_bCameraConnected = false;
// 算法检测线程相关
std::atomic<bool> m_bAlgoDetectThreadRunning;
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;
// 相机重连定时器
QTimer* m_pCameraReconnectTimer;
std::vector<DeviceInfo> m_cameraConfigList;
int m_expectedCameraCount;
};
#endif // BASEPRESENTER_H