Skip to content

Commit b2a4334

Browse files
committed
Use a single function to keep OTA properties in sync with cloud
1 parent a73ce6b commit b2a4334

File tree

2 files changed

+13
-59
lines changed

2 files changed

+13
-59
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -654,18 +654,18 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
654654
/* Clear the request flag. */
655655
_ota_req = false;
656656
/* Transmit the cleared request flags to the cloud. */
657-
sendClearedOTARequestToCloud();
657+
sendDevicePropertyToCloud("OTA_REQ");
658658
/* Call member function to handle OTA request. */
659659
onOTARequest();
660660
/* If something fails send the OTA error to the cloud */
661-
sendOTAErrorToCloud();
661+
sendDevicePropertyToCloud("OTA_ERROR");
662662
}
663663
}
664664

665665
/* Check if we have received the OTA_URL property and provide
666666
* echo to the cloud.
667667
*/
668-
sendOTAUrlToCloud();
668+
sendDevicePropertyToCloud("OTA_URL");
669669

670670
#endif /* OTA_ENABLED */
671671

@@ -775,61 +775,17 @@ void ArduinoIoTCloudTCP::sendDevicePropertiesToCloud()
775775
}
776776

777777
#if OTA_ENABLED
778-
void ArduinoIoTCloudTCP::sendClearedOTARequestToCloud()
778+
void ArduinoIoTCloudTCP::sendDevicePropertyToCloud(String const name)
779779
{
780-
PropertyContainer ota_property_container;
781-
unsigned int last_ota_property_index = 0;
782-
783-
std::list<String> ota_property_list {"OTA_REQ"};
784-
std::for_each(ota_property_list.begin(),
785-
ota_property_list.end(),
786-
[this, &ota_property_container ] (String const & name)
787-
{
788-
Property* p = getProperty(this->_device_property_container, name);
789-
if(p != nullptr)
790-
addPropertyToContainer(ota_property_container, *p, p->name(), p->isWriteableByCloud() ? Permission::ReadWrite : Permission::Read);
791-
}
792-
);
793-
794-
sendPropertyContainerToCloud(_deviceTopicOut, ota_property_container, last_ota_property_index);
795-
}
796-
797-
void ArduinoIoTCloudTCP::sendOTAErrorToCloud()
798-
{
799-
PropertyContainer ota_property_container;
800-
unsigned int last_ota_property_index = 0;
801-
802-
std::list<String> ota_property_list {"OTA_ERROR"};
803-
std::for_each(ota_property_list.begin(),
804-
ota_property_list.end(),
805-
[this, &ota_property_container ] (String const & name)
806-
{
807-
Property* p = getProperty(this->_device_property_container, name);
808-
if(p != nullptr)
809-
addPropertyToContainer(ota_property_container, *p, p->name(), p->isWriteableByCloud() ? Permission::ReadWrite : Permission::Read);
810-
}
811-
);
812-
813-
sendPropertyContainerToCloud(_deviceTopicOut, ota_property_container, last_ota_property_index);
814-
}
815-
816-
void ArduinoIoTCloudTCP::sendOTAUrlToCloud()
817-
{
818-
PropertyContainer ota_property_container;
819-
unsigned int last_ota_property_index = 0;
820-
821-
std::list<String> ota_property_list {"OTA_URL"};
822-
std::for_each(ota_property_list.begin(),
823-
ota_property_list.end(),
824-
[this, &ota_property_container ] (String const & name)
825-
{
826-
Property* p = getProperty(this->_device_property_container, name);
827-
if(p != nullptr)
828-
addPropertyToContainer(ota_property_container, *p, p->name(), p->isWriteableByCloud() ? Permission::ReadWrite : Permission::Read);
829-
}
830-
);
780+
PropertyContainer temp_device_property_container;
781+
unsigned int last_device_property_index = 0;
831782

832-
sendPropertyContainerToCloud(_deviceTopicOut, ota_property_container, last_ota_property_index);
783+
Property* p = getProperty(this->_device_property_container, name);
784+
if(p != nullptr)
785+
{
786+
addPropertyToContainer(temp_device_property_container, *p, p->name(), p->isWriteableByCloud() ? Permission::ReadWrite : Permission::Read);
787+
sendPropertyContainerToCloud(_deviceTopicOut, temp_device_property_container, last_device_property_index);
788+
}
833789
}
834790
#endif
835791

src/ArduinoIoTCloudTCP.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
193193

194194
#if OTA_ENABLED
195195
void onOTARequest();
196-
void sendClearedOTARequestToCloud();
197-
void sendOTAErrorToCloud();
198-
void sendOTAUrlToCloud();
196+
void sendDevicePropertyToCloud(String const name);
199197
#endif
200198

201199
void updateThingTopics();

0 commit comments

Comments
 (0)