2025-10-13 13:55:15 +08:00
|
|
|
|
#include <iostream>
|
2025-10-13 16:39:46 +08:00
|
|
|
|
#include <chrono>
|
2025-10-16 17:42:59 +08:00
|
|
|
|
// #include "dataStorage/data_storage.h" // 包含你的头文件
|
|
|
|
|
|
// 假设这些头文件已经包含在你的主程序中
|
|
|
|
|
|
#include "modbus/modbus_common.h" // 包含 ModbusRtuDeviceConfig, DataPointConfig, ModbusDataType
|
|
|
|
|
|
#include "modbus/modbus_rtu_poller_service.h" // 包含 ModbusRtuPollerService 类
|
2025-10-13 13:55:15 +08:00
|
|
|
|
|
2025-10-16 17:42:59 +08:00
|
|
|
|
// 模拟一个从设备接收到的原始数据
|
|
|
|
|
|
// void mockDataReceiving() {
|
|
|
|
|
|
// DataStorage& storage = DataStorage::getInstance();
|
|
|
|
|
|
|
|
|
|
|
|
// // 1. 初始化数据库(只需要做一次)
|
|
|
|
|
|
|
|
|
|
|
|
// if (!storage.initialize("/app/db/my_app_data.db")) {
|
|
|
|
|
|
// std::cerr << "Failed to initialize database. Exiting." << std::endl;
|
|
|
|
|
|
// return;
|
|
|
|
|
|
// }
|
2025-10-13 13:55:15 +08:00
|
|
|
|
|
2025-10-16 17:42:59 +08:00
|
|
|
|
// // 2. 模拟收到一批原始数据
|
|
|
|
|
|
// for (int i = 0; i < 10; ++i) {
|
|
|
|
|
|
// std::string raw_payload = "RAW_SENSOR_DATA_" + std::to_string(i) + "_VALUE=123";
|
|
|
|
|
|
// std::string device_id = "sensor-alpha-001";
|
|
|
|
|
|
// std::string protocol = "custom_protocol_v1";
|
|
|
|
|
|
|
|
|
|
|
|
// // 2.1 存储原始数据
|
|
|
|
|
|
// if (!storage.storeRawData(device_id, raw_payload, protocol)) {
|
|
|
|
|
|
// std::cerr << "Failed to store raw data for " << device_id << std::endl;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// // 3. 模拟协议解析器工作,并将数据转换为 UnifiedData 格式
|
|
|
|
|
|
// for (int i = 0; i < 5; ++i) {
|
|
|
|
|
|
// UnifiedData data;
|
|
|
|
|
|
// data.device_id = "sensor-beta-002";
|
|
|
|
|
|
// data.timestamp_ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
|
|
|
|
|
|
|
|
|
|
|
|
// // 构造一个复杂的 JSON 字符串
|
|
|
|
|
|
// data.data_json = "{"
|
|
|
|
|
|
// "\"temperature\": " + std::to_string(25.5 + i * 0.1) + ","
|
|
|
|
|
|
// "\"humidity\": " + std::to_string(60.2 - i * 0.5) + ","
|
|
|
|
|
|
// "\"status\": \"active\","
|
|
|
|
|
|
// "\"error_code\": 0"
|
|
|
|
|
|
// "}";
|
|
|
|
|
|
|
|
|
|
|
|
// // 3.2 存储处理后的数据
|
|
|
|
|
|
// if (!storage.storeProcessedData(data)) {
|
|
|
|
|
|
// std::cerr << "Failed to store processed data for " << data.device_id << std::endl;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// std::cout << "Data storage simulation finished." << std::endl;
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// int main() {
|
|
|
|
|
|
// mockDataReceiving();
|
|
|
|
|
|
// return 0;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// 假设这些头文件已经包含在你的主程序中
|
|
|
|
|
|
// #include "modbus/modbus_common.h" // 包含 ModbusRtuDeviceConfig, DataPointConfig, ModbusDataType
|
|
|
|
|
|
// #include "modbus_rtu_poller_service.h" // 包含 ModbusRtuPollerService 类
|
|
|
|
|
|
// #include "data_storage.h" // 上一个问题中的数据存储类,用来接收最终数据
|
|
|
|
|
|
// #include <iostream>
|
|
|
|
|
|
// 这是你的主程序或某个初始化函数中的代码
|
|
|
|
|
|
int main() {
|
|
|
|
|
|
// 1. 创建具体的数据点配置列表
|
|
|
|
|
|
std::vector<DataPointConfig> data_points;
|
|
|
|
|
|
|
|
|
|
|
|
// 配置第一个数据点:温度
|
|
|
|
|
|
DataPointConfig temp_config;
|
|
|
|
|
|
temp_config.name = "temperature_celsius";
|
|
|
|
|
|
temp_config.address = 0x01;
|
|
|
|
|
|
temp_config.type = ModbusDataType::FLOAT32;
|
|
|
|
|
|
temp_config.scale = 1.0; // 假设原始值需要乘以0.1才能得到真实温度
|
|
|
|
|
|
data_points.push_back(temp_config);
|
|
|
|
|
|
// 配置第二个数据点:状态
|
|
|
|
|
|
DataPointConfig status_config;
|
|
|
|
|
|
status_config.name = "device_status";
|
|
|
|
|
|
status_config.address = 0x03;
|
|
|
|
|
|
status_config.type = ModbusDataType::FLOAT32;
|
|
|
|
|
|
status_config.scale = 1.0; // 不需要缩放
|
|
|
|
|
|
data_points.push_back(status_config);
|
|
|
|
|
|
// 2. 创建完整的设备配置
|
|
|
|
|
|
ModbusRtuDeviceConfig device_config;
|
|
|
|
|
|
device_config.device_id = "boiler-room-sensor-01"; // 给设备一个唯一的ID,很重要!
|
|
|
|
|
|
device_config.port_path = "/dev/ttyS7"; // Linux下的串口设备名
|
|
|
|
|
|
device_config.baud_rate = 9600; // 波特率
|
|
|
|
|
|
device_config.slave_id = 0x02; // Modbus从站地址
|
|
|
|
|
|
device_config.poll_interval_ms = 2000; // 每2秒轮询一次
|
|
|
|
|
|
device_config.data_points = data_points; // 将我们刚刚配置的数据点列表放进去
|
|
|
|
|
|
// ... 接下来进入管理层 ...
|
|
|
|
|
|
}
|