Skip to content

Commit

Permalink
Add retain flag to mqtt_send_state #781
Browse files Browse the repository at this point in the history
  • Loading branch information
fvanroie committed Aug 22, 2024
1 parent 73c1539 commit e6eae8b
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/mqtt/hasp_mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void mqttStart();
void mqttStop();

// int mqtt_send_object_state(uint8_t pageid, uint8_t btnid, const char* payload);
int mqtt_send_state(const char* subtopic, const char* payload);
int mqtt_send_state(const char* subtopic, const char* payload, bool retain=false);
int mqtt_send_discovery(const char* payload, size_t len);
int mqttPublish(const char* topic, const char* payload, size_t len, bool retain);

Expand Down
4 changes: 2 additions & 2 deletions src/mqtt/hasp_mqtt_esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ bool mqtt_send_lwt(bool online)
// return mqttPublish(tmp_topic, payload, false);
// }

int mqtt_send_state(const char* subtopic, const char* payload)
int mqtt_send_state(const char* subtopic, const char* payload, bool retain)
{
String tmp_topic((char*)0);
tmp_topic.reserve(128);
Expand All @@ -174,7 +174,7 @@ int mqtt_send_state(const char* subtopic, const char* payload)
// tmp_topic += "/";
tmp_topic += subtopic;

return mqttPublish(tmp_topic.c_str(), payload, false);
return mqttPublish(tmp_topic.c_str(), payload, retain);
}

int mqtt_send_discovery(const char* payload, size_t len)
Expand Down
4 changes: 2 additions & 2 deletions src/mqtt/hasp_mqtt_paho_async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,11 @@ bool mqttIsConnected()
return mqttConnected; // MQTTAsync_isConnected(mqtt_client); // <- deadlocking on Linux
}

int mqtt_send_state(const __FlashStringHelper* subtopic, const char* payload)
int mqtt_send_state(const __FlashStringHelper* subtopic, const char* payload, bool retain)
{
char tmp_topic[mqttNodeTopic.length() + 20];
snprintf_P(tmp_topic, sizeof(tmp_topic), ("%s" MQTT_TOPIC_STATE "/%s"), mqttNodeTopic.c_str(), subtopic);
return mqttPublish(tmp_topic, payload, strlen(payload), false);
return mqttPublish(tmp_topic, payload, strlen(payload), retain);
}

int mqtt_send_discovery(const char* payload, size_t len)
Expand Down
4 changes: 2 additions & 2 deletions src/mqtt/hasp_mqtt_paho_single.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,11 @@ bool mqttIsConnected()
return MQTTClient_isConnected(mqtt_client);
}

int mqtt_send_state(const __FlashStringHelper* subtopic, const char* payload)
int mqtt_send_state(const __FlashStringHelper* subtopic, const char* payload, bool retain)
{
char tmp_topic[mqttNodeTopic.length() + 20];
snprintf_P(tmp_topic, sizeof(tmp_topic), ("%s" MQTT_TOPIC_STATE "/%s"), mqttNodeTopic.c_str(), subtopic);
return mqttPublish(tmp_topic, payload, strlen(payload), false);
return mqttPublish(tmp_topic, payload, strlen(payload), retain);
}

int mqtt_send_discovery(const char* payload, size_t len)
Expand Down
4 changes: 2 additions & 2 deletions src/mqtt/hasp_mqtt_pubsubclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ bool mqtt_send_lwt(bool online)
// return mqttPublish(tmp_topic, payload, false);
// }

int mqtt_send_state(const char* subtopic, const char* payload)
int mqtt_send_state(const char* subtopic, const char* payload, bool retain)
{
char tmp_topic[strlen(mqttNodeTopic) + strlen(subtopic) + 16];
snprintf_P(tmp_topic, sizeof(tmp_topic), PSTR("%s" MQTT_TOPIC_STATE "/%s"), mqttNodeTopic, subtopic);
return mqttPublish(tmp_topic, payload, false);
return mqttPublish(tmp_topic, payload, retain);
}

int mqtt_send_discovery(const char* payload, size_t len)
Expand Down

0 comments on commit e6eae8b

Please sign in to comment.