mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
调试修改MQTT通讯解析
This commit is contained in:
@@ -165,9 +165,10 @@ static std::map<std::string, HandlerOptions> g_mapHttpHandlerGet =
|
||||
{"/queryPredictionDetail", HandlerOptions(&HttpEntity::queryPredictionDetail, {})},
|
||||
|
||||
{"/queryStatSystem", HandlerOptions(&HttpEntity::queryStatSystem, {})},
|
||||
{"/queryStatStation", HandlerOptions(&HttpEntity::queryStatStation, {})},
|
||||
{"/queryStatStation", HandlerOptions(&HttpEntity::queryStatStationGroup, {})},
|
||||
{"/queryStatTotal", HandlerOptions(&HttpEntity::queryStatTotal, {})},
|
||||
{"/queryStatDayList", HandlerOptions(&HttpEntity::queryStatDayList, {})},
|
||||
{"/queryStatDetailList", HandlerOptions(&HttpEntity::queryStatDetailList, {})},
|
||||
{"/queryStatCharts", HandlerOptions(&HttpEntity::queryStatCharts, {})},
|
||||
|
||||
{"/queryEnvironment", HandlerOptions(&HttpEntity::queryEnvironment, { "station_id"})},
|
||||
@@ -827,7 +828,7 @@ Errcode HttpEntity::queryDevicByCategory(const httplib::Request& req, njson& jso
|
||||
if (station && station->status == 1)
|
||||
{
|
||||
std::vector<std::shared_ptr<Device>> vecDevice;
|
||||
station->getDeviceByGroup(category, vecDevice);
|
||||
station->getDeviceByCategory(category, vecDevice);
|
||||
|
||||
for(auto& device: vecDevice)
|
||||
{
|
||||
@@ -1041,13 +1042,13 @@ Errcode HttpEntity::queryStatSystem(const httplib::Request& req, njson& json, st
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
Errcode HttpEntity::queryStatStation(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
Errcode HttpEntity::queryStatStationGroup(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
|
||||
std::string sql = R"(SELECT s.station_id, s.name station_name, ss.income_elect, ss.income_charge, ss.storage_usage FROM station s LEFT JOIN
|
||||
(SELECT station_id, SUM(income_elect ) income_elect , SUM(income_charge) income_charge, avg(storage_usage) storage_usage FROM stat_station GROUP BY station_id) AS ss
|
||||
ON ss.station_id = s.station_id)";
|
||||
std::vector<Fields> vecStations;
|
||||
auto err = dao->exec(sql, vecStations);
|
||||
@@ -1056,96 +1057,151 @@ Errcode HttpEntity::queryStatStation(const httplib::Request& req, njson& json, s
|
||||
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");
|
||||
jsonnode["usage_rate"] = fields.get<float>("storage_usage");
|
||||
jsondata.push_back(jsonnode);
|
||||
}
|
||||
json["data"] = jsondata;
|
||||
return Errcode(err);
|
||||
}
|
||||
|
||||
Errcode HttpEntity::queryStatTotal(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
static void VerifyRequstParamsStatDate(Fields& params)
|
||||
{
|
||||
std::string station_id = req.get_param_value("station_id");
|
||||
std::string category = req.get_param_value("category");
|
||||
|
||||
njson jsondata;
|
||||
auto station = Application::data().getStation(Utils::toInt(station_id));
|
||||
if (station)
|
||||
if (!params.contains("end_date"))
|
||||
{
|
||||
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
|
||||
|
||||
}
|
||||
json["data"] = jsondata;
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
Errcode HttpEntity::queryStatDayList(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
//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())
|
||||
{
|
||||
if (dtStart.empty())
|
||||
if (!params.contains("start_date"))
|
||||
{
|
||||
dtEnd = Utils::dateStr();
|
||||
dtStart = Utils::dateStr(Utils::date() - 86400*7);
|
||||
params.set("end_date", Utils::dateStr());
|
||||
params.set("start_date", Utils::dateStr(Utils::date() - 86400*7));
|
||||
}
|
||||
else
|
||||
{
|
||||
dtEnd = Utils::dateStr(Utils::time(dtStart + " 00:00:00") + 86400*7);
|
||||
params.set("end_date", Utils::dateStr(Utils::time(params.value("start_date") +" 00:00:00") + 86400*7));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static std::string VerifyStatSqlCondition(Fields& params)
|
||||
{
|
||||
std::string stationId = params.value("station_id");
|
||||
std::string category = params.value("category");
|
||||
std::string dtStart = params.value("start_date");
|
||||
std::string dtEnd = params.value("end_date");
|
||||
|
||||
std::string sqlCondition;
|
||||
if (!dtStart.empty() && dtEnd.empty())
|
||||
{
|
||||
sqlCondition += "dt BETWEEN '" + dtStart + "' AND '" + dtEnd + "'";
|
||||
}
|
||||
if (!stationId.empty())
|
||||
{
|
||||
if (!sqlCondition.empty()) sqlCondition += " AND ";
|
||||
sqlCondition += "ss.station_id='" + stationId + "'";
|
||||
}
|
||||
if (!category.empty() && category != "0")
|
||||
{
|
||||
if (!sqlCondition.empty()) sqlCondition += " AND ";
|
||||
sqlCondition += "ss.category='" + category + "'";;
|
||||
}
|
||||
if (!sqlCondition.empty()) { sqlCondition = " WHERE " + sqlCondition; }
|
||||
return sqlCondition;
|
||||
}
|
||||
|
||||
static std::string GetRequestStatParams(const httplib::Request& req)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"station_id", "category", "start_date", "end_date"}, params);
|
||||
VerifyRequstParamsStatDate(params);
|
||||
return VerifyStatSqlCondition(params);
|
||||
}
|
||||
|
||||
Errcode HttpEntity::queryStatTotal(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
std::string sqlCondition = GetRequestStatParams(req);
|
||||
std::string sql = R"(SELECT SUM(ss.storage_elect_in) storage_elect_in,
|
||||
SUM(storage_elect_in) storage_elect_in,
|
||||
SUM(storage_elect_out) storage_elect_out,
|
||||
SUM(storage_num_in) storage_num_in,
|
||||
SUM(storage_num_out) storage_num_out,
|
||||
SUM(storage_num_err) storage_num_err,
|
||||
SUM(solar_elect_gen) solar_elect_gen,
|
||||
SUM(solar_elect_grid) solar_elect_grid,
|
||||
SUM(solar_num_err) solar_num_err,
|
||||
AVG(storage_usage) storage_usage,
|
||||
SUM(charge_elect) charge_elect,
|
||||
SUM(charge_num) charge_num,
|
||||
SUM(charge_num_err) charge_num_err,
|
||||
AVG(charge_usage) charge_usage,
|
||||
SUM(income_elect) income_elect,
|
||||
SUM(income_charge) income_charge
|
||||
FROM stat_station ss)" + sqlCondition + ";";
|
||||
|
||||
std::vector<Fields> result;
|
||||
Errcode err = DAO::queryStatDataList(NULL, dtStart, dtEnd, result);
|
||||
DaoEntity::execOnce(sql, result);
|
||||
if (result.size() > 0)
|
||||
{
|
||||
auto& fields = result[0];
|
||||
njson jsondata;
|
||||
// jsondata["launch_date"] = "2025-09-01"; //场站上线日期
|
||||
// jsondata["station_id"] = station_id;
|
||||
jsondata["storage_elect_in"] = fields.value("storage_elect_in"); //储能充电电量(kWh),精度:0.001
|
||||
jsondata["storage_elect_out"] = fields.value("storage_elect_out"); //储能放电电量(kWh),精度:0.001
|
||||
jsondata["storage_num_in"] = fields.value("storage_elect_out"); //储能设备充电次数
|
||||
jsondata["storage_num_out"] = fields.value("storage_num_out"); //储能设备放电次数
|
||||
jsondata["storage_num_err"] = fields.value("storage_num_err"); //储能设备故障次数
|
||||
jsondata["solar_elect_gen"] = fields.value("solar_elect_gen"); //光伏发电电量(kWh),精度:0.001
|
||||
jsondata["solar_elect_grid"] = fields.value("solar_elect_grid"); //光伏入网电量(kWh),精度:0.001
|
||||
jsondata["solar_num_err"] = fields.value("solar_num_err"); //光伏设备故障次数
|
||||
jsondata["charge_elect"] = fields.value("charge_elect"); //充电设备充电电量(kWh),精度:0.001
|
||||
jsondata["charge_num"] = fields.value("charge_num"); //充电设备充电次数
|
||||
jsondata["charge_num_err"] = fields.value("charge_num_err"); //充电设备故障次数
|
||||
jsondata["income_elect"] = fields.value("income_elect"); //发电收益(元),精度:0.01
|
||||
jsondata["income_charge"] = fields.value("income_charge"); //充电收益(元),精度:0.01
|
||||
jsondata["usage_rate"] = Utils::toStr(float(fields.get<int>("storage_usage")+fields.get<int>("storage_usage"))*0.5f, 0);
|
||||
json["data"] = jsondata;
|
||||
}
|
||||
|
||||
return Errcode::OK;
|
||||
}
|
||||
Errcode HttpEntity::queryStatDayList(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"station_id", "category", "start_date", "end_date"}, params);
|
||||
std::string stationId = params.value("station_id");
|
||||
std::string category = params.value("category");
|
||||
std::string startDate = params.value("start_date");
|
||||
std::string endDate = params.value("end_date");
|
||||
std::vector<Fields> result;
|
||||
auto err = DAO::queryStatStationGroup(NULL, stationId, category, startDate, endDate, 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;
|
||||
return Errcode::OK;
|
||||
//Errcode HttpEntity::queryStatDayList(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
//{
|
||||
// std::string sqlCondition = GetRequestStatParams(req);
|
||||
// std::string sql = R"(SELECT ss.*, d.name device_name, ddt.name device_type FROM stat_station ss
|
||||
// LEFT JOIN device d ON d.device_id = ss.device_id
|
||||
// LEFT JOIN def_device_type ddt ON ddt.device_type_id = d.`type`)" + sqlCondition + ";";
|
||||
//
|
||||
// std::vector<Fields> result;
|
||||
// int ret = DaoEntity::execOnce(sql, result);
|
||||
// json["data"] = FieldsToJsonArray(result);
|
||||
// return Errcode(ret);
|
||||
//}
|
||||
|
||||
Errcode HttpEntity::queryStatDetailList(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"));
|
||||
|
||||
Fields params;
|
||||
GetRequestParam(req, {"station_id", "category", "start_date", "end_date"}, params);
|
||||
|
||||
std::vector<Fields> result;
|
||||
auto err = DAO::queryStatStationList(pageinfo, params, result);
|
||||
json["data"] = FieldsToJsonArray(result);
|
||||
return err;
|
||||
}
|
||||
|
||||
Errcode HttpEntity::queryStatCharts(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
@@ -1243,7 +1299,6 @@ Errcode HttpEntity::queryEnvironment(const httplib::Request& req, njson& json, s
|
||||
njson nodearray = njson::array();
|
||||
if (unit)
|
||||
{
|
||||
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 ? "关闭" : "启动"}});
|
||||
@@ -1262,14 +1317,22 @@ Errcode HttpEntity::queryEnvironment(const httplib::Request& req, njson& json, s
|
||||
{ // 消防
|
||||
static std::map<int, std::string> mapFireStatusDef = { {0, "正常"}, {1,"预警"}, {2,"火警"} };
|
||||
|
||||
|
||||
std::map<int, string> mapStatusDef = {{0, "无效"}, {1, "掉线"}, {2, "正常"}, {3, "启动"}};
|
||||
|
||||
auto& mapFire40Unit = station->mapFire40Unit;
|
||||
njson nodearray = njson::array();
|
||||
for (auto iter = mapFire40Unit.begin(); iter!=mapFire40Unit.end(); ++iter)
|
||||
{
|
||||
njson node;
|
||||
node["pos"] = "#" + std::to_string(iter->first);
|
||||
node["status"] = mapFireStatusDef[iter->second]; // 0:正常 1:预警 2:火警
|
||||
nodearray.push_back(node);
|
||||
auto& unit = iter->second;
|
||||
nodearray.push_back({{"pos", "主控状态"}, {"status", unit.statusMain == 0 ? "正常" : (unit.statusMain == 1 ? "预警" : "火警")}});
|
||||
nodearray.push_back({{"pos", "警铃是否使用"}, {"status", unit.usedAlarm == 0 ? "否" : "是"}});
|
||||
nodearray.push_back({{"pos", "警铃状态"}, {"status", mapStatusDef[unit.statusAlarm]}}); // 0:无效 1:掉线 2:正常 3:启动
|
||||
nodearray.push_back({{"pos", "瓶头阀是否使用"}, {"status", unit.usedValve == 0 ? "否" : "是"}});
|
||||
nodearray.push_back({{"pos", "瓶头阀状态"}, {"status", mapStatusDef[unit.statusAlarm]}}); // 0:无效 1:掉线 2:正常 3:启动
|
||||
nodearray.push_back({{"pos", "手报是否使用"}, {"status", unit.usedMCP == 0 ? "否" : "是"}});
|
||||
nodearray.push_back({{"pos", "手报状态"}, {"status", mapStatusDef[unit.statusAlarm]}}); // 0:无效 1:掉线 2:正常 3:启动
|
||||
break;
|
||||
}
|
||||
jsondata["fire40"] = nodearray;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user