1010using System . Security . Cryptography . X509Certificates ;
1111using System . Text ;
1212using System . Threading ;
13+ using System . Web ;
1314
1415namespace 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 {
0 commit comments