实现MQTT协议消息订阅和消息解析流程

This commit is contained in:
lixiaoyuan
2025-09-08 19:34:12 +08:00
parent 566a3b050c
commit e2995eff92
17 changed files with 642 additions and 288 deletions

View File

@@ -82,7 +82,7 @@ static void GetRequestParam(const httplib::Request& req, const std::vector<std::
{
njson json;
JSON::parse(req.body, json);
JsonToFields(json, vecKeys, fields);
}
}
@@ -169,6 +169,10 @@ static std::map<std::string, HandlerOptions> g_mapHttpHandlerGet =
{"/queryStatDayList", HandlerOptions(&HttpEntity::queryStatDayList, {})},
{"/queryEnvironment", HandlerOptions(&HttpEntity::queryEnvironment, { "station_id"})},
{"/queryServiceApiList", HandlerOptions(&HttpEntity::queryServiceApiList, {})},
{"/deleteServiceApi", HandlerOptions(&HttpEntity::deleteServiceApi, {"api_id"})},
//{"/insert", HandlerOptions(&HttpEntity::insert, {})},
//{"/update", HandlerOptions(&HttpEntity::update, {})},
@@ -194,6 +198,9 @@ static std::map<std::string, HandlerOptions> g_mapHttpHandlerPost
{"/insertPolicy", HandlerOptions(&HttpEntity::insertPolicy, { DMPolicy::NAME})},
{"/updatePolicy", HandlerOptions(&HttpEntity::updatePolicy, { DMPolicy::POLICY_ID})},
{"/insertServiceApi", HandlerOptions(&HttpEntity::insertServiceApi, {})},
{"/updateServiceApi", HandlerOptions(&HttpEntity::updateServiceApi, {"api_id"})},
};
bool CheckHttpToken(const httplib::Request& req)
@@ -495,6 +502,10 @@ Errcode HttpEntity::updateStation(const httplib::Request& req, njson& json, std:
{
Fields params;
GetRequestParam(req, {"station_id", "name", "address", "lon", "lat", "tel", "capacity", "status"}, params);
params.check("capacity", "", "0.0");
params.check("lon", "", "0.0");
params.check("lat", "", "0.0");
params.check("status", "", "1");
return DAO::updateStationById(params);
};
@@ -683,6 +694,8 @@ Errcode HttpEntity::queryDevicByCategory(const httplib::Request& req, njson& jso
jsonnode["name"] = device->name;
jsonnode["code"] = device->code;
jsonnode["type"] = device->type;
jsonnode["typename"] = Application::data().getDeviceNameById(device->type);
jsonnode["view"] = 1;
jsonnode["is_online"] = device->online;// ? "在线" : "离线";
jsonnode["is_error"] = device->err;// ? "故障" : "正常";
@@ -799,19 +812,25 @@ Errcode HttpEntity::updateAlertLog(const httplib::Request& req, njson& json, std
Errcode HttpEntity::queryPredictionDetail(const httplib::Request& req, njson& json, std::string& errmsg)
{
njson jsonData = njson::array();
for (int i = 1; i<=5; i++)
std::vector<std::string> vecStoreIn(144), vecStoreOut(144), vecCharge(144), vecSolar(144), vecSolarP(144);
for (int i = 0; i<144; ++i)
{
njson jnode;
jnode["datatype"] = i;
njson jsonValues = njson::array();
for (int i = 0; i<1440; ++i)
{
jsonValues.push_back(float(Utils::random(50, 100)));
}
jnode["values"] = jsonValues;
jsonData.push_back(jnode);
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)));
}
json["data"] = jsonData;
json["data"] = {
{"W_store_in", vecStoreIn},
{"W_store_out", vecStoreOut},
{"W_charge", vecCharge},
{"W_solar", vecSolar},
{"P_solar", vecSolarP }
};
return Errcode::OK;
}
@@ -1010,4 +1029,42 @@ Errcode HttpEntity::queryEnvironment(const httplib::Request& req, njson& json, s
}
json["data"] = jsondata;
return Errcode::OK;
}
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"));
}