task_auto_plan_execute_project/include/config.hpp

290 lines
7.4 KiB
C++
Raw Permalink Normal View History

2026-05-01 05:54:47 +00:00
#ifndef C2SYS_CONFIG_HPP
#define C2SYS_CONFIG_HPP
/**
* @file config.hpp
* @brief C2SYS
*
*
*
* SRS-C2SYS I_PLAN_DATAI_EVT_RAWI_EVT_STD
* I_TMPL_SETI_TASK_TELEMI_NODE_ACK
*/
#include <string>
#include <vector>
#include <chrono>
#include <unordered_map>
#include <cstdint>
#include <fstream>
#include <sstream>
#include <stdexcept>
namespace c2sys {
/// @brief 运行模式枚举
enum class RunMode : uint8_t {
/// 人在回路模式 — 人工干预决策
HumanInLoop = 0,
/// 自主执行模式 — 系统自动决策
Autonomous = 1
};
/// @brief 方案类型枚举
enum class PlanType : uint8_t {
/// 集中式方案
Centralized = 0,
/// 分布式方案
Distributed = 1
};
/// @brief 事件处理状态
enum class EventStatus : uint8_t {
/// 待处理
Pending = 0,
/// 已忽略
Ignored = 1,
/// 已处理
Processed = 2
};
/// @brief 分发响应状态
enum class AckStatus : uint8_t {
/// 确认接收
Confirmed = 0,
/// 拒绝执行
Rejected = 1,
/// 响应超时
Timeout = 2
};
/// @brief 事件类型
enum class EventType : uint8_t {
/// 临机事件
Contingency = 0,
/// 规划事件
Planned = 1
};
/**
* @brief (SRS-C2SYS_I_PLAN_DATA)
*
*
*/
struct PlanData {
/// 方案唯一标识
std::string id;
/// 方案类型(集中式 / 分布式)
PlanType type;
/// 资源占用百分比 (0.0 ~ 100.0)
double resourceUsage;
/// 执行周期(秒)
double executionPeriod;
/// 预估成功率 (0.0 ~ 1.0)
double successRate;
/// 抗毁性评分 (0.0 ~ 1.0)
double survivability;
/// 协同效率评分 (0.0 ~ 1.0)
double collaborationEfficiency;
/// 子任务ID列表
std::vector<std::string> subTaskIds;
/// 创建时间Unix 时间戳,毫秒)
int64_t createTimestamp;
};
/**
* @brief (SRS-C2SYS_I_EVT_RAW)
*
*
*/
struct EventRaw {
/// 源设备标识
std::string sourceId;
/// 事件发生时间戳(毫秒)
int64_t timestamp;
/// 威胁等级 (1 ~ 5)
int threatLevel;
/// 原始报文 SHA256 哈希
std::string payloadHash;
/// 协议类型标识
std::string protocolType;
/// 报文内容体(十六进制字符串 or JSON
std::string payload;
};
/**
* @brief (SRS-C2SYS_I_EVT_STD)
*
*
*/
struct StandardEvent {
/// 统一事件 ID
std::string id;
/// 事件类型(临机 / 规划)
EventType type;
/// 处理状态
EventStatus status;
/// 关联的任务 ID可为空
std::string relatedTaskId;
/// 元数据标签键值对
std::unordered_map<std::string, std::string> metadata;
};
/**
* @brief (SRS-C2SYS_I_TMPL_SET)
*
*
*/
struct TaskTemplate {
/// 模板唯一标识
std::string id;
/// 版本号
std::string version;
/// 适用场景描述
std::string applicableScenario;
/// 权重配置JSON 序列化字符串)
std::string weightConfig;
/// 子任务需求列表
std::vector<std::string> subTaskRequirements;
/// 匹配度评分 (0.0 ~ 1.0)
double matchScore;
};
/**
* @brief (SRS-C2SYS_I_TASK_TELEM)
*
*
*/
struct ExecutionStatus {
/// 任务 ID
std::string taskId;
/// 当前阶段名称
std::string currentPhase;
/// 完成百分比 (0.0 ~ 100.0)
double completionPercent;
/// 心跳时间戳(毫秒)
int64_t heartbeatTimestamp;
/// 异常码0 表示正常)
int errorCode;
/// 影响范围描述
std::string impactScope;
};
/**
* @brief (SRS-C2SYS_I_NODE_ACK)
*
*
*/
struct DistributionAck {
/// 节点 ID
std::string nodeId;
/// 响应状态
AckStatus status;
/// 回执时间戳(毫秒)
int64_t timestamp;
/// 已重试次数
int retryCount;
};
/**
* @brief
*
*
*/
struct OperationLog {
/// 操作员标识
std::string operatorId;
/// 操作时间戳(毫秒)
int64_t timestamp;
/// 动作类型描述
std::string actionType;
/// 操作对象 ID
std::string objectId;
/// 操作结果状态
std::string resultStatus;
};
/**
* @brief
*
*
*/
struct ConfigSnapshot {
/// 快照版本号
std::string version;
/// 快照时间戳(毫秒)
int64_t timestamp;
/// 配置内容JSON 序列化字符串)
std::string configData;
};
/**
* @brief
*
*
*/
struct ModeStateCache {
/// 当前运行模式
RunMode currentMode;
/// 上下文快照JSON 序列化字符串)
std::string contextSnapshot;
/// 最后更新时间戳(毫秒)
int64_t lastUpdateTimestamp;
};
/**
* @brief
*
* JSON KV
*/
struct SystemConfig {
/// 刷新周期(毫秒),默认 50ms
int refreshIntervalMs = 50;
/// WebSocket 端口号
int websocketPort = 8080;
/// 事件队列最大容量
int eventQueueCapacity = 10000;
/// 分发重试最大次数
int maxRetryCount = 3;
/// 分发超时时间(毫秒)
int ackTimeoutMs = 2000;
/// 日志级别 (0=TRACE, 1=DEBUG, 2=INFO, 3=WARN, 4=ERROR)
int logLevel = 2;
/**
* @brief KV
* @param filepath
* @return true
*
* key=value
* refreshIntervalMs=100
* websocketPort=9090
* maxRetryCount=5
*/
bool loadFromFile(const std::string& filepath) {
std::ifstream file(filepath);
if (!file.is_open()) {
return false;
}
std::string line;
while (std::getline(file, line)) {
if (line.empty() || line[0] == '#') continue;
auto eqPos = line.find('=');
if (eqPos == std::string::npos) continue;
std::string key = line.substr(0, eqPos);
std::string val = line.substr(eqPos + 1);
if (key == "refreshIntervalMs") refreshIntervalMs = std::stoi(val);
else if (key == "websocketPort") websocketPort = std::stoi(val);
else if (key == "eventQueueCapacity") eventQueueCapacity = std::stoi(val);
else if (key == "maxRetryCount") maxRetryCount = std::stoi(val);
else if (key == "ackTimeoutMs") ackTimeoutMs = std::stoi(val);
else if (key == "logLevel") logLevel = std::stoi(val);
}
return true;
}
};
} // namespace c2sys
#endif // C2SYS_CONFIG_HPP