mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 05:24:53 +01:00
Receive long MQTT payload (#1590)
This commit is contained in:
parent
eaf9735eda
commit
e6f8e73705
2 changed files with 12 additions and 3 deletions
|
@ -25,9 +25,17 @@ void MQTTClientComponent::setup() {
|
|||
ESP_LOGCONFIG(TAG, "Setting up MQTT...");
|
||||
this->mqtt_client_.onMessage([this](char *topic, char *payload, AsyncMqttClientMessageProperties properties,
|
||||
size_t len, size_t index, size_t total) {
|
||||
std::string payload_s(payload, len);
|
||||
std::string topic_s(topic);
|
||||
this->on_message(topic_s, payload_s);
|
||||
if (index == 0)
|
||||
this->payload_buffer_.reserve(total);
|
||||
|
||||
// append new payload, may contain incomplete MQTT message
|
||||
this->payload_buffer_.append(payload, len);
|
||||
|
||||
// MQTT fully received
|
||||
if (len + index == total) {
|
||||
this->on_message(topic, this->payload_buffer_);
|
||||
this->payload_buffer_.clear();
|
||||
}
|
||||
});
|
||||
this->mqtt_client_.onDisconnect([this](AsyncMqttClientDisconnectReason reason) {
|
||||
this->state_ = MQTT_CLIENT_DISCONNECTED;
|
||||
|
|
|
@ -259,6 +259,7 @@ class MQTTClientComponent : public Component {
|
|||
};
|
||||
std::string topic_prefix_{};
|
||||
MQTTMessage log_message_;
|
||||
std::string payload_buffer_;
|
||||
int log_level_{ESPHOME_LOG_LEVEL};
|
||||
|
||||
std::vector<MQTTSubscription> subscriptions_;
|
||||
|
|
Loading…
Reference in a new issue