How to correctly receive IotHub command (message)? - Damir Dobric Posts - developers.de

How to correctly receive IotHub command (message)?

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives


When using IotHub, we can send Cloud-to-Device messages from any application to device, without of need to tune anything in the infrastructure.  As long device is connected to internet, we can send (push) messages (commands) to it.

Following code snippet shows how to start receiver loop and how to receive message and convert it into dynamic type instance. At the end the code demonstrates how to complete and/or abandon the message.

  private static Task runCommandLoopReceiver(CancellationToken cancelToken)
  {           
           
return Task
.Run(() =>
            {
               
// Goto DeviceExplorer and copy copy
                // Shared Access Policy for your device.

               
string connectionString = "HostName=yourhubname.azure-
                devices.de;DeviceId=mypidevice;SharedAccessKey=**="
;

               
var deviceClient =
                eviceClient
.CreateFromConnectionString(connectionString); 

                while (true)
                {
                   
var
msg = deviceClient.ReceiveAsync().AsTask().Result;
                   
if (msg != null
)
                    {
                       
try

                        {
                           
dynamic cmd = 
                           
JsonConvert.DeserializeObject<dynamic>
                            (
Encoding
.UTF8.GetString(msg.GetBytes()));

                           
if ((dynamic)cmd.CommandType == "led"
)
                            {
                               
if ((dynamic)cmd.Data == "on"
)
                                {
                                    m_LedPin.Write(
GpioPinValue
.High);
                                }
                               
else

                                {
                                    m_LedPin.Write(
GpioPinValue.Low);
                                }

                               
// This removes a message from device command queue.

                                deviceClient.CompleteAsync(msg).AsTask().Wait();
                            }
                           
else
                            {
                               
Debug.WriteLine("Unknown command.");

                               
// Here we remove unknown commands from the queue.

                                deviceClient.AbandonAsync(msg).AsTask().Wait();
                            }
                        }
                       
catch (Exception ex)
                        {
                          

                          
Debug.WriteLine(ex.Message);
                          

                        }
                    }

                   
Task
.Delay(1000).Wait();
                }



             


Posted May 26 2016, 07:31 AM by Damir Dobric
developers.de is a .Net Community Blog powered by daenet GmbH.