@@ -333,12 +333,6 @@ void ArduinoIoTCloudTCP::update()
333
333
_next_state = State::Invalid;
334
334
}
335
335
336
- if (getThingIdOutdatedFlag ()) {
337
- DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s Thing id outdated, reconfiguring..." , __FUNCTION__);
338
- if (_mqttClient.connected ())
339
- _state = State::CheckDeviceConfig;
340
- }
341
-
342
336
/* Run through the state machine. */
343
337
State next_state = _state;
344
338
switch (_state)
@@ -353,6 +347,7 @@ void ArduinoIoTCloudTCP::update()
353
347
case State::SubscribeThingTopics: next_state = handle_SubscribeThingTopics (); break ;
354
348
case State::RequestLastValues: next_state = handle_RequestLastValues (); break ;
355
349
case State::Connected: next_state = handle_Connected (); break ;
350
+ case State::Disconnect: next_state = handle_Disconnect (); break ;
356
351
}
357
352
_state = next_state;
358
353
@@ -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
unsigned long const now = millis ();
@@ -590,10 +580,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
590
580
{
591
581
if (!_mqttClient.connected ())
592
582
{
593
- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
594
- _mqttClient.stop ();
595
- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
596
- return State::ConnectPhy;
583
+ return State::Disconnect;
584
+ }
585
+
586
+ if (getThingIdOutdatedFlag ())
587
+ {
588
+ return State::CheckDeviceConfig;
597
589
}
598
590
599
591
/* Check whether or not we need to send a new request. */
@@ -627,22 +619,18 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
627
619
{
628
620
if (!_mqttClient.connected ())
629
621
{
630
- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
631
-
632
- /* Forcefully disconnect MQTT client and trigger a reconnection. */
633
- _mqttClient.stop ();
634
-
635
622
/* The last message was definitely lost, trigger a retransmit. */
636
623
_mqtt_data_request_retransmit = true ;
637
-
638
- /* We are not connected anymore, trigger the callback for a disconnected event. */
639
- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
640
-
641
- return State::ConnectPhy;
624
+ return State::Disconnect;
642
625
}
643
626
/* We are connected so let's to our stuff here. */
644
627
else
645
628
{
629
+ if (getThingIdOutdatedFlag ())
630
+ {
631
+ return State::CheckDeviceConfig;
632
+ }
633
+
646
634
/* Check if a primitive property wrapper is locally changed.
647
635
* This function requires an existing time service which in
648
636
* turn requires an established connection. Not having that
@@ -696,6 +684,14 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
696
684
}
697
685
}
698
686
687
+ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect ()
688
+ {
689
+ DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
690
+ _mqttClient.stop ();
691
+ execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
692
+ return State::ConnectPhy;
693
+ }
694
+
699
695
void ArduinoIoTCloudTCP::onMessage (int length)
700
696
{
701
697
ArduinoCloud.handleMessage (length);
0 commit comments