@@ -326,12 +326,6 @@ void ArduinoIoTCloudTCP::update()
326
326
#endif
327
327
328
328
329
- if (getThingIdOutdatedFlag ()) {
330
- DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s Thing id outdated, reconfiguring..." , __FUNCTION__);
331
- if (_mqttClient.connected ())
332
- _state = State::CheckDeviceConfig;
333
- }
334
-
335
329
/* Run through the state machine. */
336
330
State next_state = _state;
337
331
switch (_state)
@@ -346,6 +340,7 @@ void ArduinoIoTCloudTCP::update()
346
340
case State::SubscribeThingTopics: next_state = handle_SubscribeThingTopics (); break ;
347
341
case State::RequestLastValues: next_state = handle_RequestLastValues (); break ;
348
342
case State::Connected: next_state = handle_Connected (); break ;
343
+ case State::Disconnect: next_state = handle_Disconnect (); break ;
349
344
}
350
345
_state = next_state;
351
346
@@ -420,10 +415,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SendDeviceProperties()
420
415
{
421
416
if (!_mqttClient.connected ())
422
417
{
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;
427
419
}
428
420
429
421
#if OTA_ENABLED
@@ -436,10 +428,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeDeviceTopic()
436
428
{
437
429
if (!_mqttClient.connected ())
438
430
{
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;
443
432
}
444
433
445
434
if (!_mqttClient.subscribe (_deviceTopicIn))
@@ -468,10 +457,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_WaitDeviceConfig()
468
457
{
469
458
if (!_mqttClient.connected ())
470
459
{
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;
475
466
}
476
467
477
468
if (millis () > _next_device_subscribe_attempt_tick)
@@ -489,10 +480,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_CheckDeviceConfig()
489
480
{
490
481
if (!_mqttClient.connected ())
491
482
{
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;
496
484
}
497
485
498
486
if (_deviceSubscribedToThing == true )
@@ -527,10 +515,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeThingTopics()
527
515
{
528
516
if (!_mqttClient.connected ())
529
517
{
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;
534
524
}
535
525
536
526
unsigned long const now = millis ();
@@ -583,10 +573,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
583
573
{
584
574
if (!_mqttClient.connected ())
585
575
{
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;
590
582
}
591
583
592
584
/* Check whether or not we need to send a new request. */
@@ -620,22 +612,18 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
620
612
{
621
613
if (!_mqttClient.connected ())
622
614
{
623
- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
624
-
625
- /* Forcefully disconnect MQTT client and trigger a reconnection. */
626
- _mqttClient.stop ();
627
-
628
615
/* The last message was definitely lost, trigger a retransmit. */
629
616
_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;
635
618
}
636
619
/* We are connected so let's to our stuff here. */
637
620
else
638
621
{
622
+ if (getThingIdOutdatedFlag ())
623
+ {
624
+ return State::CheckDeviceConfig;
625
+ }
626
+
639
627
/* Check if a primitive property wrapper is locally changed.
640
628
* This function requires an existing time service which in
641
629
* turn requires an established connection. Not having that
@@ -689,6 +677,14 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
689
677
}
690
678
}
691
679
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
+
692
688
void ArduinoIoTCloudTCP::onMessage (int length)
693
689
{
694
690
ArduinoCloud.handleMessage (length);
0 commit comments