221 lines
6.7 KiB
C++
221 lines
6.7 KiB
C++
#pragma once
|
||
|
||
#include <QDateTime>
|
||
#include <QString>
|
||
#include <QVector>
|
||
#include <QMetaType>
|
||
#include <QTimer>
|
||
|
||
#include "CaptureCoordinator.h"
|
||
|
||
// ==================== 枚举定义 ====================
|
||
|
||
// 任务状态
|
||
enum class TaskStatus {
|
||
Waiting, // 等待
|
||
Running, // 运行中
|
||
Finished // 结束
|
||
};
|
||
|
||
// 子任务类型
|
||
enum class SubTaskType {
|
||
HyperSpectual400_1000nm, // 400nm-1000nm高光谱相机
|
||
HyperSpectual1000_1700nm, // 1000nm-1700nm高光谱相机
|
||
SingleLensReflex, // 单反相机
|
||
DepthCamera // 深度相机
|
||
};
|
||
|
||
// ==================== 统一子任务封装 ====================
|
||
|
||
struct SubTask {
|
||
SubTaskType type; // 子任务类型
|
||
|
||
// 共享属性
|
||
QDateTime startTime;
|
||
QDateTime endTime;
|
||
int durationSeconds = 0;
|
||
int estimatedDurationSeconds = 0;
|
||
QString pathLineFilePath;
|
||
TaskStatus status = TaskStatus::Waiting;
|
||
|
||
// 类型特有属性(根据type选择使用)
|
||
double frameRate = 0.0; // 高光谱相机用
|
||
double exposureTime = 0.0; // 高光谱相机用
|
||
int defaultRenderBand = 550; // 1000-1700nm高光谱用
|
||
int captureIntervalSeconds = 5; // 单反/深度相机用
|
||
};
|
||
|
||
// ==================== 定时任务 ====================
|
||
|
||
struct TimedTask {
|
||
int id = 0; // 任务ID
|
||
QDateTime scheduledTime; // 计划时间
|
||
QDateTime startTime; // 开始时间
|
||
QDateTime endTime; // 结束时间
|
||
int durationSeconds = 0; // 耗时(秒)
|
||
QString savePath; // 数据保存路径
|
||
QVector<SubTask> subTasks; // 子任务列表
|
||
TaskStatus status = TaskStatus::Waiting; // 状态
|
||
|
||
// 计算所有子任务的预计总时间
|
||
int totalEstimatedDuration() const {
|
||
int total = 0;
|
||
for (const auto& subTask : subTasks) {
|
||
total += subTask.estimatedDurationSeconds;
|
||
}
|
||
return total;
|
||
}
|
||
|
||
// 获取子任务数量
|
||
int subTaskCount() const {
|
||
return subTasks.size();
|
||
}
|
||
};
|
||
|
||
// ==================== Qt元类型声明 ====================
|
||
Q_DECLARE_METATYPE(TaskStatus)
|
||
Q_DECLARE_METATYPE(SubTaskType)
|
||
Q_DECLARE_METATYPE(SubTask)
|
||
Q_DECLARE_METATYPE(TimedTask)
|
||
|
||
// ==================== 任务文件读写类 ====================
|
||
|
||
class TimedDataCollectionDataStructuresReaderWriter
|
||
{
|
||
public:
|
||
// 保存任务到文件
|
||
static bool saveTasksToFile(const QString& filePath, const QVector<TimedTask>& tasks);
|
||
|
||
// 从文件读取任务
|
||
static bool loadTasksFromFile(const QString& filePath, QVector<TimedTask>& tasks);
|
||
|
||
private:
|
||
// SubTask序列化
|
||
static QJsonObject subTaskToJson(const SubTask& subTask);
|
||
static bool jsonToSubTask(const QJsonObject& json, SubTask& subTask);
|
||
|
||
// TimedTask序列化
|
||
static QJsonObject timedTaskToJson(const TimedTask& task);
|
||
static bool jsonToTimedTask(const QJsonObject& json, TimedTask& task);
|
||
|
||
// 枚举转换
|
||
static QString taskStatusToString(TaskStatus status);
|
||
static TaskStatus stringToTaskStatus(const QString& str);
|
||
|
||
static QString subTaskTypeToString(SubTaskType type);
|
||
static SubTaskType stringToSubTaskType(const QString& str);
|
||
};
|
||
|
||
// ==================== 任务执行器 ====================
|
||
|
||
class TaskExecutor : public QObject
|
||
{
|
||
Q_OBJECT
|
||
|
||
public:
|
||
explicit TaskExecutor(QObject* parent = nullptr);
|
||
~TaskExecutor();
|
||
|
||
// 执行任务
|
||
void execute(const TimedTask& task);
|
||
void makeFolder(QString savePath);
|
||
QString makeSubTaskDataFolder(QString suffix);
|
||
|
||
// 获取当前执行的子任务索引
|
||
int currentSubTaskIndex() const { return m_currentSubTaskIndex; }
|
||
|
||
// 获取正在执行的任务
|
||
const TimedTask& currentTask() const { return m_task; }
|
||
|
||
// 是否正在执行
|
||
bool isRunning() const { return m_isRunning; }
|
||
|
||
// 停止执行
|
||
void stop();
|
||
|
||
signals:
|
||
void finished(bool success); // 任务完成
|
||
void subTaskStarted(int subTaskIndex, SubTaskType type); // 子任务开始
|
||
void subTaskFinished(int subTaskIndex, SubTaskType type, bool success); // 子任务完成
|
||
void errorOccurred(const QString& error); // 错误发生
|
||
|
||
// 采集相关信号
|
||
void hyperCamParm(int camType, double f, double e, QString filePath, QString fileName);
|
||
void camParm(int camType, int captureIntervalSeconds, QString folder);
|
||
void motorParm(QString pathLineFilePath);
|
||
void startRecordSignal(int camType);
|
||
|
||
public slots:
|
||
void onSequenceComplete(int status);
|
||
void onError(const QString& error);
|
||
|
||
private:
|
||
void executeNextSubTask();
|
||
|
||
TimedTask m_task;
|
||
int m_currentSubTaskIndex;
|
||
bool m_isRunning;
|
||
};
|
||
|
||
// ==================== 任务调度器 ====================
|
||
|
||
class TaskScheduler : public QObject
|
||
{
|
||
Q_OBJECT
|
||
|
||
public:
|
||
explicit TaskScheduler(QObject* parent = nullptr);
|
||
~TaskScheduler();
|
||
|
||
// 加载任务列表
|
||
void loadTasks(const QVector<TimedTask>& tasks);
|
||
|
||
// 获取任务列表
|
||
QVector<TimedTask> tasks() const { return m_tasks; }
|
||
|
||
// 开始调度
|
||
void start();
|
||
|
||
// 停止调度
|
||
void stop();
|
||
|
||
// 是否正在运行
|
||
bool isRunning() const { return m_timer != nullptr && m_timer->isActive(); }
|
||
|
||
signals:
|
||
void taskStarted(int taskId); // 任务开始
|
||
void taskFinished(int taskId, bool success); // 任务完成
|
||
|
||
// 子任务的信号
|
||
void subTaskStarted(int taskId, int subTaskIndex); // 子任务开始
|
||
void subTaskFinished(int taskId, int subTaskIndex); // 子任务完成
|
||
void errorOccurred(const QString& error); // 错误发生
|
||
void schedulerStateChanged(bool running); // 调度器状态变化
|
||
|
||
// 采集相关信号 (透传 TaskExecutor)
|
||
void hyperCamParm(int camType, double f, double e, QString filePath, QString fileName);
|
||
void camParm(int camType, int captureIntervalSeconds, QString folder);
|
||
void motorParm(QString pathLineFilePath);
|
||
void startRecordSignal(int camType);
|
||
|
||
// 马达反馈的信号
|
||
void sequenceCompleteSignal(int status);
|
||
|
||
private slots:
|
||
void checkTasks(); // 检查任务是否该启动
|
||
void onTaskFinished(bool success);
|
||
void onSubTaskStarted(int subTaskIndex, SubTaskType type);
|
||
void onSubTaskFinished(int subTaskIndex, SubTaskType type, bool success);
|
||
void onExecutorError(const QString& error);
|
||
|
||
private:
|
||
void executeTask(TimedTask& task);
|
||
void updateTaskStatus(int taskId, TaskStatus status);
|
||
void emitError(const QString& error);
|
||
|
||
QTimer* m_timer;
|
||
QVector<TimedTask> m_tasks;
|
||
TaskExecutor* m_currentExecutor;
|
||
int m_currentTaskId;
|
||
};
|