新增http、mqtt运行库,实现mqtt功能, 新增spdlog

This commit is contained in:
lixiaoyuan
2025-09-01 20:08:40 +08:00
parent e0b64a20c4
commit 94e467b65e
245 changed files with 54182 additions and 117 deletions

View File

@@ -5,6 +5,7 @@
#include "common/Snowflake.h"
#include "app/Application.h"
#include "app/AppData.h"
#include "app/Config.h"
static NJsonNode FieldsToJsonArray(std::vector<Fields> vecFields)
{
@@ -116,65 +117,72 @@ static std::map<std::string, HandlerOptions> g_mapHttpHandler =
{"/queryPredictionDetail", HandlerOptions(&HttpEntity::queryPredictionDetail, {"token"})},
{"/queryStatSystem", HandlerOptions(&HttpEntity::queryStatSystem, {"token"})},
{"/queryStatTotal", HandlerOptions(&HttpEntity::queryStatTotal, {"token"})},
{"/queryStatDayList", HandlerOptions(&HttpEntity::queryStatDayList, {"token"})},
//{"/insert", HandlerOptions(&HttpEntity::insert, {})},
//{"/update", HandlerOptions(&HttpEntity::update, {})},
//{"/delete", HandlerOptions(&HttpEntity::delete, {})},
};
void HttpEntity::listen(std::string addr, int port)
HttpEntity::HttpEntity()
{
bool useToken = Config::option.http.useToken;
for (auto& item : g_mapHttpHandler)
{
std::string name = item.first;
HandlerOptions& handler = item.second;
this->httpsvr.Get(name, [=, &handler](const httplib::Request& req, httplib::Response& resp)
{
NJsonNode json;
Errcode errcode = Errcode::OK;
if (name != "/login" && Config::option.useToken)
this->httpsvr.Get(name, [=, &handler](const httplib::Request& req, httplib::Response& resp)
{
// 验证token
std::string token = req.get_param_value("token");
if (token.empty())
spdlog::info("[http] request: {}", name);
NJsonNode json;
Errcode errcode = Errcode::OK;
if (name != "/login" && useToken)
{
errcode = Errcode::ERR_TOKEN;
}
else
{
User user = Application::data().getUser(token);
if (user.userId.empty())
// 验证token
std::string token = req.get_param_value("token");
if (token.empty())
{
errcode = Errcode::ERR_TOKEN;
}
else
{
User user = Application::data().getUser(token);
if (user.userId.empty())
{
errcode = Errcode::ERR_TOKEN;
}
}
}
}
std::string errmsg;
if (errcode == Errcode::OK)
{
if (!HttpHelper::CheckRequestParam(req, resp, handler.requiredKeys, errmsg))
std::string errmsg;
if (errcode == Errcode::OK)
{
errcode = Errcode::ERR_PARAM;
if (!HttpHelper::CheckRequestParam(req, resp, handler.requiredKeys, errmsg))
{
errcode = Errcode::ERR_PARAM;
}
else
{
errcode = (this->*(handler.func))(req, resp, json);
}
}
else
{
errcode = (this->*(handler.func))(req, resp, json);
}
}
json["errcode"] = errcode;
json["errmsg"] = ErrcodeStr(errcode) + (errmsg.empty() ? "" : (":"+errmsg));
resp.set_content(json.dump(), "text/plain; charset=utf-8");
resp.status = 200;
});
json["errcode"] = errcode;
json["errmsg"] = ErrcodeStr(errcode) + (errmsg.empty() ? "" : (":"+errmsg));
resp.set_content(json.dump(), "text/plain; charset=utf-8");
resp.status = 200;
});
}
}
void HttpEntity::listen(std::string addr, int port)
{
if (addr.empty()) addr = "0.0.0.0";
httpsvr.listen(addr, port);
spdlog::info("[http] start listen: addr={}:{},token={}", addr, port, Config::option.http.useToken);
httpsvr.listen(addr, port); // 阻塞
}
void HttpEntity::registGet(std::string name, void (HttpEntity::* func)(const httplib::Request& req, httplib::Response& resp))
@@ -498,5 +506,60 @@ Errcode HttpEntity::queryPredictionDetail(const httplib::Request& req, httplib::
jsonData.push_back(jnode);
}
json["data"] = jsonData;
return Errcode::OK;
}
Errcode HttpEntity::queryStatSystem(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
{
auto& appdata = Application::data();
json["launch_date"] = "2025-01-01"; //: 系统上线启用日期格式yyyy-mm-dd
json["income_total"] = "0.00"; // : 累计收益精度0.01
json["station_num"] = Utils::toStr(appdata.getStationCount()); // : 能源站数量
json["storage_device_num "] = Utils::toStr(appdata.getStationCount()); //: 储能设备数量
json["solar_device_num"] = "0"; // : 光伏设备数量
json["capacity_total"] = "0.000"; // : 储能总容量kWh精度0.001
json["elect_gen"] = "0.000"; // : 发电总电量kWh精度0.001
json["elect_grid"] = "0.000"; // : 入网种电量kWh精度0.001
json["storage_elect_in"] = "0.000"; // : 储能充电总电量kWh精度0.001
json["storage_elect_out"] = "0.000"; // : 储能放电总电量kWh精度0.001
return Errcode::OK;
}
Errcode HttpEntity::queryStatTotal(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
{
std::string station_id = req.get_param_value("station_id");
std::string category = req.get_param_value("category");
json["dt"] = "2025-01-01"; //日期
json["storage_elect_in"] = "123.123"; //储能充电电量kWh精度0.001
json["storage_elect_out"] = "123.123"; //储能放电电量kWh精度0.001
json["storage_num_in"] = "1"; //储能设备充电次数
json["storage_num_out"] = "1"; //储能设备放电次数
json["storage_num_err"] = "1"; //储能设备故障次数
json["solar_elect_gen"] = "123.123"; //光伏发电电量kWh精度0.001
json["solar_elect_grid"] = "123.123"; //光伏入网电量kWh精度0.001
json["solar_num_err"] = "1"; //光伏设备故障次数
json["charge_elect"] = "123.123"; //充电设备充电电量kWh精度0.001
json["charge_num"] = "1"; //充电设备充电次数
json["charge_num_err"] = "1"; //充电设备故障次数
json["income_elect"] = ""; //发电收益精度0.01
json["income_charge"] = ""; //充电收益精度0.01
json["usage"] = "";
return Errcode::OK;
}
Errcode HttpEntity::queryStatDayList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
{
std::string station_id = req.get_param_value("station_id");
std::string category = req.get_param_value("category");
std::string dt_start = req.get_param_value("dt_start");
std::string dt_end = req.get_param_value("dt_end");
if (!dt_start.empty() && dt_end.empty())
{
}
return Errcode::OK;
}