@@ -60,13 +60,10 @@ unsigned long getTime()
60
60
ArduinoIoTCloudTCP::ArduinoIoTCloudTCP ()
61
61
: _state{State::ConnectPhy}
62
62
, _connection_attempt(0 ,0 )
63
+ , _message_stream(std::bind(&ArduinoIoTCloudTCP::sendMessage, this , std::placeholders::_1))
64
+ , _thing(&_message_stream)
65
+ , _thing_id_property{nullptr }
63
66
, _device_property_container{0 }
64
- , _thing_property_container{0 }
65
- , _last_checked_property_index{0 }
66
- , _tz_offset{0 }
67
- , _tz_offset_property{nullptr }
68
- , _tz_dst_until{0 }
69
- , _tz_dst_until_property{nullptr }
70
67
, _mqtt_data_buf{0 }
71
68
, _mqtt_data_len{0 }
72
69
, _mqtt_data_request_retransmit{false }
@@ -214,10 +211,8 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
214
211
#endif /* OTA_ENABLED */
215
212
p = new CloudWrapperString (_thing_id);
216
213
_thing_id_property = &addPropertyToContainer (_device_property_container, *p, " thing_id" , Permission::ReadWrite, -1 ).writeOnDemand ();
217
- p = new CloudWrapperInt (_tz_offset);
218
- _tz_offset_property = &addPropertyToContainer (_thing_property_container, *p, " tz_offset" , Permission::ReadWrite, -1 ).writeOnDemand ();
219
- p = new CloudWrapperUnsignedInt (_tz_dst_until);
220
- _tz_dst_until_property = &addPropertyToContainer (_thing_property_container, *p, " tz_dst_until" , Permission::ReadWrite, -1 ).writeOnDemand ();
214
+
215
+ _thing.begin ();
221
216
222
217
#if OTA_ENABLED
223
218
_ota_cap = OTA::isCapable ();
@@ -274,7 +269,6 @@ void ArduinoIoTCloudTCP::update()
274
269
case State::SubscribeDeviceTopic: next_state = handle_SubscribeDeviceTopic (); break ;
275
270
case State::CheckDeviceConfig: next_state = handle_CheckDeviceConfig (); break ;
276
271
case State::SubscribeThingTopics: next_state = handle_SubscribeThingTopics (); break ;
277
- case State::RequestLastValues: next_state = handle_RequestLastValues (); break ;
278
272
case State::Connected: next_state = handle_Connected (); break ;
279
273
case State::Disconnect: next_state = handle_Disconnect (); break ;
280
274
}
@@ -478,38 +472,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeThingTopics()
478
472
479
473
/* Successfully subscribed to thing topics, reconfigure timers for next state and go on */
480
474
_connection_attempt.begin (AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms);
481
- return State::RequestLastValues;
482
- }
483
-
484
- ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues ()
485
- {
486
- if (!_mqttClient.connected () || _thing_id_property->isDifferentFromCloud ())
487
- {
488
- return State::Disconnect;
489
- }
490
-
491
- /* Check whether or not we need to send a new request. */
492
- if (_connection_attempt.isRetry () && !_connection_attempt.isExpired ())
493
- return State::RequestLastValues;
494
-
495
- /* Track the number of times a get-last-values request was sent to the cloud.
496
- * If no data is received within a certain number of retry-requests it's a better
497
- * strategy to disconnect and re-establish connection from the ground up.
498
- */
499
- if (_connection_attempt.getRetryCount () > AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT)
500
- {
501
- return State::Disconnect;
502
- }
503
-
504
- _connection_attempt.retry ();
505
- requestLastValue ();
506
- DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s [%d] last values requested" , __FUNCTION__, _time_service.getTime ());
507
- return State::RequestLastValues;
475
+ return State::Connected;
508
476
}
509
477
510
478
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected ()
511
479
{
512
- if (!_mqttClient.connected ())
480
+ if (!_mqttClient.connected () || _thing_id_property-> isDifferentFromCloud () || !_thing. connected () )
513
481
{
514
482
/* The last message was definitely lost, trigger a retransmit. */
515
483
_mqtt_data_request_retransmit = true ;
@@ -518,20 +486,6 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
518
486
/* We are connected so let's to our stuff here. */
519
487
else
520
488
{
521
- if (_thing_id_property->isDifferentFromCloud ())
522
- {
523
- return State::Disconnect;
524
- }
525
-
526
- /* Check if a primitive property wrapper is locally changed.
527
- * This function requires an existing time service which in
528
- * turn requires an established connection. Not having that
529
- * leads to a wrong time set in the time service which inhibits
530
- * the connection from being established due to a wrong data
531
- * in the reconstructed certificate.
532
- */
533
- updateTimestampOnLocallyChangedProperties (_thing_property_container);
534
-
535
489
/* Retransmit data in case there was a lost transaction due
536
490
* to phy layer or MQTT connectivity loss.
537
491
*/
@@ -540,27 +494,11 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
540
494
_mqtt_data_request_retransmit = false ;
541
495
}
542
496
543
- /* Configure Time service with timezone data:
544
- * _tz_offset [offset + dst]
545
- * _tz_dst_until [posix timestamp until _tz_offset is valid]
546
- */
547
- if (_tz_offset_property->isDifferentFromCloud () || _tz_dst_until_property->isDifferentFromCloud ()) {
548
- _tz_offset_property->fromCloudToLocal ();
549
- _tz_dst_until_property->fromCloudToLocal ();
550
- _time_service.setTimeZoneData (_tz_offset, _tz_dst_until);
551
- }
497
+ /* Call CloudThing process to synchronize properties */
498
+ _thing.update ();
552
499
553
- /* Check if any properties need encoding and send them to
554
- * the cloud if necessary.
555
- */
556
- sendThingPropertiesToCloud ();
500
+ return State::Connected;
557
501
558
- unsigned long const internal_posix_time = _time_service.getTime ();
559
- if (internal_posix_time < _tz_dst_until) {
560
- return State::Connected;
561
- } else {
562
- return State::RequestLastValues;
563
- }
564
502
}
565
503
}
566
504
@@ -605,9 +543,15 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect()
605
543
/* TODO add device topic */
606
544
_mqttClient.stop ();
607
545
}
546
+
547
+ Message message = { ResetCmdId };
548
+ _thing.handleMessage (&message);
549
+
608
550
DEBUG_INFO (" Disconnected from Arduino IoT Cloud" );
609
551
execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
610
552
553
+ updateThingTopics ();
554
+
611
555
/* Setup timer for broker connection and restart */
612
556
_connection_attempt.begin (AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
613
557
return State::ConnectPhy;
@@ -631,25 +575,47 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
631
575
/* Topic for OTA properties and device configuration */
632
576
if (_deviceTopicIn == topic) {
633
577
CBORDecoder::decode (_device_property_container, (uint8_t *)bytes, length);
634
- _state = State::CheckDeviceConfig;
578
+ if (_thing_id_property->isDifferentFromCloud () && (_thing_id.length () != 0 )) {
579
+ _state = State::Disconnect;
580
+ } else {
581
+ _state = State::CheckDeviceConfig;
582
+ }
635
583
}
636
584
637
585
/* Topic for user input data */
638
586
if (_dataTopicIn == topic) {
639
- CBORDecoder::decode (_thing_property_container , (uint8_t *)bytes, length);
587
+ CBORDecoder::decode (_thing. getPropertyContainer () , (uint8_t *)bytes, length);
640
588
}
641
589
642
590
/* Topic for sync Thing last values on connect */
643
- if (( _shadowTopicIn == topic) && (_state == State::RequestLastValues) )
591
+ if (_shadowTopicIn == topic)
644
592
{
645
593
DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s [%d] last values received" , __FUNCTION__, millis ());
646
- CBORDecoder::decode (_thing_property_container, (uint8_t *)bytes, length, true );
647
- _time_service.setTimeZoneData (_tz_offset, _tz_dst_until);
594
+ CBORDecoder::decode (_thing.getPropertyContainer (), (uint8_t *)bytes, length, true );
595
+ Message message = { LastValuesUpdateCmdId };
596
+ _thing.handleMessage (&message);
648
597
execCloudEventCallback (ArduinoIoTCloudEvent::SYNC);
649
598
_state = State::Connected;
650
599
}
651
600
}
652
601
602
+ void ArduinoIoTCloudTCP::sendMessage (Message * msg)
603
+ {
604
+ switch (msg->id )
605
+ {
606
+ case PropertiesUpdateCmdId:
607
+ sendThingPropertiesToCloud ();
608
+ break ;
609
+
610
+ case LastValuesBeginCmdId:
611
+ requestLastValue ();
612
+ break ;
613
+
614
+ default :
615
+ break ;
616
+ }
617
+ }
618
+
653
619
void ArduinoIoTCloudTCP::sendPropertyContainerToCloud (String const topic, PropertyContainer & property_container, unsigned int & current_property_index)
654
620
{
655
621
int bytes_encoded = 0 ;
@@ -670,7 +636,7 @@ void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(String const topic, Proper
670
636
671
637
void ArduinoIoTCloudTCP::sendThingPropertiesToCloud ()
672
638
{
673
- sendPropertyContainerToCloud (_dataTopicOut, _thing_property_container, _last_checked_property_index );
639
+ sendPropertyContainerToCloud (_dataTopicOut, _thing. getPropertyContainer (), _thing. getPropertyContainerIndex () );
674
640
}
675
641
676
642
void ArduinoIoTCloudTCP::sendDevicePropertiesToCloud ()
0 commit comments