Skip to content

Commit cf9ff2d

Browse files
committed
Added new content
1 parent bdf4c81 commit cf9ff2d

4 files changed

+168
-112
lines changed

includes/iot-hub-howto-schedule-broadcast-jobs-dotnet.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ms.custom: [amqp, mqtt, "Role: Cloud Development", "Role: IoT Device", devx-trac
1515

1616
## Overview
1717

18-
This article describes how to use the [Azure IoT SDK for .NET](https://github.com/Azure/azure-iot-sdk-csharp/blob/main/readme.md) to create backend service application code for job scheduling.
18+
This article describes how to use the [Azure IoT SDK for .NET](https://github.com/Azure/azure-iot-sdk-csharp/blob/main/readme.md) to create backend service application code to schedule job to invoke a direct method or perform a device twin desired property update on one or more devices.
1919

2020
### Add service NuGet Package
2121

@@ -46,7 +46,7 @@ You can connect a backend service to IoT Hub using the following methods:
4646

4747
Connect a backend application to a device using [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.jobclient.createfromconnectionstring).
4848

49-
In this article, you create a back-end service that schedules a job to invoke a direct method on a device, schedules a job to update the device twin, and monitors the progress of each job. To perform these operations, your service needs the **registry read** and **registry write permissions**. By default, every IoT hub is created with a shared access policy named **registryReadWrite** that grants these permissions.
49+
This article describes back-end code that can schedule a job to invoke a direct method, schedule a job to update a device twin, and monitors the progress of a job for one or more devices. To perform these operations, your service needs the **registry read** and **registry write permissions**. By default, every IoT hub is created with a shared access policy named **registryReadWrite** that grants these permissions.
5050

5151
For more information about shared access policies, see [Control access to IoT Hub with shared access signatures](/azure/iot-hub/authenticate-authorize-sas).
5252

@@ -60,11 +60,11 @@ jobClient = JobClient.CreateFromConnectionString(connString);
6060

6161
[!INCLUDE [iot-hub-howto-connect-service-iothub-entra-dotnet](iot-hub-howto-connect-service-iothub-entra-dotnet.md)]
6262

63-
### Create a device method update job
63+
### Create a direct method update job
6464

65-
Use [ScheduleDeviceMethodAsync](/dotnet/api/microsoft.azure.devices.jobclient.scheduledevicemethodasync) to create a new device method to run a device method on one or multiple devices.
65+
Use [ScheduleDeviceMethodAsync](/dotnet/api/microsoft.azure.devices.jobclient.scheduledevicemethodasync) to create a new direct method to run a direct method on one or multiple devices.
6666

67-
This example schedules a device method call job for a specific job ID.
67+
This example schedules a job for a direct method method named "LockDoor".
6868

6969
```csharp
7070
string methodJobId = Guid.NewGuid().ToString();
@@ -83,39 +83,43 @@ JobResponse result = await jobClient.ScheduleDeviceMethodAsync(methodJobId,
8383
Console.WriteLine("Started Method Job");
8484
```
8585

86-
### Schedule a device twin update job
86+
### Schedule a device desired twin update job
8787

88-
Use [ScheduleTwinUpdateAsync](/dotnet/api/microsoft.azure.devices.jobclient.scheduledevicemethodasync) to create a new device twin update job to run a device twin update on one or multiple devices.
88+
Use [ScheduleTwinUpdateAsync](/dotnet/api/microsoft.azure.devices.jobclient.scheduledevicemethodasync) to create a new desired twin update job to run on one or multiple devices.
8989

90-
This example schedules a device twin update job for a specific job ID.
90+
First, create and populate a device `Twin` object for the update.
91+
92+
For example:
9193

9294
```csharp
93-
string twinJobId = Guid.NewGuid().ToString();
9495
static string deviceId = "Device-1";
9596

9697
Twin twin = new Twin(deviceId);
9798
twin.Tags = new TwinCollection();
9899
twin.Tags["Building"] = "43";
99100
twin.Tags["Floor"] = "3";
100101
twin.ETag = "*";
101-
102102
twin.Properties.Desired["LocationUpdate"] = DateTime.UtcNow;
103+
```
104+
105+
Next, call `ScheduleTwinUpdateAsync`. Specify the devices to be updated in the second parameter.
106+
107+
```csharp
108+
string twinJobId = Guid.NewGuid().ToString();
103109

104110
JobResponse createJobResponse = jobClient.ScheduleTwinUpdateAsync(
105111
twinJobId,
106112
$"DeviceId IN ['{deviceId}']",
107113
twin,
108114
DateTime.UtcNow,
109115
(long)TimeSpan.FromMinutes(2).TotalSeconds).Result;
110-
111-
Console.WriteLine("Started Twin Update Job");
112116
```
113117

114118
### Monitor a job
115119

116120
Use [GetJobAsync](/dotnet/api/microsoft.azure.devices.jobclient.getjobasync?#microsoft-azure-devices-jobclient-getjobasync(system-string)) to monitor a job status.
117121

118-
This example checks the job status for a specific job ID periodically until the job is complete or failed.
122+
This example checks the job status for a job ID periodically until the job is complete or failed.
119123

120124
```csharp
121125
JobResponse result;
@@ -132,4 +136,4 @@ do
132136
The Azure IoT SDK for .NET provides working samples of service apps that handle job scheduling tasks. For more information, see:
133137

134138
* [Schedule twin update sample](https://github.com/Azure/azure-iot-sdk-csharp/blob/main/iothub/service/samples/getting%20started/JobsSample/JobsSample.cs)
135-
* [E2E schedule twin update sample](https://github.com/Azure/azure-iot-sdk-csharp/blob/86065001a92fedb42877722c6a57ae37e45eed30/e2e/test/iothub/service/IoTHubCertificateValidationE2ETest.cs#L69)
139+
* [E2E schedule twin update sample](https://github.com/Azure/azure-iot-sdk-csharp/blob/86065001a92fedb42877722c6a57ae37e45eed30/e2e/test/iothub/service/IoTHubCertificateValidationE2ETest.cs).

includes/iot-hub-howto-schedule-broadcast-jobs-java.md

Lines changed: 64 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ms.custom: mqtt, devx-track-java, devx-track-extended-java
1515

1616
## Overview
1717

18-
This article describes how to use the [Azure IoT SDK for Java](https://github.com/Azure/azure-iot-sdk-java) to create backend service application code for job scheduling.
18+
This article describes how to use the [Azure IoT SDK for Java](https://github.com/Azure/azure-iot-sdk-java) to create backend service application code to schedule job to invoke a direct method or perform a device twin desired property update on one or more devices.
1919

2020
The [JobClient](/java/api/com.microsoft.azure.sdk.iot.service.jobs.jobclient) class contains methods that services can use to schedule jobs.
2121

@@ -52,82 +52,94 @@ You can connect a backend service to IoT Hub using the following methods:
5252

5353
Use a [JobClient](/java/api/com.microsoft.azure.sdk.iot.service.jobs.jobclient) constructor to create the connection to IoT hub. The `JobClient` object handles the communication with your IoT hub.
5454

55-
In this article, you create a back-end service that schedules a job to invoke a direct method on a device, schedules a job to update the device twin, and monitors the progress of each job. To perform these operations, your service needs the **registry read** and **registry write permissions**. By default, every IoT hub is created with a shared access policy named **registryReadWrite** that grants these permissions.
55+
This article describes back-end code that can schedule a job to invoke a direct method, schedule a job to update a device twin, and monitors the progress of a job for one or more devices. To perform these operations, your service needs the **registry read** and **registry write permissions**. By default, every IoT hub is created with a shared access policy named **registryReadWrite** that grants these permissions.
5656

5757
For more information about shared access policies, see [Control access to IoT Hub with shared access signatures](/azure/iot-hub/authenticate-authorize-sas).
5858

5959
For example:
6060

6161
```java
6262
public static final String iotHubConnectionString = "{Shared access policy connection string}";
63-
public static final String deviceId = "myDeviceId";
64-
6563
JobClient jobClient = new JobClient(iotHubConnectionString);
6664
```
6765

6866
#### Connect using Microsoft Entra
6967

7068
[!INCLUDE [iot-hub-howto-connect-service-iothub-entra-java](iot-hub-howto-connect-service-iothub-entra-java.md)]
7169

72-
### Create a device method update job
70+
### Create a direct method update job
7371

74-
Use [scheduleDeviceMethod](/java/api/com.microsoft.azure.sdk.iot.service.jobs.jobclient?#com-microsoft-azure-sdk-iot-service-jobs-jobclient-scheduledevicemethod(java-lang-string-java-lang-string-java-lang-string-java-lang-long-java-lang-long-java-lang-object-java-util-date-long)) to run a device method on one or multiple devices.
72+
Use [scheduleDeviceMethod](/java/api/com.microsoft.azure.sdk.iot.service.jobs.jobclient?#com-microsoft-azure-sdk-iot-service-jobs-jobclient-scheduledevicemethod(java-lang-string-java-lang-string-java-lang-string-java-lang-long-java-lang-long-java-lang-object-java-util-date-long)) to run a direct method on one or multiple devices.
7573

76-
This example schedules a device method call job for a specific job ID.
74+
This example method schedules a direct method call job for a specific job ID.
7775

7876
```java
79-
private static JobResult scheduleJobCallDirectMethod(JobClient jobClient, String jobId) {
80-
// Schedule a job now to call the lockDoor direct method
81-
// against a single device. Response and connection
82-
// timeouts are set to 5 seconds.
83-
System.out.println("Schedule job " + jobId + " for device " + deviceId);
84-
try {
85-
JobResult jobResult = jobClient.scheduleDeviceMethod(jobId,
86-
"deviceId='" + deviceId + "'",
87-
"lockDoor",
88-
5L, 5L, null,
89-
new Date(),
90-
maxExecutionTimeInSeconds);
91-
return jobResult;
92-
} catch (Exception e) {
93-
System.out.println("Exception scheduling direct method job: " + jobId);
94-
System.out.println(e.getMessage());
95-
return null;
96-
}
97-
};
77+
// Schedule a job now to call the lockDoor direct method
78+
// against a single device. Response and connection
79+
// timeouts are set to 5 seconds.
80+
String deviceId = "Device-1";
81+
String jobId = "DMCMD" + UUID.randomUUID();
82+
83+
// How long the job is permitted to run without
84+
// completing its work on the set of devices
85+
private static final long maxExecutionTimeInSeconds = 30;
86+
87+
System.out.println("Schedule job " + jobId + " for device " + deviceId);
88+
try {
89+
JobResult jobResult = jobClient.scheduleDeviceMethod(jobId,
90+
"deviceId='" + deviceId + "'",
91+
"lockDoor",
92+
5L, 5L, null,
93+
new Date(),
94+
maxExecutionTimeInSeconds);
95+
} catch (Exception e) {
96+
System.out.println("Exception scheduling direct method job: " + jobId);
97+
System.out.println(e.getMessage());
98+
}
9899
```
99100

100101
### Schedule a device twin update job
101102

102103
Use [scheduleUpdateTwin](/java/api/com.microsoft.azure.sdk.iot.service.jobs.jobclient?#com-microsoft-azure-sdk-iot-service-jobs-jobclient-scheduleupdatetwin(java-lang-string-java-lang-string-com-microsoft-azure-sdk-iot-service-devicetwin-devicetwindevice-java-util-date-long)) to create a new job to run a device twin update on one or multiple devices.
103104

104-
This example schedules a device twin update job for a specific job Id.
105+
This example method schedules a device twin update job for a specific job Id.
106+
107+
First, prepare a `DeviceTwinDevice` record.
108+
109+
For example:
105110

106111
```java
107-
private static JobResult scheduleJobSetDesiredProperties(JobClient jobClient, String jobId) {
108-
DeviceTwinDevice twin = new DeviceTwinDevice(deviceId);
109-
Set<Pair> desiredProperties = new HashSet<Pair>();
110-
desiredProperties.add(new Pair("Building", 43));
111-
desiredProperties.add(new Pair("Floor", 3));
112-
twin.setDesiredProperties(desiredProperties);
113-
// Optimistic concurrency control
114-
twin.setETag("*");
115-
116-
// Schedule the update twin job to run now
117-
// against a single device
118-
System.out.println("Schedule job " + jobId + " for device " + deviceId);
119-
try {
120-
JobResult jobResult = jobClient.scheduleUpdateTwin(jobId,
121-
"deviceId='" + deviceId + "'",
122-
twin,
123-
new Date(),
124-
maxExecutionTimeInSeconds);
125-
return jobResult;
126-
} catch (Exception e) {
127-
System.out.println("Exception scheduling desired properties job: " + jobId);
128-
System.out.println(e.getMessage());
129-
return null;
130-
}
112+
String deviceId = "Device-1";
113+
String jobId = "DPCMD" + UUID.randomUUID();
114+
115+
// How long the job is permitted to run without
116+
// completing its work on the set of devices
117+
private static final long maxExecutionTimeInSeconds = 30;
118+
119+
//Create a device twin desired properties update object
120+
DeviceTwinDevice twin = new DeviceTwinDevice(deviceId);
121+
Set<Pair> desiredProperties = new HashSet<Pair>();
122+
desiredProperties.add(new Pair("Building", 43));
123+
desiredProperties.add(new Pair("Floor", 3));
124+
twin.setDesiredProperties(desiredProperties);
125+
// Optimistic concurrency control
126+
twin.setETag("*");
127+
```
128+
129+
Next, call `scheduleUpdateTwin` to schedule the update job.
130+
131+
```java
132+
// Schedule the update twin job to run now for a single device
133+
System.out.println("Schedule job " + jobId + " for device " + deviceId);
134+
try {
135+
JobResult jobResult = jobClient.scheduleUpdateTwin(jobId,
136+
"deviceId='" + deviceId + "'",
137+
twin,
138+
new Date(),
139+
maxExecutionTimeInSeconds);
140+
} catch (Exception e) {
141+
System.out.println("Exception scheduling desired properties job: " + jobId);
142+
System.out.println(e.getMessage());
131143
}
132144
```
133145

@@ -185,4 +197,4 @@ private static void queryDeviceJobs(JobClient jobClient, String start) throws Ex
185197

186198
### SDK schedule job example
187199

188-
The Azure IoT SDK for Java provides a working sample of a service app that handles job scheduling tasks. For more information, see [Job Client Sample](https://github.com/Azure/azure-iot-service-sdk-java/blob/main/service/iot-service-samples/job-client-sample/src/main/java/samples/com/microsoft/azure/sdk/iot/JobClientSample.java)
200+
The Azure IoT SDK for Java provides a working sample of a service app that handles job scheduling tasks. For more information, see [Job Client Sample](https://github.com/Azure/azure-iot-service-sdk-java/blob/main/service/iot-service-samples/job-client-sample/src/main/java/samples/com/microsoft/azure/sdk/iot/JobClientSample.java).

includes/iot-hub-howto-schedule-broadcast-jobs-node.md

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ms.custom: [amqp, mqtt, devx-track-js]
1515

1616
## Overview
1717

18-
This article describes how to use the [Azure IoT SDK for Node.js](https://github.com/Azure/azure-iot-sdk-node) to create backend service application code for job scheduling.
18+
This article describes how to use the [Azure IoT SDK for Node.js](https://github.com/Azure/azure-iot-sdk-node) to create backend service application code to schedule job to invoke a direct method or perform a device twin desired property update on one or more devices.
1919

2020
### Install service SDK package
2121

@@ -40,10 +40,12 @@ You can connect a backend service to IoT Hub using the following methods:
4040

4141
Use [fromConnectionString](/javascript/api/azure-iothub/jobclient?#azure-iothub-jobclient-fromconnectionstring) to connect to IoT hub.
4242

43-
In this article, you create a back-end service that schedules a job to invoke a direct method on a device, schedules a job to update the device twin, and monitors the progress of each job. To perform these operations, your service needs the **registry read** and **registry write permissions**. By default, every IoT hub is created with a shared access policy named **registryReadWrite** that grants these permissions.
43+
This article describes back-end code that can schedule a job to invoke a direct method, schedule a job to update a device twin, and monitors the progress of a job for one or more devices. To perform these operations, your service needs the **registry read** and **registry write permissions**. By default, every IoT hub is created with a shared access policy named **registryReadWrite** that grants these permissions.
4444

4545
For more information about shared access policies, see [Control access to IoT Hub with shared access signatures](/azure/iot-hub/authenticate-authorize-sas).
4646

47+
For example:
48+
4749
```javascript
4850
'use strict';
4951
var JobClient = require('azure-iothub').JobClient;
@@ -55,33 +57,44 @@ var jobClient = JobClient.fromConnectionString(connectionString);
5557

5658
[!INCLUDE [iot-hub-howto-connect-service-iothub-entra-node](iot-hub-howto-connect-service-iothub-entra-node.md)]
5759

58-
### Create a device method update job
60+
### Create a direct method update job
5961

60-
Use [scheduleDeviceMethod](/javascript/api/azure-iothub/jobclient?#azure-iothub-jobclient-scheduledevicemethod) to create a new device method job to run a device method on one or multiple devices.
62+
Use [scheduleDeviceMethod](/javascript/api/azure-iothub/jobclient?#azure-iothub-jobclient-scheduledevicemethod) to create a new direct method job to run a direct method on one or multiple devices.
6163

62-
This example schedules a device method call job for a specific job ID.
64+
First, create a direct method update variable.
6365

6466
```javascript
6567
var methodParams = {
6668
methodName: 'lockDoor',
6769
payload: null,
6870
responseTimeoutInSeconds: 15 // Timeout after 15 seconds if device is unable to process method
6971
};
72+
```
73+
74+
Call `scheduleDeviceMethod` to schedule the direct method call job:
7075

76+
* Each job must have a unique job ID. You can use this job ID to monitor a job as described in the **Monitor a job** section of this article.
77+
* Specify a `queryCondition` parameter to evaluate which devices to run the job on.
78+
* Check the `jobResult` callback for the job schedule result. If the job was successfully scheduled, you can monitor the job status as shown in the **Monitor a job** section of this article.
79+
80+
```javascript
7181
var methodJobId = uuid.v4();
72-
console.log('scheduling Device Method job with id: ' + methodJobId);
82+
var queryCondition = "deviceId IN ['myDeviceId']";
83+
var startTime = new Date();
84+
var maxExecutionTimeInSeconds = 300;
85+
7386
jobClient.scheduleDeviceMethod(methodJobId,
7487
queryCondition,
7588
methodParams,
7689
startTime,
7790
maxExecutionTimeInSeconds,
7891
function(err) {
7992
if (err) {
80-
console.error('Could not schedule device method job: ' + err.message);
93+
console.error('Could not schedule direct method job: ' + err.message);
8194
} else {
8295
monitorJob(methodJobId, function(err, result) {
8396
if (err) {
84-
console.error('Could not monitor device method job: ' + err.message);
97+
console.error('Could not monitor direct method job: ' + err.message);
8598
} else {
8699
console.log(JSON.stringify(result, null, 2));
87100
}
@@ -94,6 +107,8 @@ jobClient.scheduleDeviceMethod(methodJobId,
94107

95108
Use [scheduleTwinUpdate](/javascript/api/azure-iothub/jobclient?#azure-iothub-jobclient-scheduletwinupdate) to create a new job to run a device twin update on one or multiple devices.
96109

110+
First, create a device twin desired property update variable.
111+
97112
```javascript
98113
var twinPatch = {
99114
etag: '*',
@@ -104,7 +119,15 @@ var twinPatch = {
104119
}
105120
}
106121
};
122+
```
123+
124+
Call `scheduleTwinUpdate` to schedule the device twin desired property update job:
107125

126+
* Each job must have a unique job ID. You can use this job ID to monitor a job as described in the **Monitor a job** section of this article.
127+
* Specify a `queryCondition` parameter to evaluate which devices to run the job on.
128+
* Check the `jobResult` callback for the job schedule result. If the job was successfully scheduled, you can monitor the job status as shown in the **Monitor a job** section of this article.
129+
130+
```javascript
108131
var twinJobId = uuid.v4();
109132

110133
console.log('scheduling Twin Update job with id: ' + twinJobId);
@@ -130,9 +153,9 @@ jobClient.scheduleTwinUpdate(twinJobId,
130153

131154
### Monitor a job
132155

133-
Use [getJob](/javascript/api/azure-iothub/jobclient?#azure-iothub-jobclient-getjob) to monitor a job status.
156+
Use [getJob](/javascript/api/azure-iothub/jobclient?#azure-iothub-jobclient-getjob) to monitor a job status for a specific job ID.
134157

135-
This example checks the job status for a specific job ID periodically until the job is complete or failed.
158+
This example function checks the job status for a specific job ID periodically until the job is complete or failed.
136159

137160
```javascript
138161
function monitorJob (jobId, callback) {
@@ -154,4 +177,4 @@ function monitorJob (jobId, callback) {
154177

155178
### SDK schedule job example
156179

157-
The Azure IoT SDK for Node.js provides a working sample of a service app that handles job scheduling tasks. For more information, see [Job client E2E test](https://github.com/Azure/azure-iot-sdk-node/blob/a85e280350a12954f46672761b0b516d08d374b5/e2etests/test/job_client.js)
180+
The Azure IoT SDK for Node.js provides a working sample of a service app that handles job scheduling tasks. For more information, see [Job client E2E test](https://github.com/Azure/azure-iot-sdk-node/blob/a85e280350a12954f46672761b0b516d08d374b5/e2etests/test/job_client.js).

0 commit comments

Comments
 (0)