use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class JobClient method queryJobResponse.
/**
* Query the iot hub for a jobs response. Query response are limited by page size per attempt
*
* @param jobType The type of job to query for
* @param jobStatus The status of the job to query for
* @param pageSize The value to which to limit the job response size by
* @return A query object on which to look for responses by
* @throws IOException If any of the input parameters are incorrect
* @throws IotHubException If IotHub failed to respond
*/
public synchronized Query queryJobResponse(JobType jobType, JobStatus jobStatus, Integer pageSize) throws IOException, IotHubException {
if (pageSize <= 0) {
throw new IllegalArgumentException("pagesize cannot be negative or zero");
}
Query jobResponseQuery = new Query(pageSize, QueryType.JOB_RESPONSE);
String jobTypeString = (jobType == null) ? null : jobType.toString();
String jobStatusString = (jobStatus == null) ? null : jobStatus.toString();
ProxyOptions proxyOptions = options.getProxyOptions();
Proxy proxy = proxyOptions != null ? proxyOptions.getProxy() : null;
jobResponseQuery.sendQueryRequest(this.credentialCache, this.azureSasCredential, this.iotHubConnectionString, IotHubConnectionString.getUrlQuery(this.hostName, jobTypeString, jobStatusString), HttpMethod.GET, options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
return jobResponseQuery;
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class JobClientTests method queryJobResponseResult.
// Since this is a helper method, the params can be passed any value.
@SuppressWarnings("SameParameterValue")
private JobResult queryJobResponseResult(String jobId, JobType jobType, JobStatus jobStatus) throws IOException, IotHubException {
Query query = jobClient.queryJobResponse(jobType, jobStatus);
JobResult jobResult;
while (jobClient.hasNextJob(query)) {
jobResult = jobClient.getNextJob(query);
if (jobResult.getJobId().equals(jobId) && (jobResult.getJobType() == jobType) && (jobResult.getJobStatus() == jobStatus)) {
log.info("Iothub confirmed {} {} for type {}", jobId, jobStatus, jobType);
return jobResult;
}
}
throw new AssertionError("queryDeviceJob did not find the job");
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class JobClientTests method queryDeviceJobResult.
// Since this is a helper method, the params can be passed any value.
@SuppressWarnings("SameParameterValue")
private static JobResult queryDeviceJobResult(String jobId, JobType jobType, JobStatus jobStatus) throws IOException, IotHubException {
String queryContent = SqlQuery.createSqlQuery("*", SqlQuery.FromType.JOBS, "devices.jobs.jobId = '" + jobId + "' and devices.jobs.jobType = '" + jobType.toString() + "'", null).getQuery();
Query query = jobClient.queryDeviceJob(queryContent);
JobResult jobResult;
while (jobClient.hasNextJob(query)) {
jobResult = jobClient.getNextJob(query);
if (jobResult.getJobId().equals(jobId)) {
if (jobResult.getJobType() == jobType) {
if (jobResult.getJobStatus() == jobStatus) {
// query confirmed that the specified job has the correct type, and status
return jobResult;
} else {
throw new AssertionError("queryDeviceJob received job unexpected status. Expected " + jobStatus + " but job ended with status " + jobResult.getJobStatus());
}
} else {
throw new AssertionError("queryDeviceJob received job with the wrong job type. Expected " + jobType + " but found " + jobResult.getJobType());
}
}
}
throw new AssertionError("queryDeviceJob did not find the job");
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class QueryTwinTests method testQueryTwin.
public void testQueryTwin() throws InterruptedException, ModuleClientException, IOException, GeneralSecurityException, IotHubException, URISyntaxException {
addMultipleDevices(MAX_DEVICES, false);
// Add same desired on multiple devices
final String queryProperty = PROPERTY_KEY_QUERY + UUID.randomUUID().toString();
final String queryPropertyValue = PROPERTY_VALUE_QUERY + UUID.randomUUID().toString();
setDesiredProperties(queryProperty, queryPropertyValue, MAX_DEVICES);
// Query multiple devices having same property
final String where = "is_defined(properties.desired." + queryProperty + ")";
SqlQuery sqlQuery = SqlQuery.createSqlQuery("*", SqlQuery.FromType.DEVICES, where, null);
Thread.sleep(MAXIMUM_TIME_FOR_IOTHUB_PROPAGATION_BETWEEN_DEVICE_SERVICE_CLIENTS);
Query twinQuery = testInstance.twinServiceClient.queryTwin(sqlQuery.getQuery(), PAGE_SIZE);
for (int i = 0; i < MAX_DEVICES; i++) {
if (testInstance.twinServiceClient.hasNextDeviceTwin(twinQuery)) {
DeviceTwinDevice d = testInstance.twinServiceClient.getNextDeviceTwin(twinQuery);
assertNotNull(d.getVersion());
assertEquals(TwinConnectionState.DISCONNECTED.toString(), d.getConnectionState());
for (Pair dp : d.getDesiredProperties()) {
Assert.assertEquals("Unexpected desired property key, expected " + queryProperty + " but was " + dp.getKey(), queryProperty, dp.getKey());
Assert.assertEquals("Unexpected desired property value, expected " + queryPropertyValue + " but was " + dp.getValue(), queryPropertyValue, dp.getValue());
}
}
}
assertFalse(testInstance.twinServiceClient.hasNextDeviceTwin(twinQuery));
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class QueryTwinTests method testRawQueryTwin.
public void testRawQueryTwin() throws IOException, InterruptedException, IotHubException, GeneralSecurityException, URISyntaxException, ModuleClientException {
addMultipleDevices(MAX_DEVICES, false);
Gson gson = new GsonBuilder().enableComplexMapKeySerialization().serializeNulls().create();
// Add same desired on multiple devices
final String queryProperty = PROPERTY_KEY_QUERY + UUID.randomUUID().toString();
final String queryPropertyValue = PROPERTY_VALUE_QUERY + UUID.randomUUID().toString();
final int expectedNumberOfDevices = MAX_DEVICES;
setDesiredProperties(queryProperty, queryPropertyValue, MAX_DEVICES);
Thread.sleep(DESIRED_PROPERTIES_PROPAGATION_TIME_MILLISECONDS);
// Raw Query for multiple devices having same property
final String select = "properties.desired." + queryProperty + " AS " + queryProperty + "," + " COUNT() AS numberOfDevices";
final String groupBy = "properties.desired." + queryProperty;
final SqlQuery sqlQuery = SqlQuery.createSqlQuery(select, SqlQuery.FromType.DEVICES, null, groupBy);
boolean querySucceeded = false;
long startTime = System.currentTimeMillis();
while (!querySucceeded) {
Query rawTwinQuery = testInstance.rawTwinQueryClient.query(sqlQuery.getQuery(), PAGE_SIZE);
while (testInstance.rawTwinQueryClient.hasNext(rawTwinQuery)) {
String result = testInstance.rawTwinQueryClient.next(rawTwinQuery);
assertNotNull(result);
Map map = gson.fromJson(result, Map.class);
if (map.containsKey("numberOfDevices") && map.containsKey(queryProperty)) {
// Casting as a double first to get the value from the map, but then casting to an int because the
// number of devices should always be an integer
int actualNumberOfDevices = (int) (double) map.get("numberOfDevices");
if (actualNumberOfDevices == expectedNumberOfDevices) {
// Due to propagation delays, there will be times when the query is executed and only a
// subset of the expected devices are queryable. This test will loop until all of them are queryable
// to avoid this issue.
querySucceeded = true;
} else {
log.info("Expected device count not correct, re-running query");
Thread.sleep(200);
}
}
}
if (System.currentTimeMillis() - startTime > QUERY_TIMEOUT_MILLISECONDS) {
fail("Timed out waiting for query results to match expectations");
}
}
}
Aggregations