2025-10-13 13:55:15 +08:00
|
|
|
#include "data_handler.h"
|
|
|
|
|
#include "spdlog/spdlog.h"
|
|
|
|
|
#include "vendor/nlohmann/json.hpp"
|
|
|
|
|
|
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
|
|
|
|
|
DataHandler::DataHandler(MqttClient& client) : m_client(client) {}
|
|
|
|
|
|
|
|
|
|
void DataHandler::handle(mqtt::const_message_ptr msg) {
|
2025-10-16 16:47:55 +08:00
|
|
|
const auto& input_topic = msg->get_topic();
|
|
|
|
|
const auto& payload_str = msg->get_payload_str();
|
|
|
|
|
|
|
|
|
|
spdlog::debug("DataHandler received data from topic: {}", input_topic);
|
|
|
|
|
|
2025-10-13 13:55:15 +08:00
|
|
|
try {
|
2025-10-16 16:47:55 +08:00
|
|
|
auto payload_json = json::parse(payload_str);
|
|
|
|
|
|
|
|
|
|
payload_json["_proxy_source_topic"] = input_topic;
|
|
|
|
|
payload_json["_proxy_processed_ts"] = time(nullptr);
|
|
|
|
|
|
|
|
|
|
const std::string output_topic = "proxy/processed/data";
|
|
|
|
|
|
|
|
|
|
m_client.publish(output_topic, payload_json.dump());
|
|
|
|
|
|
|
|
|
|
spdlog::debug("Successfully forwarded data from '{}' to '{}'", input_topic, output_topic);
|
|
|
|
|
|
2025-10-13 13:55:15 +08:00
|
|
|
} catch (const json::parse_error& e) {
|
2025-10-16 16:47:55 +08:00
|
|
|
spdlog::error("JSON parse error in DataHandler for topic '{}': {}", input_topic, e.what());
|
2025-10-13 13:55:15 +08:00
|
|
|
}
|
|
|
|
|
}
|