Files
eUi/include/subModuleBase.h
T

126 lines
3.9 KiB
C++
Raw Normal View History

2025-07-25 00:29:52 +08:00
#pragma once
2026-09-20 16:01:09 +08:00
#include <Windows.h>
#include <QByteArray>
#include <QMutex>
#include <QObject>
#include <QThread>
#include <atomic>
#include <filesystem>
#include <fstream>
#include <functional>
2025-06-05 11:30:12 +08:00
#include <iostream>
2026-09-20 16:01:09 +08:00
#include <map>
#include <vector>
2025-06-05 11:30:12 +08:00
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
2026-09-20 16:01:09 +08:00
#include <opencv2/opencv.hpp>
static const std::string getExeDirectoryx()
{
char buffer[MAX_PATH];
if (GetModuleFileNameA(nullptr, buffer, MAX_PATH) == 0) {
return ""; // 获取失败时返回空字符串。
}
std::filesystem::path exePath(buffer);
return exePath.parent_path().string();
}
2025-11-16 19:29:29 +08:00
2026-04-26 12:25:13 +08:00
class dsModuleBase : public QObject
2025-06-05 11:30:12 +08:00
{
public:
2026-09-20 16:01:09 +08:00
bool init_flag; // 是否初始化:false/true
bool status = true; // 相机状态,用于判断相机是否断开连接
std::string paras; // 传输数据
cv::Mat m_mat; // 传输图片
std::string moduleName; // 模块名称
2025-06-05 11:30:12 +08:00
public:
2025-07-25 00:29:52 +08:00
dsModuleBase() {}
virtual ~dsModuleBase() {}
2026-09-20 16:01:09 +08:00
virtual bool readData(std::vector<cv::Mat>&, QByteArray&) { return true; }
virtual bool writeData(std::vector<cv::Mat>&, QByteArray&) { return true; }
virtual bool initParas(QByteArray&) { return true; }
virtual bool run() { return true; }
virtual bool checkStatus() { return true; }
virtual void showDialog() {};
2025-06-05 11:30:12 +08:00
};
2025-07-25 00:29:52 +08:00
class rpcModuleBase : public QObject
{
public:
rpcModuleBase() {}
virtual ~rpcModuleBase() {}
2026-09-20 16:01:09 +08:00
virtual QByteArray executeReadCommand(QByteArray& input) = 0;
virtual bool executeWriteCommand(QByteArray& input) = 0;
virtual bool initParas(QByteArray&) { return true; }
virtual bool run() { return true; }
virtual void showDialog() {};
2025-07-25 00:29:52 +08:00
};
2025-06-05 11:30:12 +08:00
2026-09-20 16:01:09 +08:00
// ========== 通用超时定时器 ==========
// 继承 QThread,到达指定时间后调用外部注册的回调函数。
class CleanupTimer : public QThread
2025-08-15 16:36:54 +08:00
{
public:
2026-09-20 16:01:09 +08:00
CleanupTimer() = default;
/// 设置定时时间,单位:毫秒
void setInterval(int msec) { m_intervalMs.store(msec); }
/// 获取当前定时时间,单位:毫秒
int intervalMs() const { return m_intervalMs.load(); }
/// 注册超时回调函数
void setCallback(std::function<void()> callback) { m_callback = std::move(callback); }
/// 停止定时器
void stop() { m_runFlag.store(false); }
protected:
void run() override
{
m_runFlag.store(true);
int counter = 0;
while (m_runFlag.load() && counter < m_intervalMs.load())
{
QThread::msleep(m_checkMs);
counter += m_checkMs;
}
if (m_runFlag.load() && counter >= m_intervalMs.load())
{
if (m_callback)
m_callback();
}
}
private:
std::function<void()> m_callback; ///< 超时回调函数
std::atomic<bool> m_runFlag{ false };
std::atomic<int> m_intervalMs{ 1500 };
int m_checkMs{ 10 };
2025-08-15 16:36:54 +08:00
};
2026-09-20 16:01:09 +08:00
//enum initStatusEnum {
// CAMERA_NOT_FOUND, // 未找到相机
// INIT_FAILED, // 初始化失败
// INIT_SUCCESS, // 初始化成功
//};
struct generalResultStruct {
bool status; // 初始化状态
std::string message; // 详细信息
};
//class euiCameraModuleBase : public QObject {
//public:
// virtual ~euiCameraModuleBase() {}
// virtual initStatusEnum init(const std::string& initStr) = 0; // 输入配置文件路径,不存在时由调用方引导用户创建
// virtual bool capture(std::vector<cv::Mat>&) = 0; // 阻塞取图接口,输出参数为取图结果;超时补图由模块内部处理
// virtual bool asyncCapture(std::function<void(bool, const std::vector<cv::Mat>&)> callback) = 0; // 异步取图接口,回调参数为取图状态和图像数据
// virtual bool connectionCheck() = 0; // 获取连接状态接口,返回当前相机连接状态
// virtual void clearCache() = 0; // 手动清除相机模块内的缓存
//};