@@ -69,8 +69,8 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
69
69
, _last_device_subscribe_cnt{0 }
70
70
, _next_thing_subscribe_attempt_tick{0 }
71
71
, _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 }
74
74
, _mqtt_data_buf{0 }
75
75
, _mqtt_data_len{0 }
76
76
, _mqtt_data_request_retransmit{false }
@@ -515,27 +515,25 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
515
515
}
516
516
517
517
/* 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)
522
523
{
523
- DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s [%d] last values requested" , __FUNCTION__, now);
524
- requestLastValue ();
525
- _last_sync_request_tick = now;
526
524
/* Track the number of times a get-last-values request was sent to the cloud.
527
525
* If no data is received within a certain number of retry-requests it's a better
528
526
* strategy to disconnect and re-establish connection from the ground up.
529
527
*/
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;
537
530
}
538
531
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
+
539
537
return State::RequestLastValues;
540
538
}
541
539
@@ -674,8 +672,7 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
674
672
CBORDecoder::decode (_thing_property_container, (uint8_t *)bytes, length, true );
675
673
_time_service.setTimeZoneData (_tz_offset, _tz_dst_until);
676
674
execCloudEventCallback (ArduinoIoTCloudEvent::SYNC);
677
- _last_sync_request_cnt = 0 ;
678
- _last_sync_request_tick = 0 ;
675
+ _last_sync_attempt_cnt = 0 ;
679
676
_state = State::Connected;
680
677
}
681
678
}
0 commit comments