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