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