task_plan_execute/include/app.hpp

565 lines
18 KiB
C++
Raw Normal View History

2026-06-09 05:14:15 +00:00
#ifndef APP_HPP
#define APP_HPP
#include <string>
#include <vector>
#include <unordered_map>
#include <cstdint>
#include <chrono>
#include <functional>
/**
* @brief
*/
namespace bmp {
// ============================================================================
// 数据结构定义
// ============================================================================
/**
* @brief
*
* @requirement(name="事件接收与处理", id="SU-01")
*
*/
enum class EventStatus {
PENDING, ///< 待处理
PROCESSING, ///< 处理中
ACCEPTED, ///< 已接受
REJECTED, ///< 已拒绝
ERROR ///< 处理异常
};
/**
* @brief
*/
enum class EventPriority {
LOW = 0,
NORMAL = 1,
HIGH = 2,
CRITICAL = 3
};
/**
* @brief
*
* @requirement(name="计划管理", id="SU-06")
*
*/
enum class PlanStatus {
DRAFT, ///< 草稿
CONFIRMED, ///< 已确认
DISTRIBUTING, ///< 分发中
EXECUTING, ///< 执行中
COMPLETED, ///< 已完成
FAILED, ///< 失败
CANCELLED ///< 已取消
};
/**
* @brief
*
* @requirement(name="状态监控", id="SU-08")
*
*/
enum class AssetStatus {
ONLINE, ///< 在线
OFFLINE, ///< 离线
BUSY, ///< 忙碌
ERROR, ///< 故障
STALE ///< 数据陈旧
};
/**
* @brief
*
* @requirement(name="方案分发", id="SU-07")
*
*/
enum class DistributionResponse {
ACK, ///< 确认收到
NACK, ///< 否定确认
TIMEOUT, ///< 超时
ERROR ///< 协议错误
};
/**
* @brief
*
* @requirement(name="事件接收与处理", id="SU-01")
*
*/
struct Event {
std::string id; ///< 事件ID
std::string type; ///< 事件类型
std::string title; ///< 事件标题
std::string description; ///< 事件描述
double latitude = 0.0; ///< 纬度坐标
double longitude = 0.0; ///< 经度坐标
EventPriority priority = EventPriority::NORMAL; ///< 优先级
EventStatus status = EventStatus::PENDING; ///< 当前状态
std::string rawData; ///< 原始JSON/XML数据
std::string receiveNodeId; ///< 接收节点ID
std::chrono::system_clock::time_point occurTime; ///< 发生时间
std::chrono::system_clock::time_point createTime; ///< 创建时间
};
/**
* @brief
*
* @requirement(name="任务模板管理", id="SU-05")
*
*/
struct TaskTemplate {
std::string id; ///< 模板ID
std::string name; ///< 模板名称
std::string scenario; ///< 适用场景描述
std::string content; ///< 模板内容JSON/XML
std::string version; ///< 版本号
std::chrono::system_clock::time_point effectiveTime; ///< 生效时间
std::chrono::system_clock::time_point expireTime; ///< 过期时间
std::chrono::system_clock::time_point createTime; ///< 创建时间
};
/**
* @brief
*
* @requirement(name="计划管理", id="SU-06")
*
*/
struct PlanNode {
std::string nodeId; ///< 节点ID
std::string planId; ///< 所属计划ID
std::string parentNodeId; ///< 父节点ID空为根节点
std::string taskType; ///< 任务类型
std::string executeUnit; ///< 执行单位
std::chrono::system_clock::time_point startTime; ///< 开始时间
std::chrono::system_clock::time_point endTime; ///< 结束时间
PlanStatus status = PlanStatus::DRAFT; ///< 节点状态
};
/**
* @brief
*
* @requirement(name="计划管理", id="SU-06")
*
*/
struct TaskPlan {
std::string id; ///< 计划ID
std::string eventId; ///< 关联事件ID
std::string templateId; ///< 使用的模板ID
std::string responsiblePerson; ///< 负责人
std::string remarks; ///< 备注
PlanStatus status = PlanStatus::DRAFT; ///< 计划状态
std::chrono::system_clock::time_point generateTime; ///< 生成时间
std::vector<PlanNode> nodes; ///< 计划节点列表
};
/**
* @brief
*
* @requirement(name="方案分发", id="SU-07")
*
*/
struct DistributionLog {
std::string logId; ///< 日志ID
std::string planId; ///< 计划ID
std::string targetAsset; ///< 目标资产标识
std::chrono::system_clock::time_point sendTime; ///< 发送时间
DistributionResponse response; ///< 响应状态
int retryCount = 0; ///< 重发次数
std::string errorCode; ///< 错误码
};
/**
* @brief
*
* @requirement(name="状态监控", id="SU-08")
*
*/
struct StatusLog {
std::string recordId; ///< 记录ID
std::string assetId; ///< 资产ID
std::chrono::system_clock::time_point timestamp; ///< 时间戳
std::string statusCode; ///< 状态码
std::string detailInfo; ///< 详细信息
bool isAbnormal = false; ///< 是否异常
};
/**
* @brief
*
* @requirement(name="人机交互界面", id="SU-03")
* RBAC权限模型
*/
struct User {
std::string userId; ///< 用户ID
std::string name; ///< 姓名
std::string role; ///< 角色
int permissionLevel = 0; ///< 权限等级
std::string loginAccount; ///< 登录账号
std::string contactInfo; ///< 联系方式
};
// ============================================================================
// 核心服务接口
// ============================================================================
/**
* @brief
*
* @requirement(name="事件接收与处理", id="SU-01")
* @requirement(name="事件标准化处理", id="SU-02")
*
*/
class EventService {
public:
EventService() = default;
virtual ~EventService() = default;
/**
* @requirement(name="事件接收与处理", id="SU-01")
* JSON/XML格式
* @param rawData
* @return true false
*/
bool receiveEvent(const std::string& rawData);
/**
* @requirement(name="事件接收与处理", id="SU-01")
*
* @param eventId ID
* @return
*/
std::string sendAck(const std::string& eventId);
/**
* @requirement(name="事件标准化处理", id="SU-02")
*
* @param eventId ID
* @return nullptr
*/
const Event* getRawEvent(const std::string& eventId) const;
/**
* @requirement(name="事件标准化处理", id="SU-02")
*
* @param eventId ID
* @return true false
*/
bool processEvent(const std::string& eventId);
/**
* @requirement(name="事件标准化处理", id="SU-02")
*
* @param event
*/
void saveStandardEvent(const Event& event);
/**
* @requirement(name="事件展示与操作", id="SU-03")
*
* @param filters
* @return
*/
std::vector<Event> queryEvents(const std::unordered_map<std::string, std::string>& filters) const;
/**
* @requirement(name="事件展示与操作", id="SU-03")
*
* @return
*/
std::vector<Event> getPendingEvents() const;
private:
std::unordered_map<std::string, Event> events_; ///< 内部事件存储
};
/**
* @brief
*
* @requirement(name="任务模板管理", id="SU-05")
*
*/
class TemplateService {
public:
TemplateService() = default;
virtual ~TemplateService() = default;
/**
* @requirement(name="任务模板管理", id="SU-05")
*
* @param rawData JSON/XML
* @return
*/
size_t receiveTemplates(const std::string& rawData);
/**
* @requirement(name="任务模板管理", id="SU-05")
*
* @return
*/
std::vector<TaskTemplate> getTemplateList() const;
/**
* @requirement(name="任务模板管理", id="SU-05")
*
* @param templateId ID
* @return true
*/
bool selectTemplate(const std::string& templateId);
/**
* @requirement(name="任务模板管理", id="SU-05")
*
* @param eventType
* @return ID
*/
std::string recommendTemplate(const std::string& eventType) const;
/**
* @requirement(name="任务模板管理", id="SU-05")
*
* @return
*/
std::unordered_map<std::string, std::string> getConfig() const;
private:
std::unordered_map<std::string, TaskTemplate> templates_; ///< 模板存储
};
/**
* @brief
*
* @requirement(name="任务生成", id="SU-04")
* @requirement(name="计划管理", id="SU-06")
* HITL通知
*/
class PlanService {
public:
PlanService() = default;
virtual ~PlanService() = default;
/**
* @requirement(name="任务生成", id="SU-04")
* 使
* @param eventId ID
* @param templateId ID
* @param params
* @return ID
*/
std::string generateTask(const std::string& eventId,
const std::string& templateId,
const std::unordered_map<std::string, std::string>& params);
/**
* @requirement(name="任务生成", id="SU-04")
*
* @param planId ID
* @return
*/
TaskPlan submitTask(const std::string& planId);
/**
* @requirement(name="计划管理", id="SU-06")
*
* @return
*/
std::vector<TaskPlan> getPlanList() const;
/**
* @requirement(name="计划管理", id="SU-06")
*
* @param weights
*/
void sortPlans(const std::unordered_map<std::string, int>& weights);
/**
* @requirement(name="计划管理", id="SU-06")
*
* @param planId ID
* @param newNodes
* @return true
*/
bool reconfigurePlan(const std::string& planId, const std::vector<PlanNode>& newNodes);
/**
* @requirement(name="计划管理", id="SU-06")
* HITL
* @param planId ID
* @param message
*/
void notifyHITL(const std::string& planId, const std::string& message);
private:
std::unordered_map<std::string, TaskPlan> plans_; ///< 计划存储
};
/**
* @brief
*
* @requirement(name="方案分发", id="SU-07")
* ACK确认机制
*/
class DistributionService {
public:
DistributionService() = default;
virtual ~DistributionService() = default;
/**
* @requirement(name="方案分发", id="SU-07")
*
* @param planId ID
* @return
*/
std::vector<DistributionLog> distributePlan(const std::string& planId);
/**
* @requirement(name="方案分发", id="SU-07")
* ACK/NACK响应
* @param logId ID
* @param response
* @return true
*/
bool receiveAck(const std::string& logId, DistributionResponse response);
/**
* @requirement(name="方案分发", id="SU-07")
* UI分发状态指示灯
* @param planId ID
*/
void updateStatus(const std::string& planId);
/**
* @requirement(name="方案分发", id="SU-07")
*
* @param planId ID
* @return
*/
std::vector<DistributionLog> getLogsByPlan(const std::string& planId) const;
private:
std::vector<DistributionLog> logs_; ///< 分发日志存储
};
/**
* @brief
*
* @requirement(name="状态监控", id="SU-08")
*
*/
class StatusMonitorService {
public:
StatusMonitorService() = default;
virtual ~StatusMonitorService() = default;
/**
* @requirement(name="状态监控", id="SU-08")
*
* @param rawData
* @return
*/
StatusLog receiveTelemetry(const std::vector<uint8_t>& rawData);
/**
* @requirement(name="状态监控", id="SU-08")
*
* @param rawData
* @return
*/
StatusLog parseData(const std::vector<uint8_t>& rawData);
/**
* @requirement(name="状态监控", id="SU-08")
*
* @param statusLog
* @return
*/
std::string triggerAlarm(const StatusLog& statusLog);
/**
* @requirement(name="状态监控", id="SU-08")
*
* @param assetId ID
* @return
*/
StatusLog getLatestStatus(const std::string& assetId) const;
/**
* @requirement(name="状态监控", id="SU-08")
*
* @param thresholdMs
*/
void checkStaleData(uint64_t thresholdMs);
private:
std::unordered_map<std::string, StatusLog> latestStatus_; ///< 最新状态缓存
};
// ============================================================================
// 系统外观类
// ============================================================================
/**
* @brief
*
* @requirement(name="业务目标", id="REQ-BMP-001")
*
*
*/
class BattlefieldMissionPlanner {
public:
/**
* @brief
*/
BattlefieldMissionPlanner();
/**
* @brief
* @return true
*/
bool initialize();
/**
* @brief
*/
void run();
/**
* @brief
*/
EventService& getEventService() { return eventService_; }
/**
* @brief
*/
TemplateService& getTemplateService() { return templateService_; }
/**
* @brief
*/
PlanService& getPlanService() { return planService_; }
/**
* @brief
*/
DistributionService& getDistributionService() { return distributionService_; }
/**
* @brief
*/
StatusMonitorService& getStatusMonitorService() { return statusMonitorService_; }
private:
EventService eventService_; ///< 事件接收与处理服务
TemplateService templateService_; ///< 任务模板管理服务
PlanService planService_; ///< 任务生成与计划管理服务
DistributionService distributionService_; ///< 方案分发服务
StatusMonitorService statusMonitorService_; ///< 状态监控服务
};
} // namespace bmp
#endif // APP_HPP