71 lines
2.2 KiB
C++
71 lines
2.2 KiB
C++
// 文件名: src/deviceManager/device_manager.h
|
||
#ifndef DEVICE_MANAGER_H
|
||
#define DEVICE_MANAGER_H
|
||
|
||
#include "protocol/iprotocol_adapter.h"
|
||
#include "modbus/modbus_rtu_poller_service.h"
|
||
#include "modbus/modbus_master_poller.h"
|
||
#include <boost/asio.hpp>
|
||
#include <vector>
|
||
#include <memory>
|
||
#include <string>
|
||
#include <mutex>
|
||
|
||
/**
|
||
* @brief 用于向API层传递设备信息的统一结构体
|
||
*/
|
||
struct DeviceInfo {
|
||
std::string id;
|
||
std::string type; // 例如 "ModbusRTU", "ModbusTCP"
|
||
bool is_running;
|
||
|
||
// 使用 map 存储连接相关的详细信息,增加灵活性
|
||
// 例如: {"IP Address": "192.168.1.10", "Port": "502"}
|
||
// 或 {"Port Path": "/dev/ttyS0", "Baud Rate": "9600"}
|
||
std::map<std::string, std::string> connection_details;
|
||
};
|
||
|
||
/**
|
||
* @brief 设备管理模块
|
||
* * 负责从配置文件加载设备信息,并动态创建、启动和停止相应的
|
||
* 设备服务实例(如Modbus轮询器)。
|
||
*/
|
||
class DeviceManager {
|
||
public:
|
||
DeviceManager(boost::asio::io_context& io_context, ReportDataCallback report_cb);
|
||
~DeviceManager();
|
||
|
||
// 禁止拷贝和赋值
|
||
DeviceManager(const DeviceManager&) = delete;
|
||
DeviceManager& operator=(const DeviceManager&) = delete;
|
||
|
||
/**
|
||
* @brief 从JSON配置文件加载所有设备并启动服务
|
||
* @param config_path JSON配置文件的路径
|
||
*/
|
||
void load_and_start(const std::string& config_path);
|
||
/**
|
||
* @brief 获取所有当前管理的设备的信息
|
||
* @return 包含所有设备信息的vector
|
||
*/
|
||
std::vector<DeviceInfo> get_all_device_info() const;
|
||
|
||
/**
|
||
* @brief 安全地停止所有正在运行的设备服务
|
||
*/
|
||
void stop_all();
|
||
|
||
private:
|
||
boost::asio::io_context& m_io_context;
|
||
ReportDataCallback m_report_callback;
|
||
|
||
// 用于存储正在运行的服务实例,以管理其生命周期
|
||
std::vector<std::unique_ptr<ModbusRtuPollerService>> m_rtu_services;
|
||
std::vector<std::shared_ptr<ModbusMasterPoller>> m_tcp_pollers;
|
||
|
||
// --- <<< 新增成员 >>> ---
|
||
// mutable 关键字允许在 const 成员函数中修改它 (例如在 get_all_device_info 中加锁)
|
||
mutable std::mutex m_mutex;
|
||
};
|
||
|
||
#endif // DEVICE_MANAGER_H
|