实现QT6启动器,QT版本有qt5升级到qt6

This commit is contained in:
lixiaoyuan
2025-09-10 20:10:51 +08:00
parent 3d03bbe9b5
commit e822d57b67
188 changed files with 1376 additions and 219 deletions

View File

@@ -166,8 +166,8 @@ static std::map<std::string, HandlerOptions> g_mapHttpHandlerGet =
{"/queryStatSystem", HandlerOptions(&HttpEntity::queryStatSystem, {})},
{"/queryStatTotal", HandlerOptions(&HttpEntity::queryStatTotal, {})},
{"/queryStatStation", HandlerOptions(&HttpEntity::queryStatStation, {})},
{"/queryStatDayList", HandlerOptions(&HttpEntity::queryStatDayList, {})},
{"/queryStatCharts", HandlerOptions(&HttpEntity::queryStatCharts, {})},
{"/queryEnvironment", HandlerOptions(&HttpEntity::queryEnvironment, { "station_id"})},
@@ -300,6 +300,7 @@ Errcode HttpEntity::login(const httplib::Request& req, njson& json, std::string&
if (err == Errcode::OK)
{
json["token"] = token;
json["account"] = account;
std::vector<Fields> vecPermission;
int roleId = fields.get<int>(DMRole::ROLE_ID);
@@ -606,7 +607,7 @@ Errcode HttpEntity::queryStationOverview(const httplib::Request& req, njson& jso
{
return Errcode(ret);
}
njson jsonStorage = njson::parse(R"({"category":1, "count":0, "power":0.0})");
njson jsonStorage = njson::parse(R"({"category":1, "gateway":0, "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})");
@@ -906,7 +907,7 @@ Errcode HttpEntity::queryStatSystem(const httplib::Request& req, njson& json, st
auto& appdata = Application::data();
njson jsondata;
jsondata["launch_date"] = "2025-01-01"; //: 系统上线启用日期格式yyyy-mm-dd
jsondata["launch_date"] = appdata.launchDate; //: 系统上线启用日期格式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()); //: 储能设备数量
@@ -924,49 +925,31 @@ Errcode HttpEntity::queryStatTotal(const httplib::Request& req, njson& json, std
{
std::string station_id = req.get_param_value("station_id");
std::string category = req.get_param_value("category");
njson 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
auto station = Application::data().getStation(Utils::toInt(station_id));
if (station)
{
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::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");
njson 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;
@@ -1010,6 +993,65 @@ Errcode HttpEntity::queryStatDayList(const httplib::Request& req, njson& json, s
return Errcode::OK;
}
Errcode HttpEntity::queryStatCharts(const httplib::Request& req, njson& json, std::string& errmsg)
{
std::string dt = req.get_param_value("dt");
std::string stationId = req.get_param_value("station_id");
std::string category = req.get_param_value("category");
if (dt.empty()) { errmsg = "参数[dt]错误"; return Errcode::ERR_PARAM; }
if (stationId.empty()) { errmsg = "参数[station_id]错误"; return Errcode::ERR_PARAM; }
if (category.empty()) { errmsg = "参数[category]错误"; return Errcode::ERR_PARAM; }
njson jsondata;
std::string sql = R"(SELECT hd.*, d.`type` device_type, ddt.category FROM history_day hd
LEFT JOIN device d ON d.device_id = hd.device_id
LEFT JOIN def_device_type ddt ON d.`type` = ddt.device_type_id
WHERE dt=')" + dt + "' AND d.station_id='" + stationId + "' AND ddt.category='" + category + "';";
std::vector<Fields> result;
int ret = DaoEntity::execOnce(sql, result);
if (ret != 0)
{
return Errcode(ret);
}
std::vector<double> vecV;
std::vector<double> vecI;
std::vector<double> vecP;
for (auto fields : result)
{
int datetype = fields.get<int>("datatype"); // 1: 电压2电流3功率
std::string val = fields.value("value"); // JSON 数组(double)
njson jsonval;
if (JSON::parse(val, jsonval))
{
for (int i = 0; i<jsonval.size(); ++i)
{
double val = jsonval[i];
if (datetype == 1)
{
// 电压取最大
i >= vecV.size() ? vecV.push_back(val) : (val > vecV[i] ? (vecV[i] = val, (void)0) : (void)0);
}
else if (datetype == 2)
{
// 电流取最大
i >= vecI.size() ? vecI.push_back(val) : (val > vecI[i] ? (vecI[i] = val, (void)0) : (void)0);
}
else if (datetype == 3)
{
// 功率累加
i >= vecP.size() ? vecP.push_back(val) : (vecP[i] = vecP[i] + val, (void)0);
}
}
}
}
json["data"] = {{"V", vecV}, {"I", vecI}, {"P", vecP}};
return Errcode::OK;
}
Errcode HttpEntity::queryEnvironment(const httplib::Request& req, njson& json, std::string& errmsg)
{
std::string stationId = req.get_param_value("station_id");
@@ -1098,7 +1140,6 @@ Errcode HttpEntity::queryEnvironment(const httplib::Request& req, njson& json, s
return Errcode::OK;
}
Errcode HttpEntity::queryServiceApiList(const httplib::Request& req, njson& json, std::string& errmsg)
{
PageInfo pageinfo;

View File

@@ -80,13 +80,17 @@ public:
Errcode queryPredictionDetail(const httplib::Request& req, njson& json, std::string& errmsg);
// 系统总览所有场站统计
Errcode queryStatSystem(const httplib::Request& req, njson& json, std::string& errmsg);
// 一个场站的累计统计
Errcode queryStatTotal(const httplib::Request& req, njson& json, std::string& errmsg);
Errcode queryStatStation(const httplib::Request& req, njson& json, std::string& errmsg);
// 场站按类别按天统计
Errcode queryStatDayList(const httplib::Request& req, njson& json, std::string& errmsg);
Errcode queryEnvironment(const httplib::Request& req, njson& json, std::string& errmsg);
// 场站按类某一天的历史曲线数据
Errcode queryStatCharts(const httplib::Request& req, njson& json, std::string& errmsg);
Errcode queryEnvironment(const httplib::Request& req, njson& json, std::string& errmsg);
Errcode queryServiceApiList(const httplib::Request& req, njson& json, std::string& errmsg);
Errcode insertServiceApi(const httplib::Request& req, njson& json, std::string& errmsg);

View File

@@ -52,25 +52,30 @@ void MqttClient::loadDataStruct(std::string filename)
}
}
}
//
int MqttClient::init(string addr, string clientId, string username, string password)
{
this->addr = addr;
this->clientId = clientId;
this->vecTopic = {
"up/json/" + clientId + "/EMS_YX",
"up/json/" + clientId + "/EMS_YC",
"up/json/" + clientId + "/EMS_YT",
"up/json/" + clientId + "/PCU_YX",
"up/json/" + clientId + "/PCU_YC",
"up/json/" + clientId + "/PCS_YX",
"up/json/" + clientId + "/PCS_YC",
"up/json/" + clientId + "/BCU_YX",
"up/json/" + clientId + "/BCU_YC",
"up/json/" + clientId + "/BMS_YX",
"up/json/" + clientId + "/BMS_YC",
"up/json/" + clientId + "/MEM_YC",
};
this->mapTopicInfo["EMS_YX"] = 101;
this->mapTopicInfo["EMS_YX"] = 101;
this->mapTopicInfo["EMS_YC"] = 101;
this->mapTopicInfo["EMS_YT"] = 101;
this->mapTopicInfo["PCS_YX"] = 102;
this->mapTopicInfo["PCS_YC"] = 102;
this->mapTopicInfo["PCU_YX"] = 103;
this->mapTopicInfo["PCU_YC"] = 103;
this->mapTopicInfo["BMS_YX"] = 104;
this->mapTopicInfo["BMS_YC"] = 104;
this->mapTopicInfo["BCU_YX"] = 105;
this->mapTopicInfo["BCU_YC"] = 105;
this->mapTopicInfo["MEM_YC"] = 3;
this->mapTopicInfo["Cooling_YC"] = 110;
this->mapTopicInfo["TH_YC"] = 111;
this->mapTopicInfo["Gateway_YX"] = 112;
this->mapTopicInfo["Charger_YC"] = 113;
MQTTAsync_connectOptions option = MQTTAsync_connectOptions_initializer;
MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
@@ -163,9 +168,29 @@ void MqttClient::subscribe()
MQTTAsync_responseOptions options = MQTTAsync_responseOptions_initializer;
options.onSuccess = funcSuccess;
options.onFailure = funcFailure;
for (auto& topic: vecTopic)
std::vector<std::string> vecTopic = {
//"up/json/" + clientId + "/EMS_YX",
"up/json/" + clientId + "/EMS_YC",
//"up/json/" + clientId + "/EMS_YT",
//"up/json/" + clientId + "/PCU_YX",
//"up/json/" + clientId + "/PCU_YC",
//"up/json/" + clientId + "/PCS_YX",
"up/json/" + clientId + "/PCS_YC",
//"up/json/" + clientId + "/BCU_YX",
//"up/json/" + clientId + "/BCU_YC",
//"up/json/" + clientId + "/BMS_YX",
//"up/json/" + clientId + "/BMS_YC",
//"up/json/" + clientId + "/MEM_YC",
//"up/json/" + clientId + "/Cooling_YC",
//"up/json/" + clientId + "/TH_YC",
//"up/json/" + clientId + "/Gateway_YX",
//"up/json/" + clientId + "/Charger_YC",
};
for (auto& item: mapTopicInfo)
{
options.context = topic.data();
std::string topic = "up/json/" + clientId + "/" + item.first;
options.context = (void*)&item.first;
int rc = MQTTAsync_subscribe(client, topic.data(), qos, &options);
if (rc != MQTTASYNC_SUCCESS)
{
@@ -174,9 +199,34 @@ void MqttClient::subscribe()
}
}
int MqttClient::publish(string topic, string text)
int MqttClient::polling()
{
spdlog::info("MQTT publish: topic={}, text={}", topic, text);
// 召测 发布
std::vector<std::string> vecTopic = {
"down/json/" + clientId + "/EMS_YX",
"down/json/" + clientId + "/EMS_YC",
//"down/json/" + clientId + "/EMS_YT",
//"down/json/" + clientId + "/PCU_YX",
//"down/json/" + clientId + "/PCU_YC",
//"down/json/" + clientId + "/PCS_YX",
"down/json/" + clientId + "/PCS_YC",
//"down/json/" + clientId + "/BCU_YX",
//"down/json/" + clientId + "/BCU_YC",
//"down/json/" + clientId + "/BMS_YX",
//"down/json/" + clientId + "/BMS_YC",
//"down/json/" + clientId + "/MEM_YC",
//"down/json/" + clientId + "/Cooling_YC",
//"down/json/" + clientId + "/TH_YC",
//"down/json/" + clientId + "/Gateway_YX",
//"down/json/" + clientId + "/Charger_YC",
};
njson json;
json["ts"] = Utils::time();
json["no"] = 1;
std::string text = json.dump();
MQTTAsync_responseOptions options = MQTTAsync_responseOptions_initializer;
//options.onSuccess = onSend;
@@ -184,21 +234,24 @@ int MqttClient::publish(string topic, string text)
options.context = this;
MQTTAsync_message msg = MQTTAsync_message_initializer;
msg.qos = 1;
msg.qos = this->qos;
msg.payload = text.data();
msg.payloadlen = text.size();
msg.retained = 0;
int rc = MQTTAsync_sendMessage(client, topic.c_str(), &msg, &options);
if (rc == MQTTASYNC_SUCCESS)
for (auto& topic: vecTopic)
{
spdlog::info("MQTT send message success, topic={}, text={}", topic, text);
int rc = MQTTAsync_sendMessage(client, topic.c_str(), &msg, &options);
if (rc == MQTTASYNC_SUCCESS)
{
spdlog::info("MQTT send message success, topic={}, text={}", topic, msg.payload);
}
else
{
spdlog::error("MQTT send message error, topic={}, text={}", topic, msg.payload);
}
}
else
{
spdlog::error("MQTT send message error, topic={}, text={}", topic, text);
}
return rc;
return 0;
}
void MqttClient::onConnectionLost(char* cause)
@@ -229,16 +282,18 @@ int MqttClient::onMessageArrived(char* topic, int topicLen, MQTTAsync_message* m
{
std::string topicStr = topic;
int len = msg->payloadlen;
std::string payload = (char*)msg->payload;
spdlog::info("MQTT message arrived: topic=[{},{}], payload len={}, payload msg={}", topic, msg->qos, len, payload);
std::string payload((const char*)msg->payload, len);
// <数据方向>/<数据格式>/<厂家ID>/<指合>/<设备标识,上行可选>
std::string direction = GetSubStr("/", topicStr);
std::string datatype = GetSubStr("/", topicStr);
std::string stationId = GetSubStr("/", topicStr);
std::string stationNo = GetSubStr("/", topicStr);
std::string command = GetSubStr("/", topicStr);
std::string deviceCode = GetSubStr("/", topicStr);
spdlog::info("[mqtt] message arrived: topic=[{},{}], len={}, payload={}", topic, msg->qos, len, payload);
spdlog::info("[mqtt] parse topic: {}, stationNo={}, command={}", topic, stationNo, command);
njson json;
bool ret = JSON::parse(payload, json);
@@ -247,29 +302,31 @@ int MqttClient::onMessageArrived(char* topic, int topicLen, MQTTAsync_message* m
spdlog::error("[mqtt] json parse error.");
return 1;
}
auto station = Application::data().getStation(Utils::toInt(stationId));
auto station = Application::data().getStationByCode(stationNo);
if (!station)
{
spdlog::error("[mqtt] get station error, clientId={}, stationId={}", clientId, stationId);
spdlog::error("[mqtt] get station error, clientId={}, stationId={}", clientId, stationNo);
return 1;
}
auto iter = g_mapRegInfo.find(command);
if (iter == g_mapRegInfo.end())
{
spdlog::error("[mqtt] get register add info error, clientId={}, stationId={}, command={}", clientId, stationId, command);
spdlog::error("[mqtt] get register add info error, clientId={}, stationId={}, command={}", clientId, stationNo, command);
return 1;
}
std::map<std::string, REGInfo>& mapRegInfo = iter->second;
int deviceNo = -1;
JSON::read(json, "no", deviceNo);
auto device = station->getDeviceByType(101, Utils::toStr(deviceNo));
auto device = station->getDeviceByType(mapTopicInfo[command], Utils::toStr(deviceNo));
if (!device)
{
return 1;
}
spdlog::info("[mqtt] deviceNo={}", deviceNo);
for (auto& item: json.items())
{
std::string key = item.key();
@@ -284,7 +341,9 @@ int MqttClient::onMessageArrived(char* topic, int topicLen, MQTTAsync_message* m
if (iter != mapRegInfo.end())
{
auto addr = iter->first;
device->setParam(addr, JSON::readStr(data[i], addr));
std::string val = JSON::toStr(data[i]);
spdlog::info("[mqtt] read register addr: [{}]={}, {}", addr, val, iter->second.remark);
device->setParam(addr, val);
++iter;
}
}

View File

@@ -25,6 +25,17 @@ struct REGInfo
}
};
//struct TopicInfo
//{
// std::string name;
// std::string topic;
// int deviceType;
// TopicInfo() {};
// TopicInfo(std::string name, std::string topic, int deviceType)
// :name(name), topic(topic), deviceType(deviceType)
// {};
//};
using namespace std;
class MqttClient
@@ -36,7 +47,9 @@ public:
void destory();
void subscribe();
int publish(string topic, string text);
//int publish();
int polling();
void onConnectionLost(char* cause);
int onMessageArrived(char* topic, int len, MQTTAsync_message* msg);
@@ -46,33 +59,17 @@ public:
void onConnectFaiure(MQTTAsync_failureData* resp);
void parseEMS_YX(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parseEMS_YC(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parsePCU_YX(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parsePCU_YC(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parsePCS_YX(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parsePCS_YC(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parseBMS_YC(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parseBCU_YX(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parseBCU_YC(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parseMEM_YC(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parseTH_YC(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parseFire40_YX(std::string& text);
//void parseCooling_YX(std::string& text);
//void parseCooling_YC(std::string& text);
//void parseCharger_YC(std::string& text);
//void parseGateway_YX(std::string& text);
//void parseGateway_YC(std::string& text);
//void parseGateway_YT(std::string& text);
//void parseTQ(std::string& text);
public:
std::string clientId;
MQTTAsync client = nullptr;
std::vector<std::string> vecTopic;
std::string addr; // "tcp://localhost:1883"
int qos {0};
bool isConnected {false};
bool isSubscribed {false};
std::map<std::string, int> mapTopicInfo;
};