Skip to content

Commit 98ed735

Browse files
committed
Use a single function to keep OTA properties in sync with cloud
1 parent 92c9079 commit 98ed735

File tree

2 files changed

+13
-59
lines changed

2 files changed

+13
-59
lines changed

src/ArduinoIoTCloudTCP.cpp

+12-56
Original file line numberDiff line numberDiff line change
@@ -661,18 +661,18 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
661661
/* Clear the request flag. */
662662
_ota_req = false;
663663
/* Transmit the cleared request flags to the cloud. */
664-
sendClearedOTARequestToCloud();
664+
sendDevicePropertyToCloud("OTA_REQ");
665665
/* Call member function to handle OTA request. */
666666
onOTARequest();
667667
/* If something fails send the OTA error to the cloud */
668-
sendOTAErrorToCloud();
668+
sendDevicePropertyToCloud("OTA_ERROR");
669669
}
670670
}
671671

672672
/* Check if we have received the OTA_URL property and provide
673673
* echo to the cloud.
674674
*/
675-
sendOTAUrlToCloud();
675+
sendDevicePropertyToCloud("OTA_URL");
676676

677677
#endif /* OTA_ENABLED */
678678

@@ -782,61 +782,17 @@ void ArduinoIoTCloudTCP::sendDevicePropertiesToCloud()
782782
}
783783

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

839-
sendPropertyContainerToCloud(_deviceTopicOut, ota_property_container, last_ota_property_index);
790+
Property* p = getProperty(this->_device_property_container, name);
791+
if(p != nullptr)
792+
{
793+
addPropertyToContainer(temp_device_property_container, *p, p->name(), p->isWriteableByCloud() ? Permission::ReadWrite : Permission::Read);
794+
sendPropertyContainerToCloud(_deviceTopicOut, temp_device_property_container, last_device_property_index);
795+
}
840796
}
841797
#endif
842798

src/ArduinoIoTCloudTCP.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
195195

196196
#if OTA_ENABLED
197197
void onOTARequest();
198-
void sendClearedOTARequestToCloud();
199-
void sendOTAErrorToCloud();
200-
void sendOTAUrlToCloud();
198+
void sendDevicePropertyToCloud(String const name);
201199
#endif
202200

203201
void updateThingTopics();

0 commit comments

Comments
 (0)