Skip to content

Commit 33c27b1

Browse files
committed
Move thing_id outdated flag check outside update function and add handle_Disconnect() function
1 parent 7720670 commit 33c27b1

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
@@ -332,12 +332,6 @@ void ArduinoIoTCloudTCP::update()
332332
_next_state = State::Invalid;
333333
}
334334

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

@@ -426,10 +421,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SendDeviceProperties()
426421
{
427422
if (!_mqttClient.connected())
428423
{
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;
433425
}
434426

435427
#if OTA_ENABLED
@@ -442,10 +434,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeDeviceTopic()
442434
{
443435
if (!_mqttClient.connected())
444436
{
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;
449438
}
450439

451440
if (!_mqttClient.subscribe(_deviceTopicIn))
@@ -474,10 +463,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_WaitDeviceConfig()
474463
{
475464
if (!_mqttClient.connected())
476465
{
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;
481472
}
482473

483474
if (millis() > _next_device_subscribe_attempt_tick)
@@ -495,10 +486,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_CheckDeviceConfig()
495486
{
496487
if (!_mqttClient.connected())
497488
{
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;
502490
}
503491

504492
if(_deviceSubscribedToThing == true)
@@ -533,10 +521,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeThingTopics()
533521
{
534522
if (!_mqttClient.connected())
535523
{
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;
540530
}
541531

542532
if (!_mqttClient.subscribe(_dataTopicIn))
@@ -568,10 +558,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
568558
{
569559
if (!_mqttClient.connected())
570560
{
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;
575567
}
576568

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

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+
677673
void ArduinoIoTCloudTCP::onMessage(int length)
678674
{
679675
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)