Skip to content

Commit

Permalink
Merge pull request #262 from kivancsikert/network/fix-staying-online-…
Browse files Browse the repository at this point in the history
…indefinitely

Do not stay online waiting for messages to arrive unnecessarily
  • Loading branch information
lptr authored Dec 5, 2024
2 parents 4647ed7 + 2acf43b commit 6811112
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions main/kernel/mqtt/MqttDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ class MqttDriver {
PublishStatus clear(const String& topic, Retention retain, QoS qos, ticks timeout = ticks::zero(), milliseconds extendAlert = MQTT_ALERT_AFTER_OUTGOING) {
LOGTD("mqtt", "Clearing topic '%s'",
topic.c_str());
// Stay alert until the message is sent
extendAlert = std::max(duration_cast<milliseconds>(timeout), extendAlert);
return executeAndAwait(timeout, [&](TaskHandle_t waitingTask) {
return outgoingQueue.offerIn(MQTT_QUEUE_TIMEOUT, OutgoingMessage { topic, "", retain, qos, waitingTask, LogPublish::Log, extendAlert });
});
Expand Down Expand Up @@ -212,20 +210,27 @@ class MqttDriver {
time_point<system_clock> alertUntil = system_clock::now() + 5s;

while (true) {
milliseconds timeout = duration_cast<milliseconds>(alertUntil - system_clock::now());
if (timeout > milliseconds::zero()) {
LOGTV("mqtt", "Alert for another %lld ms, checking for incoming messages",
timeout.count());
ensureConnected(task);
timeout = std::min(timeout, MQTT_LOOP_INTERVAL);
} else {
if (powerSaveMode) {
milliseconds timeout;
if (powerSaveMode) {
timeout = duration_cast<milliseconds>(alertUntil - system_clock::now());
if (timeout > milliseconds::zero()) {
LOGTV("mqtt", "Alert for another %lld ms, checking for incoming messages",
timeout.count());
ensureConnected(task);
timeout = std::min(timeout, MQTT_LOOP_INTERVAL);
} else if (!pendingMessages.empty()) {
LOGTV("mqtt", "Alert expired, but there are pending messages, staying connected");
ensureConnected(task);
timeout = MQTT_LOOP_INTERVAL;
} else if (powerSaveMode) {
LOGTV("mqtt", "Not alert anymore, disconnecting");
disconnect();
timeout = MQTT_MAX_TIMEOUT_POWER_SAVE;
} else {
timeout = MQTT_LOOP_INTERVAL;
}
} else {
LOGTV("mqtt", "Power save mode not enabled, staying connected");
ensureConnected(task);
timeout = MQTT_LOOP_INTERVAL;
}

// LOGTV("mqtt", "Waiting for outgoing event for %lld ms", duration_cast<milliseconds>(timeout).count());
Expand Down Expand Up @@ -261,7 +266,7 @@ class MqttDriver {

void disconnect() {
if (client != nullptr) {
LOGD("Disconnecting from MQTT server");
LOGTD("mqtt", "Disconnecting from MQTT server");
mqttReady.clear();
ESP_ERROR_CHECK(esp_mqtt_client_disconnect(client));
stopMqttClient();
Expand Down Expand Up @@ -437,7 +442,7 @@ class MqttDriver {
}
case MQTT_EVENT_ERROR: {
if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
LOGE("Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
LOGTE("mqtt", "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
logErrorIfNonZero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err);
logErrorIfNonZero("reported from tls stack", event->error_handle->esp_tls_stack_err);
logErrorIfNonZero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno);
Expand Down Expand Up @@ -473,7 +478,7 @@ class MqttDriver {

static void logErrorIfNonZero(const char* message, int error) {
if (error != 0) {
LOGE(" - %s: 0x%x", message, error);
LOGTE("mqtt", " - %s: 0x%x", message, error);
}
}

Expand Down

0 comments on commit 6811112

Please sign in to comment.