Class DeviceClient
- Namespace
- nanoFramework.Azure.Devices.Client
- Assembly
- nanoFramework.Azure.Devices.Client.dll
Azure IoT Client SDK for .NET nanoFramework using MQTT.
public class DeviceClient : IDisposable
- Inheritance
-
DeviceClient
- Implements
- Inherited Members
- Extension Methods
Constructors
DeviceClient(string, string, byte[], MqttQoSLevel, byte[], string)
Creates an DeviceClient class.
public DeviceClient(string iotHubName, string deviceId, byte[] clientCert = null, MqttQoSLevel qosLevel = MqttQoSLevel.AtMostOnce, byte[] azureCert = null, string modelId = null)
Parameters
iotHubName
stringYour Azure IoT Hub fully qualified domain name (example: youriothub.azure-devices.net).
deviceId
stringThe device ID (name of your device).
clientCert
byte[]The certificate to connect the device (containing both public and private keys). Pass null if you are using the certificate store on the device.
qosLevel
MqttQoSLevelThe default quality of assurance level for delivery for the MQTT messages (defaults to the lowest quality).
azureCert
byte[]Azure certificate for the connection to Azure IoT Hub.
modelId
stringAzure Plug and Play model ID.
DeviceClient(string, string, X509Certificate2, MqttQoSLevel, X509Certificate, string)
Creates an DeviceClient class.
public DeviceClient(string iotHubName, string deviceId, X509Certificate2 clientCert, MqttQoSLevel qosLevel = MqttQoSLevel.AtMostOnce, X509Certificate azureCert = null, string modelId = null)
Parameters
iotHubName
stringYour Azure IoT Hub fully qualified domain name (example: youriothub.azure-devices.net).
deviceId
stringThe device ID (name of your device).
clientCert
X509Certificate2The certificate to connect the device (containing both public and private keys). Pass null if you are using the certificate store on the device.
qosLevel
MqttQoSLevelThe default quality of assurance level for delivery for the MQTT messages (defaults to the lowest quality).
azureCert
X509CertificateAzure certificate for the connection to Azure IoT Hub.
modelId
stringAzure Plug and Play model ID.
DeviceClient(string, string, string, byte[], MqttQoSLevel, byte[], string)
Creates an DeviceClient class.
public DeviceClient(string iotHubName, string deviceId, string moduleId, byte[] clientCert = null, MqttQoSLevel qosLevel = MqttQoSLevel.AtMostOnce, byte[] azureCert = null, string modelId = null)
Parameters
iotHubName
stringYour Azure IoT Hub fully qualified domain name (example: youriothub.azure-devices.net).
deviceId
stringThe device ID (name of your device).
moduleId
stringThe module ID which is attached to the device ID.
clientCert
byte[]The certificate to connect the device (containing both public and private keys). Pass null if you are using the certificate store on the device.
qosLevel
MqttQoSLevelThe default quality of assurance level for delivery for the MQTT messages (defaults to the lowest quality).
azureCert
byte[]Azure certificate for the connection to Azure IoT Hub.
modelId
stringAzure Plug and Play model ID.
DeviceClient(string, string, string, X509Certificate2, MqttQoSLevel, X509Certificate, string)
Creates an DeviceClient class.
public DeviceClient(string iotHubName, string deviceId, string moduleId, X509Certificate2 clientCert, MqttQoSLevel qosLevel = MqttQoSLevel.AtMostOnce, X509Certificate azureCert = null, string modelId = null)
Parameters
iotHubName
stringYour Azure IoT Hub fully qualified domain name (example: youriothub.azure-devices.net).
deviceId
stringThe device ID (name of your device).
moduleId
stringThe module ID which is attached to the device ID.
clientCert
X509Certificate2The certificate to connect the device (containing both public and private keys). Pass null if you are using the certificate store on the device.
qosLevel
MqttQoSLevelThe default quality of assurance level for delivery for the MQTT messages (defaults to the lowest quality).
azureCert
X509CertificateAzure certificate for the connection to Azure IoT Hub.
modelId
stringAzure Plug and Play model ID.
DeviceClient(string, string, string, string, MqttQoSLevel, byte[], string)
Creates an DeviceClient class.
public DeviceClient(string iotHubName, string deviceId, string moduleId, string sasKey, MqttQoSLevel qosLevel = MqttQoSLevel.AtLeastOnce, byte[] azureCert = null, string modelId = null)
Parameters
iotHubName
stringThe Azure IoT name fully qualified (ex: youriothub.azure-devices.net).
deviceId
stringThe device ID which is the name of your device.
moduleId
stringThe module ID which is attached to the device ID.
sasKey
stringOne of the SAS Key either primary, either secondary.
qosLevel
MqttQoSLevelThe default quality level delivery for the MQTT messages, default to the lower quality.
azureCert
byte[]Azure certificate for the connection to Azure IoT Hub.
modelId
stringAzure Plug and Play model ID.
DeviceClient(string, string, string, string, MqttQoSLevel, X509Certificate, string)
Creates an DeviceClient class.
public DeviceClient(string iotHubName, string deviceId, string moduleId, string sasKey, MqttQoSLevel qosLevel = MqttQoSLevel.AtLeastOnce, X509Certificate azureCert = null, string modelId = null)
Parameters
iotHubName
stringThe Azure IoT name fully qualified (ex: youriothub.azure-devices.net).
deviceId
stringThe device ID which is the name of your device.
moduleId
stringThe module ID which is attached to the device ID.
sasKey
stringOne of the SAS Key either primary, either secondary.
qosLevel
MqttQoSLevelThe default quality level delivery for the MQTT messages, default to the lower quality.
azureCert
X509CertificateAzure certificate for the connection to Azure IoT Hub.
modelId
stringAzure Plug and Play model ID.
DeviceClient(string, string, string, MqttQoSLevel, byte[], string)
Creates an DeviceClient class.
public DeviceClient(string iotHubName, string deviceId, string sasKey, MqttQoSLevel qosLevel = MqttQoSLevel.AtLeastOnce, byte[] azureCert = null, string modelId = null)
Parameters
iotHubName
stringThe Azure IoT name fully qualified (ex: youriothub.azure-devices.net).
deviceId
stringThe device ID which is the name of your device.
sasKey
stringOne of the SAS Key either primary, either secondary.
qosLevel
MqttQoSLevelThe default quality level delivery for the MQTT messages, default to the lower quality.
azureCert
byte[]Azure certificate for the connection to Azure IoT Hub.
modelId
stringAzure Plug and Play model ID.
DeviceClient(string, string, string, MqttQoSLevel, X509Certificate, string)
Creates an DeviceClient class.
public DeviceClient(string iotHubName, string deviceId, string sasKey, MqttQoSLevel qosLevel = MqttQoSLevel.AtLeastOnce, X509Certificate azureCert = null, string modelId = null)
Parameters
iotHubName
stringThe Azure IoT name fully qualified (ex: youriothub.azure-devices.net).
deviceId
stringThe device ID which is the name of your device.
sasKey
stringOne of the SAS Key either primary, either secondary.
qosLevel
MqttQoSLevelThe default quality level delivery for the MQTT messages, default to the lower quality.
azureCert
X509CertificateAzure certificate for the connection to Azure IoT Hub.
modelId
stringAzure Plug and Play model ID.
DeviceClient(IMqttClient, string, string, byte[], MqttQoSLevel, byte[], string)
Creates an DeviceClient class.
public DeviceClient(IMqttClient mqttc, string iotHubName, string deviceId, byte[] clientCert = null, MqttQoSLevel qosLevel = MqttQoSLevel.AtMostOnce, byte[] azureCert = null, string modelId = null)
Parameters
mqttc
IMqttClientThe MQTT client to use.
iotHubName
stringYour Azure IoT Hub fully qualified domain name (example: youriothub.azure-devices.net).
deviceId
stringThe device ID (name of your device).
clientCert
byte[]The certificate to connect the device (containing both public and private keys). Pass null if you are using the certificate store on the device.
qosLevel
MqttQoSLevelThe default quality of assurance level for delivery for the MQTT messages (defaults to the lowest quality).
azureCert
byte[]Azure certificate for the connection to Azure IoT Hub.
modelId
stringAzure Plug and Play model ID.
DeviceClient(IMqttClient, string, string, string, MqttQoSLevel, byte[], string)
Creates an DeviceClient class.
public DeviceClient(IMqttClient mqttc, string iotHubName, string deviceId, string sasKey, MqttQoSLevel qosLevel = MqttQoSLevel.AtLeastOnce, byte[] azureCert = null, string modelId = null)
Parameters
mqttc
IMqttClientThe MQTT client to use.
iotHubName
stringThe Azure IoT name fully qualified (ex: youriothub.azure-devices.net).
deviceId
stringThe device ID which is the name of your device.
sasKey
stringOne of the SAS Key either primary, either secondary.
qosLevel
MqttQoSLevelThe default quality level delivery for the MQTT messages, default to the lower quality.
azureCert
byte[]Azure certificate for the connection to Azure IoT Hub.
modelId
stringAzure Plug and Play model ID.
Properties
IoTHubStatus
The latest status.
public IoTHubStatus IoTHubStatus { get; }
Property Value
IsConnected
True if the device connected.
public bool IsConnected { get; }
Property Value
LastTwin
The latest Twin received.
public Twin LastTwin { get; }
Property Value
ModelId
Azure Plug and Play model ID.
public string ModelId { get; }
Property Value
ModuleId
The module ID attached to the device ID.
public string ModuleId { get; }
Property Value
QosLevel
The default level quality.
public MqttQoSLevel QosLevel { get; set; }
Property Value
Methods
AddMethodCallback(MethodCallback)
Add a callback method.
public void AddMethodCallback(MethodCallback methodCallback)
Parameters
methodCallback
MethodCallbackThe callback method to add.
Close()
Close the connection with Azure IoT and disconnect the device.
public void Close()
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
GetTwin(CancellationToken)
Gets the twin.
public Twin GetTwin(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenA cancellation token.
Returns
- Twin
The twin.
Remarks
It is strongly recommended to use a cancellation token that can be canceled and manage this on the caller code level. A reasonable time of few seconds is recommended with a retry mechanism.
Open()
Open the connection with Azure IoT. This will initiate a connection from the device to the Azure IoT Hub instance.
public bool Open()
Returns
- bool
True if open.
Reconnect()
Reconnect to Azure Iot Hub.
public void Reconnect()
RemoveMethodCallback(MethodCallback)
Remove a callback method.
public void RemoveMethodCallback(MethodCallback methodCallback)
Parameters
methodCallback
MethodCallbackThe callback method to remove.
SendMessage(string, string, ArrayList, CancellationToken, string)
Send a message to Azure IoT.
public bool SendMessage(string message, string contentType, ArrayList userProperties, CancellationToken cancellationToken = default, string dtdlComponentname = "")
Parameters
message
stringThe message to send.
contentType
stringContent of the application message.
userProperties
ArrayListUser properties to add to the application message.
cancellationToken
CancellationTokenA cancellation token. If you use the default one, the confirmation of delivery will not be awaited.
dtdlComponentname
stringThe DTDL component name.
Returns
- bool
True for successful message delivery.
SendMessage(string, string, CancellationToken)
Send a message to Azure IoT.
public bool SendMessage(string message, string contentType, CancellationToken cancellationToken = default)
Parameters
message
stringThe message to send.
contentType
stringContent of the application message.
cancellationToken
CancellationTokenA cancellation token. If you use the default one, the confirmation of delivery will not be awaited.
Returns
- bool
True for successful message delivery.
SendMessage(string, CancellationToken, string)
Send a message to Azure IoT.
public bool SendMessage(string message, CancellationToken cancellationToken = default, string dtdlComponentname = "")
Parameters
message
stringThe message to send.
cancellationToken
CancellationTokenA cancellation token. If you use the default one, the confirmation of delivery will not be awaited.
dtdlComponentname
stringThe DTDL component name.
Returns
- bool
True for successful message delivery.
UpdateReportedProperties(TwinCollection, CancellationToken)
Update the twin reported properties.
public bool UpdateReportedProperties(TwinCollection reported, CancellationToken cancellationToken = default)
Parameters
reported
TwinCollectionThe reported properties.
cancellationToken
CancellationTokenA cancellation token. If you use the default one, the confirmation of delivery will not be awaited.
Returns
- bool
True for successful message delivery.
Events
CloudToDeviceMessage
Cloud to device message received event.
public event CloudToDeviceMessage CloudToDeviceMessage
Event Type
StatusUpdated
Status change event.
public event StatusUpdated StatusUpdated
Event Type
TwinUpdated
Device twin updated event.
public event TwinUpdated TwinUpdated