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...");
}
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();
}
}
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();
}
}
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();
}
}
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);
}
Aggregations