in source/shadow/ConfigShadow.cpp [396:473]
void ConfigShadow::reconfigureWithConfigShadow(
std::shared_ptr<SharedCrtResourceManager> resourceManager,
PlainConfig &config)
{
IotShadowClient iotShadowClient(resourceManager.get()->getConnection());
thingName = *config.thingName;
if (!subscribeGetAndUpdateNamedShadowTopics(iotShadowClient))
{
LOGM_ERROR(
TAG, "Encounter error while subscribing pertinent %s shadow topic from cloud", DEFAULT_CONFIG_SHADOW_NAME);
return;
}
if (!fetchRemoteConfigShadow(iotShadowClient))
{
LOGM_ERROR(
TAG, "Encounter error while fetching device client config shadow from cloud", DEFAULT_CONFIG_SHADOW_NAME);
return;
}
auto futureConfigShadowExistsPromise = configShadowExistsPromise.get_future();
if (futureConfigShadowExistsPromise.wait_for(std::chrono::seconds(DEFAULT_WAIT_TIME_SECONDS)) ==
future_status::timeout)
{
LOGM_ERROR(
TAG,
"Waiting for %s getNamedShadow response time out or get unexpected error from service",
DEFAULT_CONFIG_SHADOW_NAME);
return;
}
if (futureConfigShadowExistsPromise.get() && configDelta.has_value())
{
// config shadow and delta exists, resetting the local config first and then updating config shaodw
if (configDelta)
{
LOG_INFO(
TAG, "Detect the delta of configuration in the config shadow, reconfiguring the device client now.");
if (!desiredConfig.has_value())
{
LOG_ERROR(
TAG, "Fail to fetch the desired value in config shaodw, aborting the configuration update now");
return;
}
JsonObject configDesiredObject = configDelta.value();
if (!configDesiredObject.WasParseSuccessful())
{
LOGM_ERROR(
TAG,
"Couldn't parse JSON config desired. GetErrorMessage returns: %s",
configDesiredObject.GetErrorMessage().c_str());
return;
}
JsonObject configDeltaObject = configDelta.value();
if (!configDeltaObject.WasParseSuccessful())
{
LOGM_ERROR(
TAG,
"Couldn't parse JSON config delta. GetErrorMessage returns: %s",
configDeltaObject.GetErrorMessage().c_str());
return;
}
JsonView desiredJsonView = desiredConfig->View();
JsonView deltaJsonView = configDelta->View();
resetClientConfigWithJSON(config, deltaJsonView, desiredJsonView);
}
updateShadowWithLocalConfig(iotShadowClient, config);
}
else
{
// config shadow doesn't exists, store the device client configuration into config shadow
updateShadowWithLocalConfig(iotShadowClient, config);
}
}