Skip to content

Commit 1b79e5e

Browse files
committed
Move thing_id outdated flag check outside update function and add handle_Disconnect() function
1 parent d0cbca0 commit 1b79e5e

File tree

3 files changed

+39
-40
lines changed

3 files changed

+39
-40
lines changed

src/ArduinoIoTCloud.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ ArduinoIoTCloudClass::ArduinoIoTCloudClass()
3333
, _thing_id{""}
3434
, _device_id{""}
3535
, _cloud_event_callback{nullptr}
36+
, _thing_id_outdated{false}
3637
{
3738

3839
}

src/ArduinoIoTCloudTCP.cpp

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,6 @@ void ArduinoIoTCloudTCP::update()
331331
_next_state = State::Invalid;
332332
}
333333

334-
if(getThingIdOutdatedFlag()) {
335-
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s Thing id outdated, reconfiguring...", __FUNCTION__);
336-
if (_mqttClient.connected())
337-
_state = State::CheckDeviceConfig;
338-
}
339-
340334
/* Run through the state machine. */
341335
State next_state = _state;
342336
switch (_state)
@@ -351,6 +345,7 @@ void ArduinoIoTCloudTCP::update()
351345
case State::SubscribeThingTopics: next_state = handle_SubscribeThingTopics(); break;
352346
case State::RequestLastValues: next_state = handle_RequestLastValues(); break;
353347
case State::Connected: next_state = handle_Connected(); break;
348+
case State::Disconnect: next_state = handle_Disconnect(); break;
354349
}
355350
_state = next_state;
356351

@@ -425,10 +420,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SendDeviceProperties()
425420
{
426421
if (!_mqttClient.connected())
427422
{
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;
432424
}
433425

434426
#if OTA_ENABLED
@@ -441,10 +433,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeDeviceTopic()
441433
{
442434
if (!_mqttClient.connected())
443435
{
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;
448437
}
449438

450439
if (!_mqttClient.subscribe(_deviceTopicIn))
@@ -473,10 +462,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_WaitDeviceConfig()
473462
{
474463
if (!_mqttClient.connected())
475464
{
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;
480471
}
481472

482473
if (millis() > _next_device_subscribe_attempt_tick)
@@ -494,10 +485,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_CheckDeviceConfig()
494485
{
495486
if (!_mqttClient.connected())
496487
{
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;
501489
}
502490

503491
if(_deviceSubscribedToThing == true)
@@ -532,10 +520,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeThingTopics()
532520
{
533521
if (!_mqttClient.connected())
534522
{
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;
539529
}
540530

541531
if (!_mqttClient.subscribe(_dataTopicIn))
@@ -567,10 +557,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
567557
{
568558
if (!_mqttClient.connected())
569559
{
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;
574566
}
575567

576568
/* Check whether or not we need to send a new request. */
@@ -604,22 +596,18 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
604596
{
605597
if (!_mqttClient.connected())
606598
{
607-
DEBUG_ERROR("ArduinoIoTCloudTCP::%s MQTT client connection lost", __FUNCTION__);
608-
609-
/* Forcefully disconnect MQTT client and trigger a reconnection. */
610-
_mqttClient.stop();
611-
612599
/* The last message was definitely lost, trigger a retransmit. */
613600
_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;
619602
}
620603
/* We are connected so let's to our stuff here. */
621604
else
622605
{
606+
if (getThingIdOutdatedFlag())
607+
{
608+
return State::CheckDeviceConfig;
609+
}
610+
623611
/* Check if a primitive property wrapper is locally changed.
624612
* This function requires an existing time service which in
625613
* turn requires an established connection. Not having that
@@ -673,6 +661,14 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
673661
}
674662
}
675663

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+
676672
void ArduinoIoTCloudTCP::onMessage(int length)
677673
{
678674
ArduinoCloud.handleMessage(length);

src/ArduinoIoTCloudTCP.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
112112
SubscribeThingTopics,
113113
RequestLastValues,
114114
Connected,
115+
Disconnect,
115116
Invalid
116117
};
117118

@@ -179,6 +180,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
179180
State handle_SubscribeThingTopics();
180181
State handle_RequestLastValues();
181182
State handle_Connected();
183+
State handle_Disconnect();
182184

183185
static void onMessage(int length);
184186
void handleMessage(int length);

0 commit comments

Comments
 (0)