Skip to content

Commit bca5d18

Browse files
authored
Merge pull request #208042 from w-azure/winona-device-twin
Adjusted wording again
2 parents 3ff6119 + c8889ff commit bca5d18

File tree

4 files changed

+413
-416
lines changed

4 files changed

+413
-416
lines changed

articles/iot-hub/iot-hub-csharp-csharp-twin-getstarted.md

Lines changed: 89 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -41,100 +41,20 @@ In this article, you create two .NET console apps:
4141

4242
[!INCLUDE [iot-hub-include-find-custom-connection-string](../../includes/iot-hub-include-find-custom-connection-string.md)]
4343

44-
## Create the service app
45-
46-
In this section, you create a .NET console app, using C#, that adds location metadata to the device twin associated with **myDeviceId**. The app queries IoT hub for devices located in the US and then queries devices that report a cellular network connection.
47-
48-
1. In Visual Studio, select **File > New > Project**. In **Create a new project**, select **Console App (.NET Framework)**, and then select **Next**.
49-
50-
1. In **Configure your new project**, name the project **AddTagsAndQuery**, the select **Next**.
51-
52-
:::image type="content" source="./media/iot-hub-csharp-csharp-twin-getstarted/config-addtagsandquery-app.png" alt-text="Screenshot of how to create a new Visual Studio project." lightbox="./media/iot-hub-csharp-csharp-twin-getstarted/config-addtagsandquery-app.png":::
53-
54-
1. Accept the default version of the .NET Framework, then select **Create** to create the project.
55-
56-
1. In Solution Explorer, right-click the **AddTagsAndQuery** project, and then select **Manage NuGet Packages**.
57-
58-
1. Select **Browse** and search for and select **Microsoft.Azure.Devices**. Select **Install**.
59-
60-
![NuGet Package Manager window](./media/iot-hub-csharp-csharp-twin-getstarted/nuget-package-addtagsandquery-app.png)
61-
62-
This step downloads, installs, and adds a reference to the [Azure IoT service SDK](https://www.nuget.org/packages/Microsoft.Azure.Devices/) NuGet package and its dependencies.
63-
64-
1. Add the following `using` statements at the top of the **Program.cs** file:
65-
66-
```csharp
67-
using Microsoft.Azure.Devices;
68-
```
69-
70-
1. Add the following fields to the **Program** class. Replace `{iot hub connection string}` with the IoT Hub connection string that you copied in [Get the IoT hub connection string](#get-the-iot-hub-connection-string).
71-
72-
```csharp
73-
static RegistryManager registryManager;
74-
static string connectionString = "{iot hub connection string}";
75-
```
76-
77-
1. Add the following method to the **Program** class:
78-
79-
```csharp
80-
public static async Task AddTagsAndQuery()
81-
{
82-
var twin = await registryManager.GetTwinAsync("myDeviceId");
83-
var patch =
84-
@"{
85-
tags: {
86-
location: {
87-
region: 'US',
88-
plant: 'Redmond43'
89-
}
90-
}
91-
}";
92-
await registryManager.UpdateTwinAsync(twin.DeviceId, patch, twin.ETag);
93-
94-
var query = registryManager.CreateQuery(
95-
"SELECT * FROM devices WHERE tags.location.plant = 'Redmond43'", 100);
96-
var twinsInRedmond43 = await query.GetNextAsTwinAsync();
97-
Console.WriteLine("Devices in Redmond43: {0}",
98-
string.Join(", ", twinsInRedmond43.Select(t => t.DeviceId)));
99-
100-
query = registryManager.CreateQuery("SELECT * FROM devices WHERE tags.location.plant = 'Redmond43' AND properties.reported.connectivity.type = 'cellular'", 100);
101-
var twinsInRedmond43UsingCellular = await query.GetNextAsTwinAsync();
102-
Console.WriteLine("Devices in Redmond43 using cellular network: {0}",
103-
string.Join(", ", twinsInRedmond43UsingCellular.Select(t => t.DeviceId)));
104-
}
105-
```
106-
107-
The **RegistryManager** class exposes all the methods required to interact with device twins from the service. The previous code first initializes the **registryManager** object, then retrieves the device twin for **myDeviceId**, and finally updates its tags with the desired location information.
108-
109-
After updating, it executes two queries: the first selects only the device twins of devices located in the **Redmond43** plant, and the second refines the query to select only the devices that are also connected through cellular network.
110-
111-
The previous code, when it creates the **query** object, specifies a maximum number of returned documents. The **query** object contains a **HasMoreResults** boolean property that you can use to invoke the **GetNextAsTwinAsync** methods multiple times to retrieve all results. A method called **GetNextAsJson** is available for results that are not device twins, for example, results of aggregation queries.
112-
113-
1. Finally, add the following lines to the **Main** method:
114-
115-
```csharp
116-
registryManager = RegistryManager.CreateFromConnectionString(connectionString);
117-
AddTagsAndQuery().Wait();
118-
Console.WriteLine("Press Enter to exit.");
119-
Console.ReadLine();
120-
```
121-
122-
1. Run this application by right-clicking on the **AddTagsAndQuery** project and selecting **Debug**, followed by **Start new instance**. You should see one device in the results for the query asking for all devices located in **Redmond43** and none for the query that restricts the results to devices that use a cellular network.
123-
124-
![Query results in window](./media/iot-hub-csharp-csharp-twin-getstarted/addtagapp.png)
125-
126-
In the next section, you create a device app that reports connectivity information and changes the result of the query in the previous section.
127-
128-
## Create the device app
44+
## Create a device app with a direct method
12945

13046
In this section, you create a .NET console app that connects to your hub as **myDeviceId**, and then updates its reported properties to confirm that it's connected using a cellular network.
13147

132-
1. In Visual Studio, select **File** > **New** > **Project**. In **Create new project**, choose **Console App (.NET Framework)**, and then select **Next**.
48+
1. Open Visual Studio and select **Create new project**.
49+
50+
1. Choose **Console App (.NET Framework)**, then select **Next**.
13351

134-
1. In **Configure your new project**, name the project **ReportConnectivity**. For **Solution**, choose **Add to solution**, and then select **Create**.
52+
1. In **Configure your new project**, name the project **ReportConnectivity**, then select **Next**.
13553

13654
1. In Solution Explorer, right-click the **ReportConnectivity** project, and then select **Manage NuGet Packages**.
13755

56+
1. Keep the default .NET Framework, then select **Create** to create the project.
57+
13858
1. Select **Browse** and search for and choose **Microsoft.Azure.Devices.Client**. Select **Install**.
13959

14060
This step downloads, installs, and adds a reference to the [Azure IoT device SDK](https://www.nuget.org/packages/Microsoft.Azure.Devices.Client/) NuGet package and its dependencies.
@@ -234,6 +154,88 @@ In this section, you create a .NET console app that connects to your hub as **my
234154

235155
![Device connectivity reported successfully](./media/iot-hub-csharp-csharp-twin-getstarted/tagappsuccess.png)
236156

157+
## Create a service app to trigger a reboot
158+
159+
In this section, you create a .NET console app, using C#, that adds location metadata to the device twin associated with **myDeviceId**. The app queries IoT hub for devices located in the US and then queries devices that report a cellular network connection.
160+
161+
1. In Visual Studio, select **File > New > Project**. In **Create a new project**, select **Console App (.NET Framework)**, and then select **Next**.
162+
163+
1. In **Configure your new project**, name the project **AddTagsAndQuery**, the select **Next**.
164+
165+
:::image type="content" source="./media/iot-hub-csharp-csharp-twin-getstarted/config-addtagsandquery-app.png" alt-text="Screenshot of how to create a new Visual Studio project." lightbox="./media/iot-hub-csharp-csharp-twin-getstarted/config-addtagsandquery-app.png":::
166+
167+
1. Accept the default version of the .NET Framework, then select **Create** to create the project.
168+
169+
1. In Solution Explorer, right-click the **AddTagsAndQuery** project, and then select **Manage NuGet Packages**.
170+
171+
1. Select **Browse** and search for and select **Microsoft.Azure.Devices**. Select **Install**.
172+
173+
![NuGet Package Manager window](./media/iot-hub-csharp-csharp-twin-getstarted/nuget-package-addtagsandquery-app.png)
174+
175+
This step downloads, installs, and adds a reference to the [Azure IoT service SDK](https://www.nuget.org/packages/Microsoft.Azure.Devices/) NuGet package and its dependencies.
176+
177+
1. Add the following `using` statements at the top of the **Program.cs** file:
178+
179+
```csharp
180+
using Microsoft.Azure.Devices;
181+
```
182+
183+
1. Add the following fields to the **Program** class. Replace `{iot hub connection string}` with the IoT Hub connection string that you copied in [Get the IoT hub connection string](#get-the-iot-hub-connection-string).
184+
185+
```csharp
186+
static RegistryManager registryManager;
187+
static string connectionString = "{iot hub connection string}";
188+
```
189+
190+
1. Add the following method to the **Program** class:
191+
192+
```csharp
193+
public static async Task AddTagsAndQuery()
194+
{
195+
var twin = await registryManager.GetTwinAsync("myDeviceId");
196+
var patch =
197+
@"{
198+
tags: {
199+
location: {
200+
region: 'US',
201+
plant: 'Redmond43'
202+
}
203+
}
204+
}";
205+
await registryManager.UpdateTwinAsync(twin.DeviceId, patch, twin.ETag);
206+
207+
var query = registryManager.CreateQuery(
208+
"SELECT * FROM devices WHERE tags.location.plant = 'Redmond43'", 100);
209+
var twinsInRedmond43 = await query.GetNextAsTwinAsync();
210+
Console.WriteLine("Devices in Redmond43: {0}",
211+
string.Join(", ", twinsInRedmond43.Select(t => t.DeviceId)));
212+
213+
query = registryManager.CreateQuery("SELECT * FROM devices WHERE tags.location.plant = 'Redmond43' AND properties.reported.connectivity.type = 'cellular'", 100);
214+
var twinsInRedmond43UsingCellular = await query.GetNextAsTwinAsync();
215+
Console.WriteLine("Devices in Redmond43 using cellular network: {0}",
216+
string.Join(", ", twinsInRedmond43UsingCellular.Select(t => t.DeviceId)));
217+
}
218+
```
219+
220+
The **RegistryManager** class exposes all the methods required to interact with device twins from the service. The previous code first initializes the **registryManager** object, then retrieves the device twin for **myDeviceId**, and finally updates its tags with the desired location information.
221+
222+
After updating, it executes two queries: the first selects only the device twins of devices located in the **Redmond43** plant, and the second refines the query to select only the devices that are also connected through cellular network.
223+
224+
The previous code, when it creates the **query** object, specifies a maximum number of returned documents. The **query** object contains a **HasMoreResults** boolean property that you can use to invoke the **GetNextAsTwinAsync** methods multiple times to retrieve all results. A method called **GetNextAsJson** is available for results that are not device twins, for example, results of aggregation queries.
225+
226+
1. Finally, add the following lines to the **Main** method:
227+
228+
```csharp
229+
registryManager = RegistryManager.CreateFromConnectionString(connectionString);
230+
AddTagsAndQuery().Wait();
231+
Console.WriteLine("Press Enter to exit.");
232+
Console.ReadLine();
233+
```
234+
235+
1. Run this application by right-clicking on the **AddTagsAndQuery** project and selecting **Debug**, followed by **Start new instance**. You should see one device in the results for the query asking for all devices located in **Redmond43** and none for the query that restricts the results to devices that use a cellular network.
236+
237+
![Query results in window](./media/iot-hub-csharp-csharp-twin-getstarted/addtagapp.png)
238+
237239
In this article, you:
238240

239241
* Configured a new IoT hub in the Azure portal

0 commit comments

Comments
 (0)