mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
实现QT6启动器,QT版本有qt5升级到qt6
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user