Skip to content

Commit cd090f5

Browse files
authored
Adding Azure plug and play support (#23)
1 parent 536e62a commit cd090f5

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

Azure.Devices.DeviceClient/DeviceClient.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Security.Cryptography.X509Certificates;
1111
using System.Text;
1212
using System.Threading;
13+
using System.Web;
1314

1415
namespace nanoFramework.Azure.Devices.Client
1516
{
@@ -62,7 +63,8 @@ public class DeviceClient : IDisposable
6263
/// <param name="sasKey">One of the SAS Key either primary, either secondary.</param>
6364
/// <param name="qosLevel">The default quality level delivery for the MQTT messages, default to the lower quality</param>
6465
/// <param name="azureCert">Azure certificate for the connection to Azure IoT Hub</param>
65-
public DeviceClient(string iotHubName, string deviceId, string sasKey, MqttQoSLevel qosLevel = MqttQoSLevel.AtMostOnce, X509Certificate azureCert = null)
66+
/// <param name="modelId">Azure Plug and Play model ID</param>
67+
public DeviceClient(string iotHubName, string deviceId, string sasKey, MqttQoSLevel qosLevel = MqttQoSLevel.AtMostOnce, X509Certificate azureCert = null, string modelId = null)
6668
{
6769
_clientCert = null;
6870
_privateKey = null;
@@ -75,6 +77,7 @@ public DeviceClient(string iotHubName, string deviceId, string sasKey, MqttQoSLe
7577
_deviceMessageTopic = $"devices/{_deviceId}/messages/devicebound/";
7678
QosLevel = qosLevel;
7779
_azureRootCACert = azureCert;
80+
ModelId = modelId;
7881
}
7982

8083
/// <summary>
@@ -85,7 +88,8 @@ public DeviceClient(string iotHubName, string deviceId, string sasKey, MqttQoSLe
8588
/// <param name="clientCert">The certificate to connect the device (containing both public and private keys).</param>
8689
/// <param name="qosLevel">The default quality of assurance level for delivery for the MQTT messages (defaults to the lowest quality).</param>
8790
/// /// <param name="azureCert">Azure certificate for the connection to Azure IoT Hub</param>
88-
public DeviceClient(string iotHubName, string deviceId, X509Certificate2 clientCert, MqttQoSLevel qosLevel = MqttQoSLevel.AtMostOnce, X509Certificate azureCert = null)
91+
/// /// <param name="modelId">Azure Plug and Play model ID</param>
92+
public DeviceClient(string iotHubName, string deviceId, X509Certificate2 clientCert, MqttQoSLevel qosLevel = MqttQoSLevel.AtMostOnce, X509Certificate azureCert = null, string modelId = null)
8993
{
9094
_clientCert = clientCert;
9195
_privateKey = Convert.ToBase64String(clientCert.PrivateKey);
@@ -98,8 +102,14 @@ public DeviceClient(string iotHubName, string deviceId, X509Certificate2 clientC
98102
_deviceMessageTopic = $"devices/{_deviceId}/messages/devicebound/";
99103
QosLevel = qosLevel;
100104
_azureRootCACert = azureCert;
105+
ModelId = modelId;
101106
}
102107

108+
/// <summary>
109+
/// Azure Plug and Play model ID
110+
/// </summary>
111+
public string ModelId { get; internal set; }
112+
103113
/// <summary>
104114
/// The latest Twin received.
105115
/// </summary>
@@ -142,11 +152,18 @@ public bool Open()
142152
// event when connection has been dropped
143153
_mqttc.ConnectionClosed += ClientConnectionClosed;
144154

155+
string userName = $"{_iotHubName}/{_deviceId}/api-version=2020-09-30";
156+
if (!string.IsNullOrEmpty(ModelId))
157+
{
158+
userName += $"&model-id={HttpUtility.UrlEncode(ModelId)}";
159+
160+
}
161+
145162
// Now connect the device
146163
string key = _clientCert == null ? Helper.GetSharedAccessSignature(null, _sasKey, $"{_iotHubName}/devices/{_deviceId}", new TimeSpan(24, 0, 0)) : _privateKey;
147164
_mqttc.Connect(
148165
_deviceId,
149-
$"{_iotHubName}/{_deviceId}/api-version=2020-09-30",
166+
userName,
150167
key,
151168
false,
152169
MqttQoSLevel.ExactlyOnce,
@@ -253,7 +270,7 @@ public bool UpdateReportedProperties(TwinCollection reported, CancellationToken
253270
StatusUpdated?.Invoke(this, new StatusUpdatedEventArgs(_ioTHubStatus));
254271

255272
if (cancellationToken.CanBeCanceled)
256-
{
273+
{
257274
_waitForConfirmation.Add(conf);
258275
while (!conf.Received && !cancellationToken.IsCancellationRequested)
259276
{

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ DeviceClient azureIoT = new DeviceClient(IotBrokerAddress, DeviceID, SasKey);
100100

101101
Note: please see the previous section to understand how to better parse the certificate for your usage. The example shows the certificate uploaded into the device and not in the code.
102102

103+
### Azure Plug&Play
104+
105+
Azure Plug&Play is supported as well. You'll need to provide a model ID when creating the DeviceClient:
106+
107+
```csharp
108+
DeviceClient azureIoT = new DeviceClient(IotBrokerAddress, DeviceID, SasKey, modelID:"dtmi:com:example:Thermostat;1");
109+
```
110+
111+
Note: the model ID has to be passed at the creation of the DeviceClient, it is not possible to pass it later on.
112+
103113
### Getting and updating Twin
104114

105115
You can request your Azure IoT Twin simply by calling the `GetTwin` function.

0 commit comments

Comments
 (0)