41 lines
1.0 KiB
C++
41 lines
1.0 KiB
C++
#include "remote_ctrl/config.hpp"
|
|
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <nlohmann/json.hpp>
|
|
|
|
using json = nlohmann::json;
|
|
|
|
bool config::load(const std::string& path, Config& cfg)
|
|
{
|
|
try
|
|
{
|
|
std::ifstream ifs(path);
|
|
if (!ifs.is_open())
|
|
{
|
|
std::cerr << "Failed to open config file: " << path << std::endl;
|
|
return false;
|
|
}
|
|
|
|
json j;
|
|
ifs >> j;
|
|
|
|
const auto& mqtt = j.at("mqtt");
|
|
|
|
cfg.mqtt_ip = mqtt.at("external_net_address").get<std::string>();
|
|
cfg.mqtt_port = mqtt.at("external_net_port").get<int>();
|
|
cfg.mqtt_username = mqtt.at("username").get<std::string>();
|
|
cfg.mqtt_password = mqtt.at("password").get<std::string>();
|
|
|
|
// remote_ctrl 用这个
|
|
cfg.remote_topic_template = mqtt.at("remote_topic").get<std::string>();
|
|
|
|
return true;
|
|
}
|
|
catch (const std::exception& e)
|
|
{
|
|
std::cerr << "Error parsing remote_ctrl config: " << e.what() << std::endl;
|
|
return false;
|
|
}
|
|
}
|