How to get deviceid of message in Azure function that is triggered by IOThub message

て烟熏妆下的殇ゞ 提交于 2019-12-11 17:03:10

问题


I have an Azure function that is triggered by IOThub. So in the Azure function, I have

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

How do I get the device id from the Event Data.

I tried

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

It gave an error saying

The given key was not present in the dictionary.

the following document says that https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-messages-construct

ConnectionDeviceId contains the deviceId. Would anybody know how to retrieve the deviceid from EventData or should I use some other class.


回答1:


You can get device ID from SystemProperties:

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);
    }


来源:https://stackoverflow.com/questions/48954173/how-to-get-deviceid-of-message-in-azure-function-that-is-triggered-by-iothub-mes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!