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-09-19 18:54:36 +08:00
|
|
|
|
#include "protocol/MqttEntity.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
|
|
|
|
|
2025-09-19 18:54:36 +08:00
|
|
|
|
JSON::read(jsonroot, "debug", option.debug);
|
|
|
|
|
|
JSON::read(jsonroot, "weburl", option.webSrvUrl);
|
|
|
|
|
|
JSON::read(jsonroot, "launchdate", option.lunchDate);
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-07-18 09:08:09 +08:00
|
|
|
|
if (jsonroot.contains("database"))
|
|
|
|
|
|
{
|
2025-09-19 18:54:36 +08:00
|
|
|
|
njson& json = jsonroot.at("database");
|
2025-09-10 20:10:51 +08:00
|
|
|
|
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);
|
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-19 18:54:36 +08:00
|
|
|
|
spdlog::error("[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-19 18:54:36 +08:00
|
|
|
|
njson& json = jsonroot.at("http");
|
|
|
|
|
|
JSON::read(json, "token", option.http.useToken);
|
2025-09-06 15:23:07 +08:00
|
|
|
|
JSON::read(json, "port", option.http.port);
|
2025-09-19 18:54:36 +08:00
|
|
|
|
JSON::read(json, "encryption", option.http.encryption);
|
|
|
|
|
|
JSON::read(json, "encryptKey", option.http.encryptKey);
|
2025-08-31 14:38:53 +08:00
|
|
|
|
}
|
2025-09-04 19:31:04 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-09-19 18:54:36 +08:00
|
|
|
|
spdlog::error("[config] parse http failed: not found.");
|
2025-09-04 19:31:04 +08:00
|
|
|
|
}
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
if (jsonroot.contains("mqtt"))
|
|
|
|
|
|
{
|
2025-09-19 18:54:36 +08:00
|
|
|
|
njson& json = jsonroot.at("mqtt");
|
2025-09-06 15:23:07 +08:00
|
|
|
|
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
|
|
|
|
|
|
{
|
2025-09-19 18:54:36 +08:00
|
|
|
|
spdlog::error("[config] parse mqtt failed: not found.");
|
2025-09-04 19:31:04 +08:00
|
|
|
|
}
|
2025-09-08 19:34:12 +08:00
|
|
|
|
|
2025-09-19 18:54:36 +08:00
|
|
|
|
if (jsonroot.contains("view"))
|
|
|
|
|
|
{
|
|
|
|
|
|
njson& json = jsonroot["view"];
|
|
|
|
|
|
JSON::read(json, "latitude", option.view.latitude);
|
|
|
|
|
|
JSON::read(json, "longitude", option.view.longitude);
|
|
|
|
|
|
JSON::read(json, "altitude", option.view.altitude);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
spdlog::error("[config] parse view failed: not found.");
|
|
|
|
|
|
}
|
2025-09-17 19:55:59 +08:00
|
|
|
|
|
2025-09-19 18:54:36 +08:00
|
|
|
|
if (jsonroot.contains("video"))
|
|
|
|
|
|
{
|
|
|
|
|
|
njson& json = jsonroot["video"];
|
|
|
|
|
|
for (auto& item: json.items())
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& key = item.key();
|
|
|
|
|
|
auto& jsonItem = item.value();
|
|
|
|
|
|
auto& itemVideo = option.mapVideo[key];
|
|
|
|
|
|
JSON::read(jsonItem, "host", itemVideo.host);
|
|
|
|
|
|
JSON::read(jsonItem, "port", itemVideo.port);
|
|
|
|
|
|
JSON::read(jsonItem, "user", itemVideo.user);
|
|
|
|
|
|
JSON::read(jsonItem, "passwd", itemVideo.passwd);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
spdlog::error("[config] parse video failed: not found.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (jsonroot.contains("topic"))
|
|
|
|
|
|
{
|
|
|
|
|
|
njson& json = jsonroot["topic"];
|
|
|
|
|
|
for (auto& item: json.items())
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& key = item.key();
|
|
|
|
|
|
auto& jsonItem = item.value();
|
|
|
|
|
|
auto& info = MqttClient::s_mapTopicInfo[key];
|
|
|
|
|
|
info.name = key;
|
|
|
|
|
|
JSON::read(jsonItem, "deviceType", info.deviceType);
|
|
|
|
|
|
JSON::read(jsonItem, "polling", info.polling);
|
|
|
|
|
|
JSON::read(jsonItem, "enabled", info.enabled);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-18 09:08:09 +08:00
|
|
|
|
return true;
|
2025-09-19 18:54:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AppOption::VideoInfo* Config::getVideoInfo(std::string name)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto iter = option.mapVideo.find(name);
|
|
|
|
|
|
if (iter!=option.mapVideo.end())
|
|
|
|
|
|
{
|
|
|
|
|
|
return &(iter->second);
|
|
|
|
|
|
}
|
|
|
|
|
|
return nullptr;
|
2025-07-18 09:08:09 +08:00
|
|
|
|
}
|