Skip to content

Commit 4abfdaa

Browse files
authored
Merge pull request #4984 from tonhuisman/feature/PublishR-mqtt-publish-with-retain
[MQTT] Add PublishR command: publish with will-retain
2 parents 887ce21 + 0f6eebc commit 4abfdaa

File tree

6 files changed

+32
-10
lines changed

6 files changed

+32
-10
lines changed

docs/source/Plugin/P000_commands.repl

+7-1
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,16 @@
469469
"
470470
Publish","
471471
:green:`Rules`","
472-
Send command using MQTT broker service. Uses the first enabled MQTT Controller.
472+
Send command using MQTT broker service. The 'Will Retain' option as configured in the MQTT Controller settings is used. Uses the first enabled MQTT Controller.
473473

474474
``Publish,<topic>[,<payload>]``"
475475
"
476+
PublishR","
477+
:green:`Rules`","
478+
Send command using MQTT broker service, with the MQTT 'Will Retain' flag enabled. Uses the first enabled MQTT Controller.
479+
480+
``PublishR,<topic>[,<payload>]``"
481+
"
476482
PutToHTTP","
477483
:green:`Rules`","
478484
*Syntax format 1:*

src/src/Commands/InternalCommands.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ bool InternalCommands::executeInternalCommand()
383383
case ESPEasy_cmd_e::pulse: COMMAND_CASE_A(Command_GPIO_Pulse, 3); // GPIO.h
384384
#if FEATURE_MQTT
385385
case ESPEasy_cmd_e::publish: COMMAND_CASE_A(Command_MQTT_Publish, -1); // MQTT.h
386+
case ESPEasy_cmd_e::publishr: COMMAND_CASE_A(Command_MQTT_PublishR, -1); // MQTT.h
386387
#endif // if FEATURE_MQTT
387388
#if FEATURE_PUT_TO_HTTP
388389
case ESPEasy_cmd_e::puttohttp: COMMAND_CASE_A(Command_HTTP_PutToHTTP, -1); // HTTP.h

src/src/Commands/InternalCommands_decoder.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ const char Internal_commands_p[] PROGMEM =
175175
"pulse|"
176176
#if FEATURE_MQTT
177177
"publish|"
178+
"publishr|"
178179
#endif // #if FEATURE_MQTT
179180
#if FEATURE_PUT_TO_HTTP
180181
"puttohttp|"

src/src/Commands/InternalCommands_decoder.h

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ enum class ESPEasy_cmd_e : uint8_t {
143143
pulse,
144144
#if FEATURE_MQTT
145145
publish,
146+
publishr,
146147
#endif // #if FEATURE_MQTT
147148
#if FEATURE_PUT_TO_HTTP
148149
puttohttp,

src/src/Commands/MQTT.cpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@
2121
#include "../Helpers/StringConverter.h"
2222

2323

24-
const __FlashStringHelper * Command_MQTT_Publish(struct EventStruct *event, const char *Line)
24+
const __FlashStringHelper* Command_MQTT_Publish(struct EventStruct *event, const char *Line) {
25+
return Command_MQTT_Publish_handler(event, Line, false);
26+
}
27+
28+
const __FlashStringHelper* Command_MQTT_PublishR(struct EventStruct *event, const char *Line) {
29+
return Command_MQTT_Publish_handler(event, Line, true);
30+
}
31+
32+
const __FlashStringHelper* Command_MQTT_Publish_handler(struct EventStruct *event, const char *Line, const bool forceRetain)
2533
{
2634
// ToDo TD-er: Not sure about this function, but at least it sends to an existing MQTTclient
2735
controllerIndex_t enabledMqttController = firstEnabledMQTT_ControllerIndex();
@@ -34,13 +42,13 @@ const __FlashStringHelper * Command_MQTT_Publish(struct EventStruct *event, cons
3442
const String topic = parseStringKeepCase(Line, 2);
3543
const String value = tolerantParseStringKeepCase(Line, 3);
3644
# ifndef BUILD_NO_DEBUG
37-
addLog(LOG_LEVEL_DEBUG, concat(F("Publish: "), topic) + value);
38-
#endif
45+
addLog(LOG_LEVEL_DEBUG, strformat(F("Publish%c: %s:%s"), forceRetain ? 'R' : ' ', topic.c_str(), value.c_str()));
46+
# endif
3947

4048
if (!topic.isEmpty()) {
4149

42-
bool mqtt_retainFlag;
43-
{
50+
bool mqtt_retainFlag = forceRetain;
51+
if (!forceRetain) {
4452
// Place the ControllerSettings in a scope to free the memory as soon as we got all relevant information.
4553
MakeControllerSettings(ControllerSettings); //-V522
4654
if (!AllocatedControllerSettings()) {

src/src/Commands/MQTT.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55

66
#if FEATURE_MQTT
77

8-
const __FlashStringHelper * Command_MQTT_Publish(struct EventStruct *event,
9-
const char *Line);
8+
const __FlashStringHelper* Command_MQTT_Publish(struct EventStruct *event,
9+
const char *Line);
10+
const __FlashStringHelper* Command_MQTT_PublishR(struct EventStruct *event,
11+
const char *Line);
12+
const __FlashStringHelper* Command_MQTT_Publish_handler(struct EventStruct *event,
13+
const char *Line,
14+
const bool forceRetain);
1015

11-
const __FlashStringHelper * Command_MQTT_Subscribe(struct EventStruct *event,
12-
const char* Line);
16+
const __FlashStringHelper* Command_MQTT_Subscribe(struct EventStruct *event,
17+
const char *Line);
1318

1419
#endif // if FEATURE_MQTT
1520

0 commit comments

Comments
 (0)