diff --git a/README.md b/README.md index 5c5dbaf..a6e2514 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/nanoFramework.Azure.Devices.Client/Azure.Devices.Client.nfproj b/nanoFramework.Azure.Devices.Client/Azure.Devices.Client.nfproj index ab8bf11..d001bc5 100644 --- a/nanoFramework.Azure.Devices.Client/Azure.Devices.Client.nfproj +++ b/nanoFramework.Azure.Devices.Client/Azure.Devices.Client.nfproj @@ -36,6 +36,7 @@ + diff --git a/nanoFramework.Azure.Devices.Client/PlugAndPlay/PnpConvention.cs b/nanoFramework.Azure.Devices.Client/PlugAndPlay/PnpConvention.cs new file mode 100644 index 0000000..b48e0f3 --- /dev/null +++ b/nanoFramework.Azure.Devices.Client/PlugAndPlay/PnpConvention.cs @@ -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 +{ + /// + /// A helper class for formatting the DPS device registration payload, per plug and play convention. + /// + public static class PnpConvention + { + /// + /// Create the DPS payload to provision a device as plug and play. + /// + /// + /// For more information on device provisioning service and plug and play compatibility, + /// and PnP device certification, see . + /// The DPS payload should be in the format: + /// + /// { + /// "modelId": "dtmi:com:example:modelName;1" + /// } + /// + /// For information on DTDL, see + /// + /// The Id of the model the device adheres to for properties, telemetry, and commands. + /// The DPS payload to provision a device as plug and play. + /// If modelId is or . + public static string CreateDpsPayload(string modelId) + { + if (string.IsNullOrEmpty(modelId)) + { + throw new ArgumentNullException(); + } + + return $"{{\"modelId\":\"{modelId}\"}}"; + } + } +}