@@ -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}
0 commit comments