Skip to content

Commit

Permalink
Add Pnp convention helper class (#184)
Browse files Browse the repository at this point in the history
- Update README with instructions on how to use it.
  • Loading branch information
josesimoes authored May 19, 2022
1 parent f51c2ae commit b0b6a34
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,34 @@ if(!res)
}
```

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.
The code above requires the following changes.

Add the model ID as a constant:

```csharp
public const string ModelId = "dtmi:orgpal:palthree:palthree_demo_0;1";

```

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()`.

```csharp
var pnpPayload = new ProvisioningRegistrationAdditionalData
{
JsonData = PnpConvention.CreateDpsPayload(ModelId),
};

var myDevice = provisioning.Register(pnpPayload, new CancellationTokenSource(60000).Token);

```

Create the device client passing the model ID to the respective parameter in the constructor.

```csharp
var device = new DeviceClient(myDevice.AssignedHub, myDevice.DeviceId, SasKey, nanoFramework.M2Mqtt.Messages.MqttQoSLevel.AtLeastOnce, azureCA, ModelId);
```

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.

### Provisioning using certificates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<Compile Include="IoTHubStatus.cs" />
<Compile Include="MethodCallback.cs" />
<Compile Include="DeviceClient.cs" />
<Compile Include="PlugAndPlay\PnpConvention.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SHA256.cs" />
<Compile Include="PropertyAcknowledge.cs" />
Expand Down
43 changes: 43 additions & 0 deletions nanoFramework.Azure.Devices.Client/PlugAndPlay/PnpConvention.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// Copyright (c) .NET Foundation and Contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//

using System;

namespace nanoFramework.Azure.Devices.Provisioning.Client.PlugAndPlay
{
/// <summary>
/// A helper class for formatting the DPS device registration payload, per plug and play convention.
/// </summary>
public static class PnpConvention
{
/// <summary>
/// Create the DPS payload to provision a device as plug and play.
/// </summary>
/// <remarks>
/// For more information on device provisioning service and plug and play compatibility,
/// and PnP device certification, see <see href="https://docs.microsoft.com/azure/iot-pnp/howto-certify-device"/>.
/// The DPS payload should be in the format:
/// <c>
/// {
/// "modelId": "dtmi:com:example:modelName;1"
/// }
/// </c>
/// For information on DTDL, see <see href="https://github.com/Azure/opendigitaltwins-dtdl/blob/master/DTDL/v2/dtdlv2.md"/>
/// </remarks>
/// <param name="modelId">The Id of the model the device adheres to for properties, telemetry, and commands.</param>
/// <returns>The DPS payload to provision a device as plug and play.</returns>
/// <exception cref="ArgumentNullException">If modelId is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
public static string CreateDpsPayload(string modelId)
{
if (string.IsNullOrEmpty(modelId))
{
throw new ArgumentNullException();
}

return $"{{\"modelId\":\"{modelId}\"}}";
}
}
}

0 comments on commit b0b6a34

Please sign in to comment.