use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class SendMessagesIT method SendMessagesOverHttps.
@Test
public void SendMessagesOverHttps() throws URISyntaxException, IOException {
String messageString = "Java client e2e test message over Https protocol";
Message msg = new Message(messageString);
DeviceClient client = new DeviceClient(DeviceConnectionString.get(iotHubConnectionString, deviceHttps), IotHubClientProtocol.HTTPS);
client.open();
for (int i = 0; i < NUM_MESSAGES_PER_CONNECTION; ++i) {
try {
Success messageSent = new Success();
EventCallback callback = new EventCallback();
client.sendEventAsync(msg, callback, messageSent);
Integer waitDuration = 0;
while (!messageSent.getResult()) {
Thread.sleep(RETRY_MILLISECONDS);
if ((waitDuration += RETRY_MILLISECONDS) > SEND_TIMEOUT_MILLISECONDS) {
break;
}
}
if (!messageSent.getResult()) {
Assert.fail("Sending message over HTTPS protocol failed");
}
} catch (Exception e) {
Assert.fail("Sending message over HTTPS protocol failed");
}
}
client.closeNow();
}
use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class SendMessagesIT method SendMessagesOverAmqps.
@Test
public void SendMessagesOverAmqps() throws URISyntaxException, IOException, InterruptedException {
String messageString = "Java client e2e test message over Amqps protocol";
Message msg = new Message(messageString);
DeviceClient client = new DeviceClient(DeviceConnectionString.get(iotHubConnectionString, deviceAmqps), IotHubClientProtocol.AMQPS);
client.open();
for (int i = 0; i < NUM_MESSAGES_PER_CONNECTION; ++i) {
try {
Success messageSent = new Success();
EventCallback callback = new EventCallback();
client.sendEventAsync(msg, callback, messageSent);
Integer waitDuration = 0;
while (!messageSent.getResult()) {
Thread.sleep(RETRY_MILLISECONDS);
if ((waitDuration += RETRY_MILLISECONDS) > SEND_TIMEOUT_MILLISECONDS) {
break;
}
}
if (!messageSent.getResult()) {
Assert.fail("Sending message over AMQPS protocol failed");
}
} catch (Exception e) {
Assert.fail("Sending message over AMQPS protocol failed");
}
}
client.closeNow();
}
use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class TokenCredentialTests method getDigitalTwinWithTokenCredential.
@Test
public void getDigitalTwinWithTokenCredential() throws IOException, IotHubException, URISyntaxException {
// only run tests for standard tier hubs
Assume.assumeFalse(isBasicTierHub);
RegistryManager registryManager = new RegistryManager(iotHubConnectionString);
DeviceClient deviceClient = createDeviceClient(MQTT, registryManager);
deviceClient.open();
// arrange
DigitalTwinClient digitalTwinClient = buildDigitalTwinClientWithTokenCredential();
// act
BasicDigitalTwin response = digitalTwinClient.getDigitalTwin(deviceClient.getConfig().getDeviceId(), BasicDigitalTwin.class);
ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> responseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(deviceClient.getConfig().getDeviceId(), BasicDigitalTwin.class);
// assert
assertEquals(response.getMetadata().getModelId(), THERMOSTAT_MODEL_ID);
assertEquals(responseWithHeaders.body().getMetadata().getModelId(), THERMOSTAT_MODEL_ID);
}
use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class TokenCredentialTests method createDeviceClient.
private DeviceClient createDeviceClient(IotHubClientProtocol protocol, RegistryManager registryManager) throws IOException, IotHubException, URISyntaxException {
ClientOptions options = new ClientOptions();
options.setModelId(THERMOSTAT_MODEL_ID);
String deviceId = "some-device-" + UUID.randomUUID();
Device device = Device.createDevice(deviceId, AuthenticationType.SAS);
Device registeredDevice = registryManager.addDevice(device);
String deviceConnectionString = registryManager.getDeviceConnectionString(registeredDevice);
return new DeviceClient(deviceConnectionString, protocol, options);
}
use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class TokenCredentialTests method invokeMethodSucceedWithTokenCredential.
@Test
public void invokeMethodSucceedWithTokenCredential() throws Exception {
// only run tests for standard tier hubs
Assume.assumeFalse(isBasicTierHub);
DeviceMethod methodServiceClient = buildDeviceMethodClientWithTokenCredential();
RegistryManager registryManager = new RegistryManager(iotHubConnectionString);
Device device = Device.createDevice("some-device-" + UUID.randomUUID(), AuthenticationType.SAS);
registryManager.addDevice(device);
DeviceClient deviceClient = new DeviceClient(registryManager.getDeviceConnectionString(device), MQTT);
deviceClient.open();
final int successStatusCode = 200;
final AtomicBoolean methodsSubscriptionComplete = new AtomicBoolean(false);
final AtomicBoolean methodsSubscribedSuccessfully = new AtomicBoolean(false);
deviceClient.subscribeToDeviceMethod((methodName, methodData, context) -> new DeviceMethodData(successStatusCode, "success"), null, (responseStatus, callbackContext) -> {
if (responseStatus == IotHubStatusCode.OK_EMPTY || responseStatus == IotHubStatusCode.OK) {
methodsSubscribedSuccessfully.set(true);
}
methodsSubscriptionComplete.set(true);
}, null);
long startTime = System.currentTimeMillis();
while (!methodsSubscriptionComplete.get()) {
Thread.sleep(200);
if (System.currentTimeMillis() - startTime > METHOD_SUBSCRIPTION_TIMEOUT_MILLISECONDS) {
fail("Timed out waiting for device registration to complete.");
}
}
assertTrue("Method subscription callback fired with non 200 status code", methodsSubscribedSuccessfully.get());
MethodResult result = methodServiceClient.invoke(device.getDeviceId(), "someMethod", 5l, 5l, null);
assertEquals((long) successStatusCode, (long) result.getStatus());
}
Aggregations