【问题标题】:How to get deviceid of message in Azure function that is triggered by IOThub message如何在 Azure 函数中获取由 IOThub 消息触发的消息的 deviceid
【发布时间】:2018-05-21 19:43:59
【问题描述】:

我有一个由 IOThub 触发的 Azure 函数。所以在 Azure 函数中,我有

public static async Task Run(EventData myIoTHubMessage1, TraceWriter log)

如何从事件数据中获取设备 ID。

我试过了

log.Info("devid="+myIoTHubMessage1.SystemProperties["ConnectionDeviceId"]);

它给出了一个错误提示

The given key was not present in the dictionary.

以下文件说 https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-messages-construct

ConnectionDeviceId 包含 deviceId。有人知道如何从 EventData 中检索设备 ID,还是应该使用其他类。

【问题讨论】:

    标签: azure azure-functions azure-iot-hub azure-iot-sdk


    【解决方案1】:

    您可以从SystemProperties获取设备ID:

    public static async Task Run(EventData myIoTHubMessage1, TraceWriter log)
    {
        var deviceId = myIoTHubMessage1.SystemProperties["iothub-connection-device-id"];
        // ....
    }
    

    【讨论】:

      【解决方案2】:
      for (EventData receivedEvent : receivedEvents) {
             String deviceId = (String) receivedEvent.getProperties().get("deviceId");
             log.info("From:" + deviceId);
          }
      

      【讨论】:

        猜你喜欢
        • 2016-06-15
        • 2017-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多