mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
67 lines
1.8 KiB
C++
67 lines
1.8 KiB
C++
#include "Config.h"
|
|
|
|
#include <fstream>
|
|
|
|
#include "common/JsonN.h"
|
|
#include "common/Spdlogger.h"
|
|
#include "AppData.h"
|
|
|
|
AppOption Config::option;
|
|
|
|
bool Config::init(std::string filename)
|
|
{
|
|
njson jsonroot;
|
|
bool ret = JSON::load(filename, jsonroot);
|
|
if (!ret)
|
|
{
|
|
spdlog::error("[config] load config file failed, filename={}", filename);
|
|
return false;
|
|
}
|
|
spdlog::info("[config] load config file success, filename={}", filename);
|
|
|
|
if (jsonroot.contains("database"))
|
|
{
|
|
njson json = jsonroot.at("database");
|
|
JSON::read(json, "host", option.database.host);
|
|
JSON::read(json, "port", option.database.port);
|
|
JSON::read(json, "user", option.database.user);
|
|
JSON::read(json, "passwd", option.database.passwd);
|
|
JSON::read(json, "dbname", option.database.dbname);
|
|
|
|
spdlog::info("[config] parse database success. host={}", option.database.host);
|
|
}
|
|
else
|
|
{
|
|
spdlog::info("[config] parse database failed: not found.");
|
|
}
|
|
|
|
if (jsonroot.contains("http"))
|
|
{
|
|
njson json = jsonroot.at("http");
|
|
std:string token;
|
|
JSON::read(json, "token", token);
|
|
option.http.useToken = !token.empty();
|
|
JSON::read(json, "port", option.http.port);
|
|
}
|
|
else
|
|
{
|
|
spdlog::info("[config] parse http failed: not found.");
|
|
}
|
|
|
|
if (jsonroot.contains("mqtt"))
|
|
{
|
|
njson json = jsonroot.at("mqtt");
|
|
JSON::read(json, "host", option.mqtt.host);
|
|
JSON::read(json, "username", option.mqtt.username);
|
|
JSON::read(json, "password", option.mqtt.password);
|
|
}
|
|
else
|
|
{
|
|
spdlog::info("[config] parse mqtt failed: not found.");
|
|
}
|
|
|
|
JSON::read(jsonroot, "weburl", option.webSrvUrl);
|
|
JSON::read(jsonroot, "launchdate", option.lunchDate);
|
|
|
|
return true;
|
|
} |