From b58754afc7bf9c16d081d27910d8dcdea7bdf1ab Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Tue, 4 May 2021 06:56:06 +0200 Subject: [PATCH] Fix: Immediately trigger a sync request on the first time this function is reached. It does not make sense to let the user wait until the timeout has passed (which is now even 30 seconds) long. Therefore the logic should trigger an immediate sync the first time this function is reached. --- src/ArduinoIoTCloudTCP.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 54d416596..55ad692ae 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -420,7 +420,9 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues() /* Check whether or not we need to send a new request. */ unsigned long const now = millis(); - if ((now - _last_sync_request_tick) > AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms) + bool const is_sync_request_timeout = (now - _last_sync_request_tick) > AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms; + bool const is_first_sync_request = (_last_sync_request_cnt == 0); + if (is_first_sync_request || is_sync_request_timeout) { DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] last values requested", __FUNCTION__, now); requestLastValue(); @@ -433,6 +435,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues() if (_last_sync_request_cnt > AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT) { _last_sync_request_cnt = 0; + _last_sync_request_tick = 0; _mqttClient.stop(); execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT); return State::ConnectPhy; @@ -531,6 +534,7 @@ void ArduinoIoTCloudTCP::handleMessage(int length) sendPropertiesToCloud(); execCloudEventCallback(ArduinoIoTCloudEvent::SYNC); _last_sync_request_cnt = 0; + _last_sync_request_tick = 0; _state = State::Connected; } }