2025-07-18 09:08:09 +08:00
|
|
|
|
#include "Config.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
|
|
|
|
#include "common/JsonN.h"
|
2025-09-01 20:08:40 +08:00
|
|
|
|
#include "common/Spdlogger.h"
|
2025-08-31 14:38:53 +08:00
|
|
|
|
#include "AppData.h"
|
2025-07-18 09:08:09 +08:00
|
|
|
|
|
|
|
|
|
|
AppOption Config::option;
|
|
|
|
|
|
|
|
|
|
|
|
bool Config::init(std::string filename)
|
|
|
|
|
|
{
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson jsonroot;
|
|
|
|
|
|
bool ret = JSON::load(filename, jsonroot);
|
2025-07-18 09:08:09 +08:00
|
|
|
|
if (!ret)
|
|
|
|
|
|
{
|
2025-09-01 20:08:40 +08:00
|
|
|
|
spdlog::error("[config] load config file failed, filename={}", filename);
|
2025-07-18 09:08:09 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-09-01 20:08:40 +08:00
|
|
|
|
spdlog::info("[config] load config file success, filename={}", filename);
|
2025-07-18 09:08:09 +08:00
|
|
|
|
|
|
|
|
|
|
if (jsonroot.contains("database"))
|
|
|
|
|
|
{
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson json = jsonroot.at("database");
|
2025-07-18 09:08:09 +08:00
|
|
|
|
option.database.host = json.contains("host") ? json.at("host") : "";
|
|
|
|
|
|
option.database.port = json.contains("port") ? json.at("port") : 0;
|
|
|
|
|
|
option.database.user = json.contains("user") ? json.at("user") : "";
|
|
|
|
|
|
option.database.passwd = json.contains("passwd") ? json.at("passwd") : "";
|
|
|
|
|
|
option.database.dbname = json.contains("dbname") ? json.at("dbname") : "";
|
|
|
|
|
|
|
2025-09-01 20:08:40 +08:00
|
|
|
|
spdlog::info("[config] parse database success. host={}", option.database.host);
|
2025-07-18 09:08:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-09-04 19:31:04 +08:00
|
|
|
|
spdlog::info("[config] parse database failed: not found.");
|
2025-07-18 09:08:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-01 20:08:40 +08:00
|
|
|
|
if (jsonroot.contains("http"))
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson json = jsonroot.at("http");
|
2025-09-01 20:08:40 +08:00
|
|
|
|
std:string token;
|
2025-09-06 15:23:07 +08:00
|
|
|
|
JSON::read(json, "token", token);
|
2025-09-01 20:08:40 +08:00
|
|
|
|
option.http.useToken = !token.empty();
|
2025-09-06 15:23:07 +08:00
|
|
|
|
JSON::read(json, "port", option.http.port);
|
2025-08-31 14:38:53 +08:00
|
|
|
|
}
|
2025-09-04 19:31:04 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
spdlog::info("[config] parse http failed: not found.");
|
|
|
|
|
|
}
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
if (jsonroot.contains("mqtt"))
|
|
|
|
|
|
{
|
2025-09-06 15:23:07 +08:00
|
|
|
|
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);
|
2025-09-04 19:31:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
spdlog::info("[config] parse mqtt failed: not found.");
|
|
|
|
|
|
}
|
2025-07-18 09:08:09 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|