Skip to content

Commit 1b4277e

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

File tree

3 files changed

+39
-40
lines changed

3 files changed

+39
-40
lines changed

src/ArduinoIoTCloud.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ ArduinoIoTCloudClass::ArduinoIoTCloudClass()
3434
, _thing_id{""}
3535
, _device_id{""}
3636
, _cloud_event_callback{nullptr}
37+
, _thing_id_outdated{false}
3738
{
3839

3940
}

src/ArduinoIoTCloudTCP.cpp

+36-40
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,6 @@ void ArduinoIoTCloudTCP::update()
333333
_next_state = State::Invalid;
334334
}
335335

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

@@ -427,10 +422,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SendDeviceProperties()
427422
{
428423
if (!_mqttClient.connected())
429424
{
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;
434426
}
435427

436428
#if OTA_ENABLED
@@ -443,10 +435,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeDeviceTopic()
443435
{
444436
if (!_mqttClient.connected())
445437
{
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;
450439
}
451440

452441
if (!_mqttClient.subscribe(_deviceTopicIn))
@@ -475,10 +464,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_WaitDeviceConfig()
475464
{
476465
if (!_mqttClient.connected())
477466
{
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;
482473
}
483474

484475
if (millis() > _next_device_subscribe_attempt_tick)
@@ -496,10 +487,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_CheckDeviceConfig()
496487
{
497488
if (!_mqttClient.connected())
498489
{
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;
503491
}
504492

505493
if(_deviceSubscribedToThing == true)
@@ -534,10 +522,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeThingTopics()
534522
{
535523
if (!_mqttClient.connected())
536524
{
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;
541531
}
542532

543533
unsigned long const now = millis();
@@ -590,10 +580,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
590580
{
591581
if (!_mqttClient.connected())
592582
{
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;
597589
}
598590

599591
/* Check whether or not we need to send a new request. */
@@ -627,22 +619,18 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
627619
{
628620
if (!_mqttClient.connected())
629621
{
630-
DEBUG_ERROR("ArduinoIoTCloudTCP::%s MQTT client connection lost", __FUNCTION__);
631-
632-
/* Forcefully disconnect MQTT client and trigger a reconnection. */
633-
_mqttClient.stop();
634-
635622
/* The last message was definitely lost, trigger a retransmit. */
636623
_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;
642625
}
643626
/* We are connected so let's to our stuff here. */
644627
else
645628
{
629+
if (getThingIdOutdatedFlag())
630+
{
631+
return State::CheckDeviceConfig;
632+
}
633+
646634
/* Check if a primitive property wrapper is locally changed.
647635
* This function requires an existing time service which in
648636
* turn requires an established connection. Not having that
@@ -696,6 +684,14 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
696684
}
697685
}
698686

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+
699695
void ArduinoIoTCloudTCP::onMessage(int length)
700696
{
701697
ArduinoCloud.handleMessage(length);

src/ArduinoIoTCloudTCP.h

+2
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

@@ -181,6 +182,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
181182
State handle_SubscribeThingTopics();
182183
State handle_RequestLastValues();
183184
State handle_Connected();
185+
State handle_Disconnect();
184186

185187
static void onMessage(int length);
186188
void handleMessage(int length);

0 commit comments

Comments
 (0)