调试修改MQTT通讯解析

This commit is contained in:
lixiaoyuan
2025-09-16 19:38:46 +08:00
parent 9377e7f8e6
commit 393f68aec9
25 changed files with 943 additions and 1428 deletions

View File

@@ -96,7 +96,7 @@ static Errcode QueryCount(DaoEntity& dao, std::string sqlFrom, int& count)
}
static Errcode QueryPagination(std::string sqlFields, std::string sqlCondition, PageInfo& page, vector<Fields>& result)
static Errcode QueryPagination(std::string sqlFields, std::string sqlFrom, PageInfo& page, vector<Fields>& result)
{
DaoEntity dao("");
if (!dao.isConnected())
@@ -104,7 +104,7 @@ static Errcode QueryPagination(std::string sqlFields, std::string sqlCondition,
return Errcode::ERR_DB_CONN;
}
int count {0};
Errcode err = QueryCount(dao, sqlCondition, count);
Errcode err = QueryCount(dao, sqlFrom, count);
if (err != Errcode::OK)
{
return err;
@@ -113,7 +113,7 @@ static Errcode QueryPagination(std::string sqlFields, std::string sqlCondition,
if (page.index < 1) page.index = 1;
if (page.size <= 0) page.size = 10;
page.total = count;
std::string sql = "SELECT " + sqlFields + " " + sqlCondition + DAO::sqlPageLimit(page.index -1, page.size);
std::string sql = "SELECT " + sqlFields + " " + sqlFrom + DAO::sqlPageLimit(page.index -1, page.size);
int ret = dao.exec(sql, result);
return Errcode(ret);
}
@@ -432,9 +432,11 @@ Errcode DAO::updateStationById(Fields& params)
}
// 查询设备信息列表
Errcode DAO::queryDeviceList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
Errcode DAO::queryDeviceList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result, bool sort/* = false*/)
{
std::string sql = "SELECT d.*, ddt.category FROM device d LEFT JOIN def_device_type ddt ON d.`type`=ddt.device_type_id;";
std::string sql = "SELECT d.*, ddt.category FROM device d LEFT JOIN def_device_type ddt ON d.`type`=ddt.device_type_id";
if (sort) sql += " ORDER BY station_id, sort_no";
sql += ";";
return DAO::exec(dao, sql, result);
}
@@ -554,6 +556,78 @@ Errcode DAO::queryStatDataList(std::shared_ptr<DaoEntity> dao, std::string start
return DAO::exec(dao, sql, result);
}
Errcode DAO::queryStatStationGroup(std::shared_ptr<DaoEntity> dao, string stationId, string category, string startDate, string endDate, vector<Fields>& result)
{
std::string sqlCondition;
if (!startDate.empty() && endDate.empty())
{
sqlCondition += "dt BETWEEN '" + startDate + "' AND '" + endDate + "'";
}
if (!stationId.empty())
{
if (!sqlCondition.empty()) sqlCondition += " AND ";
sqlCondition += "station_id='" + stationId + "'";
}
if (!category.empty() && category != "0")
{
if (!sqlCondition.empty()) sqlCondition += " AND ";
sqlCondition += "category='" + category + "'";;
}
if (!sqlCondition.empty()) { sqlCondition = " WHERE " + sqlCondition; }
std::string sql = R"(SELECT dt,
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 + "GROUP by dt;";
return DAO::exec(dao, sql, result);
}
Errcode DAO::queryStatStationList(PageInfo& pageInfo, Fields& params, vector<Fields>& result)
{
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::string sqlCondition;
if (!startDate.empty() && endDate.empty())
{
sqlCondition += "dt BETWEEN '" + startDate + "' AND '" + endDate + "'";
}
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; }
std::string sqlFields = "ss.*, d.name device_name, ddt.name device_type";
std::string sqlFrom = R"(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;
return QueryPagination(sqlFields, sqlFrom, pageInfo, result);
}
Errcode DAO::queryWorkModeDef(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
{
std::string sql = "SELECT * FROM " + DMDefWorkMode::TABLENAME + ";";
@@ -566,6 +640,37 @@ Errcode DAO::queryPolicyTypeDef(std::shared_ptr<DaoEntity> dao, vector<Fields>&
return DAO::exec(dao, sql, result);
}
Errcode DAO::insertStatStation(std::shared_ptr<DaoEntity> dao, Fields& fields)
{
// 根据主键dt、station_id、category写入或更新数据
if (!dao) { dao = DaoEntity::create("stat_station"); }
std::vector<std::string> vecKeys = {
"storage_elect_in",
"storage_elect_out",
"storage_num_in",
"storage_num_out",
"storage_num_err",
"storage_t_in",
"storage_t_out",
"storage_usage",
"solar_elect_gen",
"solar_elect_grid",
"solar_num_err",
"solar_t",
"solar_usage",
"charge_elect",
"charge_num",
"charge_num_err",
"charge_t",
"charge_usage",
"income_elect",
"income_charge",
"usage_rate"
};
int ret = dao->duplicateUpdate(fields, vecKeys);
return Errcode(ret);
}
Errcode DAO::insertRuntimeData(std::shared_ptr<DaoEntity> dao, Fields& fields)
{
if (!dao) { dao = DaoEntity::create("history_day"); }

View File

@@ -75,7 +75,7 @@ public:
// 查询设备信息列表(分页)
static Errcode queryDeviceList(PageInfo& pageInfo, vector<Fields>& result);
// 查询设备信息列表
static Errcode queryDeviceList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
static Errcode queryDeviceList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result, bool sort=false);
// 查询设备类型定义
static Errcode queryDeviceTypeDef(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
// 新增设备信息
@@ -113,12 +113,16 @@ public:
// === 统计数据管理 ===
static Errcode queryStatDataList(std::shared_ptr<DaoEntity> dao, std::string startDate, std::string endDate, vector<Fields>& result);
static Errcode queryStatStationGroup(std::shared_ptr<DaoEntity> dao, string stationId, string category, string startDate, string endDate, vector<Fields>& result);
static Errcode queryStatStationList(PageInfo& pageInfo, Fields& params, vector<Fields>& result);
static Errcode queryWorkModeDef(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
static Errcode queryPolicyTypeDef(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
static Errcode insertStatStation(std::shared_ptr<DaoEntity> dao, Fields& fields);
///////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -169,7 +169,7 @@ namespace DMLogAlert
namespace DMStatStation
{
const string TABLENAME = "stat_staion";
const string TABLENAME = "stat_station";
const string DT = "dt";
const string STATION_ID = "station_id";
const string STORAGE_ELECT_IN = "storage_elect_in";