Skip to content

Commit

Permalink
fix mqtt::on_message, emsesp#1494
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDvP committed Dec 12, 2023
1 parent b9af432 commit d1f3ead
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 19 deletions.
3 changes: 0 additions & 3 deletions lib/framework/ESP8266React.h
Expand Up @@ -66,9 +66,6 @@ class ESP8266React {
void setWill(const char * will_topic) {
_mqttSettingsService.setWill(will_topic);
}
void onMessage(espMqttClientTypes::OnMessageCallback callback) {
_mqttSettingsService.onMessage(callback);
}

void factoryReset() {
_factoryResetService.factoryReset();
Expand Down
12 changes: 4 additions & 8 deletions lib/framework/MqttSettingsService.cpp
Expand Up @@ -68,13 +68,15 @@ void MqttSettingsService::startClient() {
}
static_cast<espMqttClientSecure *>(_mqttClient)->onConnect(std::bind(&MqttSettingsService::onMqttConnect, this, _1));
static_cast<espMqttClientSecure *>(_mqttClient)->onDisconnect(std::bind(&MqttSettingsService::onMqttDisconnect, this, _1));
static_cast<espMqttClientSecure *>(_mqttClient)->onMessage(std::bind(&MqttSettingsService::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
return;
}
#endif
isSecure = false;
_mqttClient = static_cast<MqttClient *>(new espMqttClient(espMqttClientTypes::UseInternalTask::NO));
static_cast<espMqttClient *>(_mqttClient)->onConnect(std::bind(&MqttSettingsService::onMqttConnect, this, _1));
static_cast<espMqttClient *>(_mqttClient)->onDisconnect(std::bind(&MqttSettingsService::onMqttDisconnect, this, _1));
static_cast<espMqttClient *>(_mqttClient)->onMessage(std::bind(&MqttSettingsService::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
}

void MqttSettingsService::loop() {
Expand Down Expand Up @@ -108,14 +110,8 @@ void MqttSettingsService::setWill(const char * topic) {
static_cast<espMqttClient *>(_mqttClient)->setWill(topic, 1, true, "offline");
}

void MqttSettingsService::onMessage(espMqttClientTypes::OnMessageCallback callback) {
#if CONFIG_IDF_TARGET_ESP32S3
if (_state.enableTLS) {
static_cast<espMqttClientSecure *>(_mqttClient)->onMessage(callback);
return;
}
#endif
static_cast<espMqttClient *>(_mqttClient)->onMessage(callback);
void MqttSettingsService::onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) {
emsesp::EMSESP::mqtt_.on_message(topic, payload, len);
}

espMqttClientTypes::DisconnectReason MqttSettingsService::getDisconnectReason() {
Expand Down
2 changes: 1 addition & 1 deletion lib/framework/MqttSettingsService.h
Expand Up @@ -115,7 +115,6 @@ class MqttSettingsService : public StatefulService<MqttSettings> {
espMqttClientTypes::DisconnectReason getDisconnectReason();
MqttClient * getMqttClient();
void setWill(const char * topic);
void onMessage(espMqttClientTypes::OnMessageCallback callback);

protected:
void onConfigUpdated();
Expand Down Expand Up @@ -145,6 +144,7 @@ class MqttSettingsService : public StatefulService<MqttSettings> {
void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info);
void onMqttConnect(bool sessionPresent);
void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason);
void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total);
bool configureMqtt();
};

Expand Down
7 changes: 1 addition & 6 deletions src/mqtt.cpp
Expand Up @@ -212,7 +212,7 @@ void Mqtt::incoming(const char * topic, const char * payload) {
// received an MQTT message that we subscribed too
// topic is the full path
// payload is json or a single string and converted to a json with key 'value'
void Mqtt::on_message(const char * topic, const uint8_t * payload, size_t len) const {
void Mqtt::on_message(const char * topic, const uint8_t * payload, size_t len) {
// the payload is not terminated
// convert payload to a null-terminated char string
// see https://www.emelis.net/espMqttClient/#code-samples
Expand Down Expand Up @@ -390,11 +390,6 @@ void Mqtt::start() {
}

EMSESP::esp8266React.setWill(will_topic); // with qos 1, retain true

EMSESP::esp8266React.onMessage(
[this](const espMqttClientTypes::MessageProperties & properties, const char * topic, const uint8_t * payload, size_t len, size_t index, size_t total) {
on_message(topic, payload, len); // receiving mqtt
});
}

void Mqtt::set_publish_time_boiler(uint16_t publish_time) {
Expand Down
2 changes: 1 addition & 1 deletion src/mqtt.h
Expand Up @@ -60,6 +60,7 @@ class Mqtt {

static void on_connect();
static void on_disconnect(espMqttClientTypes::DisconnectReason reason);
static void on_message(const char * topic, const uint8_t * payload, size_t len);
static void subscribe(const uint8_t device_type, const std::string & topic, mqtt_sub_function_p cb);
static void subscribe(const std::string & topic);
static void resubscribe();
Expand Down Expand Up @@ -231,7 +232,6 @@ class Mqtt {
static void queue_unsubscribe_message(const std::string & topic);

void on_publish(uint16_t packetId) const;
void on_message(const char * topic, const uint8_t * payload, size_t len) const;

// function handlers for MQTT subscriptions
struct MQTTSubFunction {
Expand Down

0 comments on commit d1f3ead

Please sign in to comment.