Skip to content

Commit f04f0b0

Browse files
committed
Unify naming and logic of last values request retry
1 parent 9ff09b9 commit f04f0b0

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
6969
, _last_device_subscribe_cnt{0}
7070
, _next_thing_subscribe_attempt_tick{0}
7171
, _last_thing_subscribe_attempt_cnt{0}
72-
, _last_sync_request_tick{0}
73-
, _last_sync_request_cnt{0}
72+
, _next_sync_attempt_tick{0}
73+
, _last_sync_attempt_cnt{0}
7474
, _mqtt_data_buf{0}
7575
, _mqtt_data_len{0}
7676
, _mqtt_data_request_retransmit{false}
@@ -515,27 +515,25 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
515515
}
516516

517517
/* Check whether or not we need to send a new request. */
518-
unsigned long const now = millis();
519-
bool const is_sync_request_timeout = (now - _last_sync_request_tick) > AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms;
520-
bool const is_first_sync_request = (_last_sync_request_cnt == 0);
521-
if (is_first_sync_request || is_sync_request_timeout)
518+
bool const is_retry_attempt = (_last_sync_attempt_cnt > 0);
519+
if (is_retry_attempt && (millis() < _next_sync_attempt_tick))
520+
return State::RequestLastValues;
521+
522+
if (_last_sync_attempt_cnt > AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT)
522523
{
523-
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] last values requested", __FUNCTION__, now);
524-
requestLastValue();
525-
_last_sync_request_tick = now;
526524
/* Track the number of times a get-last-values request was sent to the cloud.
527525
* If no data is received within a certain number of retry-requests it's a better
528526
* strategy to disconnect and re-establish connection from the ground up.
529527
*/
530-
_last_sync_request_cnt++;
531-
if (_last_sync_request_cnt > AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT)
532-
{
533-
_last_sync_request_cnt = 0;
534-
_last_sync_request_tick = 0;
535-
return State::Disconnect;
536-
}
528+
_last_sync_attempt_cnt = 0;
529+
return State::Disconnect;
537530
}
538531

532+
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] last values requested", __FUNCTION__, _time_service.getTime());
533+
requestLastValue();
534+
_next_sync_attempt_tick = millis() + AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms;
535+
_last_sync_attempt_cnt++;
536+
539537
return State::RequestLastValues;
540538
}
541539

@@ -674,8 +672,7 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
674672
CBORDecoder::decode(_thing_property_container, (uint8_t*)bytes, length, true);
675673
_time_service.setTimeZoneData(_tz_offset, _tz_dst_until);
676674
execCloudEventCallback(ArduinoIoTCloudEvent::SYNC);
677-
_last_sync_request_cnt = 0;
678-
_last_sync_request_tick = 0;
675+
_last_sync_attempt_cnt = 0;
679676
_state = State::Connected;
680677
}
681678
}

src/ArduinoIoTCloudTCP.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
134134
unsigned int _last_device_subscribe_cnt;
135135
unsigned long _next_thing_subscribe_attempt_tick;
136136
unsigned int _last_thing_subscribe_attempt_cnt;
137-
unsigned long _last_sync_request_tick;
138-
unsigned int _last_sync_request_cnt;
137+
unsigned long _next_sync_attempt_tick;
138+
unsigned int _last_sync_attempt_cnt;
139139
String _brokerAddress;
140140
uint16_t _brokerPort;
141141
uint8_t _mqtt_data_buf[MQTT_TRANSMIT_BUFFER_SIZE];

0 commit comments

Comments
 (0)