Skip to content

Commit 500f416

Browse files
committed
Moving all code that's handled in State::Connected into handle_Connected func.
1 parent 04d8fa6 commit 500f416

File tree

1 file changed

+45
-37
lines changed

1 file changed

+45
-37
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,6 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
176176

177177
void ArduinoIoTCloudTCP::update()
178178
{
179-
#if OTA_ENABLED
180-
/* If a _ota_logic object has been instantiated then we are spinning its
181-
* 'update' method here in order to process incoming data and generally
182-
* to transition to the OTA logic update states.
183-
*/
184-
OTAError const err = _ota_logic.update();
185-
_ota_error = static_cast<int>(err);
186-
#endif /* OTA_ENABLED */
187-
188179
if(checkPhyConnection() != NetworkConnectionState::CONNECTED) return;
189180

190181
/* Retrieve the latest data from the MQTT Client. */
@@ -201,25 +192,6 @@ void ArduinoIoTCloudTCP::update()
201192
case State::Connected: next_state = handle_Connected(); break;
202193
}
203194
_state = next_state;
204-
205-
/* For now exit here if we are not connected. */
206-
if (_state != State::Connected) return;
207-
208-
/* Check if a primitive property wrapper is locally changed.
209-
* This function requires an existing time service which in
210-
* turn requires an established connection. Not having that
211-
* leads to a wrong time set in the time service which inhibits
212-
* the connection from being established due to a wrong data
213-
* in the reconstructed certificate.
214-
*/
215-
updateTimestampOnLocallyChangedProperties(_property_container);
216-
217-
if(_mqtt_data_request_retransmit && (_mqtt_data_len > 0)) {
218-
write(_dataTopicOut, _mqtt_data_buf, _mqtt_data_len);
219-
_mqtt_data_request_retransmit = false;
220-
}
221-
222-
sendPropertiesToCloud();
223195
}
224196

225197
int ArduinoIoTCloudTCP::connected()
@@ -307,18 +279,54 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
307279

308280
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
309281
{
310-
if (_mqttClient.connected())
311-
return State::Connected;
282+
if (!_mqttClient.connected())
283+
{
284+
/* The last message was definitely lost, trigger a retransmit. */
285+
_mqtt_data_request_retransmit = true;
312286

313-
/* The last message was definitely lost, trigger a retransmit. */
314-
_mqtt_data_request_retransmit = true;
287+
/* We are not connected anymore, trigger the callback for a disconnected event. */
288+
execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT);
315289

316-
/* We are not connected anymore, trigger the callback for a disconnected event. */
317-
execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT);
290+
/* Forcefully disconnect MQTT client and trigger a reconnection. */
291+
_mqttClient.stop();
292+
return State::ConnectMqttBroker;
293+
}
294+
/* We are connected so let's to our stuff here. */
295+
else
296+
{
297+
#if OTA_ENABLED
298+
/* If a _ota_logic object has been instantiated then we are spinning its
299+
* 'update' method here in order to process incoming data and generally
300+
* to transition to the OTA logic update states.
301+
*/
302+
OTAError const err = _ota_logic.update();
303+
_ota_error = static_cast<int>(err);
304+
#endif /* OTA_ENABLED */
318305

319-
/* Forcefully disconnect MQTT client and trigger a reconnection. */
320-
_mqttClient.stop();
321-
return State::ConnectMqttBroker;
306+
/* Check if a primitive property wrapper is locally changed.
307+
* This function requires an existing time service which in
308+
* turn requires an established connection. Not having that
309+
* leads to a wrong time set in the time service which inhibits
310+
* the connection from being established due to a wrong data
311+
* in the reconstructed certificate.
312+
*/
313+
updateTimestampOnLocallyChangedProperties(_property_container);
314+
315+
/* Retransmit data in case there was a lost transaction due
316+
* to phy layer or MQTT connectivity loss.
317+
*/
318+
if(_mqtt_data_request_retransmit && (_mqtt_data_len > 0)) {
319+
write(_dataTopicOut, _mqtt_data_buf, _mqtt_data_len);
320+
_mqtt_data_request_retransmit = false;
321+
}
322+
323+
/* Check if any properties need encoding and send them to
324+
* the cloud if necessary.
325+
*/
326+
sendPropertiesToCloud();
327+
328+
return State::Connected;
329+
}
322330
}
323331

324332
void ArduinoIoTCloudTCP::onMessage(int length)

0 commit comments

Comments
 (0)