in iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/twin/DeviceTwin.java [116:211]
public void updateReportedPropertiesAsync(
TwinCollection reportedProperties,
ReportedPropertiesUpdateCorrelatingMessageCallback reportedPropertiesUpdateCorrelatingMessageCallback,
Object callbackContext)
{
if (desiredPropertiesCallback == null)
{
throw new IllegalStateException("Must subscribe to desired properties before sending reported properties.");
}
Objects.requireNonNull(reportedProperties, "Reported properties cannot be null");
String serializedReportedProperties = reportedProperties.toJsonElement().toString();
IotHubTransportMessage updateReportedPropertiesRequest = new IotHubTransportMessage(serializedReportedProperties.getBytes(StandardCharsets.UTF_8), MessageType.DEVICE_TWIN);
updateReportedPropertiesRequest.setConnectionDeviceId(this.client.getConfig().getDeviceId());
// MQTT does not have the concept of correlationId for request/response handling but it does have a requestId
// To handle this we are setting the correlationId to the requestId to better handle correlation
// whether we use MQTT or AMQP.
updateReportedPropertiesRequest.setRequestId(UUID.randomUUID().toString());
updateReportedPropertiesRequest.setCorrelationId(updateReportedPropertiesRequest.getRequestId());
if (reportedProperties.getVersion() != null)
{
updateReportedPropertiesRequest.setVersion(reportedProperties.getVersion());
}
updateReportedPropertiesRequest.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST);
MessageSentCallback messageSentCallback = (statusCode, exception, context) ->
{
// no action needed here. The correlating message callback will handle the various message state callbacks including this one
};
updateReportedPropertiesRequest.setCorrelatingMessageCallback(new CorrelatingMessageCallback()
{
@Override
public void onRequestQueued(Message message, Object callbackContext)
{
if (reportedPropertiesUpdateCorrelatingMessageCallback != null)
{
reportedPropertiesUpdateCorrelatingMessageCallback.onRequestQueued(message, callbackContext);
}
}
@Override
public void onRequestSent(Message message, Object callbackContext)
{
if (reportedPropertiesUpdateCorrelatingMessageCallback != null)
{
reportedPropertiesUpdateCorrelatingMessageCallback.onRequestSent(message, callbackContext);
}
}
@Override
public void onRequestAcknowledged(Message message, Object callbackContext, IotHubClientException e)
{
if (reportedPropertiesUpdateCorrelatingMessageCallback != null)
{
reportedPropertiesUpdateCorrelatingMessageCallback.onRequestAcknowledged(message, callbackContext, e);
}
}
@Override
public void onResponseReceived(Message message, Object callbackContext, IotHubClientException e)
{
IotHubTransportMessage dtMessage = (IotHubTransportMessage) message;
String status = dtMessage.getStatus();
IotHubStatusCode iotHubStatus = IotHubStatusCode.ERROR;
if (status != null)
{
iotHubStatus = IotHubStatusCode.getIotHubStatusCode(Integer.parseInt(status));
}
if (reportedPropertiesUpdateCorrelatingMessageCallback != null)
{
log.trace("Executing twin status callback for device operation twin update reported properties response with status " + iotHubStatus);
reportedPropertiesUpdateCorrelatingMessageCallback.onResponseReceived(message, callbackContext, iotHubStatus, new ReportedPropertiesUpdateResponse(dtMessage.getVersion()), e);
}
}
@Override
public void onResponseAcknowledged(Message message, Object callbackContext)
{
if (reportedPropertiesUpdateCorrelatingMessageCallback != null)
{
reportedPropertiesUpdateCorrelatingMessageCallback.onResponseAcknowledged(message, callbackContext);
}
}
});
updateReportedPropertiesRequest.setCorrelatingMessageCallbackContext(callbackContext);
this.client.sendEventAsync(updateReportedPropertiesRequest, messageSentCallback, callbackContext);
}