2025-08-31 14:38:53 +08:00
|
|
|
|
#include "HttpEntity.h"
|
|
|
|
|
|
#include "database/Dao.h"
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
#include "common/Utils.h"
|
|
|
|
|
|
#include "common/Snowflake.h"
|
|
|
|
|
|
#include "app/Application.h"
|
|
|
|
|
|
#include "app/AppData.h"
|
2025-09-01 20:08:40 +08:00
|
|
|
|
#include "app/Config.h"
|
2025-09-04 19:31:04 +08:00
|
|
|
|
#include "app/Station.h"
|
2025-09-06 15:23:07 +08:00
|
|
|
|
#include "app/Device.h"
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
static void FieldsToJson(Fields& fields, njson& json)
|
2025-09-04 19:31:04 +08:00
|
|
|
|
{
|
|
|
|
|
|
for (auto& item : fields.map())
|
|
|
|
|
|
{
|
|
|
|
|
|
json[item.first] = item.second;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-06 15:23:07 +08:00
|
|
|
|
static void JsonToFields(njson& json, std::vector<std::string> vecKeys, Fields& fields)
|
2025-09-05 19:44:26 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (vecKeys.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
for (auto& item: json.items())
|
|
|
|
|
|
{
|
|
|
|
|
|
auto key = item.key();
|
|
|
|
|
|
auto& val = json[key];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (auto& key : vecKeys)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (json.contains(key)) {
|
|
|
|
|
|
|
|
|
|
|
|
switch (json[key].type())
|
|
|
|
|
|
{
|
2025-09-06 15:23:07 +08:00
|
|
|
|
case njson::value_t::string: { fields.set(key, json[key].get<std::string>()); } break;
|
|
|
|
|
|
case njson::value_t::boolean: { fields.set(key, json[key].get<bool>()); } break;
|
|
|
|
|
|
case njson::value_t::number_integer: { fields.set(key, json[key].get<int>()); } break;
|
|
|
|
|
|
case njson::value_t::number_unsigned: { fields.set(key, json[key].get<int>()); } break;
|
|
|
|
|
|
case njson::value_t::number_float: { fields.set(key, json[key].get<float>()); } break;
|
|
|
|
|
|
case njson::value_t::null: {} break;
|
2025-09-12 18:44:34 +08:00
|
|
|
|
case njson::value_t::object: { fields.set(key, json[key].dump()); } break;
|
|
|
|
|
|
case njson::value_t::array: { fields.set(key, json[key].dump()); } break;
|
2025-09-06 15:23:07 +08:00
|
|
|
|
case njson::value_t::binary: {} break;
|
|
|
|
|
|
case njson::value_t::discarded: {} break;
|
2025-09-05 19:44:26 +08:00
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-04 19:31:04 +08:00
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
static njson FieldsToJsonArray(std::vector<Fields>& vecFields)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson jsonnode = njson::array();
|
2025-08-31 14:38:53 +08:00
|
|
|
|
for (auto& fields : vecFields)
|
|
|
|
|
|
{
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson jnode;
|
2025-08-31 14:38:53 +08:00
|
|
|
|
for (auto& item : fields.map())
|
|
|
|
|
|
{
|
|
|
|
|
|
jnode[item.first] = item.second;
|
|
|
|
|
|
}
|
|
|
|
|
|
jsonnode.push_back(jnode);
|
|
|
|
|
|
}
|
|
|
|
|
|
return jsonnode;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void GetRequestParam(const httplib::Request& req, const std::vector<std::string>& vecKeys, Fields& fields)
|
|
|
|
|
|
{
|
2025-09-04 19:31:04 +08:00
|
|
|
|
if (req.method == "GET")
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
2025-09-04 19:31:04 +08:00
|
|
|
|
for (auto& key : vecKeys)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
2025-09-04 19:31:04 +08:00
|
|
|
|
if (req.has_param(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
fields.set(key, req.get_param_value(key));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (req.method == "POST")
|
|
|
|
|
|
{
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson json;
|
|
|
|
|
|
JSON::parse(req.body, json);
|
2025-09-08 19:34:12 +08:00
|
|
|
|
JsonToFields(json, vecKeys, fields);
|
2025-08-31 14:38:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class HttpHelper
|
|
|
|
|
|
{
|
|
|
|
|
|
public:
|
|
|
|
|
|
static bool CheckRequestParam(const httplib::Request& req, httplib::Response& resp, const std::vector<std::string>& vecKeys, std::string& errmsg)
|
|
|
|
|
|
{
|
|
|
|
|
|
errmsg = "";
|
|
|
|
|
|
for (auto& key : vecKeys)
|
|
|
|
|
|
{
|
2025-09-05 19:44:26 +08:00
|
|
|
|
bool hasParam = false;
|
|
|
|
|
|
if (req.method == "GET")
|
|
|
|
|
|
{
|
|
|
|
|
|
hasParam = req.has_param(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
hasParam = (req.body.find("\"" + key + "\"") != std::string::npos);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!hasParam)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (!errmsg.empty()) { errmsg += ","; }
|
|
|
|
|
|
errmsg += "缺少参数[" + key + "]";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!errmsg.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2025-09-06 15:23:07 +08:00
|
|
|
|
static void setPagination(PageInfo& pageinfo, std::vector<Fields> result, njson& json)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
json["count"] = pageinfo.total;
|
|
|
|
|
|
json["page"] = pageinfo.index;
|
|
|
|
|
|
json["page_size"] = pageinfo.size;
|
|
|
|
|
|
json["data"] = FieldsToJsonArray(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
static std::map<std::string, HandlerOptions> g_mapHttpHandlerGet =
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
{"/login", HandlerOptions(&HttpEntity::login, {DMUser::ACCOUNT, DMUser::PASSWD})},
|
2025-09-04 19:31:04 +08:00
|
|
|
|
{"/queryUserList", HandlerOptions(&HttpEntity::queryUserList, {})},
|
|
|
|
|
|
{"/deleteUser", HandlerOptions(&HttpEntity::deleteUser, { DMUser::USER_ID})},
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
{"/queryPermissionList", HandlerOptions(&HttpEntity::queryPermissionList, {})},
|
|
|
|
|
|
{"/deletePermission", HandlerOptions(&HttpEntity::deletePermission, { DMPermission::PERMISSION_ID})},
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
{"/queryRoleList", HandlerOptions(&HttpEntity::queryRoleList, {})},
|
|
|
|
|
|
{"/deleteRole", HandlerOptions(&HttpEntity::deleteRole, { DMRole::ROLE_ID})},
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
{"/queryStationList", HandlerOptions(&HttpEntity::queryStationList, {})},
|
|
|
|
|
|
{"/deleteStation", HandlerOptions(&HttpEntity::deleteStation, { DMStation::STATION_ID})},
|
|
|
|
|
|
|
|
|
|
|
|
{"/queryStationInfo", HandlerOptions(&HttpEntity::queryStationInfo, { DMStation::STATION_ID})},
|
2025-09-05 19:44:26 +08:00
|
|
|
|
{"/queryStationData", HandlerOptions(&HttpEntity::queryStationData, { DMStation::STATION_ID})},
|
2025-09-06 15:23:07 +08:00
|
|
|
|
{"/queryStationOverview", HandlerOptions(&HttpEntity::queryStationOverview, {DMStation::STATION_ID})},
|
|
|
|
|
|
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
{"/queryDeviceList", HandlerOptions(&HttpEntity::queryDeviceList, {})},
|
|
|
|
|
|
{"/deleteDevice", HandlerOptions(&HttpEntity::deleteDevice, { DMDevice::DEVICE_ID})},
|
|
|
|
|
|
{"/queryDevicTypeDef", HandlerOptions(&HttpEntity::queryDevicTypeDef, {})},
|
2025-09-06 15:23:07 +08:00
|
|
|
|
{"/queryDevicByCategory", HandlerOptions(&HttpEntity::queryDevicByCategory, {DMStation::STATION_ID, "category"})},
|
|
|
|
|
|
{"/queryDevicCharts", HandlerOptions(&HttpEntity::queryDevicCharts, {DMStation::STATION_ID, "device_id"})},
|
2025-09-09 19:26:05 +08:00
|
|
|
|
{"/queryDeviceBCUDetail", HandlerOptions(&HttpEntity::queryDeviceBCUDetail, {DMStation::STATION_ID, "device_id"})},
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
{"/queryPolicyList", HandlerOptions(&HttpEntity::queryPolicyList, {})},
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
{"/deletePolicy", HandlerOptions(&HttpEntity::deletePolicy, { DMPolicy::POLICY_ID})},
|
|
|
|
|
|
|
|
|
|
|
|
{"/querySystemLogList", HandlerOptions(&HttpEntity::querySystemLogList, {})},
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
{"/queryAlertLogList", HandlerOptions(&HttpEntity::queryAlertLogList, {})},
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
{"/queryPredictionDetail", HandlerOptions(&HttpEntity::queryPredictionDetail, {})},
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
{"/queryStatSystem", HandlerOptions(&HttpEntity::queryStatSystem, {})},
|
2025-09-12 18:44:34 +08:00
|
|
|
|
{"/queryStatStation", HandlerOptions(&HttpEntity::queryStatStation, {})},
|
2025-09-04 19:31:04 +08:00
|
|
|
|
{"/queryStatTotal", HandlerOptions(&HttpEntity::queryStatTotal, {})},
|
|
|
|
|
|
{"/queryStatDayList", HandlerOptions(&HttpEntity::queryStatDayList, {})},
|
2025-09-10 20:10:51 +08:00
|
|
|
|
{"/queryStatCharts", HandlerOptions(&HttpEntity::queryStatCharts, {})},
|
2025-09-04 19:31:04 +08:00
|
|
|
|
|
|
|
|
|
|
{"/queryEnvironment", HandlerOptions(&HttpEntity::queryEnvironment, { "station_id"})},
|
2025-09-08 19:34:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{"/queryServiceApiList", HandlerOptions(&HttpEntity::queryServiceApiList, {})},
|
|
|
|
|
|
{"/deleteServiceApi", HandlerOptions(&HttpEntity::deleteServiceApi, {"api_id"})},
|
2025-09-01 20:08:40 +08:00
|
|
|
|
|
2025-08-31 14:38:53 +08:00
|
|
|
|
//{"/insert", HandlerOptions(&HttpEntity::insert, {})},
|
|
|
|
|
|
//{"/update", HandlerOptions(&HttpEntity::update, {})},
|
|
|
|
|
|
//{"/delete", HandlerOptions(&HttpEntity::delete, {})},
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
static std::map<std::string, HandlerOptions> g_mapHttpHandlerPost
|
|
|
|
|
|
{
|
|
|
|
|
|
{"/insertUser", HandlerOptions(&HttpEntity::insertUser, { DMUser::ACCOUNT})},
|
|
|
|
|
|
{"/updateUser", HandlerOptions(&HttpEntity::updateUser, { DMUser::USER_ID})},
|
|
|
|
|
|
|
|
|
|
|
|
{"/insertPermission", HandlerOptions(&HttpEntity::insertPermission, { DMPermission::NAME})},
|
|
|
|
|
|
{"/updatePermission", HandlerOptions(&HttpEntity::updatePermission, { DMPermission::PERMISSION_ID})},
|
|
|
|
|
|
|
|
|
|
|
|
{"/insertRole", HandlerOptions(&HttpEntity::insertRole, { DMRole::NAME})},
|
|
|
|
|
|
{"/updateRole", HandlerOptions(&HttpEntity::updateRole, { DMRole::ROLE_ID})},
|
|
|
|
|
|
|
|
|
|
|
|
{"/insertStation", HandlerOptions(&HttpEntity::insertStation, { DMStation::NAME})},
|
|
|
|
|
|
{"/updateStation", HandlerOptions(&HttpEntity::updateStation, { DMStation::STATION_ID})},
|
|
|
|
|
|
|
|
|
|
|
|
{"/insertDevice", HandlerOptions(&HttpEntity::insertDevice, { DMDevice::NAME})},
|
|
|
|
|
|
{"/updateDevice", HandlerOptions(&HttpEntity::updateDevice, { DMDevice::DEVICE_ID})},
|
|
|
|
|
|
|
|
|
|
|
|
{"/insertPolicy", HandlerOptions(&HttpEntity::insertPolicy, { DMPolicy::NAME})},
|
|
|
|
|
|
{"/updatePolicy", HandlerOptions(&HttpEntity::updatePolicy, { DMPolicy::POLICY_ID})},
|
2025-09-08 19:34:12 +08:00
|
|
|
|
|
|
|
|
|
|
{"/insertServiceApi", HandlerOptions(&HttpEntity::insertServiceApi, {})},
|
|
|
|
|
|
{"/updateServiceApi", HandlerOptions(&HttpEntity::updateServiceApi, {"api_id"})},
|
2025-09-04 19:31:04 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool CheckHttpToken(const httplib::Request& req)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 验证token
|
|
|
|
|
|
std::string token = req.get_param_value("token");
|
|
|
|
|
|
if (!token.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
User user = Application::data().getUser(token);
|
|
|
|
|
|
if (!user.userId.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-09-01 20:08:40 +08:00
|
|
|
|
HttpEntity::HttpEntity()
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
2025-09-04 19:31:04 +08:00
|
|
|
|
for (auto& item : g_mapHttpHandlerGet)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::string name = item.first;
|
|
|
|
|
|
HandlerOptions& handler = item.second;
|
2025-09-01 20:08:40 +08:00
|
|
|
|
this->httpsvr.Get(name, [=, &handler](const httplib::Request& req, httplib::Response& resp)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
2025-09-05 19:44:26 +08:00
|
|
|
|
this->runHandler(name, handler, req, resp);
|
2025-09-04 19:31:04 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
for (auto& item : g_mapHttpHandlerPost)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string name = item.first;
|
|
|
|
|
|
HandlerOptions& handler = item.second;
|
|
|
|
|
|
this->httpsvr.Post(name, [=](const httplib::Request& req, httplib::Response& resp)
|
|
|
|
|
|
{
|
2025-09-05 19:44:26 +08:00
|
|
|
|
this->runHandler(name, handler, req, resp);
|
2025-09-01 20:08:40 +08:00
|
|
|
|
});
|
2025-08-31 14:38:53 +08:00
|
|
|
|
}
|
2025-09-01 20:08:40 +08:00
|
|
|
|
}
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
2025-09-01 20:08:40 +08:00
|
|
|
|
void HttpEntity::listen(std::string addr, int port)
|
|
|
|
|
|
{
|
2025-08-31 14:38:53 +08:00
|
|
|
|
if (addr.empty()) addr = "0.0.0.0";
|
2025-09-01 20:08:40 +08:00
|
|
|
|
spdlog::info("[http] start listen: addr={}:{},token={}", addr, port, Config::option.http.useToken);
|
|
|
|
|
|
httpsvr.listen(addr, port); // 阻塞
|
2025-08-31 14:38:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-05 19:44:26 +08:00
|
|
|
|
void HttpEntity::runHandler(std::string name, const HandlerOptions& handler, const httplib::Request& req, httplib::Response& resp)
|
|
|
|
|
|
{
|
|
|
|
|
|
spdlog::info("[http] request: {}", name);
|
|
|
|
|
|
bool useToken = Config::option.http.useToken;
|
|
|
|
|
|
Errcode errcode = Errcode::OK;
|
|
|
|
|
|
std::string errmsg;
|
|
|
|
|
|
|
|
|
|
|
|
if (name != "/login" && useToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool ret = CheckHttpToken(req);
|
|
|
|
|
|
errcode = ret ? Errcode::OK : Errcode::ERR_TOKEN;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson jsonresp;
|
2025-09-05 19:44:26 +08:00
|
|
|
|
if (errcode == Errcode::OK)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!HttpHelper::CheckRequestParam(req, resp, handler.requiredKeys, errmsg))
|
|
|
|
|
|
{
|
|
|
|
|
|
errcode = Errcode::ERR_PARAM;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-09-06 15:23:07 +08:00
|
|
|
|
errcode = (this->*(handler.func))(req, jsonresp, errmsg);
|
2025-09-05 19:44:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
jsonresp["errcode"] = errcode;
|
|
|
|
|
|
jsonresp["errmsg"] = ErrcodeStr(errcode) + (errmsg.empty() ? "" : (":"+errmsg));
|
|
|
|
|
|
resp.set_content(jsonresp.dump(), "text/plain; charset=utf-8");
|
|
|
|
|
|
resp.status = 200;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-31 14:38:53 +08:00
|
|
|
|
void HttpEntity::registGet(std::string name, void (HttpEntity::* func)(const httplib::Request& req, httplib::Response& resp))
|
|
|
|
|
|
{
|
|
|
|
|
|
this->httpsvr.Get(name, std::bind(func, this, std::placeholders::_1, std::placeholders::_2));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::login(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::string userId;
|
|
|
|
|
|
std::string token;
|
|
|
|
|
|
std::string account = req.get_param_value("account");
|
|
|
|
|
|
std::string passwd = req.get_param_value("passwd");
|
|
|
|
|
|
|
|
|
|
|
|
Fields fields;
|
|
|
|
|
|
auto dao = DaoEntity::create("");
|
|
|
|
|
|
Errcode err = DAO::login(dao, account, passwd, fields);
|
|
|
|
|
|
userId = fields.value(DMUser::USER_ID);
|
|
|
|
|
|
token = Application::data().userLogin(userId, account);
|
|
|
|
|
|
if (err == Errcode::OK)
|
|
|
|
|
|
{
|
|
|
|
|
|
json["token"] = token;
|
2025-09-10 20:10:51 +08:00
|
|
|
|
json["account"] = account;
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
|
|
|
|
|
std::vector<Fields> vecPermission;
|
|
|
|
|
|
int roleId = fields.get<int>(DMRole::ROLE_ID);
|
|
|
|
|
|
DAO::queryRolePermission(dao, roleId, vecPermission);
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson nodePermission = njson::array();
|
2025-09-09 19:26:05 +08:00
|
|
|
|
std::map<std::string, int> mapParentPos;
|
|
|
|
|
|
for (int i=0; i<vecPermission.size(); ++i)
|
2025-09-05 19:44:26 +08:00
|
|
|
|
{
|
2025-09-09 19:26:05 +08:00
|
|
|
|
auto& item = vecPermission[i];
|
|
|
|
|
|
if (item.value("parent_id") == "")
|
|
|
|
|
|
{
|
|
|
|
|
|
mapParentPos[item.value("permission_id")] = i;
|
|
|
|
|
|
njson node;
|
|
|
|
|
|
FieldsToJson(item, node);
|
|
|
|
|
|
node["children"] = njson::array();
|
|
|
|
|
|
nodePermission.push_back(node);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 重新遍历处理二级子权限,防止父权限信息还未创建时导致错误
|
|
|
|
|
|
for (int i = 0; i<vecPermission.size(); ++i)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& item = vecPermission[i];
|
|
|
|
|
|
std::string id = item.value("permission_id");
|
|
|
|
|
|
std::string parentId = item.value("parent_id");
|
|
|
|
|
|
if (parentId != "")
|
|
|
|
|
|
{
|
|
|
|
|
|
auto iter = mapParentPos.find(parentId);
|
|
|
|
|
|
if (iter != mapParentPos.end())
|
|
|
|
|
|
{
|
|
|
|
|
|
njson node;
|
|
|
|
|
|
FieldsToJson(item, node);
|
|
|
|
|
|
nodePermission[iter->second]["children"].push_back(node);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-05 19:44:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
json["permission"] = nodePermission;
|
2025-08-31 14:38:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DAO::insertSystemLogUser(token, "用户登录:" + ErrcodeStr(err), (err==Errcode::OK) ? 0: 1);
|
|
|
|
|
|
return err;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::queryUserList(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::string token = req.get_param_value("token");
|
|
|
|
|
|
PageInfo pageinfo;
|
|
|
|
|
|
pageinfo.index = Utils::toInt(req.get_param_value("page"));
|
|
|
|
|
|
pageinfo.size = Utils::toInt(req.get_param_value("page_size"));
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<Fields> result;
|
|
|
|
|
|
auto err = DAO::queryUserList(pageinfo, result);
|
|
|
|
|
|
if (err == Errcode::OK)
|
|
|
|
|
|
{
|
|
|
|
|
|
HttpHelper::setPagination(pageinfo, result, json);
|
|
|
|
|
|
}
|
|
|
|
|
|
DAO::insertSystemLogUser(token, "查询用户列表:" + ErrcodeStr(err), (err==Errcode::OK) ? 0 : 1);
|
|
|
|
|
|
return err;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::insertUser(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"account", "name", "gender", "age", "phone", "email", "role_id"}, params);
|
|
|
|
|
|
return DAO::insertUser(params);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::updateUser(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
2025-09-05 19:44:26 +08:00
|
|
|
|
GetRequestParam(req, {"user_id", "account", "name", "gender", "age", "phone", "email", "role_id"}, params);
|
2025-08-31 14:38:53 +08:00
|
|
|
|
return DAO::updateUserById(params);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::deleteUser(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::string userId = req.get_param_value("user_id");
|
|
|
|
|
|
return DAO::deleteUserById(userId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::queryPermissionList(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
PageInfo pageinfo;
|
|
|
|
|
|
pageinfo.index = Utils::toInt(req.get_param_value("page"));
|
|
|
|
|
|
pageinfo.size = Utils::toInt(req.get_param_value("page_size"));
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<Fields> result;
|
|
|
|
|
|
auto err = DAO::queryPermissionList(pageinfo, result);
|
|
|
|
|
|
HttpHelper::setPagination(pageinfo, result, json);
|
2025-09-12 18:44:34 +08:00
|
|
|
|
|
|
|
|
|
|
// 查询所有的角色权限关联
|
|
|
|
|
|
if (err == Errcode::OK)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::map<std::string, int> mapP;
|
|
|
|
|
|
for (int i = 0; i<result.size(); ++i)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& item = result[i];
|
|
|
|
|
|
std::string id = item.value("permission_id");
|
|
|
|
|
|
mapP[id] = json["data"].size() - 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<Fields> vecSubPermission;
|
|
|
|
|
|
DaoEntity::execOnce("SELECT * FROM permission WHERE permission.parent_id IS NOT NULL AND permission.parent_id!='';", vecSubPermission);
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i<vecSubPermission.size(); ++i)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& item = vecSubPermission[i];
|
|
|
|
|
|
std::string parentId = item.value("parent_id");
|
|
|
|
|
|
std::string id = item.value("permission_id");
|
|
|
|
|
|
if (!parentId.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (mapP.count(parentId) > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
int index = mapP[parentId];
|
|
|
|
|
|
njson jsonnode;
|
|
|
|
|
|
FieldsToJson(item, jsonnode);
|
|
|
|
|
|
json["data"][index]["children"].push_back(jsonnode);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-31 14:38:53 +08:00
|
|
|
|
return err;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::insertPermission(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"name", "describe", "is_open"}, params);
|
|
|
|
|
|
return DAO::insertPermission(params);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::updatePermission(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"permission_id", "name", "describe", "is_open"}, params);
|
|
|
|
|
|
return DAO::updatePermissionById(params);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::deletePermission(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::string permissionId = req.get_param_value("permission_id");
|
|
|
|
|
|
return DAO::deletePermissionById(permissionId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::queryRoleList(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::string token = req.get_param_value("page");
|
|
|
|
|
|
PageInfo pageinfo;
|
|
|
|
|
|
pageinfo.index = Utils::toInt(req.get_param_value("page"));
|
|
|
|
|
|
pageinfo.size = Utils::toInt(req.get_param_value("page_size"));
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<Fields> result;
|
|
|
|
|
|
auto err = DAO::queryRoleList(pageinfo, result);
|
2025-09-12 18:44:34 +08:00
|
|
|
|
HttpHelper::setPagination(pageinfo, result, json);
|
|
|
|
|
|
|
2025-08-31 14:38:53 +08:00
|
|
|
|
// 查询所有的角色权限关联
|
|
|
|
|
|
if (err == Errcode::OK)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::vector<Fields> vecPermission;
|
|
|
|
|
|
err = DAO::queryRolePermission(NULL, vecPermission);
|
|
|
|
|
|
if (err != Errcode::OK)
|
|
|
|
|
|
{
|
|
|
|
|
|
return err;
|
|
|
|
|
|
}
|
2025-09-12 18:44:34 +08:00
|
|
|
|
|
2025-09-09 19:26:05 +08:00
|
|
|
|
std::map<std::string, std::vector<Fields>> mapP;
|
|
|
|
|
|
for (int i = 0; i<vecPermission.size(); ++i)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
2025-09-09 19:26:05 +08:00
|
|
|
|
auto& item = vecPermission[i];
|
|
|
|
|
|
std::string roleId = item.value("role_id");
|
|
|
|
|
|
std::string id = item.value("permission_id");
|
|
|
|
|
|
std::string parentId = item.value("parent_id");
|
|
|
|
|
|
|
|
|
|
|
|
mapP[roleId].push_back(vecPermission[i]);
|
2025-08-31 14:38:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-09 19:26:05 +08:00
|
|
|
|
for (auto& item : json["data"])
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
2025-09-09 19:26:05 +08:00
|
|
|
|
auto jsonpermission = njson::array();
|
|
|
|
|
|
std::string roleId = item["role_id"];
|
|
|
|
|
|
std::vector<Fields>& vecP = mapP[roleId];
|
|
|
|
|
|
|
|
|
|
|
|
std::map<std::string, int> mapParentPos;
|
|
|
|
|
|
for (int i = 0; i<vecP.size(); ++i)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& item = vecPermission[i];
|
|
|
|
|
|
if (item.value("parent_id") == "")
|
|
|
|
|
|
{
|
|
|
|
|
|
mapParentPos[item.value("permission_id")] = i;
|
|
|
|
|
|
njson node;
|
|
|
|
|
|
FieldsToJson(item, node);
|
|
|
|
|
|
node["children"] = njson::array();
|
|
|
|
|
|
jsonpermission.push_back(node);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 重新遍历处理二级子权限,防止父权限信息还未创建时导致错误
|
|
|
|
|
|
for (int i = 0; i<vecP.size(); ++i)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
2025-09-09 19:26:05 +08:00
|
|
|
|
auto& item = vecP[i];
|
|
|
|
|
|
std::string id = item.value("permission_id");
|
|
|
|
|
|
std::string parentId = item.value("parent_id");
|
|
|
|
|
|
if (parentId != "")
|
|
|
|
|
|
{
|
|
|
|
|
|
auto iter = mapParentPos.find(parentId);
|
|
|
|
|
|
if (iter != mapParentPos.end())
|
|
|
|
|
|
{
|
|
|
|
|
|
njson node;
|
|
|
|
|
|
FieldsToJson(item, node);
|
|
|
|
|
|
jsonpermission[iter->second]["children"].push_back(node);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-31 14:38:53 +08:00
|
|
|
|
}
|
2025-09-09 19:26:05 +08:00
|
|
|
|
item["permission"] = jsonpermission;
|
2025-08-31 14:38:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return err;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-12 18:44:34 +08:00
|
|
|
|
Errcode HttpEntity::queryRolePermission(const httplib::Request& req, njson& json, std::string& errmsg)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"role_id"}, params);
|
|
|
|
|
|
if (!params.contains("role_id")) { errmsg = "缺少参数[role_id]"; return Errcode::ERR_PARAM; }
|
|
|
|
|
|
return Errcode::OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::insertRole(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"name", "describe", "is_open", "permission"}, params);
|
|
|
|
|
|
return DAO::insertRole(params);
|
|
|
|
|
|
};
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::updateRole(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
2025-09-12 18:44:34 +08:00
|
|
|
|
GetRequestParam(req, {"role_id", "name", "describe", "is_open", "permission"}, params);
|
2025-09-05 19:44:26 +08:00
|
|
|
|
|
|
|
|
|
|
auto roleId = params.value("role_id");
|
2025-09-12 18:44:34 +08:00
|
|
|
|
std::string permission = params.remove("permission");
|
|
|
|
|
|
|
2025-09-05 19:44:26 +08:00
|
|
|
|
auto dao = DaoEntity::create("");
|
2025-09-12 18:44:34 +08:00
|
|
|
|
auto err = Errcode::OK;
|
|
|
|
|
|
if (params.size() > 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
err = DAO::updateRoleById(dao, params);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (err == Errcode::OK && !permission.empty())
|
2025-09-05 19:44:26 +08:00
|
|
|
|
{
|
2025-09-12 18:44:34 +08:00
|
|
|
|
njson jsonarray;
|
|
|
|
|
|
if (JSON::parse(permission, jsonarray))
|
2025-09-05 19:44:26 +08:00
|
|
|
|
{
|
2025-09-12 18:44:34 +08:00
|
|
|
|
std::vector<Fields> vecFields;
|
|
|
|
|
|
for (auto& item: jsonarray)
|
2025-09-05 19:44:26 +08:00
|
|
|
|
{
|
2025-09-12 18:44:34 +08:00
|
|
|
|
Fields fields;
|
2025-09-05 19:44:26 +08:00
|
|
|
|
fields.set("role_id", roleId);
|
2025-09-12 18:44:34 +08:00
|
|
|
|
fields.set("permission_id", item["permission_id"].get<std::string>());
|
|
|
|
|
|
fields.set("is_open", item["is_open"].get<std::string>());
|
|
|
|
|
|
fields.set("is_view", item["is_view"].get<std::string>());
|
|
|
|
|
|
fields.set("is_add", item["is_add"].get<std::string>());
|
|
|
|
|
|
fields.set("is_edit", item["is_edit"].get<std::string>());
|
|
|
|
|
|
fields.set("is_del", item["is_del"].get<std::string>());
|
|
|
|
|
|
vecFields.push_back(fields);
|
2025-09-05 19:44:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
err = DAO::updateRolePermission(dao, roleId, vecFields);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return err;
|
2025-08-31 14:38:53 +08:00
|
|
|
|
};
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::deleteRole(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::string roleId = req.get_param_value(DMRole::ROLE_ID);
|
|
|
|
|
|
return DAO::remove(NULL, DMRole::TABLENAME, DMRole::ROLE_ID, roleId);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::queryStationList(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
PageInfo pageinfo;
|
|
|
|
|
|
pageinfo.index = Utils::toInt(req.get_param_value("page"));
|
|
|
|
|
|
pageinfo.size = Utils::toInt(req.get_param_value("page_size"));
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<Fields> result;
|
|
|
|
|
|
auto err = DAO::queryStationList(pageinfo, result);
|
|
|
|
|
|
HttpHelper::setPagination(pageinfo, result, json);
|
|
|
|
|
|
return err;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::insertStation(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"name", "address", "lon", "lat", "tel", "capacity", "status"}, params);
|
|
|
|
|
|
return DAO::insertStation(params);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::updateStation(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
2025-09-09 19:26:05 +08:00
|
|
|
|
GetRequestParam(req, {"station_id", "name", "address", "lon", "lat", "tel", "capacity", "status", "work_mode"}, params);
|
2025-09-08 19:34:12 +08:00
|
|
|
|
params.check("capacity", "", "0.0");
|
|
|
|
|
|
params.check("lon", "", "0.0");
|
|
|
|
|
|
params.check("lat", "", "0.0");
|
|
|
|
|
|
params.check("status", "", "1");
|
2025-08-31 14:38:53 +08:00
|
|
|
|
return DAO::updateStationById(params);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::deleteStation(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::string primaryKey = DMStation::STATION_ID;
|
|
|
|
|
|
return DAO::remove(NULL, DMStation::TABLENAME, primaryKey, req.get_param_value(primaryKey));
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::queryStationOverview(const httplib::Request& req, njson& json, std::string& errmsg)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"station_id"}, params);
|
|
|
|
|
|
std::string stationId = params.value("station_id");
|
|
|
|
|
|
if (stationId.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
return Errcode::ERR_PARAM;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auto dao = DaoEntity::create("");
|
|
|
|
|
|
std::vector<Fields> result;
|
|
|
|
|
|
|
|
|
|
|
|
// 运行模式
|
|
|
|
|
|
std::string sql = "SELECT * FROM station WHERE station_id='" + stationId + "';";
|
|
|
|
|
|
int ret = dao->exec(sql, result);
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Errcode(ret);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (result.size() > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
json["data"]["work_mode"] = result[0].get<int>("work_mode");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 储能设备:总功率,数量
|
|
|
|
|
|
// 充电设备:总功率,数量
|
|
|
|
|
|
// 光伏设备:总功率,数量
|
|
|
|
|
|
sql = R"(SELECT d.`type`, count(1) count, ddt.name typename, ddt.category FROM device d
|
|
|
|
|
|
LEFT JOIN def_device_type ddt ON ddt.device_type_id=d.`type` WHERE d.station_id=')" + stationId + "' GROUP BY `type`;";
|
|
|
|
|
|
ret = dao->exec(sql, result);
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Errcode(ret);
|
|
|
|
|
|
}
|
2025-09-10 20:10:51 +08:00
|
|
|
|
njson jsonStorage = njson::parse(R"({"category":1, "gateway":0, "count":0, "power":0.0})");
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson jsonCharge = njson::parse(R"({"category":2, "count":0, "power":0.0})");
|
|
|
|
|
|
njson jsonSolar = njson::parse(R"({"category":3, "count":0, "power":0.0})");
|
|
|
|
|
|
njson jsonSecurity = njson::parse(R"({"category":4, "count":0, "power":0.0})");
|
|
|
|
|
|
for (auto& fields : result)
|
|
|
|
|
|
{
|
|
|
|
|
|
int category = fields.get<int>("category");
|
|
|
|
|
|
int count = fields.get<int>("count");
|
|
|
|
|
|
switch (category)
|
|
|
|
|
|
{
|
|
|
|
|
|
case 1: { jsonStorage["count"] = jsonStorage["count"].get<int>() + count; } break;
|
|
|
|
|
|
case 2: { jsonCharge["count"] = jsonCharge["count"].get<int>() + count; } break;
|
|
|
|
|
|
case 3: { jsonSolar["count"] = jsonSolar["count"].get<int>() + count; } break;
|
|
|
|
|
|
case 4: { jsonSecurity["count"] = jsonSecurity["count"].get<int>() + count; } break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 从运行数据中读取功率信息(待补充)
|
|
|
|
|
|
|
|
|
|
|
|
json["data"]["device_group"] = {jsonStorage, jsonCharge, jsonSolar, jsonSecurity};
|
|
|
|
|
|
return Errcode::OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::queryStationInfo(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-09-04 19:31:04 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 查询场站的基础配置信息
|
|
|
|
|
|
std::string stationId = req.get_param_value("station_id");
|
|
|
|
|
|
if (stationId.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
return Errcode::ERR_PARAM;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string sql = "SELECT * FROM " + DMStation::TABLENAME + " WHERE station_id=" + stationId + ";";
|
|
|
|
|
|
std::vector<Fields> result;
|
|
|
|
|
|
Errcode err = DAO::exec(NULL, sql, result);
|
|
|
|
|
|
if (err != Errcode::OK)
|
|
|
|
|
|
{
|
|
|
|
|
|
return err;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (result.size() == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Errcode::ERR_DATA_NUL;
|
|
|
|
|
|
}
|
|
|
|
|
|
auto& fields = result[0];
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson jsondata;
|
2025-09-04 19:31:04 +08:00
|
|
|
|
std::string attr = fields.remove(DMStation::ATTR);
|
2025-09-06 15:23:07 +08:00
|
|
|
|
JSON::parse(attr, jsondata);
|
2025-09-04 19:31:04 +08:00
|
|
|
|
|
|
|
|
|
|
FieldsToJson(fields, jsondata);
|
|
|
|
|
|
json["data"] = jsondata;
|
|
|
|
|
|
return Errcode::OK;
|
|
|
|
|
|
|
|
|
|
|
|
// work_mode: 运行模式:
|
|
|
|
|
|
// capacity: 储能容量:
|
|
|
|
|
|
|
|
|
|
|
|
// {"batttey_type": "磷酸铁锂", "cooling_type":"风冷", "voltage_rated":"300", "power_rated": "1500"}
|
|
|
|
|
|
// batttey_type: 电池类型:
|
|
|
|
|
|
// cooling_type: 冷却方式:
|
|
|
|
|
|
// voltage_rated: 电池额定电压:
|
|
|
|
|
|
// power_rated: PCS额定功率
|
|
|
|
|
|
}
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::queryStationData(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-09-04 19:31:04 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 温度, 电压、电流、功率、功率因数、
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson jsondata;
|
2025-09-04 19:31:04 +08:00
|
|
|
|
jsondata["voltage"] = Utils::toStr(200.32);
|
|
|
|
|
|
jsondata["current"] = Utils::toStr(20.56);
|
|
|
|
|
|
jsondata["power"] = Utils::toStr(200.32);
|
|
|
|
|
|
jsondata["powerFactor"] = Utils::toStr(1);
|
|
|
|
|
|
jsondata["envTemp"] = Utils::toStr(200.32);
|
|
|
|
|
|
jsondata["envhum"] = Utils::toStr(200.32);
|
|
|
|
|
|
jsondata["aircStatus"] = Utils::toStr(1);
|
|
|
|
|
|
jsondata["coolingStatus"] = Utils::toStr(0);
|
|
|
|
|
|
|
|
|
|
|
|
json["data"] = jsondata;
|
|
|
|
|
|
return Errcode::OK;
|
|
|
|
|
|
}
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::queryDeviceList(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
PageInfo pageinfo;
|
|
|
|
|
|
pageinfo.index = Utils::toInt(req.get_param_value("page"));
|
|
|
|
|
|
pageinfo.size = Utils::toInt(req.get_param_value("page_size"));
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<Fields> result;
|
|
|
|
|
|
auto err = DAO::queryDeviceList(pageinfo, result);
|
|
|
|
|
|
HttpHelper::setPagination(pageinfo, result, json);
|
|
|
|
|
|
return err;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::insertDevice(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"station_id", "type", "name", "code", "model", "factory", "factory_tel", "is_open", "attrs"}, params);
|
2025-09-12 18:44:34 +08:00
|
|
|
|
if (!params.contains("station_id")) { errmsg = "缺少参数[station_id]"; return Errcode::ERR_PARAM; }
|
|
|
|
|
|
|
|
|
|
|
|
Errcode err = DAO::insertDevice(params);
|
|
|
|
|
|
if (err == Errcode::OK)
|
|
|
|
|
|
{
|
|
|
|
|
|
int stationId = params.get<int>("station_id");
|
|
|
|
|
|
auto station = Application::data().getStation(stationId);
|
|
|
|
|
|
if (station) { station->addDevice(params); }
|
|
|
|
|
|
}
|
|
|
|
|
|
return err;
|
2025-08-31 14:38:53 +08:00
|
|
|
|
};
|
2025-09-12 18:44:34 +08:00
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::updateDevice(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"device_id", "station_id", "type", "name", "code", "model", "factory", "factory_tel", "is_open", "attrs"}, params);
|
2025-09-12 18:44:34 +08:00
|
|
|
|
|
|
|
|
|
|
Errcode err = DAO::updateDeviceById(params);
|
|
|
|
|
|
if (err == Errcode::OK)
|
|
|
|
|
|
{
|
|
|
|
|
|
int stationId = params.get<int>("station_id");
|
|
|
|
|
|
auto station = Application::data().getStation(stationId);
|
|
|
|
|
|
if (station) { station->addDevice(params); }
|
|
|
|
|
|
}
|
|
|
|
|
|
return err;
|
2025-08-31 14:38:53 +08:00
|
|
|
|
};
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::deleteDevice(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::string primaryKey = DMDevice::DEVICE_ID;
|
|
|
|
|
|
return DAO::remove(NULL, DMDevice::TABLENAME, primaryKey, req.get_param_value(primaryKey));
|
|
|
|
|
|
};
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::queryDevicTypeDef(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::string sql = "SELECT device_type_id, name FROM def_device_type;";
|
|
|
|
|
|
std::vector<Fields> result;
|
|
|
|
|
|
auto err = DAO::exec(NULL, sql, result);
|
|
|
|
|
|
json["data"] = FieldsToJsonArray(result);
|
|
|
|
|
|
return err;
|
|
|
|
|
|
}
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::queryDevicByCategory(const httplib::Request& req, njson& json, std::string& errmsg)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"station_id", "category"}, params);
|
|
|
|
|
|
if (!params.contains("station_id")) { errmsg = "缺少参数[station_id]"; return Errcode::ERR_PARAM; }
|
|
|
|
|
|
if (!params.contains("category")) { errmsg = "缺少参数[category]"; return Errcode::ERR_PARAM; }
|
|
|
|
|
|
|
|
|
|
|
|
int stationId = params.get<int>("station_id");
|
|
|
|
|
|
int category = params.get<int>("category");
|
|
|
|
|
|
|
|
|
|
|
|
njson jsondata = njson::array();
|
|
|
|
|
|
auto station = Application::data().getStation(stationId);
|
|
|
|
|
|
if (station)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::vector<std::shared_ptr<Device>> vecDevice;
|
|
|
|
|
|
station->getDeviceByGroup(category, vecDevice);
|
|
|
|
|
|
|
|
|
|
|
|
for(auto& device: vecDevice)
|
|
|
|
|
|
{
|
|
|
|
|
|
njson jsonnode;
|
|
|
|
|
|
jsonnode["stationId"] = stationId;
|
|
|
|
|
|
jsonnode["category"] = category;
|
|
|
|
|
|
jsonnode["device_id"] = device->deviceId;
|
|
|
|
|
|
jsonnode["name"] = device->name;
|
|
|
|
|
|
jsonnode["code"] = device->code;
|
|
|
|
|
|
jsonnode["type"] = device->type;
|
2025-09-08 19:34:12 +08:00
|
|
|
|
jsonnode["typename"] = Application::data().getDeviceNameById(device->type);
|
|
|
|
|
|
jsonnode["view"] = 1;
|
2025-09-06 15:23:07 +08:00
|
|
|
|
|
|
|
|
|
|
jsonnode["is_online"] = device->online;// ? "在线" : "离线";
|
|
|
|
|
|
jsonnode["is_error"] = device->err;// ? "故障" : "正常";
|
|
|
|
|
|
jsonnode["is_running"] = device->running;// ? "工作" : "空闲";
|
|
|
|
|
|
|
|
|
|
|
|
njson jsonarrayParams = njson::array();
|
|
|
|
|
|
VecPairSS vec;
|
|
|
|
|
|
device->getRuntimeParams(vec);
|
|
|
|
|
|
for (auto& item: vec)
|
|
|
|
|
|
{
|
|
|
|
|
|
jsonarrayParams.push_back({{"k", item.first}, {"v", item.second}});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
jsonnode["params"] = jsonarrayParams;
|
|
|
|
|
|
jsondata.push_back(jsonnode);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
json["data"] = jsondata;
|
|
|
|
|
|
return Errcode::OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::queryDevicCharts(const httplib::Request& req, njson& json, std::string& errmsg)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"station_id", "device_id"}, params);
|
|
|
|
|
|
if (!params.contains("station_id")) { errmsg = "缺少参数[station_id]"; return Errcode::ERR_PARAM; }
|
|
|
|
|
|
if (!params.contains("device_id")) { errmsg = "缺少参数[device_id]"; return Errcode::ERR_PARAM; }
|
|
|
|
|
|
|
|
|
|
|
|
int stationId = params.get<int>("station_id");
|
|
|
|
|
|
int deviceId = params.get<int>("device_id");
|
|
|
|
|
|
auto device = Application::data().getDevice(stationId, deviceId);
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> vecV, vecI, vecP;
|
|
|
|
|
|
if (device)
|
|
|
|
|
|
{
|
|
|
|
|
|
device->getCacheVoltage(vecV);
|
|
|
|
|
|
device->getCacheCurrent(vecI);
|
|
|
|
|
|
device->getCachePower(vecP);
|
|
|
|
|
|
}
|
|
|
|
|
|
json["data"] = {{"V", vecV}, {"I", vecI}, {"P", vecP}};
|
|
|
|
|
|
return Errcode::OK;
|
|
|
|
|
|
}
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
2025-09-09 19:26:05 +08:00
|
|
|
|
Errcode HttpEntity::queryDeviceBCUDetail(const httplib::Request& req, njson& json, std::string& errmsg)
|
|
|
|
|
|
{
|
|
|
|
|
|
njson jsondata = njson::array();
|
|
|
|
|
|
for (int i = 0; i<=100; ++i)
|
|
|
|
|
|
{
|
|
|
|
|
|
jsondata.push_back({{"SOC", "1"}, {"SOH", "1"}, {"V", "1"}, {"T", "1"}, {"R_i", "1"}});
|
|
|
|
|
|
}
|
|
|
|
|
|
json["data"] = jsondata;
|
|
|
|
|
|
return Errcode::OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::queryPolicyList(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
PageInfo pageinfo;
|
|
|
|
|
|
pageinfo.index = Utils::toInt(req.get_param_value("page"));
|
|
|
|
|
|
pageinfo.size = Utils::toInt(req.get_param_value("page_size"));
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<Fields> result;
|
|
|
|
|
|
auto err = DAO::queryPolicyList(pageinfo, result);
|
|
|
|
|
|
HttpHelper::setPagination(pageinfo, result, json);
|
|
|
|
|
|
return err;
|
|
|
|
|
|
};
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::insertPolicy(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"type", "name", "describe", "value", "is_open"}, params);
|
|
|
|
|
|
return DAO::insertPolicy(params);
|
|
|
|
|
|
};
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::updatePolicy(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"policy_id", "type", "describe", "value", "is_open"}, params);
|
|
|
|
|
|
return DAO::updatePolicyById(params);
|
|
|
|
|
|
};
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::deletePolicy(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
2025-09-05 19:44:26 +08:00
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"policy_id"}, params);
|
|
|
|
|
|
return DAO::deletePolicyById(params.value("policy_id"));
|
2025-08-31 14:38:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::querySystemLogList(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
PageInfo pageinfo;
|
|
|
|
|
|
pageinfo.index = Utils::toInt(req.get_param_value("page"));
|
|
|
|
|
|
pageinfo.size = Utils::toInt(req.get_param_value("page_size"));
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<Fields> result;
|
|
|
|
|
|
auto err = DAO::querySystemLogList(pageinfo, result);
|
|
|
|
|
|
HttpHelper::setPagination(pageinfo, result, json);
|
|
|
|
|
|
return err;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Errcode insertSystemLog(const httplib::Request& req, httplib::Response& resp, NJsonNode& json);
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::updateSystemLog(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"log_id", "status"}, params);
|
|
|
|
|
|
return DAO::updateSystemLogById(params);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::queryAlertLogList(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
PageInfo pageinfo;
|
|
|
|
|
|
pageinfo.index = Utils::toInt(req.get_param_value("page"));
|
|
|
|
|
|
pageinfo.size = Utils::toInt(req.get_param_value("page_size"));
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<Fields> result;
|
|
|
|
|
|
auto err = DAO::queryAlertLogList(pageinfo, result);
|
|
|
|
|
|
HttpHelper::setPagination(pageinfo, result, json);
|
|
|
|
|
|
return err;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Errcode insertAlertLog(const httplib::Request& req, httplib::Response& resp, NJsonNode& json);
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::updateAlertLog(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"log_id", "status"}, params);
|
|
|
|
|
|
return DAO::updateAlertLogById(params);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::queryPredictionDetail(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson jsonData = njson::array();
|
2025-09-08 19:34:12 +08:00
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> vecStoreIn(144), vecStoreOut(144), vecCharge(144), vecSolar(144), vecSolarP(144);
|
|
|
|
|
|
for (int i = 0; i<144; ++i)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
2025-09-08 19:34:12 +08:00
|
|
|
|
vecStoreIn[i] = Utils::toStr(float(Utils::random(50, 100)));
|
|
|
|
|
|
vecStoreOut[i] = Utils::toStr(float(Utils::random(50, 100)));
|
|
|
|
|
|
vecCharge[i] = Utils::toStr(float(Utils::random(50, 100)));
|
|
|
|
|
|
vecSolar[i] = Utils::toStr(float(Utils::random(50, 100)));
|
|
|
|
|
|
vecSolarP[i] = Utils::toStr(float(Utils::random(50, 100)));
|
2025-08-31 14:38:53 +08:00
|
|
|
|
}
|
2025-09-08 19:34:12 +08:00
|
|
|
|
|
2025-08-31 14:38:53 +08:00
|
|
|
|
json["data"] = jsonData;
|
2025-09-08 19:34:12 +08:00
|
|
|
|
json["data"] = {
|
|
|
|
|
|
{"W_store_in", vecStoreIn},
|
|
|
|
|
|
{"W_store_out", vecStoreOut},
|
|
|
|
|
|
{"W_charge", vecCharge},
|
|
|
|
|
|
{"W_solar", vecSolar},
|
|
|
|
|
|
{"P_solar", vecSolarP }
|
|
|
|
|
|
};
|
2025-09-01 20:08:40 +08:00
|
|
|
|
return Errcode::OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::queryStatSystem(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-09-01 20:08:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
auto& appdata = Application::data();
|
|
|
|
|
|
|
2025-09-12 18:44:34 +08:00
|
|
|
|
double incomeTotal {};
|
|
|
|
|
|
double station_num = appdata.getStationCount();
|
|
|
|
|
|
double solarDeviceNum {};
|
|
|
|
|
|
double capacityTotal {};
|
|
|
|
|
|
double electGenTotal {};
|
|
|
|
|
|
double electGridTotal {};
|
|
|
|
|
|
double electStorageIn {};
|
|
|
|
|
|
double electStorageOut {};
|
|
|
|
|
|
for (auto& item : appdata.mapStation)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& station = item.second;
|
|
|
|
|
|
solarDeviceNum += station->getDeviceCount(3);
|
|
|
|
|
|
capacityTotal += station->capacity;
|
|
|
|
|
|
electGenTotal += station->electGenTotal;
|
|
|
|
|
|
electGridTotal += station->electGridTotal;
|
|
|
|
|
|
electStorageIn += station->electStorageIn;
|
|
|
|
|
|
electStorageOut += station->electStorageOut;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson jsondata;
|
2025-09-10 20:10:51 +08:00
|
|
|
|
jsondata["launch_date"] = appdata.launchDate; //: 系统上线启用日期,格式:yyyy-mm-dd
|
2025-09-12 18:44:34 +08:00
|
|
|
|
jsondata["income_total"] = incomeTotal; // : 累计收益(元),精度0.01
|
|
|
|
|
|
jsondata["station_num"] = station_num; // : 能源站数量
|
|
|
|
|
|
jsondata["storage_device_num"] = station_num; //: 储能设备数量
|
|
|
|
|
|
jsondata["solar_device_num"] = solarDeviceNum; //: 光伏设备数量
|
|
|
|
|
|
jsondata["capacity_total"] = capacityTotal; // : 储能总容量(kWh),精度0.001
|
|
|
|
|
|
jsondata["solar_elect_gen"] = electGenTotal; // : 发电总电量(kWh),精度0.001
|
|
|
|
|
|
jsondata["solar_elect_grid"] = electGridTotal; // : 入网种电量(kWh),精度0.001
|
|
|
|
|
|
jsondata["storage_elect_in"] = electStorageIn; // : 储能充电总电量(kWh),精度0.001
|
|
|
|
|
|
jsondata["storage_elect_out"] = electStorageOut; // : 储能放电总电量(kWh),精度0.001
|
2025-09-04 19:31:04 +08:00
|
|
|
|
|
|
|
|
|
|
json["data"] = jsondata;
|
2025-09-01 20:08:40 +08:00
|
|
|
|
return Errcode::OK;
|
|
|
|
|
|
}
|
2025-09-12 18:44:34 +08:00
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::queryStatStation(const httplib::Request& req, njson& json, std::string& errmsg)
|
|
|
|
|
|
{
|
|
|
|
|
|
njson jsondata = njson::array();
|
|
|
|
|
|
|
|
|
|
|
|
auto dao = DaoEntity::create("");
|
|
|
|
|
|
std::string sql = R"(SELECT s.station_id, s.name station_name, ss.income_elect, ss.income_charge, ss.usage_rate FROM station s LEFT JOIN
|
|
|
|
|
|
(SELECT station_id, SUM(income_elect ) income_elect , SUM(income_charge) income_charge, avg(usage_rate) usage_rate FROM stat_staion GROUP BY station_id) AS ss
|
|
|
|
|
|
ON ss.station_id = s.station_id)";
|
|
|
|
|
|
std::vector<Fields> vecStations;
|
|
|
|
|
|
auto err = dao->exec(sql, vecStations);
|
|
|
|
|
|
for (auto& fields: vecStations)
|
|
|
|
|
|
{
|
|
|
|
|
|
njson jsonnode;
|
|
|
|
|
|
jsonnode["station_name"] = fields.value("station_name");
|
|
|
|
|
|
jsonnode["income"] = fields.get<float>("income_elect") + fields.get<float>("income_charge");
|
|
|
|
|
|
jsonnode["usage_rate"] = fields.get<float>("usage_rate");
|
|
|
|
|
|
jsondata.push_back(jsonnode);
|
|
|
|
|
|
}
|
|
|
|
|
|
json["data"] = jsondata;
|
|
|
|
|
|
return Errcode(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::queryStatTotal(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-09-01 20:08:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::string station_id = req.get_param_value("station_id");
|
|
|
|
|
|
std::string category = req.get_param_value("category");
|
2025-09-10 20:10:51 +08:00
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson jsondata;
|
2025-09-10 20:10:51 +08:00
|
|
|
|
auto station = Application::data().getStation(Utils::toInt(station_id));
|
|
|
|
|
|
if (station)
|
|
|
|
|
|
{
|
|
|
|
|
|
jsondata["station_id"] = station_id;
|
|
|
|
|
|
jsondata["launch_date"] = "2025-09-01"; //场站上线日期
|
|
|
|
|
|
jsondata["usage_rate"] = "12";
|
|
|
|
|
|
jsondata["storage_elect_in"] = "123.123"; //储能充电电量(kWh),精度:0.001
|
|
|
|
|
|
jsondata["storage_elect_out"] = "123.123"; //储能放电电量(kWh),精度:0.001
|
|
|
|
|
|
jsondata["storage_num_in"] = "1"; //储能设备充电次数
|
|
|
|
|
|
jsondata["storage_num_out"] = "1"; //储能设备放电次数
|
|
|
|
|
|
jsondata["storage_num_err"] = "1"; //储能设备故障次数
|
|
|
|
|
|
jsondata["solar_elect_gen"] = "123.123"; //光伏发电电量(kWh),精度:0.001
|
|
|
|
|
|
jsondata["solar_elect_grid"] = "123.123"; //光伏入网电量(kWh),精度:0.001
|
|
|
|
|
|
jsondata["solar_num_err"] = "1"; //光伏设备故障次数
|
|
|
|
|
|
jsondata["charge_elect"] = "123.123"; //充电设备充电电量(kWh),精度:0.001
|
|
|
|
|
|
jsondata["charge_num"] = "1"; //充电设备充电次数
|
|
|
|
|
|
jsondata["charge_num_err"] = "1"; //充电设备故障次数
|
|
|
|
|
|
jsondata["income_elect"] = "123.123"; //发电收益(元),精度:0.01
|
|
|
|
|
|
jsondata["income_charge"] = "123.123"; //充电收益(元),精度:0.01
|
2025-09-04 19:31:04 +08:00
|
|
|
|
|
2025-09-10 20:10:51 +08:00
|
|
|
|
}
|
2025-09-04 19:31:04 +08:00
|
|
|
|
json["data"] = jsondata;
|
2025-09-01 20:08:40 +08:00
|
|
|
|
return Errcode::OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::queryStatDayList(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-09-01 20:08:40 +08:00
|
|
|
|
{
|
2025-09-12 18:44:34 +08:00
|
|
|
|
//std::string stationId = req.get_param_value("station_id");
|
|
|
|
|
|
//std::string category = req.get_param_value("category");
|
|
|
|
|
|
std::string dtStart = req.get_param_value("start_date");
|
|
|
|
|
|
std::string dtEnd = req.get_param_value("end_date");
|
|
|
|
|
|
if (dtEnd.empty())
|
2025-09-01 20:08:40 +08:00
|
|
|
|
{
|
2025-09-12 18:44:34 +08:00
|
|
|
|
if (dtStart.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
dtEnd = Utils::dateStr();
|
|
|
|
|
|
dtStart = Utils::dateStr(Utils::date() - 86400*7);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
dtEnd = Utils::dateStr(Utils::time(dtStart + " 00:00:00") + 86400*7);
|
|
|
|
|
|
}
|
2025-09-01 20:08:40 +08:00
|
|
|
|
}
|
2025-09-12 18:44:34 +08:00
|
|
|
|
|
|
|
|
|
|
std::vector<Fields> result;
|
|
|
|
|
|
Errcode err = DAO::queryStatDataList(NULL, dtStart, dtEnd, result);
|
|
|
|
|
|
json["data"] = FieldsToJsonArray(result);
|
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
|
|
//int64_t t1 = Utils::time(dtStart);
|
|
|
|
|
|
//int64_t t2 = Utils::time(dtEnd);
|
|
|
|
|
|
//int64_t tMax = t1+ 86400 * 30;
|
|
|
|
|
|
//njson jsondata = njson::array();
|
|
|
|
|
|
//for (int64_t t = t1; t<=t2 && t<=tMax; t += 86400)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// njson jnode;
|
|
|
|
|
|
// jnode["station_id"] = station_id;
|
|
|
|
|
|
// if (!category.empty()) jnode["category"] = category;
|
|
|
|
|
|
// jnode["dt"] = Utils::dateStr(t); //日期
|
|
|
|
|
|
// jnode["storage_elect_in"] = std::to_string(Utils::random(100, 200)); //储能充电电量(kWh),精度:0.001
|
|
|
|
|
|
// jnode["storage_elect_out"] = std::to_string(Utils::random(100, 200)); //储能放电电量(kWh),精度:0.001
|
|
|
|
|
|
// jnode["storage_num_in"] = std::to_string(Utils::random(1,5)); //储能设备充电次数
|
|
|
|
|
|
// jnode["storage_num_out"] = std::to_string(Utils::random(1, 5)); //储能设备放电次数
|
|
|
|
|
|
// jnode["storage_num_err"] = std::to_string(Utils::random(1, 5)); //储能设备故障次数
|
|
|
|
|
|
// jnode["solar_elect_gen"] = std::to_string(Utils::random(100, 200)); //光伏发电电量(kWh),精度:0.001
|
|
|
|
|
|
// jnode["solar_elect_grid "] = std::to_string(Utils::random(100, 200)); //光伏入网电量(kWh),精度:0.001
|
|
|
|
|
|
// jnode["solar_num_err"] = std::to_string(Utils::random(1, 5)); //光伏设备故障次数
|
|
|
|
|
|
// jnode["charge_elect"] = std::to_string(Utils::random(100, 200)); //充电设备充电电量(kWh),精度:0.001
|
|
|
|
|
|
// jnode["charge_num"] = std::to_string(Utils::random(1, 5)); //充电设备充电次数
|
|
|
|
|
|
// jnode["charge_num_err"] = std::to_string(Utils::random(1, 5)); //充电设备故障次数
|
|
|
|
|
|
// jnode["income_elect"] = std::to_string(Utils::random(100, 200)); //发电收益(元),精度:0.01
|
|
|
|
|
|
// jnode["income_charge"] = std::to_string(Utils::random(100, 200)); //充电收益(元),精度:0.01
|
|
|
|
|
|
// jnode["usage_rate"] = std::to_string(Utils::random(10, 50)); //利用率
|
|
|
|
|
|
// jsondata.push_back(jnode);
|
|
|
|
|
|
//}
|
|
|
|
|
|
//json["data"] = jsondata;
|
2025-09-04 19:31:04 +08:00
|
|
|
|
return Errcode::OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-10 20:10:51 +08:00
|
|
|
|
Errcode HttpEntity::queryStatCharts(const httplib::Request& req, njson& json, std::string& errmsg)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string dt = req.get_param_value("dt");
|
|
|
|
|
|
std::string stationId = req.get_param_value("station_id");
|
|
|
|
|
|
std::string category = req.get_param_value("category");
|
|
|
|
|
|
|
|
|
|
|
|
if (stationId.empty()) { errmsg = "参数[station_id]错误"; return Errcode::ERR_PARAM; }
|
|
|
|
|
|
if (category.empty()) { errmsg = "参数[category]错误"; return Errcode::ERR_PARAM; }
|
|
|
|
|
|
|
2025-09-12 18:44:34 +08:00
|
|
|
|
if (dt.empty()) { dt=Utils::dateStr(); }
|
|
|
|
|
|
|
2025-09-10 20:10:51 +08:00
|
|
|
|
njson jsondata;
|
|
|
|
|
|
|
|
|
|
|
|
std::string sql = R"(SELECT hd.*, d.`type` device_type, ddt.category FROM history_day hd
|
|
|
|
|
|
LEFT JOIN device d ON d.device_id = hd.device_id
|
|
|
|
|
|
LEFT JOIN def_device_type ddt ON d.`type` = ddt.device_type_id
|
|
|
|
|
|
WHERE dt=')" + dt + "' AND d.station_id='" + stationId + "' AND ddt.category='" + category + "';";
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<Fields> result;
|
|
|
|
|
|
int ret = DaoEntity::execOnce(sql, result);
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Errcode(ret);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<double> vecV;
|
|
|
|
|
|
std::vector<double> vecI;
|
|
|
|
|
|
std::vector<double> vecP;
|
|
|
|
|
|
for (auto fields : result)
|
|
|
|
|
|
{
|
|
|
|
|
|
int datetype = fields.get<int>("datatype"); // 1: 电压,2:电流,3:功率
|
|
|
|
|
|
std::string val = fields.value("value"); // JSON 数组(double)
|
|
|
|
|
|
njson jsonval;
|
|
|
|
|
|
if (JSON::parse(val, jsonval))
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int i = 0; i<jsonval.size(); ++i)
|
|
|
|
|
|
{
|
|
|
|
|
|
double val = jsonval[i];
|
|
|
|
|
|
if (datetype == 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 电压取最大
|
|
|
|
|
|
i >= vecV.size() ? vecV.push_back(val) : (val > vecV[i] ? (vecV[i] = val, (void)0) : (void)0);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (datetype == 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 电流取最大
|
|
|
|
|
|
i >= vecI.size() ? vecI.push_back(val) : (val > vecI[i] ? (vecI[i] = val, (void)0) : (void)0);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (datetype == 3)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 功率累加
|
|
|
|
|
|
i >= vecP.size() ? vecP.push_back(val) : (vecP[i] = vecP[i] + val, (void)0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
json["data"] = {{"V", vecV}, {"I", vecI}, {"P", vecP}};
|
|
|
|
|
|
return Errcode::OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
Errcode HttpEntity::queryEnvironment(const httplib::Request& req, njson& json, std::string& errmsg)
|
2025-09-04 19:31:04 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::string stationId = req.get_param_value("station_id");
|
|
|
|
|
|
auto& appdata = Application::data();
|
2025-09-01 20:08:40 +08:00
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
auto station = appdata.getStation(Utils::toInt(stationId));
|
|
|
|
|
|
if (!station)
|
|
|
|
|
|
{
|
|
|
|
|
|
spdlog::error("[http] request queryEnvironment failed, get station info error, station_id={}", stationId);
|
|
|
|
|
|
return Errcode::ERR_PARAM;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson jsondata;
|
2025-09-04 19:31:04 +08:00
|
|
|
|
|
|
|
|
|
|
{ // 温湿度
|
|
|
|
|
|
auto& mapTempHumUnit = station->mapTempHumUnit;
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson nodearray = njson::array();
|
2025-09-04 19:31:04 +08:00
|
|
|
|
for (auto iter = mapTempHumUnit.begin(); iter!=mapTempHumUnit.end(); iter++)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& unit = iter->second;
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson node;
|
2025-09-04 19:31:04 +08:00
|
|
|
|
node["pos"] = "#" + std::to_string(iter->first);
|
|
|
|
|
|
node["temp"] = unit.temp;
|
|
|
|
|
|
node["hum"] = unit.hum;
|
|
|
|
|
|
nodearray.push_back(node);
|
|
|
|
|
|
}
|
|
|
|
|
|
jsondata["temp_hum"] = nodearray;
|
|
|
|
|
|
}
|
|
|
|
|
|
{ //空调
|
|
|
|
|
|
auto& mapAircUnit = station->mapAircUnit;
|
|
|
|
|
|
AircUnit unitTmp;
|
|
|
|
|
|
AircUnit* unit = (mapAircUnit.size() > 0) ? &(mapAircUnit[0]) : &unitTmp;
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson nodearray = njson::array();
|
2025-09-04 19:31:04 +08:00
|
|
|
|
if (unit)
|
|
|
|
|
|
{
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson node;
|
2025-09-04 19:31:04 +08:00
|
|
|
|
nodearray.push_back({{"pos", "开关"}, {"status", unit->powerOn == 0 ? "关机" : "开机"}});
|
|
|
|
|
|
nodearray.push_back({{"pos", "启动制冷指令"}, {"status", unit->cooling == 0 ? "启动" : "关闭"}});
|
|
|
|
|
|
nodearray.push_back({{"pos", "启动送风指令"}, {"status", unit->airSupply == 0 ? "关闭" : "启动"}});
|
|
|
|
|
|
nodearray.push_back({{"pos", "启动待机指令"}, {"status", unit->standby == 0 ? "关闭" : "启动"}});
|
|
|
|
|
|
nodearray.push_back({{"pos", "启动加热指令"}, {"status", unit->heating == 0 ? "关闭" : "启动"}});
|
|
|
|
|
|
nodearray.push_back({{"pos", "传感器故障"}, {"status", unit->sensorAlarm == 0 ? "正常" : "告警"}});
|
|
|
|
|
|
nodearray.push_back({{"pos", "高低电压告警"}, {"status", unit->voltageAlarm == 0 ? "正常" : "告警"}});
|
|
|
|
|
|
nodearray.push_back({{"pos", "高低温告警"}, {"status", unit->tempAlarm == 0 ? "正常" : "告警"}});
|
|
|
|
|
|
nodearray.push_back({{"pos", "高低压告警"}, {"status", unit->pressureAlarm == 0 ? "正常" : "告警"}});
|
|
|
|
|
|
nodearray.push_back({{"pos", "压缩机告警"}, {"status", unit->compressorAlarm == 0 ? "正常" : "告警"}});
|
|
|
|
|
|
nodearray.push_back({{"pos", "当前温度"}, {"status", std::to_string(unit->temp) + "℃"}});
|
|
|
|
|
|
nodearray.push_back({{"pos", "当前湿度"}, {"status", std::to_string(unit->hum) + "%"}});
|
|
|
|
|
|
}
|
|
|
|
|
|
jsondata["airc"] = nodearray;
|
|
|
|
|
|
}
|
|
|
|
|
|
{ // 消防
|
|
|
|
|
|
static std::map<int, std::string> mapFireStatusDef = { {0, "正常"}, {1,"预警"}, {2,"火警"} };
|
|
|
|
|
|
|
|
|
|
|
|
auto& mapFire40Unit = station->mapFire40Unit;
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson nodearray = njson::array();
|
2025-09-04 19:31:04 +08:00
|
|
|
|
for (auto iter = mapFire40Unit.begin(); iter!=mapFire40Unit.end(); ++iter)
|
|
|
|
|
|
{
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson node;
|
2025-09-04 19:31:04 +08:00
|
|
|
|
node["pos"] = "#" + std::to_string(iter->first);
|
|
|
|
|
|
node["status"] = mapFireStatusDef[iter->second]; // 0:正常 1:预警 2:火警
|
|
|
|
|
|
nodearray.push_back(node);
|
|
|
|
|
|
}
|
|
|
|
|
|
jsondata["fire40"] = nodearray;
|
|
|
|
|
|
}
|
|
|
|
|
|
{ // 冷机
|
|
|
|
|
|
auto& mapCoolingUnit = station->mapCoolingUnit;
|
|
|
|
|
|
CoolingUnit unitTmp;
|
|
|
|
|
|
CoolingUnit* unit = (mapCoolingUnit.size() > 0) ? &(mapCoolingUnit[0]) : &unitTmp;
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson nodearray = njson::array();
|
2025-09-04 19:31:04 +08:00
|
|
|
|
if (unit)
|
|
|
|
|
|
{
|
2025-09-06 15:23:07 +08:00
|
|
|
|
njson node;
|
2025-09-04 19:31:04 +08:00
|
|
|
|
nodearray.push_back({{"pos", "开关"}, {"status", unit->powerOn == 0 ? "关机" : "开机"}});
|
|
|
|
|
|
nodearray.push_back({{"pos", "采样模式"}, {"status", unit->mode == 0 ? "出水温度" : "电芯温度"}});
|
|
|
|
|
|
nodearray.push_back({{"pos", "制冷状态"}, {"status", unit->cooling == 0 ? "关闭" : "启动"}});
|
|
|
|
|
|
nodearray.push_back({{"pos", "制热状态"}, {"status", unit->heating == 0 ? "关闭" : "启动"}});
|
|
|
|
|
|
nodearray.push_back({{"pos", "高温告警"}, {"status", unit->highTemp == 0 ? "正常" : "告警"}});
|
|
|
|
|
|
nodearray.push_back({{"pos", "低温告警"}, {"status", unit->lowTemp == 0 ? "正常" : "告警"}});
|
|
|
|
|
|
nodearray.push_back({{"pos", "高压告警"}, {"status", unit->highPressure == 0 ? "正常" : "告警"}});
|
|
|
|
|
|
nodearray.push_back({{"pos", "低压告警"}, {"status", unit->lowPressure == 0 ? "正常" : "告警"}});
|
|
|
|
|
|
}
|
|
|
|
|
|
jsondata["cooling"] = nodearray;
|
|
|
|
|
|
}
|
|
|
|
|
|
json["data"] = jsondata;
|
2025-08-31 14:38:53 +08:00
|
|
|
|
return Errcode::OK;
|
2025-09-08 19:34:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::queryServiceApiList(const httplib::Request& req, njson& json, std::string& errmsg)
|
|
|
|
|
|
{
|
|
|
|
|
|
PageInfo pageinfo;
|
|
|
|
|
|
pageinfo.index = Utils::toInt(req.get_param_value("page"));
|
|
|
|
|
|
pageinfo.size = Utils::toInt(req.get_param_value("page_size"));
|
|
|
|
|
|
std::vector<Fields> result;
|
|
|
|
|
|
std::string sql = "SELECT * FROM serviceapi;" ;
|
|
|
|
|
|
auto err = DAO::exec(NULL, sql, result);
|
|
|
|
|
|
HttpHelper::setPagination(pageinfo, result, json);
|
|
|
|
|
|
return err;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::insertServiceApi(const httplib::Request& req, njson& json, std::string& errmsg)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"name", "describe", "params", "is_open"}, params);
|
|
|
|
|
|
if (params.size() == 0) { return Errcode::ERR_PARAM; }
|
|
|
|
|
|
|
|
|
|
|
|
return DAO::insert(NULL, "serviceapi", params);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::updateServiceApi(const httplib::Request& req, njson& json, std::string& errmsg)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"api_id", "name", "describe", "params", "is_open"}, params);
|
|
|
|
|
|
if (params.size() == 0) { return Errcode::ERR_PARAM; }
|
|
|
|
|
|
|
|
|
|
|
|
return DAO::update(NULL, "serviceapi", params, "api_id");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::deleteServiceApi(const httplib::Request& req, njson& json, std::string& errmsg)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"api_id"}, params);
|
|
|
|
|
|
return DAO::remove(NULL, "serviceapi", "api_id", params.value("api_id"));
|
2025-08-31 14:38:53 +08:00
|
|
|
|
}
|