Search in sources :

Example 36 with JobResult

use of com.microsoft.azure.sdk.iot.service.jobs.JobResult in project azure-iot-sdk-java by Azure.

the class RoleBasedAuthenticationSample method runJobClientSample.

private static void runJobClientSample(String iotHubHostName, TokenCredential credential) {
    // JobClient has some configurable options for HTTP read and connect timeouts, as well as for setting proxies.
    // For this sample, the default options will be used though.
    JobClientOptions options = JobClientOptions.builder().build();
    // This constructor takes in your implementation of TokenCredential which allows you to use RBAC authentication
    // rather than symmetric key based authentication that comes with constructors that take connection strings.
    JobClient jobClient = new JobClient(iotHubHostName, credential, options);
    try {
        System.out.println("Querying all active jobs for your IoT Hub");
        Query deviceJobQuery = jobClient.queryDeviceJob(SqlQuery.createSqlQuery("*", SqlQuery.FromType.JOBS, null, null).getQuery());
        int queriedJobCount = 0;
        while (jobClient.hasNextJob(deviceJobQuery)) {
            queriedJobCount++;
            JobResult job = jobClient.getNextJob(deviceJobQuery);
            System.out.println(String.format("Job %s of type %s has status %s", job.getJobId(), job.getJobType(), job.getJobStatus()));
        }
        if (queriedJobCount == 0) {
            System.out.println("No active jobs found for your IoT Hub");
        }
    } catch (IotHubException | IOException e) {
        System.err.println("Failed to query the jobs for your IoT Hub");
        e.printStackTrace();
        System.exit(-1);
    }
}
Also used : JobClientOptions(com.microsoft.azure.sdk.iot.service.jobs.JobClientOptions) 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) IOException(java.io.IOException) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)

Example 37 with JobResult

use of com.microsoft.azure.sdk.iot.service.jobs.JobResult in project azure-iot-sdk-java by Azure.

the class DeviceTwinSample method scheduleUpdateDesiredProperties.

private static void scheduleUpdateDesiredProperties(DeviceTwin twinClient, DeviceTwinDevice device) throws IOException, IotHubException, InterruptedException {
    // new set of desired properties
    Set<Pair> desiredProperties = new HashSet<>();
    desiredProperties.add(new Pair("temp", new Random().nextInt(TEMPERATURE_RANGE)));
    desiredProperties.add(new Pair("hum", new Random().nextInt(HUMIDITY_RANGE)));
    device.setDesiredProperties(desiredProperties);
    // ScheduleUpdateTwin job type is a force update, which only accepts '*' as the Etag
    device.setETag("*");
    // date when the update shall be executed
    // 10 seconds in the future.
    Date updateDateInFuture = new Date(new Date().getTime() + ADD_10_SECONDS_IN_MILLISECONDS);
    // query condition that defines the list of device to be updated
    String queryCondition = "DeviceId IN ['" + deviceId + "']";
    System.out.println("Schedule updating Device twin (new temp, hum) in 10 seconds");
    Job job = twinClient.scheduleUpdateTwin(queryCondition, device, updateDateInFuture, MAX_EXECUTION_TIME_IN_SECONDS);
    System.out.println("Wait for job completed...");
    JobResult jobResult = job.get();
    while (jobResult.getJobStatus() != JobStatus.completed) {
        Thread.sleep(GIVE_100_MILLISECONDS_TO_IOTHUB);
        jobResult = job.get();
    }
    System.out.println("job completed");
    System.out.println("Getting the updated Device twin");
    twinClient.getTwin(device);
    System.out.println(device);
}
Also used : JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult)

Example 38 with JobResult

use of com.microsoft.azure.sdk.iot.service.jobs.JobResult in project azure-iot-sdk-java by Azure.

the class DeviceMethodSample method scheduleInvokeMethod.

