简体   繁体   中英

How to receive from IoT Hub the all messages a specific device sent?

The code sends a message to the IoT Hub which stores the message in a BLOB storage.

_device = DeviceClient.Create(_iotHubUri, new 
DeviceAuthenticationWithRegistrySymmetricKey(_deviceId, _deviceKey), 
TransportType.Amqp_Tcp_Only);

await _device.OpenAsync();

await _device.SendEventAsync(message);

I see the message in the Azure portal.

What I do not understand is how to get the all messages (C#) from the IoT Hub the device has sent?

The Azure IoT Hub is an entry gateway for real-time iot streaming pipe from the devices. From this iot stream pipe can be captured (filtered) any specific events, analyzing events in the time window, etc.

Basically, (from the Azure IoT Hub point of the view) there are two places where can be this streaming pipe filtered, such as:

  1. With a built-in Azure IoT Hub Routes feature, where the device event can be routed based on the routing query string to the custom endpoints, see more details here . Note, there is a some limitation, like max number (100) of routes and max number (10) of custom endpoints.

  2. Outside of the Azure IoT Hub as a consumer of the stream pipe. The simple way is to use an Azure EventHubTrigger function, see more details in the sample .

The following code snippet shows an example of the EventHubTrigger function:

    using System;

    public static void Run(string myIoTHubMessage, IDictionary<string, object> properties, IDictionary<string, object> systemproperties, TraceWriter log)
    {
       log.Info($"C# IoT Hub trigger function processed a message: \n\t{myIoTHubMessage}");

       log.Info($"\nSystemProperties:\n\t{string.Join("\n\t", systemproperties.Select(i => $"{i.Key}={i.Value}"))}");

       log.Info($"\nProperties:\n\t{string.Join("\n\t", properties.Select(i => $"{i.Key}={i.Value}"))}");
    }

function.json file:

    {
      "bindings": [
       {
         "type": "eventHubTrigger",
         "name": "myIoTHubMessage",
         "direction": "in",
         "path": "myIoTHubName",
         "connection": "myIoTHubConnectionString",
         "consumerGroup": "function"
       }
     ],
     "disabled": false
   }

Note, that the deviceId can be obtained from the systemproperties["iothub-connection-device-id"].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM