Search in sources :

Example 1 with IotHubException

use of com.microsoft.azure.sdk.iot.service.exceptions.IotHubException 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 2 with IotHubException

use of com.microsoft.azure.sdk.iot.service.exceptions.IotHubException in project azure-iot-sdk-java by Azure.

the class DeviceManagerSample method RemoveDevice.

private static void RemoveDevice() throws Exception {
    RegistryManager registryManager = RegistryManager.createFromConnectionString(SampleUtils.iotHubConnectionString);
    try {
        registryManager.removeDevice(SampleUtils.deviceId);
        System.out.println("Device removed: " + SampleUtils.deviceId);
    } catch (IotHubException iote) {
        iote.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)

Example 3 with IotHubException

use of com.microsoft.azure.sdk.iot.service.exceptions.IotHubException in project azure-iot-sdk-java by Azure.

the class DeviceManagerSample method AddDevice.

private static void AddDevice() throws Exception {
    RegistryManager registryManager = RegistryManager.createFromConnectionString(SampleUtils.iotHubConnectionString);
    Device device = Device.createFromId(SampleUtils.deviceId, null, null);
    try {
        device = registryManager.addDevice(device);
        System.out.println("Device created: " + device.getDeviceId());
        System.out.println("Device key: " + device.getPrimaryKey());
    } catch (IotHubException iote) {
        iote.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)

Example 4 with IotHubException

use of com.microsoft.azure.sdk.iot.service.exceptions.IotHubException in project azure-iot-sdk-java by Azure.

the class DeviceManagerSample method UpdateDevice.

private static void UpdateDevice() throws Exception {
    String primaryKey = "[New primary key goes here]";
    String secondaryKey = "[New secondary key goes here]";
    RegistryManager registryManager = RegistryManager.createFromConnectionString(SampleUtils.iotHubConnectionString);
    Device device = Device.createFromId(SampleUtils.deviceId, null, null);
    device.getSymmetricKey().setPrimaryKey(primaryKey);
    device.getSymmetricKey().setSecondaryKey(secondaryKey);
    try {
        device = registryManager.updateDevice(device);
        System.out.println("Device updated: " + device.getDeviceId());
        System.out.println("Device primary key: " + device.getPrimaryKey());
        System.out.println("Device secondary key: " + device.getSecondaryKey());
    } catch (IotHubException iote) {
        iote.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)

Example 5 with IotHubException

use of com.microsoft.azure.sdk.iot.service.exceptions.IotHubException in project azure-iot-sdk-java by Azure.

the class DeviceTwinTest method updateTwinThrowsVerificationThrows.

@Test(expected = IotHubException.class)
public void updateTwinThrowsVerificationThrows() throws Exception {
    //arrange
    final String connectionString = "testString";
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    Map<String, Object> testMap = new HashMap<>();
    testMap.put("TestKey", "TestValue");
    new NonStrictExpectations() {

        {
            mockedDevice.getDeviceId();
            result = "SomeDevID";
            Deencapsulation.invoke(mockedDevice, "getDesiredMap");
            result = testMap;
            Deencapsulation.invoke(mockedDevice, "getTagsMap");
            result = testMap;
            Deencapsulation.invoke(mockedDevice, "getTwinParser");
            result = mockedTwinParser;
            mockedTwinParser.updateTwin((Map<String, Object>) any, null, (Map<String, Object>) any);
            result = "SomeJsonString";
            IotHubExceptionManager.httpResponseVerification(mockedHttpResponse);
            result = new IotHubException();
        }
    };
    //act
    testTwin.updateTwin(mockedDevice);
}
Also used : HashMap(java.util.HashMap) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) NonStrictExpectations(mockit.NonStrictExpectations) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Aggregations

IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)11 IOException (java.io.IOException)5 Test (org.junit.Test)5 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)4 Device (com.microsoft.azure.sdk.iot.service.Device)3 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)3 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)3 HashMap (java.util.HashMap)3 DeviceMethod (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod)2 IotHubExceptionManager (com.microsoft.azure.sdk.iot.service.exceptions.IotHubExceptionManager)2 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)2 List (java.util.List)2 NonStrictExpectations (mockit.NonStrictExpectations)2 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)1 MethodResult (com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult)1 Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)1 HttpMethod (com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1