private static void scheduleInvokeMethod(DeviceMethod methodClient) throws IotHubException, IOException, InterruptedException {
    // query condition that defines the list of device to invoke
    String queryCondition = "DeviceId IN ['" + deviceId + "']";
    // date when the invoke shall be executed
    // 10 seconds in the future.
    Date invokeDateInFuture = new Date(new Date().getTime() + ADD_10_SECONDS_IN_MILLISECONDS);
    System.out.println("Schedule invoke method on the Device in 10 seconds");
    Job job = methodClient.scheduleDeviceMethod(queryCondition, methodName, responseTimeout, connectTimeout, payload, invokeDateInFuture, MAX_EXECUTION_TIME_IN_SECONDS);
    System.out.println("Wait for job completed...");
    JobResult jobResult = job.get();
    while (jobResult.getJobStatus() != JobStatus.completed) {
        Thread.sleep(GIVE_100_MILLISECONDS_TO_IOTHUB);
        jobResult = job.get();
    }
    System.out.println("job completed");
}
Also used : JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) Job(com.microsoft.azure.sdk.iot.service.devicetwin.Job) Date(java.util.Date)

Example 39 with JobResult

use of com.microsoft.azure.sdk.iot.service.jobs.JobResult in project azure-iot-sdk-java by Azure.

the class JobClientSample method main.

public static void main(String[] args) throws Exception {
    System.out.println("Starting sample...");
    // *************************************** Create JobClient ***************************************
    JobClient jobClient = createJobClient();
    // *************************************** Schedule twin job ***************************************
    JobResult jobResultTwin = scheduleUpdateTwin(jobClient);
    monitorJob(jobClient, jobIdTwin);
    // *************************************** Schedule method job ***************************************
    JobResult jobResultMethod = scheduleUpdateMethod(jobClient);
    monitorJob(jobClient, jobIdMethod);
    // *************************************** Query Jobs Response ***************************************
    queryJobsResponse(jobClient);
    // *************************************** Query Device Job ***************************************
    queryDeviceJobs(jobClient);
    System.out.println("Shutting down sample...");
}
Also used : JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient)

Example 40 with JobResult

use of com.microsoft.azure.sdk.iot.service.jobs.JobResult in project azure-iot-sdk-java by Azure.

the class JobClientSample method monitorJob.

private static void monitorJob(JobClient jobClient, String jobId) throws IOException, IotHubException, InterruptedException {
    System.out.println("Monitoring jobClient for job completion...");
    JobResult jobResult = jobClient.getJob(jobId);
    System.out.println("First get response");
    System.out.println(jobResult);
    while (jobResult.getJobStatus() != JobStatus.completed) {
        Thread.sleep(1000);
        jobResult = jobClient.getJob(jobId);
    }
    System.out.println("Job ends with status " + jobResult.getJobStatus());
    System.out.println("Last get response");
    System.out.println(jobResult);
}
Also used : JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult)

Aggregations

JobResult (com.microsoft.azure.sdk.iot.service.jobs.JobResult)40 Test (org.junit.Test)27 Date (java.util.Date)25 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)17 JobClient (com.microsoft.azure.sdk.iot.service.jobs.JobClient)14 TwinState (com.microsoft.azure.sdk.iot.deps.twin.TwinState)13 HashSet (java.util.HashSet)11 NonStrictExpectations (mockit.NonStrictExpectations)11 JobsParser (com.microsoft.azure.sdk.iot.deps.serializer.JobsParser)9 TwinCollection (com.microsoft.azure.sdk.iot.deps.twin.TwinCollection)9 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)9 Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)9 IOException (java.io.IOException)8 Expectations (mockit.Expectations)8 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)6 MethodParser (com.microsoft.azure.sdk.iot.deps.serializer.MethodParser)5 DeviceTestManager (tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager)5 Job (com.microsoft.azure.sdk.iot.service.devicetwin.Job)4 MethodResult (com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult)4 Query (com.microsoft.azure.sdk.iot.service.devicetwin.Query)4