bonus-edge-proxy/src/deviceManager/device_manager.h

71 lines
2.2 KiB
C
Raw Normal View History

// 文件名: src/deviceManager/device_manager.h
2025-10-14 16:45:50 +08:00
#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;
};
2025-10-14 16:45:50 +08:00
/**
* @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;
2025-10-14 16:45:50 +08:00
/**
* @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;
2025-10-14 16:45:50 +08:00
};
#endif // DEVICE_MANAGER_H