Search in sources :

Example 21 with DeviceTwinDevice

use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice in project azure-iot-sdk-java by Azure.

the class DeviceTwinDeviceTest method getDesiredReturnsEmptyIfNullMap.

/*
    Tests_SRS_DEVICETWINDEVICE_25_014: [ If the desiredProperties map is null then this method shall return empty set of pairs.]
     */
@Test
public void getDesiredReturnsEmptyIfNullMap() {
    //arrange
    DeviceTwinDevice testDevice = new DeviceTwinDevice("testDevice");
    //act
    Set<Pair> actualDesProp = testDevice.getDesiredProperties();
    //assert
    assertNotNull(actualDesProp);
    assertTrue(actualDesProp.size() == 0);
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair) Test(org.junit.Test)

Example 22 with DeviceTwinDevice

use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice in project azure-iot-sdk-java by Azure.

the class DeviceTwinDeviceTest method setDesiredThrowsIfNullInput.

/*
    **Tests_SRS_DEVICETWINDEVICE_25_012: [** If the desiredProperties Set is null then this method shall throw IllegalArgumentException.**]**
     */
@Test(expected = IllegalArgumentException.class)
public void setDesiredThrowsIfNullInput() {
    //arrange
    DeviceTwinDevice testDevice = new DeviceTwinDevice("testDevice");
    Set<Pair> testDesProp = null;
    //act
    testDevice.setDesiredProperties(testDesProp);
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair) Test(org.junit.Test)

Example 23 with DeviceTwinDevice

use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice in project azure-iot-sdk-java by Azure.

the class DeviceTwinDeviceTest method constructorCreatesNewDevice.

/*
    **Tests_SRS_DEVICETWINDEVICE_25_003: [** The constructor shall create a new instance of twin object for this device and store the device id.**]**
     */
@Test
public void constructorCreatesNewDevice() {
    //act
    DeviceTwinDevice testDevice = new DeviceTwinDevice("testDevice");
    //assert
    assertNotNull(testDevice);
    Map<String, Object> tagsMap = Deencapsulation.getField(testDevice, "tag");
    assertNull(tagsMap);
    Map<String, Object> repPropMap = Deencapsulation.getField(testDevice, "reportedProperties");
    assertNull(repPropMap);
    Map<String, Object> desPropMap = Deencapsulation.getField(testDevice, "reportedProperties");
    assertNull(desPropMap);
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) Test(org.junit.Test)

Example 24 with DeviceTwinDevice

use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice in project azure-iot-sdk-java by Azure.

the class DeviceTwinSample method main.

/**
     * @param args
     * @throws IOException
     * @throws URISyntaxException
     */
public static void main(String[] args) throws Exception {
    System.out.println("Starting sample...");
    DeviceTwin twinClient = DeviceTwin.createFromConnectionString(iotHubConnectionString);
    DeviceTwinDevice device = new DeviceTwinDevice(deviceId);
    try {
        // Manage complete twin
        System.out.println("Getting device twin");
        twinClient.getTwin(device);
        System.out.println(device);
        //Update Twin Tags and Desired Properties
        Set<Pair> tags = new HashSet<Pair>();
        tags.add(new Pair("HomeID", UUID.randomUUID()));
        device.setTags(tags);
        Set<Pair> desProp = new HashSet<Pair>();
        int temp = new Random().nextInt(100);
        desProp.add(new Pair("temp", temp));
        device.setDesiredProperties(desProp);
        System.out.println("Updating device twin");
        twinClient.updateTwin(device);
        System.out.println("Getting device twin");
        twinClient.getTwin(device);
        System.out.println(device);
    } catch (IotHubException e) {
        System.out.println(e.getMessage());
    }
    System.out.println("Shutting down sample...");
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) Random(java.util.Random) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) HashSet(java.util.HashSet) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair)

Example 25 with DeviceTwinDevice

use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice in project azure-iot-sdk-java by Azure.

the class DeviceTwinIT method setUpTwin.

private void setUpTwin(DeviceState deviceState) throws IOException, URISyntaxException, IotHubException, InterruptedException {
    /*
            Because of the bug in device client on MQTT, we cannot have multiple device client open
            in the main thread at the same time. Hence restricting to use single device client.
         */
    if (deviceClient == null) {
        deviceState.dCDeviceForTwin = new DeviceExtension();
        deviceClient = new DeviceClient(DeviceConnectionString.get(iotHubConnectionString, deviceState.sCDeviceForRegistryManager), IotHubClientProtocol.MQTT);
        deviceClient.open();
        deviceClient.startDeviceTwin(new DeviceTwinStatusCallBack(), deviceState, deviceState.dCDeviceForTwin, deviceState);
        deviceState.deviceTwinStatus = STATUS.SUCCESS;
    }
    // set up twin on ServiceClient
    if (sCDeviceTwin != null) {
        deviceState.sCDeviceForTwin = new DeviceTwinDevice(deviceState.sCDeviceForRegistryManager.getDeviceId());
        sCDeviceTwin.getTwin(deviceState.sCDeviceForTwin);
        Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB);
    }
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient)

Aggregations

DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)35 Test (org.junit.Test)33 Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)20 HashMap (java.util.HashMap)12 HashSet (java.util.HashSet)10 Map (java.util.Map)6 TwinParser (com.microsoft.azure.sdk.iot.deps.serializer.TwinParser)2 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)1 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)1 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)1 Random (java.util.Random)1