@@ -391,24 +391,21 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
391
391
case CommandId::ThingUpdateCmdId:
392
392
{
393
393
DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s [%d] device configuration received" , __FUNCTION__, millis ());
394
- if ( _thing_id != String (command.thingUpdateCmd .params .thing_id )) {
395
- _thing_id = String (command.thingUpdateCmd .params .thing_id );
394
+ String new_thing_id = String (command.thingUpdateCmd .params .thing_id );
395
+
396
+ if (!new_thing_id.length ()) {
397
+ /* Send message to device state machine to inform we have received a null thing-id */
398
+ _thing_id = " xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
396
399
Message message;
397
- /* If we are attached we need first to detach */
398
- if (_device.isAttached ()) {
400
+ message = { DeviceRegisteredCmdId };
401
+ _device.handleMessage (&message);
402
+ } else {
403
+ if (_device.isAttached () && _thing_id != new_thing_id) {
399
404
detachThing ();
400
- message = { DeviceDetachedCmdId };
401
405
}
402
- /* If received thing id is valid attach to the new thing */
403
- if (_thing_id.length ()) {
404
- attachThing ();
405
- message = { DeviceAttachedCmdId };
406
- } else {
407
- /* Send message to device state machine to inform we have received a null thing-id */
408
- _thing_id = " xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
409
- message = { DeviceRegisteredCmdId };
406
+ if (!_device.isAttached ()) {
407
+ attachThing (new_thing_id);
410
408
}
411
- _device.handleMessage (&message);
412
409
}
413
410
}
414
411
break ;
@@ -491,17 +488,23 @@ void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(String const topic, Proper
491
488
}
492
489
}
493
490
494
- void ArduinoIoTCloudTCP::attachThing ()
491
+ void ArduinoIoTCloudTCP::attachThing (String thingId )
495
492
{
493
+ _thing_id = thingId;
496
494
497
495
_dataTopicIn = getTopic_datain ();
498
496
_dataTopicOut = getTopic_dataout ();
499
497
if (!_mqttClient.subscribe (_dataTopicIn)) {
500
498
DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not subscribe to %s" , __FUNCTION__, _dataTopicIn.c_str ());
501
499
DEBUG_ERROR (" Check your thing configuration, and press the reset button on your board." );
500
+ _thing_id = " xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
502
501
return ;
503
502
}
504
503
504
+ Message message;
505
+ message = { DeviceAttachedCmdId };
506
+ _device.handleMessage (&message);
507
+
505
508
DEBUG_INFO (" Connected to Arduino IoT Cloud" );
506
509
DEBUG_INFO (" Thing ID: %s" , getThingId ().c_str ());
507
510
execCloudEventCallback (ArduinoIoTCloudEvent::CONNECT);
@@ -514,6 +517,12 @@ void ArduinoIoTCloudTCP::detachThing()
514
517
return ;
515
518
}
516
519
520
+ Message message;
521
+ message = { DeviceDetachedCmdId };
522
+ _device.handleMessage (&message);
523
+
524
+ _thing_id = " xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
525
+
517
526
DEBUG_INFO (" Disconnected from Arduino IoT Cloud" );
518
527
execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
519
528
}
0 commit comments