From 22ae8b80d71ee6592c7b6b7249c6dc33a230c1ce Mon Sep 17 00:00:00 2001 From: Oguz Bastemur Date: Fri, 21 Jun 2019 11:16:17 -0700 Subject: [PATCH] esp8266: split twin callback for device settings and ease ESP WD --- ESP8266/src/iotc/arduino/iotc.cpp | 4 +++- ESP8266/src/iotc/common/iotc_internal.cpp | 22 ++++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/ESP8266/src/iotc/arduino/iotc.cpp b/ESP8266/src/iotc/arduino/iotc.cpp index 16b5900..db31526 100644 --- a/ESP8266/src/iotc/arduino/iotc.cpp +++ b/ESP8266/src/iotc/arduino/iotc.cpp @@ -213,13 +213,15 @@ int getHubHostName(IOTContextInternal* internal, const char* dpsEndpoint, int retval = 0; if ((retval = _getOperationId(internal, dpsEndpoint, scopeId, deviceId, *authHeader, *operationId, NULL)) == 0) { - WAITMS(5000); + WAITMS(2500); IOTC_LOG(F("- iotc.dps : getting host name...")); + WAITMS(2500); for (int i = 0; i < 5; i++) { retval = _getOperationId(internal, dpsEndpoint, scopeId, deviceId, *authHeader, *operationId, hostName); if (retval == 0) break; WAITMS(3000); + IOTC_LOG(F("- iotc.dps : getting host name...")); // re-trigger WD } } diff --git a/ESP8266/src/iotc/common/iotc_internal.cpp b/ESP8266/src/iotc/common/iotc_internal.cpp index 431961f..30ca463 100644 --- a/ESP8266/src/iotc/common/iotc_internal.cpp +++ b/ESP8266/src/iotc/common/iotc_internal.cpp @@ -334,19 +334,25 @@ static void deviceTwinGetStateCallback(AzureIOT::StringBuffer &topicName, return; } - jsobject_t desired; + jsobject_t desired, outDesired, outReported; jsobject_initialize(&desired, *payload, payload.getLength()); - for (unsigned i = 0, count = jsobject_get_count(&desired); i < count; - i += 2) { - char *itemName = jsobject_get_name_at(&desired, i); - if (itemName != NULL && itemName[0] != '$') { - callDesiredCallback(internal, topicName, itemName, payload); + if (jsobject_get_object_by_name(&desired, "desired", &outDesired) != -1 && + jsobject_get_object_by_name(&desired, "reported", &outReported) != -1) { + callDesiredCallback(internal, topicName, "twin", payload); + } else { + for (unsigned i = 0, count = jsobject_get_count(&desired); i < count; + i += 2) { + char *itemName = jsobject_get_name_at(&desired, i); + if (itemName != NULL && itemName[0] != '$') { + callDesiredCallback(internal, topicName, itemName, payload); + } + if (itemName) IOTC_FREE(itemName); } - if (itemName) IOTC_FREE(itemName); } - + jsobject_free(&outReported); jsobject_free(&desired); + jsobject_free(&outDesired); } void handlePayload(char *msg, unsigned long msg_length, char *topic,