Skip to content

Commit b0b6a34

Browse files
authored
Add Pnp convention helper class (#184)
- Update README with instructions on how to use it.
1 parent f51c2ae commit b0b6a34

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,34 @@ if(!res)
371371
}
372372
```
373373

374+
In case a [DPS model](https://github.com/Azure/opendigitaltwins-dtdl/blob/master/DTDL/v2/dtdlv2.md) is going to be used, the ID of the model has to be passed to the ProvisioningDeviceClient and DeviceClient constructor.
375+
The code above requires the following changes.
376+
377+
Add the model ID as a constant:
378+
379+
```csharp
380+
public const string ModelId = "dtmi:orgpal:palthree:palthree_demo_0;1";
381+
382+
```
383+
384+
Create the additional payload information with the model ID to be sent along the registration with DPS and pass that to the call to `Register()`.
385+
386+
```csharp
387+
var pnpPayload = new ProvisioningRegistrationAdditionalData
388+
{
389+
JsonData = PnpConvention.CreateDpsPayload(ModelId),
390+
};
391+
392+
var myDevice = provisioning.Register(pnpPayload, new CancellationTokenSource(60000).Token);
393+
394+
```
395+
396+
Create the device client passing the model ID to the respective parameter in the constructor.
397+
398+
```csharp
399+
var device = new DeviceClient(myDevice.AssignedHub, myDevice.DeviceId, SasKey, nanoFramework.M2Mqtt.Messages.MqttQoSLevel.AtLeastOnce, azureCA, ModelId);
400+
```
401+
374402
Note: like for the `DeviceClient` you need to make sure you are connected to a network properly and also have a proper data and time set on the device.
375403

376404
### Provisioning using certificates

nanoFramework.Azure.Devices.Client/Azure.Devices.Client.nfproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<Compile Include="IoTHubStatus.cs" />
3737
<Compile Include="MethodCallback.cs" />
3838
<Compile Include="DeviceClient.cs" />
39+
<Compile Include="PlugAndPlay\PnpConvention.cs" />
3940
<Compile Include="Properties\AssemblyInfo.cs" />
4041
<Compile Include="SHA256.cs" />
4142
<Compile Include="PropertyAcknowledge.cs" />
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
4+
// See LICENSE file in the project root for full license information.
5+
//
6+
7+
using System;
8+
9+
namespace nanoFramework.Azure.Devices.Provisioning.Client.PlugAndPlay
10+
{
11+
/// <summary>
12+
/// A helper class for formatting the DPS device registration payload, per plug and play convention.
13+
/// </summary>
14+
public static class PnpConvention
15+
{
16+
/// <summary>
17+
/// Create the DPS payload to provision a device as plug and play.
18+
/// </summary>
19+
/// <remarks>
20+
/// For more information on device provisioning service and plug and play compatibility,
21+
/// and PnP device certification, see <see href="https://docs.microsoft.com/azure/iot-pnp/howto-certify-device"/>.
22+
/// The DPS payload should be in the format:
23+
/// <c>
24+
/// {
25+
/// "modelId": "dtmi:com:example:modelName;1"
26+
/// }
27+
/// </c>
28+
/// For information on DTDL, see <see href="https://github.com/Azure/opendigitaltwins-dtdl/blob/master/DTDL/v2/dtdlv2.md"/>
29+
/// </remarks>
30+
/// <param name="modelId">The Id of the model the device adheres to for properties, telemetry, and commands.</param>
31+
/// <returns>The DPS payload to provision a device as plug and play.</returns>
32+
/// <exception cref="ArgumentNullException">If modelId is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
33+
public static string CreateDpsPayload(string modelId)
34+
{
35+
if (string.IsNullOrEmpty(modelId))
36+
{
37+
throw new ArgumentNullException();
38+
}
39+
40+
return $"{{\"modelId\":\"{modelId}\"}}";
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)