Search in sources :

Example 1 with JobResult

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

the class DeviceTwinSample method cancelJob.

private static void cancelJob(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 minutes in the future.
    Date updateDateInFuture = new Date(new Date().getTime() + ADD_10_MINUTES_IN_MILLISECONDS);
    // query condition that defines the list of device to be updated
    String queryCondition = "DeviceId IN ['" + deviceId + "']";
    System.out.println("Cancel updating Device twin (new temp, hum) in 10 minutes");
    Job job = twinClient.scheduleUpdateTwin(queryCondition, device, updateDateInFuture, MAX_EXECUTION_TIME_IN_SECONDS);
    Thread.sleep(WAIT_1_SECOND_TO_CANCEL_IN_MILLISECONDS);
    System.out.println("Cancel job after 1 second");
    job.cancel();
    System.out.println("Wait for job cancelled...");
    JobResult jobResult = job.get();
    while (jobResult.getJobStatus() != JobStatus.cancelled) {
        Thread.sleep(GIVE_100_MILLISECONDS_TO_IOTHUB);
        jobResult = job.get();
    }
    System.out.println("job cancelled");
    System.out.println("Getting the updated Device twin (no changes)");
    twinClient.getTwin(device);
    System.out.println(device);
}
Also used : JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult)

Example 2 with JobResult

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

the class DeviceMethodSample method cancelScheduleInvokeMethod.

private static void cancelScheduleInvokeMethod(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 minutes in the future.
    Date invokeDateInFuture = new Date(new Date().getTime() + ADD_10_MINUTES_IN_MILLISECONDS);
    System.out.println("Schedule invoke method on the Device in 10 minutes");
    Job job = methodClient.scheduleDeviceMethod(queryCondition, methodName, responseTimeout, connectTimeout, payload, invokeDateInFuture, MAX_EXECUTION_TIME_IN_SECONDS);
    Thread.sleep(WAIT_1_SECOND_TO_CANCEL_IN_MILLISECONDS);
    System.out.println("Cancel job after 1 second");
    job.cancel();
    System.out.println("Wait for job cancelled...");
    JobResult jobResult = job.get();
    while (jobResult.getJobStatus() != JobStatus.cancelled) {
        Thread.sleep(GIVE_100_MILLISECONDS_TO_IOTHUB);
        jobResult = job.get();
    }
    System.out.println("job cancelled");
}
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 3 with JobResult

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

the class JobClientSample method scheduleUpdateTwin.

private static JobResult scheduleUpdateTwin(JobClient jobClient) throws IOException, IotHubException {
    final String queryCondition = "DeviceId IN ['" + deviceId + "']";
    DeviceTwinDevice updateTwin = new DeviceTwinDevice(deviceId);
    Set<Pair> tags = new HashSet<>();
    tags.add(new Pair("HomeID", UUID.randomUUID()));
    updateTwin.setTags(tags);
    System.out.println("Schedule twin job " + jobIdTwin + " for device " + deviceId + "...");
    JobResult jobResult = jobClient.scheduleUpdateTwin(jobIdTwin, queryCondition, updateTwin, startTimeUtc, maxExecutionTimeInSeconds);
    if (jobResult == null) {
        throw new IOException("Schedule Twin Job returns null");
    }
    System.out.println("Schedule twin job response");
    System.out.println(jobResult);
    System.out.println();
    return jobResult;
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) IOException(java.io.IOException) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair)

Example 4 with JobResult

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

the class JobClientSample method scheduleUpdateMethod.

private static JobResult scheduleUpdateMethod(JobClient jobClient) throws IOException, IotHubException {
    final String queryCondition = "DeviceId IN ['" + deviceId + "']";
    System.out.println("Schedule method job " + jobIdMethod + " for device " + deviceId + "...");
    JobResult jobResultMethod = jobClient.scheduleDeviceMethod(jobIdMethod, queryCondition, methodName, responseTimeout, connectTimeout, payload, startTimeUtc, maxExecutionTimeInSeconds);
    if (jobResultMethod == null) {
        throw new IOException("Schedule method Job returns null");
    }
    System.out.println("Schedule method job response");
    System.out.println(jobResultMethod);
    System.out.println();
    return jobResultMethod;
}
Also used : JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) IOException(java.io.IOException)

Example 5 with JobResult

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

the class JobResultTest method constructorParseJson.

/* Tests_SRS_JOBRESULT_21_002: [The constructor shall parse the body using the JobsResponseParser.] */
@Test
public void constructorParseJson() throws IOException {
    // arrange
    final String json = "validJson";
    TwinCollection tags = new TwinCollection();
    tags.putFinal("tag1", "val1");
    TwinState twinState = new TwinState(tags, null, null);
    twinState.setDeviceId(DEVICE_ID);
    twinState.setETag(ETAG);
    JobsResponseParserExpectations(json, twinState, null, new Date(), null, "scheduleUpdateTwin");
    // act
    JobResult jobResult = Deencapsulation.newInstance(JobResult.class, new Class[] { byte[].class }, json.getBytes(StandardCharsets.UTF_8));
    // assert
    new Verifications() {

        {
            JobsResponseParser.createFromJson(json);
            times = 1;
        }
    };
}
Also used : TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState) JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) Date(java.util.Date) Test(org.junit.Test)

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