@@ -326,12 +326,6 @@ void ArduinoIoTCloudTCP::update()
326326#endif
327327
328328
329- if (getThingIdOutdatedFlag ()) {
330- DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s Thing id outdated, reconfiguring..." , __FUNCTION__);
331- if (_mqttClient.connected ())
332- _state = State::CheckDeviceConfig;
333- }
334-
335329 /* Run through the state machine. */
336330 State next_state = _state;
337331 switch (_state)
@@ -346,6 +340,7 @@ void ArduinoIoTCloudTCP::update()
346340 case State::SubscribeThingTopics: next_state = handle_SubscribeThingTopics (); break ;
347341 case State::RequestLastValues: next_state = handle_RequestLastValues (); break ;
348342 case State::Connected: next_state = handle_Connected (); break ;
343+ case State::Disconnect: next_state = handle_Disconnect (); break ;
349344 }
350345 _state = next_state;
351346
@@ -420,10 +415,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SendDeviceProperties()
420415{
421416 if (!_mqttClient.connected ())
422417 {
423- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
424- _mqttClient.stop ();
425- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
426- return State::ConnectPhy;
418+ return State::Disconnect;
427419 }
428420
429421#if OTA_ENABLED
@@ -436,10 +428,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeDeviceTopic()
436428{
437429 if (!_mqttClient.connected ())
438430 {
439- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
440- _mqttClient.stop ();
441- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
442- return State::ConnectPhy;
431+ return State::Disconnect;
443432 }
444433
445434 if (!_mqttClient.subscribe (_deviceTopicIn))
@@ -468,10 +457,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_WaitDeviceConfig()
468457{
469458 if (!_mqttClient.connected ())
470459 {
471- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
472- _mqttClient.stop ();
473- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
474- return State::ConnectPhy;
460+ return State::Disconnect;
461+ }
462+
463+ if (getThingIdOutdatedFlag ())
464+ {
465+ return State::CheckDeviceConfig;
475466 }
476467
477468 if (millis () > _next_device_subscribe_attempt_tick)
@@ -489,10 +480,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_CheckDeviceConfig()
489480{
490481 if (!_mqttClient.connected ())
491482 {
492- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
493- _mqttClient.stop ();
494- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
495- return State::ConnectPhy;
483+ return State::Disconnect;
496484 }
497485
498486 if (_deviceSubscribedToThing == true )
@@ -527,10 +515,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeThingTopics()
527515{
528516 if (!_mqttClient.connected ())
529517 {
530- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
531- _mqttClient.stop ();
532- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
533- return State::ConnectPhy;
518+ return State::Disconnect;
519+ }
520+
521+ if (getThingIdOutdatedFlag ())
522+ {
523+ return State::CheckDeviceConfig;
534524 }
535525
536526 unsigned long const now = millis ();
@@ -583,10 +573,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
583573{
584574 if (!_mqttClient.connected ())
585575 {
586- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
587- _mqttClient.stop ();
588- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
589- return State::ConnectPhy;
576+ return State::Disconnect;
577+ }
578+
579+ if (getThingIdOutdatedFlag ())
580+ {
581+ return State::CheckDeviceConfig;
590582 }
591583
592584 /* Check whether or not we need to send a new request. */
@@ -620,22 +612,18 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
620612{
621613 if (!_mqttClient.connected ())
622614 {
623- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
624-
625- /* Forcefully disconnect MQTT client and trigger a reconnection. */
626- _mqttClient.stop ();
627-
628615 /* The last message was definitely lost, trigger a retransmit. */
629616 _mqtt_data_request_retransmit = true ;
630-
631- /* We are not connected anymore, trigger the callback for a disconnected event. */
632- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
633-
634- return State::ConnectPhy;
617+ return State::Disconnect;
635618 }
636619 /* We are connected so let's to our stuff here. */
637620 else
638621 {
622+ if (getThingIdOutdatedFlag ())
623+ {
624+ return State::CheckDeviceConfig;
625+ }
626+
639627 /* Check if a primitive property wrapper is locally changed.
640628 * This function requires an existing time service which in
641629 * turn requires an established connection. Not having that
@@ -689,6 +677,14 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
689677 }
690678}
691679
680+ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect ()
681+ {
682+ DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
683+ _mqttClient.stop ();
684+ execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
685+ return State::ConnectPhy;
686+ }
687+
692688void ArduinoIoTCloudTCP::onMessage (int length)
693689{
694690 ArduinoCloud.handleMessage (length);
0 commit comments