task_plan/include/app.hpp

358 lines
12 KiB
C++
Raw Normal View History

2026-05-20 08:00:11 +00:00
#ifndef ETMS_APP_HPP
#define ETMS_APP_HPP
2026-05-20 09:17:29 +00:00
#include <cstdint>
2026-05-20 08:00:11 +00:00
#include <string>
#include <vector>
2026-05-20 09:17:29 +00:00
#include <unordered_map>
#include <chrono>
2026-05-20 08:00:11 +00:00
namespace etms {
2026-05-20 09:17:29 +00:00
// ────────────────────────────────────────────────────────────────────────────
// Enums
// ────────────────────────────────────────────────────────────────────────────
/// @brief 事件等级枚举
enum class EventLevel : uint8_t {
Info = 0, ///< 普通信息
Warning = 1, ///< 警告
Serious = 2, ///< 严重
Critical = 3 ///< 危急
};
/// @brief 事件状态枚举
enum class EventStatus : uint8_t {
Received = 0, ///< 已接收
Processed = 1, ///< 已处理
PendingTask = 2, ///< 待生成任务
Rejected = 3, ///< 已拒绝
TaskGenerated = 4 ///< 任务已生成
};
/// @brief 知识库版本状态
enum class KBVersionStatus : uint8_t {
Disabled = 0, ///< 禁用
Enabled = 1 ///< 启用
};
/// @brief 工作模式
enum class WorkMode : uint8_t {
Manual = 0, ///< 人机协同模式(手动选择)
Automatic = 1 ///< 自主推荐模式(自动推荐)
2026-05-20 08:00:11 +00:00
};
2026-05-20 09:17:29 +00:00
// ────────────────────────────────────────────────────────────────────────────
// Data Models
// ────────────────────────────────────────────────────────────────────────────
2026-05-20 08:00:11 +00:00
2026-05-20 09:17:29 +00:00
/**
* @brief
*
* t_event
*/
2026-05-20 08:00:11 +00:00
struct Event {
2026-05-20 09:17:29 +00:00
std::string id; ///< 主键ID
std::string eventId; ///< 事件唯一标识
std::string eventType; ///< 事件类型
int64_t timestamp; ///< 事件发生时间戳Unix毫秒
EventLevel level; ///< 事件等级
double longitude; ///< 经度
double latitude; ///< 纬度
std::string description; ///< 事件描述
EventStatus status; ///< 事件状态
std::chrono::system_clock::time_point createTime; ///< 记录创建时间
/** @brief 默认构造函数 */
Event() : timestamp(0), level(EventLevel::Info), longitude(0.0), latitude(0.0),
status(EventStatus::Received) {}
2026-05-20 08:00:11 +00:00
};
2026-05-20 09:17:29 +00:00
/**
* @brief
*
* t_task_template
*/
2026-05-20 08:00:11 +00:00
struct TaskTemplate {
2026-05-20 09:17:29 +00:00
std::string id; ///< 模板ID
std::string name; ///< 模板名称
std::string type; ///< 模板类型
std::string paramConfig; ///< 参数配置JSON字符串
std::vector<std::string> subTaskIds; ///< 子任务ID列表树状结构
std::string version; ///< 版本号
std::string createTime; ///< 创建时间ISO8601字符串
/** @brief 默认构造函数 */
TaskTemplate() = default;
2026-05-20 08:00:11 +00:00
};
2026-05-20 09:17:29 +00:00
/**
* @brief
*
* t_kb_version
*/
struct KBVersion {
std::string id; ///< 版本ID
std::string description; ///< 版本描述
int64_t effectiveTime; ///< 生效时间Unix毫秒
KBVersionStatus status; ///< 版本状态
std::string creator; ///< 创建人
/** @brief 默认构造函数 */
KBVersion() : effectiveTime(0), status(KBVersionStatus::Disabled) {}
2026-05-20 08:00:11 +00:00
};
2026-05-20 09:17:29 +00:00
/**
* @brief
*
* HTTP POST
*/
struct TaskRequest {
std::string requestId; ///< 请求唯一ID
std::string eventRef; ///< 事件引用eventId
std::string templateId; ///< 选定的模板ID
std::string kbVersionId; ///< 锁定的知识库版本ID
std::string workMode; ///< 工作模式标识
std::string initialParams; ///< 初始任务参数JSON字符串
int64_t createTime; ///< 请求创建时间戳
/** @brief 默认构造函数 */
TaskRequest() : createTime(0) {}
2026-05-20 08:00:11 +00:00
};
2026-05-20 09:17:29 +00:00
// ────────────────────────────────────────────────────────────────────────────
// Core Business Classes
// ────────────────────────────────────────────────────────────────────────────
/**
* @brief
*
*
* JSON
*/
class EventManager {
public:
/** @brief 默认构造函数 */
EventManager();
/** @brief 虚析构 */
virtual ~EventManager();
/**
* @brief
*
* MQTT/Kafka JSON
* @param rawJson JSON
* @return true ACK
* @return false NACK
*/
bool receiveEvent(const std::string& rawJson);
/**
* @brief
*
* Received Processed
* @param eventId
* @return true
* @return false
*/
bool processEvent(const std::string& eventId);
/**
* @brief
*
* Rejected
* @param eventId
* @return true
* @return false
*/
bool rejectEvent(const std::string& eventId);
/**
* @brief ID查询事件
* @param eventId
* @return Event Event
*/
Event getEvent(const std::string& eventId) const;
/**
* @brief
* @return std::vector<Event> PendingTask
*/
std::vector<Event> getPendingEvents() const;
/**
* @brief
* @return size_t
*/
size_t eventCount() const;
/**
* @brief "待生成任务"
*
* PendingTask
* @param eventId
* @return true
* @return false Processed
*/
bool markPendingTask(const std::string& eventId);
private:
/** @brief 内部事件存储(模拟数据库) */
std::unordered_map<std::string, Event> events_;
/**
* @brief JSON
*
* eventIdeventTypetimestamp
* @param rawJson JSON
* @return true
* @return false
*/
bool validateRawJson(const std::string& rawJson) const;
/**
* @brief JSON Event
* @param rawJson JSON
* @return Event
*/
Event parseFromJson(const std::string& rawJson) const;
2026-05-20 08:00:11 +00:00
};
2026-05-20 09:17:29 +00:00
/**
* @brief
*
*
*
*/
class TaskGenerator {
public:
/** @brief 默认构造函数 */
TaskGenerator();
/** @brief 虚析构 */
virtual ~TaskGenerator();
// ── 模板管理 ──
/**
* @brief
*
*
* @param jsonTemplate JSON
* @return true /
* @return false
*/
bool loadTemplate(const std::string& jsonTemplate);
/**
* @brief ID查询模板
* @param templateId ID
* @return TaskTemplate TaskTemplate
*/
TaskTemplate getTemplate(const std::string& templateId) const;
/**
* @brief
* @return std::vector<TaskTemplate>
*/
std::vector<TaskTemplate> getAllTemplates() const;
/**
* @brief
* @param keyword
* @return std::vector<TaskTemplate>
*/
std::vector<TaskTemplate> searchTemplates(const std::string& keyword) const;
// ── 知识库版本管理 ──
/**
* @brief
* @param version
*/
void addKBVersion(const KBVersion& version);
/**
* @brief
* @return std::vector<KBVersion> Enabled
*/
std::vector<KBVersion> getAvailableKBVersions() const;
/**
* @brief
* @param versionId ID
* @return true
* @return false
*/
bool selectKBVersion(const std::string& versionId);
// ── 任务生成 ──
/**
* @brief
* @param mode
*/
void setWorkMode(WorkMode mode);
/**
* @brief
* @return WorkMode
*/
WorkMode getWorkMode() const;
/**
* @brief
*
*
* @param eventId ID
* @param templateId ID
* @return true
*/
bool mapEventToTemplate(const std::string& eventId, const std::string& templateId);
/**
* @brief
*
* TaskRequest
* @param eventId ID
* @return TaskRequest TaskRequest
*/
TaskRequest buildTaskRequest(const std::string& eventId);
/**
* @brief ID
* @return std::string ID
*/
std::string getLockedKBVersion() const;
private:
/** @brief 本地模板库(模拟数据库 t_task_template */
std::unordered_map<std::string, TaskTemplate> templates_;
/** @brief 知识库版本库(模拟数据库 t_kb_version */
std::unordered_map<std::string, KBVersion> kbVersions_;
/** @brief 事件→模板映射表 */
std::unordered_map<std::string, std::string> eventTemplateMap_;
/** @brief 当前工作模式 */
WorkMode workMode_;
/** @brief 当前锁定的知识库版本ID */
std::string lockedKBVersionId_;
/**
* @brief
*
*
* @param ev
* @return std::string ID
*/
std::string autoRecommendTemplate(const Event& ev) const;
};
2026-05-20 08:00:11 +00:00
} // namespace etms
#endif // ETMS_APP_HPP