简体   繁体   English

如何从IoT中心接收特定设备发送的所有消息?

[英]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. 该代码将消息发送到IoT中心,该中心将消息存储在BLOB存储中。

_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. 我在Azure门户中看到该消息。

What I do not understand is how to get the all messages (C#) from the IoT Hub the device has sent? 我不明白的是如何从设备已发送的IoT中心获取所有消息 (C#)?

The Azure IoT Hub is an entry gateway for real-time iot streaming pipe from the devices. Azure IoT中心是用于设备中实时物联网流管道的入口网关。 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: 基本上,(从Azure IoT中心的角度来看),可以在两个地方过滤此流管道,例如:

  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 . 借助内置的Azure IoT中心路由功能,可以根据路由查询字符串将设备事件路由到自定义终结点,请参阅此处的更多详细信息。 Note, there is a some limitation, like max number (100) of routes and max number (10) of custom endpoints. 请注意,存在一些限制,例如路由的最大数量(100)和自定义端点的最大数量(10)。

  2. Outside of the Azure IoT Hub as a consumer of the stream pipe. 在Azure IoT中心之外,作为流管道的使用者。 The simple way is to use an Azure EventHubTrigger function, see more details in the sample . 简单的方法是使用Azure EventHubTrigger函数,请参见示例中的更多详细信息。

The following code snippet shows an example of the EventHubTrigger function: 下面的代码片段显示了EventHubTrigger函数的示例:

    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: function.json文件:

    {
      "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"]. 注意,可以从systemproperties [“ iothub-connection-device-id”]获取deviceId。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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