plan_execute_t1/include/app.hpp

396 lines
17 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 CMS_APP_HPP
#define CMS_APP_HPP
#include <cstdint>
#include <string>
#include <vector>
#include <chrono>
#include <unordered_map>
#include <functional>
// ══════════════════════════════════════════════════════════════════════════════
// Core Data Structures
// ══════════════════════════════════════════════════════════════════════════════
/**
* @brief 运行模式枚举
*
* 描述系统当前所处的运行模式,影响交互方式与自动化程度。
*/
enum class RunMode : uint8_t {
Idle = 0, ///< 空闲模式
HumanLoop = 1, ///< 人在环模式(人工干预)
AutoExec = 2, ///< 自主执行模式
Degraded = 3 ///< 降级维护模式
};
/**
* @brief 任务方案类型
*/
enum class PlanType : uint8_t {
Centralized = 0, ///< 集中式方案
Distributed = 1 ///< 分布式方案
};
/**
* @brief 任务方案状态
*/
enum class PlanStatus : uint8_t {
Drafting = 0, ///< 草稿中
Confirmed = 1, ///< 已确认
Dispatching = 2, ///< 分发中
Executing = 3, ///< 执行中
Completed = 4, ///< 已完成
Aborted = 5 ///< 已中止
};
/**
* @brief 事件处理状态
*/
enum class EventStatus : uint8_t {
Pending = 0, ///< 待处理
Rejected = 1, ///< 已拒绝
Generated = 2 ///< 已生成任务
};
/**
* @brief 通知消息类型
*/
enum class NotificationType : uint8_t {
Alert = 0, ///< 告警
Hint = 1, ///< 提示
Confirm = 2 ///< 确认
};
/**
* @brief TaskPlan — 任务方案数据结构
*
* 包含任务 ID、名称、类型集中/分布)、状态、创建时间、
* 关联事件、拓扑结构、资源配额与执行约束等字段。
*/
struct TaskPlan {
std::string id; ///< 任务唯一标识
std::string name; ///< 任务名称
PlanType type; ///< 方案类型
PlanStatus status; ///< 当前状态
std::chrono::system_clock::time_point createTime; ///< 创建时间戳
std::string relatedEventId; ///< 关联事件 ID
std::vector<std::string> topologyNodes; ///< 拓扑节点列表
double resourceQuota; ///< 资源配额(归一化 0.0 ~ 1.0
std::string constraints; ///< 执行约束描述
};
/**
* @brief EventRecord — 战场事件数据结构
*
* 包括事件 ID、类型标识、时空坐标、载荷数据、优先级、威胁等级、
* 接收时间与处理状态。
*/
struct EventRecord {
std::string id; ///< 事件唯一标识
std::string typeTag; ///< 类型标识
double latitude; ///< 纬度
double longitude; ///< 经度
double altitude; ///< 高度(米)
std::string payload; ///< 载荷数据
uint32_t priority; ///< 优先级0-255越大越紧急
uint32_t threatLevel; ///< 威胁等级1-5
std::chrono::system_clock::time_point receiveTime; ///< 接收时间
EventStatus status; ///< 处理状态
};
/**
* @brief TemplateInstance — 模板实例数据结构
*
* 模板 ID、适用场景、复杂度、版本号、子任务需求树、前置依赖关系、生成置信度。
*/
struct TemplateInstance {
std::string id; ///< 模板唯一标识
std::string scenario; ///< 适用场景描述
double complexity; ///< 复杂度0.0 ~ 1.0
std::string version; ///< 版本号
std::vector<std::string> subTaskTree; ///< 子任务需求树
std::vector<std::string> dependencies; ///< 前置依赖关系
double confidence; ///< 生成置信度0.0 ~ 1.0
};
/**
* @brief ExecutionStatus — 执行状态数据结构
*
* 节点 ID、任务进度百分比、健康指标、资源消耗、心跳信号、异常码、最后上报时间。
*/
struct ExecutionStatus {
std::string nodeId; ///< 节点唯一标识
double progress; ///< 任务进度0.0 ~ 1.0
double healthIndex; ///< 健康指标0.0 ~ 1.0
double resourceUsage; ///< 资源消耗0.0 ~ 1.0
uint64_t heartbeatSignal; ///< 心跳信号计数
uint32_t errorCode; ///< 异常码0 表示正常)
std::chrono::system_clock::time_point lastReport; ///< 最后上报时间
};
/**
* @brief UserSession — 用户会话数据结构
*
* 用户 ID、角色编码、权限位图、访问令牌、会话有效期、登录终端 IP。
*/
struct UserSession {
std::string userId; ///< 用户唯一标识
std::string roleCode; ///< 角色编码
uint64_t permissionBitmap; ///< 权限位图
std::string accessToken; ///< 访问令牌(内存中隔离保护)
std::chrono::system_clock::time_point expireTime; ///< 会话有效期
std::string clientIp; ///< 登录终端 IP
};
/**
* @brief NotificationMessage — 通知消息数据结构
*
* 消息 ID、优先级、类型告警/提示/确认)、内容摘要、触发时间、是否已读。
*/
struct NotificationMessage {
std::string id; ///< 消息唯一标识
uint32_t priority; ///< 优先级
NotificationType type; ///< 消息类型
std::string summary; ///< 内容摘要
std::chrono::system_clock::time_point triggerTime; ///< 触发时间
bool isRead; ///< 是否已读
};
/**
* @brief SystemStateContext — 系统状态上下文数据结构
*
* 当前运行模式(空闲/人在环/自主/降级)、上下文快照、状态一致性标记、切换时间戳。
*/
struct SystemStateContext {
RunMode currentMode; ///< 当前运行模式
std::string contextSnapshot; ///< 上下文快照JSON 序列化)
uint64_t consistencyMark; ///< 状态一致性标记
std::chrono::system_clock::time_point switchTime; ///< 切换时间戳
};
// ══════════════════════════════════════════════════════════════════════════════
// CmsEngine — 核心引擎类
// ══════════════════════════════════════════════════════════════════════════════
/**
* @brief CMS 核心引擎
*
* 负责战场事件接入、任务方案管理、模板匹配、执行状态监控、
* 用户会话维护、通知推送以及系统状态切换等核心业务逻辑的协调。
*/
class CmsEngine {
public:
/// @brief 默认构造函数
CmsEngine();
/// @brief 虚析构函数
virtual ~CmsEngine() = default;
// ── 删除拷贝语义 ────────────────────────────────────────────────────────
CmsEngine(const CmsEngine&) = delete;
CmsEngine& operator=(const CmsEngine&) = delete;
// ══════════════════════════════════════════════════════════════════════════
// 事件处理
// ══════════════════════════════════════════════════════════════════════════
/**
* @brief 接入原始战场事件
*
* 将外部事件总线推送的原始事件接入系统,经初步校验后存入待处理队列。
*
* @param event 待接入的战场事件
* @return true 接入成功
* @return false 接入失败(校验未通过)
*/
bool ingestEvent(const EventRecord& event);
/**
* @brief 处理待处理事件队列
*
* 遍历所有状态为 Pending 的事件,根据优先级和威胁等级自动生成任务草案。
*
* @return size_t 本次处理的事件数量
*/
size_t processPendingEvents();
/**
* @brief 根据事件 ID 获取事件记录
*
* @param eventId 事件唯一标识
* @return const EventRecord* 事件指针,未找到时返回 nullptr
*/
const EventRecord* findEventById(const std::string& eventId) const;
// ══════════════════════════════════════════════════════════════════════════
// 任务方案管理
// ══════════════════════════════════════════════════════════════════════════
/**
* @brief 创建任务方案
*
* 基于模板实例和关联事件生成一个新的任务方案。
*
* @param plan 待创建的任务方案id 由系统自动生成)
* @return true 创建成功
* @return false 创建失败(参数校验不通过)
*/
bool createTaskPlan(TaskPlan& plan);
/**
* @brief 获取所有任务方案列表
*
* @return const std::vector<TaskPlan>& 任务方案列表引用
*/
const std::vector<TaskPlan>& getAllPlans() const;
/**
* @brief 根据任务 ID 查询方案
*
* @param planId 任务唯一标识
* @return const TaskPlan* 方案指针,未找到时返回 nullptr
*/
const TaskPlan* findPlanById(const std::string& planId) const;
/**
* @brief 更新任务方案状态
*
* @param planId 任务标识
* @param newStatus 新状态
* @return true 更新成功
* @return false 未找到对应方案
*/
bool updatePlanStatus(const std::string& planId, PlanStatus newStatus);
// ══════════════════════════════════════════════════════════════════════════
// 模板管理
// ══════════════════════════════════════════════════════════════════════════
/**
* @brief 注册一个任务模板实例
*
* @param tmpl 模板实例
*/
void registerTemplate(const TemplateInstance& tmpl);
/**
* @brief 根据场景匹配最佳模板
*
* 遍历已注册模板库,返回与给定场景匹配度最高的模板。
*
* @param scenario 场景描述关键字
* @return const TemplateInstance* 最佳匹配模板,无匹配时返回 nullptr
*/
const TemplateInstance* matchTemplate(const std::string& scenario) const;
// ══════════════════════════════════════════════════════════════════════════
// 执行监控
// ══════════════════════════════════════════════════════════════════════════
/**
* @brief 上报节点执行状态
*
* 接收分布式节点回传的执行遥测数据并更新内部状态表。
*
* @param status 执行状态快照
*/
void reportExecutionStatus(const ExecutionStatus& status);
/**
* @brief 检查所有节点健康状态,返回异常节点列表
*
* 异常判据:健康指数 < 0.5 或异常码 != 0 或心跳超时(> 30秒
*
* @return std::vector<ExecutionStatus> 异常节点状态列表
*/
std::vector<ExecutionStatus> checkHealth() const;
// ══════════════════════════════════════════════════════════════════════════
// 用户会话管理
// ══════════════════════════════════════════════════════════════════════════
/**
* @brief 创建用户会话
*
* @param session 用户会话信息
* @return true 创建成功
* @return false 会话已存在或参数无效
*/
bool createSession(const UserSession& session);
/**
* @brief 校验用户是否具有指定权限
*
* @param userId 用户 ID
* @param permissionMask 所需权限位掩码
* @return true 有权限
* @return false 无权限或用户不存在
*/
bool checkPermission(const std::string& userId, uint64_t permissionMask) const;
// ══════════════════════════════════════════════════════════════════════════
// 通知管理
// ══════════════════════════════════════════════════════════════════════════
/**
* @brief 推送一条通知消息
*
* @param msg 通知消息
*/
void pushNotification(const NotificationMessage& msg);
/**
* @brief 获取所有未读通知
*
* @return std::vector<NotificationMessage> 未读通知列表
*/
std::vector<NotificationMessage> getUnreadNotifications() const;
// ══════════════════════════════════════════════════════════════════════════
// 系统状态管理
// ══════════════════════════════════════════════════════════════════════════
/**
* @brief 切换系统运行模式
*
* @param newMode 目标模式
* @return true 切换成功
* @return false 切换被拒绝(如降级模式下某些切换受限)
*/
bool switchMode(RunMode newMode);
/**
* @brief 获取当前系统状态上下文
*
* @return const SystemStateContext& 当前状态上下文
*/
const SystemStateContext& getSystemContext() const;
/**
* @brief 获取引擎摘要信息
*
* @return std::string 包含事件数、方案数、节点数、未读通知数等信息的摘要字符串
*/
std::string getSummary() const;
private:
std::vector<TaskPlan> plans_; ///< 任务方案库
std::vector<EventRecord> events_; ///< 事件库
std::vector<TemplateInstance> templates_; ///< 模板库
std::unordered_map<std::string, ExecutionStatus> nodeStatus_; ///< 节点执行状态表
std::unordered_map<std::string, UserSession> sessions_; ///< 用户会话表
std::vector<NotificationMessage> notifications_; ///< 通知消息队列
SystemStateContext systemContext_; ///< 系统状态上下文
uint64_t nextId_; ///< 自增 ID 计数器
/**
* @brief 生成下一个唯一标识
*
* @return std::string 格式为 "ID-xxxxx" 的标识
*/
std::string generateNextId();
};
#endif // CMS_APP_HPP