Skip to content

Commit 2cac3a9

Browse files
committed
Updates
1 parent 7861d70 commit 2cac3a9

File tree

4 files changed

+28
-88
lines changed

4 files changed

+28
-88
lines changed

articles/iot-hub/how-to-module-twins.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ ms.custom: mqtt, devx-track-csharp, devx-track-dotnet
2121

2222
This article shows you how to develop two types of applications:
2323

24-
* Device apps that view and update reported properties and handle requests to update module desired properties.
25-
* Service apps that can add modules, and read and set desired properties.
24+
* Device apps that view and update module identity twin reported properties and handle requests to update desired properties.
25+
* Service apps that can read and set module identity desired properties.
2626

2727
> [!NOTE]
2828
> This article is meant to complement [Azure IoT SDKs](iot-hub-devguide-sdks.md) samples that are referenced from within this article. You can use SDK tools to build both device and back-end applications.

includes/iot-hub-howto-module-twins-dotnet.md

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@ This article describes how to use the [Azure IoT SDK for .NET](https://github.co
1717

1818
## Create a device application
1919

20-
Device applications can read and write module identity twin reported properties, and be notified of desired module identity twin property changes that are set by a backend application or IoT Hub.
21-
2220
This section describes how to use device application code to:
2321

24-
* Retrieve module identity twin and examine reported properties
22+
* Retrieve a module identity twin and examine reported properties
2523
* Update reported module identity twin properties
2624
* Create a module desired property update callback handler
2725

28-
### Add device NuGet Package
26+
### Required device NuGet package
2927

3028
Device client applications written in C# require the **Microsoft.Azure.Devices.Client** NuGet package.
3129

@@ -40,26 +38,26 @@ using Microsoft.Azure.Devices.Shared;
4038

4139
### Connect to a device
4240

43-
The [ModuleClient](/dotnet/api/microsoft.azure.devices.client.moduleclient) class exposes all the methods required to interact with module identity twins from the device.
41+
The [ModuleClient](/dotnet/api/microsoft.azure.devices.client.moduleclient) class exposes all methods required to interact with module identity twins from the device.
4442

4543
Connect to the device using the [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.client.moduleclient.createfromconnectionstring) method with the module connection string.
46-
For example:
4744

4845
Calling `CreateFromConnectionString` without a transport parameter connects using the default AMQP transport.
4946

47+
This example connects to the device using the default AMQP transport.
48+
5049
```csharp
5150
static string ModuleConnectionString = "{Device module identity connection string}";
5251
private static ModuleClient _moduleClient = null;
5352

54-
_moduleClient = ModuleClient.CreateFromConnectionString(ModuleConnectionString,
55-
null);
53+
_moduleClient = ModuleClient.CreateFromConnectionString(ModuleConnectionString, null);
5654
```
5755

5856
### Retrieve a module identity twin and examine properties
5957

6058
Call [GetTwinAsync](/dotnet/api/microsoft.azure.devices.client.moduleclient.gettwinasync?#microsoft-azure-devices-client-moduleclient-gettwinasync) to retrieve the current module identity twin properties into a [Twin](/dotnet/api/microsoft.azure.devices.shared.twin?) object.
6159

62-
This example retrieves and displays module identity twin properties.
60+
This example retrieves and displays module identity twin properties in JSON format.
6361

6462
```csharp
6563
Console.WriteLine("Retrieving twin...");
@@ -95,7 +93,7 @@ catch (Exception ex)
9593

9694
### Create a desired property update callback handler
9795

98-
Create a desired property update callback handler that executes when a desired property is changed in the module identity twin by passing the callback handler method name to [SetDesiredPropertyUpdateCallbackAsync](/dotnet/api/microsoft.azure.devices.client.moduleclient.setdesiredpropertyupdatecallbackasync).
96+
Pass the callback handler method name to [SetDesiredPropertyUpdateCallbackAsync](/dotnet/api/microsoft.azure.devices.client.moduleclient.setdesiredpropertyupdatecallbackasync) to create a desired property update callback handler that executes when a desired property is changed in the module identity twin.
9997

10098
For example, this call sets up the system to notify a method named `OnDesiredPropertyChangedAsync` whenever a desired module property is changed.
10199

@@ -140,12 +138,9 @@ The Azure IoT SDK for .NET provides working samples of device apps that handle m
140138

141139
The [RegistryManager](/dotnet/api/microsoft.azure.devices.registrymanager) class exposes all methods required to create a backend application to interact with module identity twins from the service.
142140

143-
This section describes how to create backend application code to:
141+
This section describes how to read and update module identity fields.
144142

145-
* Add a module
146-
* Read and update module fields
147-
148-
### Add service NuGet Package
143+
### Required service NuGet package
149144

150145
Backend service applications require the **Microsoft.Azure.Devices** NuGet package.
151146

@@ -177,33 +172,7 @@ static string connectionString = "{IoT hub shared access policy connection strin
177172
registryManager = RegistryManager.CreateFromConnectionString(connectionString);
178173
```
179174

180-
### Add a module
181-
182-
Call [AddModuleAsync](/dotnet/api/microsoft.azure.devices.registrymanager.addmoduleasync) to add a module to a device.
183-
184-
In this example, the code calls `AddModuleAsync` to add a module named **myFirstModule** to a device named **myFirstDevice**. if the module already exists, the code calls [GetModuleAsync](/dotnet/api/microsoft.azure.devices.registrymanager.getmoduleasync) to fetch the module data into a [Module](/dotnet/api/microsoft.azure.devices.module) object.
185-
186-
```csharp
187-
const string deviceID = "myFirstDevice";
188-
const string moduleID = "myFirstModule";
189-
Module module;
190-
191-
// Add the module
192-
try
193-
{
194-
module =
195-
await registryManager.AddModuleAsync(new Module(deviceID, moduleID));
196-
}
197-
catch (ModuleAlreadyExistsException)
198-
{
199-
Console.WriteLine("ModuleID already exists for this device.");
200-
}
201-
202-
// Show the module primary key
203-
Console.WriteLine("Generated module key: {0}", module.Authentication.SymmetricKey.PrimaryKey);
204-
```
205-
206-
### Read and update module fields
175+
### Read and update module identity fields
207176

208177
Call [GetModuleAsync](/dotnet/api/microsoft.azure.devices.registrymanager.getmoduleasync) to retrieve current module identity twin fields into a [Module](/dotnet/api/microsoft.azure.devices.module) object.
209178

includes/iot-hub-howto-module-twins-node.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ client.open(function(err) {
110110

111111
Call [getTwin](/javascript/api/azure-iot-device/client?#azure-iot-device-client-gettwin-1) to retrieve current module identity twin information into a [Twin](/javascript/api/azure-iot-device/twin) object.
112112

113-
Device code can then access the twin properties.
113+
Device code can then access the module identity twin properties.
114114

115115
For example:
116116

@@ -306,7 +306,7 @@ The Azure IoT SDK for Node.js provides working samples of a device apps that han
306306
307307
This section describes how to create a backend application that retrieves a module identity twin and update desired properties.
308308
309-
### Install service SDK packages
309+
### Install service SDK package
310310
311311
Run this command to install **azure-iothub** on your development machine:
312312

includes/iot-hub-howto-module-twins-python.md

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ Device applications can read and write module identity twin reported properties,
4141

4242
This section describes how to use device application code to:
4343

44-
* Retrieve module identity twin and examine reported properties
45-
* Update reported module identity twin properties
46-
* Create a module desired property update callback handler
44+
* Retrieve a module identity twin and examine reported properties
45+
* Update module identity twin reported properties
46+
* Create a module identity twin desired property update callback handler
4747

4848
### Import statements
4949

@@ -78,9 +78,9 @@ device_client = IoTHubDeviceClient.create_from_connection_string(conn_str)
7878
await device_client.connect()
7979
```
8080

81-
### Retrieve a device twin and examine reported properties
81+
### Retrieve a module identity twin and examine reported properties
8282

83-
Call [get_twin](/python/api/azure-iot-device/azure.iot.device.iothubmoduleclient?#azure-iot-device-iothubmoduleclient-get-twin) to retrieve the module identity twin from the Azure IoT Hub service. The twin information is placed into a variable that can be printed or examined.
83+
Call [get_twin](/python/api/azure-iot-device/azure.iot.device.iothubmoduleclient?#azure-iot-device-iothubmoduleclient-get-twin) to retrieve the module identity twin from the Azure IoT Hub service. The twin information is placed into a variable that can be examined.
8484

8585
This example retrieves the device twin and uses the `print` command to view the device twin in JSON format.
8686

@@ -91,7 +91,7 @@ print("Twin document:")
9191
print("{}".format(twin))
9292
```
9393

94-
### Patch device reported twin properties
94+
### Update module identity twin reported properties
9595

9696
You can apply a patch to update module identity twin reported properties in JSON format.
9797

@@ -100,8 +100,6 @@ To apply a patch to update reported properties:
100100
1. Assign a reported property JSON patch to a variable.
101101
1. Call [patch_twin_reported_properties](/python/api/azure-iot-device/azure.iot.device.iothubmoduleclient?#azure-iot-device-iothubmoduleclient-patch-twin-reported-properties) to apply the JSON patch to reported properties.
102102

103-
If `patch_twin_reported_properties` returns an error, this function raises the corresponding error.
104-
105103
For example:
106104

107105
```python
@@ -112,7 +110,7 @@ print("Setting reported temperature to {}".format(reported_properties["temperatu
112110
await device_client.patch_twin_reported_properties(reported_properties)
113111
```
114112

115-
### Incoming desired properties patch handler
113+
### Create a module identity twin desired property update callback handler
116114

117115
Call [on_twin_desired_properties_patch_received](/python/api/azure-iot-device/azure.iot.device.iothubmoduleclient?#azure-iot-device-iothubmoduleclient-on-twin-desired-properties-patch-received) to create a handler function or coroutine that is called when a module identity twin desired properties patch is received. The handler takes one argument, which is the twin patch in the form of a JSON dictionary object.
118116

@@ -148,10 +146,7 @@ The Azure IoT SDK for Python includes the following samples:
148146

149147
## Create a backend application
150148

151-
This section describes how to create a backend application to:
152-
153-
* Create a module
154-
* Update desired properties
149+
This section describes how to create a backend application to retrieve and update module identity twin desired properties.
155150

156151
The [IoTHubRegistryManager](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager) class exposes all methods required to create a backend application to interact with module identity twins from the service.
157152

@@ -184,38 +179,14 @@ IOTHUB_CONNECTION_STRING = "{IoT hub shared access policy connection string}"
184179
iothub_registry_manager = IoTHubRegistryManager.from_connection_string(IOTHUB_CONNECTION_STRING)
185180
```
186181

187-
### Create a module
188-
189-
Use [create_module_with_sas](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager?#azure-iot-hub-iothubregistrymanager-create-module-with-sas) to create a device identity module using a shared access key.
190-
191-
This example creates a new module named `myFirstModule` for device `myFirstDevice`. If the module already exists, the code calls [get_module](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager?#azure-iot-hub-iothubregistrymanager-get-module) to retrieve the device identity module from IoT Hub.
192-
193-
```python
194-
DEVICE_ID = "myFirstDevice"
195-
MODULE_ID = "myFirstModule"
196-
197-
try:
198-
# CreateModule - let IoT Hub assign keys
199-
primary_key = ""
200-
secondary_key = ""
201-
managed_by = ""
202-
new_module = iothub_registry_manager.create_module_with_sas(
203-
DEVICE_ID, MODULE_ID, managed_by, primary_key, secondary_key
204-
)
205-
except HttpOperationError as ex:
206-
if ex.response.status_code == 409:
207-
# 409 indicates a conflict. This happens because the module already exists.
208-
new_module = iothub_registry_manager.get_module(DEVICE_ID, MODULE_ID)
209-
else:
210-
raise
211-
```
182+
### Retrieve and update module identity twin desired properties
212183

213-
### Update desired properties
184+
You can update desired properties from a backend application using [update_module_twin](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager?#azure-iot-hub-iothubregistrymanager-update-module-twin).
214185

215-
You can update both module identity twin tags and desired properties from a backend application at the same time using [update_module_twin](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager?#azure-iot-hub-iothubregistrymanager-update-module-twin).
186+
To retrieve and update module identity twin desired properties:
216187

217-
1. Call [get_module_twin](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager?#azure-iot-hub-iothubregistrymanager-get-module-twin) to get the current version of the module identity twin
218-
1. Use the [Twin](/python/api/azure-iot-hub/azure.iot.hub.protocol.models.twin(class)) class to add module tags and properties in JSON format.
188+
1. Call [get_module_twin](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager?#azure-iot-hub-iothubregistrymanager-get-module-twin) to get the current version of the module identity twin.
189+
1. Use the [Twin](/python/api/azure-iot-hub/azure.iot.hub.protocol.models.twin(class)) class to add desired properties in JSON format.
219190
1. Call `update_module_twin` to apply the patch to the device twin. You can also use [replace_module_twin](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager?#azure-iot-hub-iothubregistrymanager-replace-module-twin) to replace desired properties and tags for a module identity twin.
220191

221192
This example updates the `telemetryInterval` desired property to `122`.

0 commit comments

Comments
 (0)