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