@@ -332,12 +332,6 @@ void ArduinoIoTCloudTCP::update()
332
332
_next_state = State::Invalid;
333
333
}
334
334
335
- if (getThingIdOutdatedFlag ()) {
336
- DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s Thing id outdated, reconfiguring..." , __FUNCTION__);
337
- if (_mqttClient.connected ())
338
- _state = State::CheckDeviceConfig;
339
- }
340
-
341
335
/* Run through the state machine. */
342
336
State next_state = _state;
343
337
switch (_state)
@@ -352,6 +346,7 @@ void ArduinoIoTCloudTCP::update()
352
346
case State::SubscribeThingTopics: next_state = handle_SubscribeThingTopics (); break ;
353
347
case State::RequestLastValues: next_state = handle_RequestLastValues (); break ;
354
348
case State::Connected: next_state = handle_Connected (); break ;
349
+ case State::Disconnect: next_state = handle_Disconnect (); break ;
355
350
}
356
351
_state = next_state;
357
352
@@ -427,10 +422,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SendDeviceProperties()
427
422
{
428
423
if (!_mqttClient.connected ())
429
424
{
430
- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
431
- _mqttClient.stop ();
432
- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
433
- return State::ConnectPhy;
425
+ return State::Disconnect;
434
426
}
435
427
436
428
#if OTA_ENABLED
@@ -443,10 +435,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeDeviceTopic()
443
435
{
444
436
if (!_mqttClient.connected ())
445
437
{
446
- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
447
- _mqttClient.stop ();
448
- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
449
- return State::ConnectPhy;
438
+ return State::Disconnect;
450
439
}
451
440
452
441
if (!_mqttClient.subscribe (_deviceTopicIn))
@@ -475,10 +464,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_WaitDeviceConfig()
475
464
{
476
465
if (!_mqttClient.connected ())
477
466
{
478
- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
479
- _mqttClient.stop ();
480
- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
481
- return State::ConnectPhy;
467
+ return State::Disconnect;
468
+ }
469
+
470
+ if (getThingIdOutdatedFlag ())
471
+ {
472
+ return State::CheckDeviceConfig;
482
473
}
483
474
484
475
if (millis () > _next_device_subscribe_attempt_tick)
@@ -496,10 +487,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_CheckDeviceConfig()
496
487
{
497
488
if (!_mqttClient.connected ())
498
489
{
499
- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
500
- _mqttClient.stop ();
501
- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
502
- return State::ConnectPhy;
490
+ return State::Disconnect;
503
491
}
504
492
505
493
if (_deviceSubscribedToThing == true )
@@ -534,10 +522,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeThingTopics()
534
522
{
535
523
if (!_mqttClient.connected ())
536
524
{
537
- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
538
- _mqttClient.stop ();
539
- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
540
- return State::ConnectPhy;
525
+ return State::Disconnect;
526
+ }
527
+
528
+ if (getThingIdOutdatedFlag ())
529
+ {
530
+ return State::CheckDeviceConfig;
541
531
}
542
532
543
533
if (!_mqttClient.subscribe (_dataTopicIn))
@@ -569,10 +559,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
569
559
{
570
560
if (!_mqttClient.connected ())
571
561
{
572
- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
573
- _mqttClient.stop ();
574
- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
575
- return State::ConnectPhy;
562
+ return State::Disconnect;
563
+ }
564
+
565
+ if (getThingIdOutdatedFlag ())
566
+ {
567
+ return State::CheckDeviceConfig;
576
568
}
577
569
578
570
/* Check whether or not we need to send a new request. */
@@ -606,22 +598,18 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
606
598
{
607
599
if (!_mqttClient.connected ())
608
600
{
609
- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
610
-
611
- /* Forcefully disconnect MQTT client and trigger a reconnection. */
612
- _mqttClient.stop ();
613
-
614
601
/* The last message was definitely lost, trigger a retransmit. */
615
602
_mqtt_data_request_retransmit = true ;
616
-
617
- /* We are not connected anymore, trigger the callback for a disconnected event. */
618
- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
619
-
620
- return State::ConnectPhy;
603
+ return State::Disconnect;
621
604
}
622
605
/* We are connected so let's to our stuff here. */
623
606
else
624
607
{
608
+ if (getThingIdOutdatedFlag ())
609
+ {
610
+ return State::CheckDeviceConfig;
611
+ }
612
+
625
613
/* Check if a primitive property wrapper is locally changed.
626
614
* This function requires an existing time service which in
627
615
* turn requires an established connection. Not having that
@@ -675,6 +663,14 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
675
663
}
676
664
}
677
665
666
+ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect ()
667
+ {
668
+ DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
669
+ _mqttClient.stop ();
670
+ execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
671
+ return State::ConnectPhy;
672
+ }
673
+
678
674
void ArduinoIoTCloudTCP::onMessage (int length)
679
675
{
680
676
ArduinoCloud.handleMessage (length);
0 commit comments