Skip to content

Track the number of unsuccessful request-last-values. #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/AIoTC_Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,6 @@
#define AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms (1000UL)
#define AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms (32000UL)
#define AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms (30000UL)
#define AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT (10UL)

#endif /* ARDUINO_AIOTC_CONFIG_H_ */
14 changes: 14 additions & 0 deletions src/ArduinoIoTCloudTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
, _next_connection_attempt_tick{0}
, _last_connection_attempt_cnt{0}
, _last_sync_request_tick{0}
, _last_sync_request_cnt{0}
, _mqtt_data_buf{0}
, _mqtt_data_len{0}
, _mqtt_data_request_retransmit{false}
Expand Down Expand Up @@ -424,6 +425,18 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] last values requested", __FUNCTION__, now);
requestLastValue();
_last_sync_request_tick = now;
/* Track the number of times a get-last-values request was sent to the cloud.
* If no data is received within a certain number of retry-requests it's a better
* strategy to disconnect and re-establish connection from the ground up.
*/
_last_sync_request_cnt++;
if (_last_sync_request_cnt > AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT)
{
_last_sync_request_cnt = 0;
_mqttClient.stop();
execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT);
return State::ConnectPhy;
}
}

return State::RequestLastValues;
Expand Down Expand Up @@ -517,6 +530,7 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
CBORDecoder::decode(_property_container, (uint8_t*)bytes, length, true);
sendPropertiesToCloud();
execCloudEventCallback(ArduinoIoTCloudEvent::SYNC);
_last_sync_request_cnt = 0;
_state = State::Connected;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/ArduinoIoTCloudTCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
unsigned long _next_connection_attempt_tick;
unsigned int _last_connection_attempt_cnt;
unsigned long _last_sync_request_tick;
unsigned int _last_sync_request_cnt;
String _brokerAddress;
uint16_t _brokerPort;
uint8_t _mqtt_data_buf[MQTT_TRANSMIT_BUFFER_SIZE];
Expand Down