mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
修改运行监控场站及设备信息查询接口
This commit is contained in:
@@ -7,15 +7,16 @@
|
||||
#include "app/AppData.h"
|
||||
#include "app/Config.h"
|
||||
#include "app/Station.h"
|
||||
#include "app/Device.h"
|
||||
|
||||
static void FieldsToJson(Fields& fields, NJsonNode& json)
|
||||
static void FieldsToJson(Fields& fields, njson& json)
|
||||
{
|
||||
for (auto& item : fields.map())
|
||||
{
|
||||
json[item.first] = item.second;
|
||||
}
|
||||
}
|
||||
static void JsonToFields(NJsonNode& json, std::vector<std::string> vecKeys, Fields& fields)
|
||||
static void JsonToFields(njson& json, std::vector<std::string> vecKeys, Fields& fields)
|
||||
{
|
||||
if (vecKeys.empty())
|
||||
{
|
||||
@@ -32,16 +33,16 @@ static void JsonToFields(NJsonNode& json, std::vector<std::string> vecKeys, Fiel
|
||||
|
||||
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;
|
||||
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;
|
||||
case njson::value_t::object: {} break;
|
||||
case njson::value_t::array: {} break;
|
||||
case njson::value_t::binary: {} break;
|
||||
case njson::value_t::discarded: {} break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -50,12 +51,12 @@ static void JsonToFields(NJsonNode& json, std::vector<std::string> vecKeys, Fiel
|
||||
}
|
||||
}
|
||||
|
||||
static NJsonNode FieldsToJsonArray(std::vector<Fields>& vecFields)
|
||||
static njson FieldsToJsonArray(std::vector<Fields>& vecFields)
|
||||
{
|
||||
NJsonNode jsonnode = NJsonNode::array();
|
||||
njson jsonnode = njson::array();
|
||||
for (auto& fields : vecFields)
|
||||
{
|
||||
NJsonNode jnode;
|
||||
njson jnode;
|
||||
for (auto& item : fields.map())
|
||||
{
|
||||
jnode[item.first] = item.second;
|
||||
@@ -79,8 +80,8 @@ static void GetRequestParam(const httplib::Request& req, const std::vector<std::
|
||||
}
|
||||
else if (req.method == "POST")
|
||||
{
|
||||
NJsonNode json;
|
||||
NJson::parse(req.body, json);
|
||||
njson json;
|
||||
JSON::parse(req.body, json);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -114,7 +115,7 @@ public:
|
||||
}
|
||||
return true;
|
||||
}
|
||||
static void setPagination(PageInfo& pageinfo, std::vector<Fields> result, NJsonNode& json)
|
||||
static void setPagination(PageInfo& pageinfo, std::vector<Fields> result, njson& json)
|
||||
{
|
||||
json["count"] = pageinfo.total;
|
||||
json["page"] = pageinfo.index;
|
||||
@@ -142,11 +143,16 @@ static std::map<std::string, HandlerOptions> g_mapHttpHandlerGet =
|
||||
|
||||
{"/queryStationInfo", HandlerOptions(&HttpEntity::queryStationInfo, { DMStation::STATION_ID})},
|
||||
{"/queryStationData", HandlerOptions(&HttpEntity::queryStationData, { DMStation::STATION_ID})},
|
||||
{"/queryStationOverview", HandlerOptions(&HttpEntity::queryStationOverview, {DMStation::STATION_ID})},
|
||||
|
||||
|
||||
{"/queryDeviceList", HandlerOptions(&HttpEntity::queryDeviceList, {})},
|
||||
{"/deleteDevice", HandlerOptions(&HttpEntity::deleteDevice, { DMDevice::DEVICE_ID})},
|
||||
{"/queryDevicTypeDef", HandlerOptions(&HttpEntity::queryDevicTypeDef, {})},
|
||||
{"/queryDevicByCategory", HandlerOptions(&HttpEntity::queryDevicByCategory, {DMStation::STATION_ID, "category"})},
|
||||
{"/queryDevicCharts", HandlerOptions(&HttpEntity::queryDevicCharts, {DMStation::STATION_ID, "device_id"})},
|
||||
|
||||
|
||||
{"/queryPolicyList", HandlerOptions(&HttpEntity::queryPolicyList, {})},
|
||||
|
||||
{"/deletePolicy", HandlerOptions(&HttpEntity::deletePolicy, { DMPolicy::POLICY_ID})},
|
||||
@@ -248,7 +254,7 @@ void HttpEntity::runHandler(std::string name, const HandlerOptions& handler, con
|
||||
errcode = ret ? Errcode::OK : Errcode::ERR_TOKEN;
|
||||
}
|
||||
|
||||
NJsonNode jsonresp;
|
||||
njson jsonresp;
|
||||
if (errcode == Errcode::OK)
|
||||
{
|
||||
if (!HttpHelper::CheckRequestParam(req, resp, handler.requiredKeys, errmsg))
|
||||
@@ -257,7 +263,7 @@ void HttpEntity::runHandler(std::string name, const HandlerOptions& handler, con
|
||||
}
|
||||
else
|
||||
{
|
||||
errcode = (this->*(handler.func))(req, resp, jsonresp);
|
||||
errcode = (this->*(handler.func))(req, jsonresp, errmsg);
|
||||
}
|
||||
}
|
||||
jsonresp["errcode"] = errcode;
|
||||
@@ -271,7 +277,7 @@ void HttpEntity::registGet(std::string name, void (HttpEntity::* func)(const htt
|
||||
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)
|
||||
Errcode HttpEntity::login(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
std::string userId;
|
||||
std::string token;
|
||||
@@ -291,10 +297,10 @@ Errcode HttpEntity::login(const httplib::Request& req, httplib::Response& resp,
|
||||
int roleId = fields.get<int>(DMRole::ROLE_ID);
|
||||
DAO::queryRolePermission(dao, roleId, vecPermission);
|
||||
|
||||
NJsonNode nodePermission = NJsonNode::array();
|
||||
njson nodePermission = njson::array();
|
||||
for (auto& item : vecPermission)
|
||||
{
|
||||
NJsonNode node;
|
||||
njson node;
|
||||
FieldsToJson(item, node);
|
||||
nodePermission.push_back(node);
|
||||
}
|
||||
@@ -305,7 +311,7 @@ Errcode HttpEntity::login(const httplib::Request& req, httplib::Response& resp,
|
||||
return err;
|
||||
}
|
||||
|
||||
Errcode HttpEntity::queryUserList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::queryUserList(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
std::string token = req.get_param_value("token");
|
||||
PageInfo pageinfo;
|
||||
@@ -322,27 +328,27 @@ Errcode HttpEntity::queryUserList(const httplib::Request& req, httplib::Response
|
||||
return err;
|
||||
}
|
||||
|
||||
Errcode HttpEntity::insertUser(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::insertUser(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
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)
|
||||
Errcode HttpEntity::updateUser(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"user_id", "account", "name", "gender", "age", "phone", "email", "role_id"}, params);
|
||||
return DAO::updateUserById(params);
|
||||
}
|
||||
|
||||
Errcode HttpEntity::deleteUser(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::deleteUser(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
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)
|
||||
Errcode HttpEntity::queryPermissionList(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
PageInfo pageinfo;
|
||||
pageinfo.index = Utils::toInt(req.get_param_value("page"));
|
||||
@@ -354,28 +360,28 @@ Errcode HttpEntity::queryPermissionList(const httplib::Request& req, httplib::Re
|
||||
return err;
|
||||
}
|
||||
|
||||
Errcode HttpEntity::insertPermission(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::insertPermission(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
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)
|
||||
Errcode HttpEntity::updatePermission(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
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)
|
||||
Errcode HttpEntity::deletePermission(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
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)
|
||||
Errcode HttpEntity::queryRoleList(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
std::string token = req.get_param_value("page");
|
||||
PageInfo pageinfo;
|
||||
@@ -394,12 +400,12 @@ Errcode HttpEntity::queryRoleList(const httplib::Request& req, httplib::Response
|
||||
return err;
|
||||
}
|
||||
|
||||
std::map<std::string, std::vector<NJsonNode>> mapPermission;
|
||||
std::map<std::string, std::vector<njson>> mapPermission;
|
||||
for (auto& fields: vecPermission)
|
||||
{
|
||||
std::string roleId = fields.value("role_id");
|
||||
auto& v = mapPermission[roleId];
|
||||
NJsonNode node;
|
||||
njson node;
|
||||
FieldsToJson(fields, node);
|
||||
v.push_back(node);
|
||||
}
|
||||
@@ -417,19 +423,19 @@ Errcode HttpEntity::queryRoleList(const httplib::Request& req, httplib::Response
|
||||
return err;
|
||||
}
|
||||
|
||||
Errcode HttpEntity::insertRole(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::insertRole(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
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)
|
||||
Errcode HttpEntity::updateRole(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
//GetRequestParam(req, {"role_id", "name", "describe", "is_open", "permission"}, params);
|
||||
|
||||
NJsonNode jsonparam;
|
||||
if (!NJson::parse(req.body, jsonparam))
|
||||
njson jsonparam;
|
||||
if (!JSON::parse(req.body, jsonparam))
|
||||
{
|
||||
return Errcode::ERR_PARAM;
|
||||
}
|
||||
@@ -459,13 +465,13 @@ Errcode HttpEntity::updateRole(const httplib::Request& req, httplib::Response& r
|
||||
}
|
||||
return err;
|
||||
};
|
||||
Errcode HttpEntity::deleteRole(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::deleteRole(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
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)
|
||||
Errcode HttpEntity::queryStationList(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
PageInfo pageinfo;
|
||||
pageinfo.index = Utils::toInt(req.get_param_value("page"));
|
||||
@@ -477,7 +483,7 @@ Errcode HttpEntity::queryStationList(const httplib::Request& req, httplib::Respo
|
||||
return err;
|
||||
};
|
||||
|
||||
Errcode HttpEntity::insertStation(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::insertStation(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
|
||||
Fields params;
|
||||
@@ -485,20 +491,80 @@ Errcode HttpEntity::insertStation(const httplib::Request& req, httplib::Response
|
||||
return DAO::insertStation(params);
|
||||
};
|
||||
|
||||
Errcode HttpEntity::updateStation(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::updateStation(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
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)
|
||||
Errcode HttpEntity::deleteStation(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
std::string primaryKey = DMStation::STATION_ID;
|
||||
return DAO::remove(NULL, DMStation::TABLENAME, primaryKey, req.get_param_value(primaryKey));
|
||||
};
|
||||
|
||||
Errcode HttpEntity::queryStationInfo(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
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);
|
||||
}
|
||||
njson jsonStorage = njson::parse(R"({"category":1, "count":0, "power":0.0})");
|
||||
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)
|
||||
{
|
||||
// 查询场站的基础配置信息
|
||||
std::string stationId = req.get_param_value("station_id");
|
||||
@@ -520,9 +586,9 @@ Errcode HttpEntity::queryStationInfo(const httplib::Request& req, httplib::Respo
|
||||
}
|
||||
auto& fields = result[0];
|
||||
|
||||
NJsonNode jsondata;
|
||||
njson jsondata;
|
||||
std::string attr = fields.remove(DMStation::ATTR);
|
||||
NJson::parse(attr, jsondata);
|
||||
JSON::parse(attr, jsondata);
|
||||
|
||||
FieldsToJson(fields, jsondata);
|
||||
json["data"] = jsondata;
|
||||
@@ -537,10 +603,10 @@ Errcode HttpEntity::queryStationInfo(const httplib::Request& req, httplib::Respo
|
||||
// voltage_rated: 电池额定电压:
|
||||
// power_rated: PCS额定功率
|
||||
}
|
||||
Errcode HttpEntity::queryStationData(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::queryStationData(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
// 温度, 电压、电流、功率、功率因数、
|
||||
NJsonNode jsondata;
|
||||
njson jsondata;
|
||||
jsondata["voltage"] = Utils::toStr(200.32);
|
||||
jsondata["current"] = Utils::toStr(20.56);
|
||||
jsondata["power"] = Utils::toStr(200.32);
|
||||
@@ -554,7 +620,7 @@ Errcode HttpEntity::queryStationData(const httplib::Request& req, httplib::Respo
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
Errcode HttpEntity::queryDeviceList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::queryDeviceList(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
PageInfo pageinfo;
|
||||
pageinfo.index = Utils::toInt(req.get_param_value("page"));
|
||||
@@ -566,24 +632,24 @@ Errcode HttpEntity::queryDeviceList(const httplib::Request& req, httplib::Respon
|
||||
return err;
|
||||
};
|
||||
|
||||
Errcode HttpEntity::insertDevice(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::insertDevice(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
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)
|
||||
Errcode HttpEntity::updateDevice(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
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)
|
||||
Errcode HttpEntity::deleteDevice(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
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)
|
||||
Errcode HttpEntity::queryDevicTypeDef(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
std::string sql = "SELECT device_type_id, name FROM def_device_type;";
|
||||
std::vector<Fields> result;
|
||||
@@ -591,8 +657,76 @@ Errcode HttpEntity::queryDevicTypeDef(const httplib::Request& req, httplib::Resp
|
||||
json["data"] = FieldsToJsonArray(result);
|
||||
return err;
|
||||
}
|
||||
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; }
|
||||
|
||||
Errcode HttpEntity::queryPolicyList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Errcode HttpEntity::queryPolicyList(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
PageInfo pageinfo;
|
||||
pageinfo.index = Utils::toInt(req.get_param_value("page"));
|
||||
@@ -603,26 +737,26 @@ Errcode HttpEntity::queryPolicyList(const httplib::Request& req, httplib::Respon
|
||||
HttpHelper::setPagination(pageinfo, result, json);
|
||||
return err;
|
||||
};
|
||||
Errcode HttpEntity::insertPolicy(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::insertPolicy(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
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)
|
||||
Errcode HttpEntity::updatePolicy(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
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)
|
||||
Errcode HttpEntity::deletePolicy(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"policy_id"}, params);
|
||||
return DAO::deletePolicyById(params.value("policy_id"));
|
||||
};
|
||||
|
||||
Errcode HttpEntity::querySystemLogList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::querySystemLogList(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
PageInfo pageinfo;
|
||||
pageinfo.index = Utils::toInt(req.get_param_value("page"));
|
||||
@@ -635,14 +769,14 @@ Errcode HttpEntity::querySystemLogList(const httplib::Request& req, httplib::Res
|
||||
}
|
||||
|
||||
//Errcode insertSystemLog(const httplib::Request& req, httplib::Response& resp, NJsonNode& json);
|
||||
Errcode HttpEntity::updateSystemLog(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::updateSystemLog(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"log_id", "status"}, params);
|
||||
return DAO::updateSystemLogById(params);
|
||||
}
|
||||
|
||||
Errcode HttpEntity::queryAlertLogList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::queryAlertLogList(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
PageInfo pageinfo;
|
||||
pageinfo.index = Utils::toInt(req.get_param_value("page"));
|
||||
@@ -655,21 +789,21 @@ Errcode HttpEntity::queryAlertLogList(const httplib::Request& req, httplib::Resp
|
||||
}
|
||||
|
||||
//Errcode insertAlertLog(const httplib::Request& req, httplib::Response& resp, NJsonNode& json);
|
||||
Errcode HttpEntity::updateAlertLog(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::updateAlertLog(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"log_id", "status"}, params);
|
||||
return DAO::updateAlertLogById(params);
|
||||
}
|
||||
|
||||
Errcode HttpEntity::queryPredictionDetail(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::queryPredictionDetail(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
NJsonNode jsonData = NJsonNode::array();
|
||||
njson jsonData = njson::array();
|
||||
for (int i = 1; i<=5; i++)
|
||||
{
|
||||
NJsonNode jnode;
|
||||
njson jnode;
|
||||
jnode["datatype"] = i;
|
||||
NJsonNode jsonValues = NJsonNode::array();
|
||||
njson jsonValues = njson::array();
|
||||
for (int i = 0; i<1440; ++i)
|
||||
{
|
||||
jsonValues.push_back(float(Utils::random(50, 100)));
|
||||
@@ -681,11 +815,11 @@ Errcode HttpEntity::queryPredictionDetail(const httplib::Request& req, httplib::
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
Errcode HttpEntity::queryStatSystem(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::queryStatSystem(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
auto& appdata = Application::data();
|
||||
|
||||
NJsonNode jsondata;
|
||||
njson 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()); // : 能源站数量
|
||||
@@ -700,12 +834,12 @@ Errcode HttpEntity::queryStatSystem(const httplib::Request& req, httplib::Respon
|
||||
json["data"] = jsondata;
|
||||
return Errcode::OK;
|
||||
}
|
||||
Errcode HttpEntity::queryStatTotal(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::queryStatTotal(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
std::string station_id = req.get_param_value("station_id");
|
||||
std::string category = req.get_param_value("category");
|
||||
|
||||
NJsonNode jsondata;
|
||||
njson jsondata;
|
||||
jsondata["station_id"] = "1";
|
||||
jsondata["launch_date"] = "2025-01-01"; //场站上线日期
|
||||
jsondata["usage_rate"] = "12";
|
||||
@@ -726,11 +860,11 @@ Errcode HttpEntity::queryStatTotal(const httplib::Request& req, httplib::Respons
|
||||
json["data"] = jsondata;
|
||||
return Errcode::OK;
|
||||
}
|
||||
Errcode HttpEntity::queryStatStation(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::queryStatStation(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
std::string station_id = req.get_param_value("station_id");
|
||||
std::string category = req.get_param_value("category");
|
||||
NJsonNode jsondata;
|
||||
njson jsondata;
|
||||
jsondata["station_id"] = "1";
|
||||
jsondata["launch_date"] = "2025-01-01"; //场站上线日期
|
||||
jsondata["usage_rate"] = "12";
|
||||
@@ -752,7 +886,7 @@ Errcode HttpEntity::queryStatStation(const httplib::Request& req, httplib::Respo
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
Errcode HttpEntity::queryStatDayList(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::queryStatDayList(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
std::string station_id = req.get_param_value("station_id");
|
||||
std::string category = req.get_param_value("category");
|
||||
@@ -763,10 +897,10 @@ Errcode HttpEntity::queryStatDayList(const httplib::Request& req, httplib::Respo
|
||||
int64_t t2 = Utils::time(dt_end)/1000;
|
||||
|
||||
int64_t tMax = t1+ 86400 * 30;
|
||||
NJsonNode jsondata = NJsonNode::array();
|
||||
njson jsondata = njson::array();
|
||||
for (int64_t t = t1; t<=t2 && t<=tMax; t += 86400)
|
||||
{
|
||||
NJsonNode jnode;
|
||||
njson jnode;
|
||||
jnode["station_id"] = station_id;
|
||||
if (!category.empty()) jnode["category"] = category;
|
||||
jnode["dt"] = Utils::dateStr(t*1000); //日期
|
||||
@@ -790,7 +924,7 @@ Errcode HttpEntity::queryStatDayList(const httplib::Request& req, httplib::Respo
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
Errcode HttpEntity::queryEnvironment(const httplib::Request& req, httplib::Response& resp, NJsonNode& json)
|
||||
Errcode HttpEntity::queryEnvironment(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
std::string stationId = req.get_param_value("station_id");
|
||||
auto& appdata = Application::data();
|
||||
@@ -802,15 +936,15 @@ Errcode HttpEntity::queryEnvironment(const httplib::Request& req, httplib::Respo
|
||||
return Errcode::ERR_PARAM;
|
||||
}
|
||||
|
||||
NJsonNode jsondata;
|
||||
njson jsondata;
|
||||
|
||||
{ // 温湿度
|
||||
auto& mapTempHumUnit = station->mapTempHumUnit;
|
||||
NJsonNode nodearray = NJsonNode::array();
|
||||
njson nodearray = njson::array();
|
||||
for (auto iter = mapTempHumUnit.begin(); iter!=mapTempHumUnit.end(); iter++)
|
||||
{
|
||||
auto& unit = iter->second;
|
||||
NJsonNode node;
|
||||
njson node;
|
||||
node["pos"] = "#" + std::to_string(iter->first);
|
||||
node["temp"] = unit.temp;
|
||||
node["hum"] = unit.hum;
|
||||
@@ -822,10 +956,10 @@ Errcode HttpEntity::queryEnvironment(const httplib::Request& req, httplib::Respo
|
||||
auto& mapAircUnit = station->mapAircUnit;
|
||||
AircUnit unitTmp;
|
||||
AircUnit* unit = (mapAircUnit.size() > 0) ? &(mapAircUnit[0]) : &unitTmp;
|
||||
NJsonNode nodearray = NJsonNode::array();
|
||||
njson nodearray = njson::array();
|
||||
if (unit)
|
||||
{
|
||||
NJsonNode node;
|
||||
njson 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 ? "关闭" : "启动"}});
|
||||
@@ -845,10 +979,10 @@ Errcode HttpEntity::queryEnvironment(const httplib::Request& req, httplib::Respo
|
||||
static std::map<int, std::string> mapFireStatusDef = { {0, "正常"}, {1,"预警"}, {2,"火警"} };
|
||||
|
||||
auto& mapFire40Unit = station->mapFire40Unit;
|
||||
NJsonNode nodearray = NJsonNode::array();
|
||||
njson nodearray = njson::array();
|
||||
for (auto iter = mapFire40Unit.begin(); iter!=mapFire40Unit.end(); ++iter)
|
||||
{
|
||||
NJsonNode node;
|
||||
njson node;
|
||||
node["pos"] = "#" + std::to_string(iter->first);
|
||||
node["status"] = mapFireStatusDef[iter->second]; // 0:正常 1:预警 2:火警
|
||||
nodearray.push_back(node);
|
||||
@@ -859,10 +993,10 @@ Errcode HttpEntity::queryEnvironment(const httplib::Request& req, httplib::Respo
|
||||
auto& mapCoolingUnit = station->mapCoolingUnit;
|
||||
CoolingUnit unitTmp;
|
||||
CoolingUnit* unit = (mapCoolingUnit.size() > 0) ? &(mapCoolingUnit[0]) : &unitTmp;
|
||||
NJsonNode nodearray = NJsonNode::array();
|
||||
njson nodearray = njson::array();
|
||||
if (unit)
|
||||
{
|
||||
NJsonNode node;
|
||||
njson 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 ? "关闭" : "启动"}});
|
||||
|
||||
Reference in New Issue
Block a user