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);
}
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");
}
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;
}
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;
}
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;
}
};
}
Aggregations