Search in sources :

Example 36 with TwinState

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);
}
Also used : TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState) Proxy(java.net.Proxy) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) URL(java.net.URL)

Aggregations

TwinState (com.microsoft.azure.sdk.iot.deps.twin.TwinState)36 Test (org.junit.Test)31 Date (java.util.Date)27 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)15 JobResult (com.microsoft.azure.sdk.iot.service.jobs.JobResult)13 TwinCollection (com.microsoft.azure.sdk.iot.deps.twin.TwinCollection)12 NonStrictExpectations (mockit.NonStrictExpectations)12 JobsParser (com.microsoft.azure.sdk.iot.deps.serializer.JobsParser)8 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)8 Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)8 JobClient (com.microsoft.azure.sdk.iot.service.jobs.JobClient)8 HashSet (java.util.HashSet)8 Expectations (mockit.Expectations)5 Verifications (mockit.Verifications)5 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)4 Proxy (java.net.Proxy)3 ProxyOptions (com.microsoft.azure.sdk.iot.service.ProxyOptions)2 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)2 URL (java.net.URL)2 SimpleDateFormat (java.text.SimpleDateFormat)2