193 lines
5.8 KiB
C++
193 lines
5.8 KiB
C++
#ifndef ODF_MANAGER_APP_HPP
|
||
#define ODF_MANAGER_APP_HPP
|
||
|
||
#include <string>
|
||
#include <vector>
|
||
#include <map>
|
||
#include <optional>
|
||
#include <chrono>
|
||
#include <mutex>
|
||
|
||
namespace odf {
|
||
|
||
// 端口状态枚举
|
||
enum class PortStatus {
|
||
ACTIVE,
|
||
INACTIVE,
|
||
ERROR_STATE
|
||
};
|
||
|
||
// 端口映射数据结构
|
||
struct PortMapping {
|
||
int mapping_id;
|
||
std::string port_code;
|
||
std::string fiber_core;
|
||
std::string splice_tray;
|
||
std::string cable_core;
|
||
std::string route_info;
|
||
std::chrono::system_clock::time_point create_time;
|
||
std::chrono::system_clock::time_point update_time;
|
||
};
|
||
|
||
// 设备健康状态
|
||
struct DeviceHealth {
|
||
std::string node_id;
|
||
double temperature_celsius;
|
||
double humidity_percent;
|
||
bool is_online;
|
||
PortStatus port_status;
|
||
};
|
||
|
||
// 用户角色枚举
|
||
enum class UserRole {
|
||
ADMIN,
|
||
OPERATOR,
|
||
VIEWER
|
||
};
|
||
|
||
// 用户信息
|
||
struct UserInfo {
|
||
std::string username;
|
||
UserRole role;
|
||
bool is_session_valid;
|
||
};
|
||
|
||
/**
|
||
* 端口映射管理器,负责端口唯一性校验、映射关系的增删改查及冲突检测。
|
||
*/
|
||
class PortMappingManager {
|
||
public:
|
||
/**
|
||
* 添加新的端口映射记录。
|
||
*
|
||
* @requirement(name="端口唯一编号系统", id="SRS-F-05-001")
|
||
* @requirement(name="产品型号编码结构", id="SRS-F-05-002")
|
||
* @requirement(name="内部组件编号与溯源", id="SRS-F-05-003")
|
||
* @requirement(name="端口映射记录表支持", id="SRS-F-05-004")
|
||
* @param port_code 端口唯一编号
|
||
* @param fiber_core 纤芯编号
|
||
* @param splice_tray 熔接盘号
|
||
* @param cable_core 光缆芯号
|
||
* @param route_info 路由信息
|
||
* @return 映射ID,如果失败(如重复或格式错误)返回 -1
|
||
*/
|
||
int addMapping(const std::string& port_code, const std::string& fiber_core,
|
||
const std::string& splice_tray = "", const std::string& cable_core = "",
|
||
const std::string& route_info = "");
|
||
|
||
/**
|
||
* 删除指定端口的映射记录。
|
||
*
|
||
* @requirement(name="端口映射记录表支持", id="SRS-F-05-004")
|
||
* @param port_code 要解除绑定的端口编号
|
||
* @return 是否删除成功
|
||
*/
|
||
bool deleteMapping(const std::string& port_code);
|
||
|
||
/**
|
||
* 查询指定端口的完整映射详情。
|
||
*
|
||
* @requirement(name="端口唯一编号系统", id="SRS-F-05-001")
|
||
* @param port_code 端口编号
|
||
* @return 映射对象,若未找到则返回 std::nullopt
|
||
*/
|
||
std::optional<PortMapping> getMapping(const std::string& port_code);
|
||
|
||
/**
|
||
* 更新指定端口的纤芯绑定及其他信息。
|
||
*
|
||
* @requirement(name="内部组件编号与溯源", id="SRS-F-05-003")
|
||
* @requirement(name="端口映射记录表支持", id="SRS-F-05-004")
|
||
* @param port_code 端口编号
|
||
* @param new_fiber_core 新的纤芯编号
|
||
* @param splice_tray 新的熔接盘号
|
||
* @param cable_core 新的光缆芯号
|
||
* @param route_info 新的路由信息
|
||
* @return 是否更新成功
|
||
*/
|
||
bool updateMapping(const std::string& port_code, const std::string& new_fiber_core,
|
||
const std::string& splice_tray = "", const std::string& cable_core = "",
|
||
const std::string& route_info = "");
|
||
|
||
private:
|
||
// 模拟数据库存储
|
||
std::map<std::string, PortMapping> mappings_;
|
||
int next_id_ = 1;
|
||
std::mutex mutex_;
|
||
|
||
// 辅助:验证端口编码格式 (SRS-F-05-002)
|
||
bool validatePortCodeFormat(const std::string& code) const;
|
||
};
|
||
|
||
/**
|
||
* 设备监控器,负责硬件节点健康度轮询、离线/故障告警生成及环境参数采集。
|
||
*/
|
||
class DeviceMonitor {
|
||
public:
|
||
/**
|
||
* 检查并上报设备节点的健康状态。
|
||
*
|
||
* @requirement(name="CSCI运行适应性需求", id="SRS-NFR-SRS-010")
|
||
* @param node_id 设备节点ID
|
||
* @param temperature 当前温度
|
||
* @param humidity 当前湿度
|
||
* @param is_connected 物理连接状态
|
||
* @return 设备健康快照
|
||
*/
|
||
DeviceHealth checkStatus(const std::string& node_id, double temperature,
|
||
double humidity, bool is_connected);
|
||
|
||
/**
|
||
* 根据阈值判断是否产生告警。
|
||
*
|
||
* @requirement(name="CSCI运行适应性需求", id="SRS-NFR-SRS-010")
|
||
* @param health 设备健康状态
|
||
* @param temp_threshold 温度告警阈值
|
||
* @param humidity_threshold 湿度告警阈值
|
||
* @return 告警消息,若无异常返回空字符串
|
||
*/
|
||
std::string generateAlert(const DeviceHealth& health, double temp_threshold = 40.0,
|
||
double humidity_threshold = 80.0);
|
||
};
|
||
|
||
/**
|
||
* 用户认证服务,负责账号登录验证、RBAC角色权限控制及会话管理。
|
||
*/
|
||
class UserService {
|
||
public:
|
||
/**
|
||
* 验证用户登录凭证并签发会话令牌。
|
||
*
|
||
* @requirement(name="安全性需求", id="SRS-NFR-SRS-011")
|
||
* @param username 用户名
|
||
* @param password 密码
|
||
* @return 用户信息,若验证失败返回 nullopt
|
||
*/
|
||
std::optional<UserInfo> authenticate(const std::string& username, const std::string& password);
|
||
|
||
/**
|
||
* 检查用户是否具有执行特定操作所需的角色权限。
|
||
*
|
||
* @requirement(name="安全性需求", id="SRS-NFR-SRS-011")
|
||
* @param user 当前用户
|
||
* @param required_role 所需最低角色
|
||
* @return 是否拥有权限
|
||
*/
|
||
bool hasPermission(const UserInfo& user, UserRole required_role);
|
||
|
||
/**
|
||
* 注销用户会话,使其失效。
|
||
*
|
||
* @requirement(name="安全性需求", id="SRS-NFR-SRS-011")
|
||
* @param username 用户名
|
||
*/
|
||
void logout(const std::string& username);
|
||
|
||
private:
|
||
std::map<std::string, UserInfo> users_;
|
||
std::mutex mutex_;
|
||
};
|
||
|
||
} // namespace odf
|
||
|
||
#endif // ODF_MANAGER_APP_HPP
|