use of com.microsoft.azure.sdk.iot.deps.twin.TwinState in project azure-iot-sdk-java by Azure.
the class DeviceTwin method updateTwin.
/**
* This method updates device twin for the specified device.
* <p>This API uses the IoT Hub PATCH API when sending updates, but it sends the full twin with each patch update.
* As a result, devices subscribed to twin will receive notifications that each property is changed when this API is
* called, even if only some of the properties were changed.</p>
* <p>See <a href="https://docs.microsoft.com/en-us/rest/api/iothub/service/devices/updatetwin">PATCH</a> for
* more details.</p>
*
* @param device The device with a valid Id for which device twin is to be updated.
* @throws IOException This exception is thrown if the IO operation failed.
* @throws IotHubException This exception is thrown if the response verification failed.
*/
public synchronized void updateTwin(DeviceTwinDevice device) throws IotHubException, IOException {
if (device == null || device.getDeviceId() == null || device.getDeviceId().length() == 0) {
throw new IllegalArgumentException("Instantiate a device and set device Id to be used.");
}
if ((device.getDesiredMap() == null || device.getDesiredMap().isEmpty()) && (device.getTagsMap() == null || device.getTagsMap().isEmpty())) {
throw new IllegalArgumentException("Set either desired properties or tags for the device to be updated.");
}
URL url;
if ((device.getModuleId() == null) || device.getModuleId().length() == 0) {
url = IotHubConnectionString.getUrlTwin(this.hostName, device.getDeviceId());
} else {
url = IotHubConnectionString.getUrlModuleTwin(this.hostName, device.getDeviceId(), device.getModuleId());
}
TwinState twinState = new TwinState(device.getTagsMap(), device.getDesiredMap(), null);
String twinJson = twinState.toJsonElement().toString();
ProxyOptions proxyOptions = options.getProxyOptions();
Proxy proxy = proxyOptions != null ? proxyOptions.getProxy() : null;
DeviceOperations.request(this.getAuthenticationToken(), url, HttpMethod.PATCH, twinJson.getBytes(StandardCharsets.UTF_8), String.valueOf(requestId++), options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
}
Aggregations