Search in sources :

Example 56 with Query

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;
}
Also used : Proxy(java.net.Proxy) Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString)

Example 57 with Query

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");
}
Also used : SqlQuery(com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery) Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult)

Example 58 with Query

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");
}
Also used : SqlQuery(com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery) Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString)

Example 59 with Query

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));
}
Also used : SqlQuery(com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) SqlQuery(com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery) Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair)

Example 60 with Query

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");
        }
    }
}
Also used : SqlQuery(com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery) SqlQuery(com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery) Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Map(java.util.Map)

Aggregations

Query (com.microsoft.azure.sdk.iot.service.devicetwin.Query)63 Test (org.junit.Test)51 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)49 SqlQuery (com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery)34 HashMap (java.util.HashMap)17 NonStrictExpectations (mockit.NonStrictExpectations)17 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)10 JobClient (com.microsoft.azure.sdk.iot.service.jobs.JobClient)10 RawTwinQuery (com.microsoft.azure.sdk.iot.service.devicetwin.RawTwinQuery)8 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)8 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)6 HttpRequest (com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)6 JobResult (com.microsoft.azure.sdk.iot.service.jobs.JobResult)4 IOException (java.io.IOException)3 NoSuchElementException (java.util.NoSuchElementException)3 Verifications (mockit.Verifications)3 TwinCollection (com.microsoft.azure.sdk.iot.deps.twin.TwinCollection)2 ProxyOptions (com.microsoft.azure.sdk.iot.service.ProxyOptions)2 JobClientOptions (com.microsoft.azure.sdk.iot.service.jobs.JobClientOptions)2 Proxy (java.net.Proxy)2