Skip to content

Commit 4e24837

Browse files
Merge pull request #412 from KipK/master
fix retain flag faulty after VirtualBool switch.
2 parents d6f1012 + 06c4b4d commit 4e24837

File tree

5 files changed

+8
-7
lines changed

5 files changed

+8
-7
lines changed

models/Config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ properties:
273273
type: string
274274
minLength: 1
275275
mqtt_retained:
276-
type: bool
276+
type: boolean
277277
mqtt_user:
278278
type: string
279279
minLength: 1

src/app_config.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,19 +334,21 @@ void config_save_emoncms(bool enable, String server, String node, String apikey,
334334
void
335335
config_save_mqtt(bool enable, int protocol, String server, uint16_t port, String topic, bool retained, String user, String pass, String solar, String grid_ie, String live_pwr, bool reject_unauthorized)
336336
{
337-
uint32_t newflags = flags & ~(CONFIG_SERVICE_MQTT | CONFIG_MQTT_PROTOCOL | CONFIG_MQTT_ALLOW_ANY_CERT);
337+
uint32_t newflags = flags & ~(CONFIG_SERVICE_MQTT | CONFIG_MQTT_PROTOCOL | CONFIG_MQTT_ALLOW_ANY_CERT | CONFIG_MQTT_RETAINED);
338338
if(enable) {
339339
newflags |= CONFIG_SERVICE_MQTT;
340340
}
341341
if(!reject_unauthorized) {
342342
newflags |= CONFIG_MQTT_ALLOW_ANY_CERT;
343343
}
344+
if (retained) {
345+
newflags |= CONFIG_MQTT_RETAINED;
346+
}
344347
newflags |= protocol << 4;
345348

346349
config.set("mqtt_server", server);
347350
config.set("mqtt_port", port);
348351
config.set("mqtt_topic", topic);
349-
config.set("mqtt_retained", retained);
350352
config.set("mqtt_user", user);
351353
config.set("mqtt_pass", pass);
352354
config.set("mqtt_solar", solar);

src/app_config.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ extern String emoncms_fingerprint;
3535
extern String mqtt_server;
3636
extern uint32_t mqtt_port;
3737
extern String mqtt_topic;
38-
extern bool mqtt_retained;
3938
extern String mqtt_user;
4039
extern String mqtt_pass;
4140
extern String mqtt_solar;
@@ -114,7 +113,7 @@ inline uint8_t config_mqtt_protocol() {
114113
}
115114

116115
inline bool config_mqtt_retained() {
117-
return (flags & CONFIG_MQTT_RETAINED);
116+
return CONFIG_MQTT_RETAINED == (flags & CONFIG_MQTT_RETAINED);
118117
}
119118

120119
inline bool config_mqtt_reject_unauthorized() {

src/mqtt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ mqtt_publish(JsonDocument &data) {
329329
String topic = mqtt_topic + "/";
330330
topic += kv.key().c_str();
331331
String val = kv.value().as<String>();
332-
mqttclient.publish(topic, val, mqtt_retained);
332+
mqttclient.publish(topic, val, config_mqtt_retained());
333333
topic = mqtt_topic + "/";
334334
}
335335

src/web_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ handleSaveMqtt(MongooseHttpServerRequest *request) {
364364

365365
char tmpStr[200];
366366
snprintf(tmpStr, sizeof(tmpStr), "Saved: %s %s %d %s %s %s %s", mqtt_server.c_str(),
367-
mqtt_topic.c_str(), mqtt_retained, mqtt_user.c_str(), mqtt_pass.c_str(),
367+
mqtt_topic.c_str(), config_mqtt_retained(), mqtt_user.c_str(), mqtt_pass.c_str(),
368368
mqtt_solar.c_str(), mqtt_grid_ie.c_str());
369369
DBUGLN(tmpStr);
370370

0 commit comments

Comments
 (0)