Skip to content

Commit

Permalink
retain option for last will
Browse files Browse the repository at this point in the history
  • Loading branch information
SciLor committed Nov 13, 2024
1 parent 7363c2b commit c0646c8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ typedef struct
char *identification;
char *topic;
uint32_t qosLevel;
bool retain_will;
} settings_mqtt_t;

typedef struct
Expand Down
6 changes: 4 additions & 2 deletions src/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typedef struct
char *username;
char *password;
char *topic;
bool retain_will;

} mqtt_ctx_t;

Expand Down Expand Up @@ -306,7 +307,7 @@ error_t mqttConnect(MqttClientContext *mqtt_context)

mqttClientSetIdentifier(mqtt_context, mqtt_context->mqtt_ctx->identification);
mqttClientSetAuthInfo(mqtt_context, mqtt_context->mqtt_ctx->username, mqtt_context->mqtt_ctx->password);
mqttClientSetWillMessage(mqtt_context, mqtt_prefix("status", mqtt_context->mqtt_ctx->topic), "offline", 7, MQTT_QOS_LEVEL_2, TRUE);
mqttClientSetWillMessage(mqtt_context, mqtt_prefix("status", mqtt_context->mqtt_ctx->topic), "offline", 7, MQTT_QOS_LEVEL_2, mqtt_context->mqtt_ctx->retain_will);

do
{
Expand Down Expand Up @@ -346,7 +347,7 @@ error_t mqttConnect(MqttClientContext *mqtt_context)
if (error)
break;

error = mqttClientPublish(mqtt_context, mqtt_prefix("status", mqtt_context->mqtt_ctx->topic), "online", 6, MQTT_QOS_LEVEL_2, TRUE, NULL);
error = mqttClientPublish(mqtt_context, mqtt_prefix("status", mqtt_context->mqtt_ctx->topic), "online", 6, MQTT_QOS_LEVEL_2, mqtt_context->mqtt_ctx->retain_will, NULL);
if (error)
break;

Expand Down Expand Up @@ -394,6 +395,7 @@ void mqtt_get_settings(mqtt_ctx_t *mqtt_ctx)
mqtt_ctx->username = strdup(settings_get_string("mqtt.username"));
mqtt_ctx->password = strdup(settings_get_string("mqtt.password"));
mqtt_ctx->topic = strdup(settings_get_string("mqtt.topic"));
mqtt_ctx->retain_will = settings_get_bool("mqtt.retain_will");
}

void mqtt_thread()
Expand Down
1 change: 1 addition & 0 deletions src/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ static void option_map_init(uint8_t settingsId)
OPTION_STRING("mqtt.identification", &settings->mqtt.identification, "", "Client identification", "Client identification", LEVEL_DETAIL)
OPTION_STRING("mqtt.topic", &settings->mqtt.topic, "teddyCloud", "Topic prefix", "Topic prefix", LEVEL_DETAIL)
OPTION_UNSIGNED("mqtt.qosLevel", &settings->mqtt.qosLevel, 0, 0, 2, "QoS level", "QoS level", LEVEL_DETAIL)
OPTION_BOOL("mqtt.retain_will", &settings->mqtt.retain_will, TRUE, "Retain last will", "Retain last will message", LEVEL_DETAIL)

OPTION_TREE_DESC("hass", "Home Assistant", LEVEL_DETAIL)
OPTION_STRING("hass.name", &settings->hass.name, "teddyCloud - Server", "Home Assistant name", "Home Assistant name", LEVEL_DETAIL)
Expand Down

0 comments on commit c0646c8

Please sign in to comment.