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-08-31 14:38:53 +08:00
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
static void FieldsToJson(Fields& fields, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (auto& item : fields.map())
|
|
|
|
|
|
{
|
|
|
|
|
|
json[item.first] = item.second;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static NJsonNode FieldsToJsonArray(std::vector<Fields>& vecFields)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
NJsonNode jsonnode = NJsonNode::array();
|
|
|
|
|
|
for (auto& fields : vecFields)
|
|
|
|
|
|
{
|
|
|
|
|
|
NJsonNode jnode;
|
|
|
|
|
|
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")
|
|
|
|
|
|
{
|
|
|
|
|
|
NJsonNode json;
|
|
|
|
|
|
NJson::parse(req.body, json);
|
|
|
|
|
|
for (auto& key : vecKeys)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (json.contains(key)) {
|
|
|
|
|
|
|
|
|
|
|
|
switch (json[key].type())
|
|
|
|
|
|
{
|
|
|
|
|
|
case NJsonNode::value_t::string: { fields.set(key, json[key].get<std::string>()); } break;
|
|
|
|
|
|
case NJsonNode::value_t::boolean: { fields.set(key, json[key].get<bool>()); } break;
|
|
|
|
|
|
case NJsonNode::value_t::number_integer: { fields.set(key, json[key].get<int>()); } break;
|
|
|
|
|
|
case NJsonNode::value_t::number_unsigned: { fields.set(key, json[key].get<int>()); } break;
|
|
|
|
|
|
case NJsonNode::value_t::number_float: { fields.set(key, json[key].get<float>()); } break;
|
|
|
|
|
|
case NJsonNode::value_t::null: {} break;
|
|
|
|
|
|
case NJsonNode::value_t::object: {} break;
|
|
|
|
|
|
case NJsonNode::value_t::array: {} break;
|
|
|
|
|
|
case NJsonNode::value_t::binary: {} break;
|
|
|
|
|
|
case NJsonNode::value_t::discarded: {} break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-08-31 14:38:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-04 19:31:04 +08:00
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!req.has_param(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!errmsg.empty()) { errmsg += ","; }
|
|
|
|
|
|
errmsg += "缺少参数[" + key + "]";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!errmsg.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
static void setPagination(PageInfo& pageinfo, std::vector<Fields> result, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
json["count"] = pageinfo.total;
|
|
|
|
|
|
json["page"] = pageinfo.index;
|
|
|
|
|
|
json["page_size"] = pageinfo.size;
|
|
|
|
|
|
json["data"] = FieldsToJsonArray(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
using HandlerFunc = Errcode(HttpEntity::*)(const httplib::Request& req, httplib::Response& resp, NJsonNode& jnode);
|
|
|
|
|
|
|
|
|
|
|
|
struct HandlerOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
HandlerFunc func;
|
|
|
|
|
|
std::vector<std::string> requiredKeys;
|
|
|
|
|
|
std::vector<std::string> keys;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HandlerOptions(HandlerFunc func, const std::vector<std::string>& requiredKeys)
|
|
|
|
|
|
: func(func), requiredKeys(requiredKeys)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
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})},
|
|
|
|
|
|
{"/queryStationRuntime", HandlerOptions(&HttpEntity::queryStationRuntime, { 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-08-31 14:38:53 +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, {})},
|
|
|
|
|
|
{"/queryStatTotal", HandlerOptions(&HttpEntity::queryStatTotal, {})},
|
|
|
|
|
|
{"/queryStatStation", HandlerOptions(&HttpEntity::queryStatStation, {})},
|
|
|
|
|
|
{"/queryStatDayList", HandlerOptions(&HttpEntity::queryStatDayList, {})},
|
|
|
|
|
|
|
|
|
|
|
|
{"/queryEnvironment", HandlerOptions(&HttpEntity::queryEnvironment, { "station_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})},
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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-01 20:08:40 +08:00
|
|
|
|
bool useToken = Config::option.http.useToken;
|
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-01 20:08:40 +08:00
|
|
|
|
spdlog::info("[http] request: {}", name);
|
|
|
|
|
|
Errcode errcode = Errcode::OK;
|
|
|
|
|
|
if (name != "/login" && useToken)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
2025-09-04 19:31:04 +08:00
|
|
|
|
bool ret = CheckHttpToken(req);
|
|
|
|
|
|
errcode = ret ? Errcode::OK : Errcode::ERR_TOKEN;
|
2025-08-31 14:38:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
NJsonNode json;
|
2025-09-01 20:08:40 +08:00
|
|
|
|
std::string errmsg;
|
|
|
|
|
|
if (errcode == Errcode::OK)
|
2025-08-31 14:38:53 +08:00
|
|
|
|
{
|
2025-09-01 20:08:40 +08:00
|
|
|
|
if (!HttpHelper::CheckRequestParam(req, resp, handler.requiredKeys, errmsg))
|
|
|
|
|
|
{
|
|
|
|
|
|
errcode = Errcode::ERR_PARAM;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
errcode = (this->*(handler.func))(req, resp, json);
|
|
|
|
|
|
}
|
2025-08-31 14:38:53 +08:00
|
|
|
|
}
|
2025-09-04 19:31:04 +08:00
|
|
|
|
json["errcode"] = errcode;
|
|
|
|
|
|
json["errmsg"] = ErrcodeStr(errcode) + (errmsg.empty() ? "" : (":"+errmsg));
|
|
|
|
|
|
resp.set_content(json.dump(), "text/plain; charset=utf-8");
|
|
|
|
|
|
resp.status = 200;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
Errcode errcode = Errcode::OK;
|
|
|
|
|
|
std::string errmsg;
|
|
|
|
|
|
if (name != "/login" && useToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool ret = CheckHttpToken(req);
|
|
|
|
|
|
errcode = ret ? Errcode::OK : Errcode::ERR_TOKEN;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (errcode == Errcode::OK)
|
|
|
|
|
|
{
|
|
|
|
|
|
NJsonNode jsonparam;
|
|
|
|
|
|
bool ret = NJson::parse(req.body, jsonparam);
|
|
|
|
|
|
if (ret)
|
|
|
|
|
|
{
|
|
|
|
|
|
errcode = (this->*(handler.func))(req, resp, jsonparam);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
NJsonNode json;
|
2025-09-01 20:08:40 +08:00
|
|
|
|
json["errcode"] = errcode;
|
|
|
|
|
|
json["errmsg"] = ErrcodeStr(errcode) + (errmsg.empty() ? "" : (":"+errmsg));
|
|
|
|
|
|
resp.set_content(json.dump(), "text/plain; charset=utf-8");
|
|
|
|
|
|
resp.status = 200;
|
|
|
|
|
|
});
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::login(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<Fields> vecPermission;
|
|
|
|
|
|
int roleId = fields.get<int>(DMRole::ROLE_ID);
|
|
|
|
|
|
DAO::queryRolePermission(dao, roleId, vecPermission);
|
|
|
|
|
|
|
|
|
|
|
|
NJsonNode jnode = NJsonNode::array();
|
|
|
|
|
|
for (auto& item : vecPermission) { jnode.push_back(item.value("name")); }
|
|
|
|
|
|
json["permission"] = jnode;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DAO::insertSystemLogUser(token, "用户登录:" + ErrcodeStr(err), (err==Errcode::OK) ? 0: 1);
|
|
|
|
|
|
return err;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::queryUserList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::insertUser(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"account", "name", "gender", "age", "phone", "email", "role_id"}, params);
|
|
|
|
|
|
return DAO::insertUser(params);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::updateUser(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"user_id", "name", "gender", "age", "phone", "email", "role_id"}, params);
|
|
|
|
|
|
return DAO::updateUserById(params);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::deleteUser(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string userId = req.get_param_value("user_id");
|
|
|
|
|
|
return DAO::deleteUserById(userId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::queryPermissionList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
return err;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::insertPermission(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"name", "describe", "is_open"}, params);
|
|
|
|
|
|
return DAO::insertPermission(params);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::updatePermission(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"permission_id", "name", "describe", "is_open"}, params);
|
|
|
|
|
|
return DAO::updatePermissionById(params);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::deletePermission(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string permissionId = req.get_param_value("permission_id");
|
|
|
|
|
|
return DAO::deletePermissionById(permissionId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::queryRoleList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
// 查询所有的角色权限关联
|
|
|
|
|
|
if (err == Errcode::OK)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::vector<Fields> vecPermission;
|
|
|
|
|
|
err = DAO::queryRolePermission(NULL, vecPermission);
|
|
|
|
|
|
if (err != Errcode::OK)
|
|
|
|
|
|
{
|
|
|
|
|
|
return err;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::map<std::string, std::vector<NJsonNode>> mapPermission;
|
|
|
|
|
|
for (auto& item: vecPermission)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string roleId = item.value("role_id");
|
|
|
|
|
|
auto& v = mapPermission[roleId];
|
|
|
|
|
|
NJsonNode jnode;
|
|
|
|
|
|
jnode["id"] = item.value("permission_id");
|
|
|
|
|
|
jnode["name"] = item.value("permission_name");
|
|
|
|
|
|
v.push_back(jnode);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HttpHelper::setPagination(pageinfo, result, json);
|
|
|
|
|
|
if (json.contains("data"))
|
|
|
|
|
|
{
|
|
|
|
|
|
for (auto& item : json["data"])
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string roleId = item["role_id"];
|
|
|
|
|
|
item["permission"] = mapPermission[roleId];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return err;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::insertRole(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"name", "describe", "is_open", "permission"}, params);
|
|
|
|
|
|
return DAO::insertRole(params);
|
|
|
|
|
|
};
|
|
|
|
|
|
Errcode HttpEntity::updateRole(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"role_id", "name", "describe", "is_open", "permission"}, params);
|
|
|
|
|
|
return DAO::updateRoleById(params);
|
|
|
|
|
|
};
|
|
|
|
|
|
Errcode HttpEntity::deleteRole(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string roleId = req.get_param_value(DMRole::ROLE_ID);
|
|
|
|
|
|
return DAO::remove(NULL, DMRole::TABLENAME, DMRole::ROLE_ID, roleId);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::queryStationList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
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;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::insertStation(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"name", "address", "lon", "lat", "tel", "capacity", "status"}, params);
|
|
|
|
|
|
return DAO::insertStation(params);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::updateStation(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"station_id", "name", "address", "lon", "lat", "tel", "capacity", "status"}, params);
|
|
|
|
|
|
return DAO::updateStationById(params);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::deleteStation(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string primaryKey = DMStation::STATION_ID;
|
|
|
|
|
|
return DAO::remove(NULL, DMStation::TABLENAME, primaryKey, req.get_param_value(primaryKey));
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
Errcode HttpEntity::queryStationInfo(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 查询场站的基础配置信息
|
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
|
|
|
|
NJsonNode jsondata;
|
|
|
|
|
|
std::string attr = fields.remove(DMStation::ATTR);
|
|
|
|
|
|
NJson::parse(attr, jsondata);
|
|
|
|
|
|
|
|
|
|
|
|
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额定功率
|
|
|
|
|
|
}
|
|
|
|
|
|
Errcode HttpEntity::queryStationData(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 温度, 电压、电流、功率、功率因数、
|
|
|
|
|
|
NJsonNode jsondata;
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::queryDeviceList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
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;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::insertDevice(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"station_id", "type", "name", "code", "model", "factory", "factory_tel", "is_open", "attrs"}, params);
|
|
|
|
|
|
return DAO::insertDevice(params);
|
|
|
|
|
|
};
|
|
|
|
|
|
Errcode HttpEntity::updateDevice(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"device_id", "station_id", "type", "name", "code", "model", "factory", "factory_tel", "is_open", "attrs"}, params);
|
|
|
|
|
|
return DAO::updateDeviceById(params);
|
|
|
|
|
|
};
|
|
|
|
|
|
Errcode HttpEntity::deleteDevice(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string primaryKey = DMDevice::DEVICE_ID;
|
|
|
|
|
|
return DAO::remove(NULL, DMDevice::TABLENAME, primaryKey, req.get_param_value(primaryKey));
|
|
|
|
|
|
};
|
|
|
|
|
|
Errcode HttpEntity::queryDevicTypeDef(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::queryPolicyList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
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;
|
|
|
|
|
|
};
|
|
|
|
|
|
Errcode HttpEntity::insertPolicy(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"type", "name", "describe", "value", "is_open"}, params);
|
|
|
|
|
|
return DAO::insertPolicy(params);
|
|
|
|
|
|
};
|
|
|
|
|
|
Errcode HttpEntity::updatePolicy(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"policy_id", "type", "describe", "value", "is_open"}, params);
|
|
|
|
|
|
return DAO::updatePolicyById(params);
|
|
|
|
|
|
};
|
|
|
|
|
|
Errcode HttpEntity::deletePolicy(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
return DAO::deletePolicyById(req.get_param_value("prolicy_id"));
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::querySystemLogList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
Errcode HttpEntity::updateSystemLog(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"log_id", "status"}, params);
|
|
|
|
|
|
return DAO::updateSystemLogById(params);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::queryAlertLogList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
Errcode HttpEntity::updateAlertLog(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
Fields params;
|
|
|
|
|
|
GetRequestParam(req, {"log_id", "status"}, params);
|
|
|
|
|
|
return DAO::updateAlertLogById(params);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::queryPredictionDetail(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
NJsonNode jsonData = NJsonNode::array();
|
|
|
|
|
|
for (int i = 1; i<=5; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
NJsonNode jnode;
|
|
|
|
|
|
jnode["datatype"] = i;
|
|
|
|
|
|
NJsonNode jsonValues = NJsonNode::array();
|
|
|
|
|
|
for (int i = 0; i<1440; ++i)
|
|
|
|
|
|
{
|
|
|
|
|
|
jsonValues.push_back(float(Utils::random(50, 100)));
|
|
|
|
|
|
}
|
|
|
|
|
|
jnode["values"] = jsonValues;
|
|
|
|
|
|
jsonData.push_back(jnode);
|
|
|
|
|
|
}
|
|
|
|
|
|
json["data"] = jsonData;
|
2025-09-01 20:08:40 +08:00
|
|
|
|
return Errcode::OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::queryStatSystem(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& appdata = Application::data();
|
|
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
NJsonNode jsondata;
|
|
|
|
|
|
jsondata["launch_date"] = "2025-01-01"; //: 系统上线启用日期,格式:yyyy-mm-dd
|
|
|
|
|
|
jsondata["income_total"] = std::to_string(Utils::random(100, 200)); // : 累计收益(元),精度0.01
|
|
|
|
|
|
jsondata["station_num"] = Utils::toStr(appdata.getStationCount()); // : 能源站数量
|
|
|
|
|
|
jsondata["storage_device_num "] = Utils::toStr(appdata.getStationCount()); //: 储能设备数量
|
|
|
|
|
|
jsondata["solar_device_num"] = "0"; // : 光伏设备数量
|
|
|
|
|
|
jsondata["capacity_total"] = std::to_string(Utils::random(100, 200)); // : 储能总容量(kWh),精度0.001
|
|
|
|
|
|
jsondata["solar_elect_gen"] = std::to_string(Utils::random(100, 200)); // : 发电总电量(kWh),精度0.001
|
|
|
|
|
|
jsondata["solar_elect_grid"] = std::to_string(Utils::random(100, 200)); // : 入网种电量(kWh),精度0.001
|
|
|
|
|
|
jsondata["storage_elect_in"] = std::to_string(Utils::random(100, 200)); // : 储能充电总电量(kWh),精度0.001
|
|
|
|
|
|
jsondata["storage_elect_out"] = std::to_string(Utils::random(100, 200)); // : 储能放电总电量(kWh),精度0.001
|
|
|
|
|
|
|
|
|
|
|
|
json["data"] = jsondata;
|
2025-09-01 20:08:40 +08:00
|
|
|
|
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");
|
|
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
NJsonNode jsondata;
|
|
|
|
|
|
jsondata["station_id"] = "1";
|
|
|
|
|
|
jsondata["launch_date"] = "2025-01-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
|
|
|
|
|
|
|
|
|
|
|
|
json["data"] = jsondata;
|
|
|
|
|
|
return Errcode::OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
Errcode HttpEntity::queryStatStation(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");
|
|
|
|
|
|
NJsonNode jsondata;
|
|
|
|
|
|
jsondata["station_id"] = "1";
|
|
|
|
|
|
jsondata["launch_date"] = "2025-01-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
|
|
|
|
|
|
|
|
|
|
|
|
json["data"] = jsondata;
|
2025-09-01 20:08:40 +08:00
|
|
|
|
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");
|
2025-09-04 19:31:04 +08:00
|
|
|
|
std::string dt_start = req.get_param_value("start_date");
|
|
|
|
|
|
std::string dt_end = req.get_param_value("end_date");
|
|
|
|
|
|
|
|
|
|
|
|
int64_t t1 = Utils::time(dt_start)/1000;
|
|
|
|
|
|
int64_t t2 = Utils::time(dt_end)/1000;
|
2025-09-01 20:08:40 +08:00
|
|
|
|
|
2025-09-04 19:31:04 +08:00
|
|
|
|
int64_t tMax = t1+ 86400 * 30;
|
|
|
|
|
|
NJsonNode jsondata = NJsonNode::array();
|
|
|
|
|
|
for (int64_t t = t1; t<=t2 && t<=tMax; t += 86400)
|
2025-09-01 20:08:40 +08:00
|
|
|
|
{
|
2025-09-04 19:31:04 +08:00
|
|
|
|
NJsonNode jnode;
|
|
|
|
|
|
jnode["station_id"] = station_id;
|
|
|
|
|
|
if (!category.empty()) jnode["category"] = category;
|
|
|
|
|
|
jnode["dt"] = Utils::dateStr(t*1000); //日期
|
|
|
|
|
|
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);
|
2025-09-01 20:08:40 +08:00
|
|
|
|
}
|
2025-09-04 19:31:04 +08:00
|
|
|
|
json["data"] = jsondata;
|
|
|
|
|
|
return Errcode::OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Errcode HttpEntity::queryEnvironment(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
|
|
|
|
|
{
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NJsonNode jsondata;
|
|
|
|
|
|
|
|
|
|
|
|
{ // 温湿度
|
|
|
|
|
|
auto& mapTempHumUnit = station->mapTempHumUnit;
|
|
|
|
|
|
NJsonNode nodearray = NJsonNode::array();
|
|
|
|
|
|
for (auto iter = mapTempHumUnit.begin(); iter!=mapTempHumUnit.end(); iter++)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& unit = iter->second;
|
|
|
|
|
|
NJsonNode node;
|
|
|
|
|
|
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;
|
|
|
|
|
|
NJsonNode nodearray = NJsonNode::array();
|
|
|
|
|
|
if (unit)
|
|
|
|
|
|
{
|
|
|
|
|
|
NJsonNode node;
|
|
|
|
|
|
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;
|
|
|
|
|
|
NJsonNode nodearray = NJsonNode::array();
|
|
|
|
|
|
for (auto iter = mapFire40Unit.begin(); iter!=mapFire40Unit.end(); ++iter)
|
|
|
|
|
|
{
|
|
|
|
|
|
NJsonNode node;
|
|
|
|
|
|
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;
|
|
|
|
|
|
NJsonNode nodearray = NJsonNode::array();
|
|
|
|
|
|
if (unit)
|
|
|
|
|
|
{
|
|
|
|
|
|
NJsonNode node;
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|