mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 00:09:51 +03:00
change call to not use json, prevent duplicate inits
This commit is contained in:
20
src/mqtt.cpp
20
src/mqtt.cpp
@@ -41,6 +41,7 @@ std::vector<Mqtt::MQTTSubFunction> Mqtt::mqtt_subfunctions_;
|
|||||||
|
|
||||||
uint16_t Mqtt::mqtt_publish_fails_ = 0;
|
uint16_t Mqtt::mqtt_publish_fails_ = 0;
|
||||||
bool Mqtt::connecting_ = false;
|
bool Mqtt::connecting_ = false;
|
||||||
|
bool Mqtt::initialized_ = false;
|
||||||
uint8_t Mqtt::connectcount_ = 0;
|
uint8_t Mqtt::connectcount_ = 0;
|
||||||
uint16_t Mqtt::mqtt_message_id_ = 0;
|
uint16_t Mqtt::mqtt_message_id_ = 0;
|
||||||
std::list<Mqtt::QueuedMqttMessage> Mqtt::mqtt_messages_;
|
std::list<Mqtt::QueuedMqttMessage> Mqtt::mqtt_messages_;
|
||||||
@@ -268,22 +269,20 @@ void Mqtt::on_message(const char * topic, const char * payload, size_t len) {
|
|||||||
bool cmd_known = false;
|
bool cmd_known = false;
|
||||||
JsonVariant data = doc["data"];
|
JsonVariant data = doc["data"];
|
||||||
|
|
||||||
JsonObject json; // empty object
|
|
||||||
|
|
||||||
if (data.is<char *>()) {
|
if (data.is<char *>()) {
|
||||||
cmd_known = Command::call(mf.device_type_, command, data.as<char *>(), n, json);
|
cmd_known = Command::call(mf.device_type_, command, data.as<char *>(), n);
|
||||||
} else if (data.is<int>()) {
|
} else if (data.is<int>()) {
|
||||||
char data_str[10];
|
char data_str[10];
|
||||||
cmd_known = Command::call(mf.device_type_, command, Helpers::itoa(data_str, (int16_t)data.as<int>()), n, json);
|
cmd_known = Command::call(mf.device_type_, command, Helpers::itoa(data_str, (int16_t)data.as<int>()), n);
|
||||||
} else if (data.is<float>()) {
|
} else if (data.is<float>()) {
|
||||||
char data_str[10];
|
char data_str[10];
|
||||||
cmd_known = Command::call(mf.device_type_, command, Helpers::render_value(data_str, (float)data.as<float>(), 2), n, json);
|
cmd_known = Command::call(mf.device_type_, command, Helpers::render_value(data_str, (float)data.as<float>(), 2), n);
|
||||||
} else if (data.isNull()) {
|
} else if (data.isNull()) {
|
||||||
cmd_known = Command::call(mf.device_type_, command, "", n, json);
|
cmd_known = Command::call(mf.device_type_, command, "", n);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!cmd_known) {
|
if (!cmd_known) {
|
||||||
LOG_ERROR(F("MQTT: no matching cmd (%s), invalid data or command failed"), command);
|
LOG_ERROR(F("No matching cmd (%s), invalid data or command failed"), command);
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@@ -358,9 +357,10 @@ void Mqtt::start() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// if MQTT disabled, quit
|
// if MQTT disabled, quit
|
||||||
if (!mqtt_enabled_) {
|
if (!mqtt_enabled_ || initialized_) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
initialized_ = true;
|
||||||
|
|
||||||
mqttClient_->onConnect([this](bool sessionPresent) { on_connect(); });
|
mqttClient_->onConnect([this](bool sessionPresent) { on_connect(); });
|
||||||
|
|
||||||
@@ -475,7 +475,7 @@ void Mqtt::on_connect() {
|
|||||||
// first time to connect
|
// first time to connect
|
||||||
if (connectcount_ == 1) {
|
if (connectcount_ == 1) {
|
||||||
// send info topic appended with the version information as JSON
|
// send info topic appended with the version information as JSON
|
||||||
StaticJsonDocument<90> doc;
|
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_SMALL> doc;
|
||||||
doc["event"] = "start";
|
doc["event"] = "start";
|
||||||
doc["version"] = EMSESP_APP_VERSION;
|
doc["version"] = EMSESP_APP_VERSION;
|
||||||
#ifndef EMSESP_STANDALONE
|
#ifndef EMSESP_STANDALONE
|
||||||
@@ -821,7 +821,7 @@ void Mqtt::register_mqtt_ha_sensor(const char * prefix,
|
|||||||
LOG_ERROR(F("Failed to publish topic %s"), topic);
|
LOG_ERROR(F("Failed to publish topic %s"), topic);
|
||||||
} else {
|
} else {
|
||||||
#if defined(EMSESP_STANDALONE)
|
#if defined(EMSESP_STANDALONE)
|
||||||
LOG_DEBUG(F("Publishing topic=%s, payload=%s"), topic, payload_text);
|
LOG_DEBUG(F("Publishing topic=%s, payload=%s"), topic, payload_text.c_str());
|
||||||
#else
|
#else
|
||||||
LOG_DEBUG(F("Publishing topic %s"), topic);
|
LOG_DEBUG(F("Publishing topic %s"), topic);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -219,6 +219,7 @@ class Mqtt {
|
|||||||
uint32_t last_publish_sensor_ = 0;
|
uint32_t last_publish_sensor_ = 0;
|
||||||
|
|
||||||
static bool connecting_;
|
static bool connecting_;
|
||||||
|
static bool initialized_;
|
||||||
static uint16_t mqtt_publish_fails_;
|
static uint16_t mqtt_publish_fails_;
|
||||||
static uint8_t connectcount_;
|
static uint8_t connectcount_;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user