Shadesfear
Shadesfear

Reputation: 749

Getting deviceId from iothub in azure function

I am in the middle of a project where I having to connect some IoT devices to azure using IoTHub. I have been following this guide: https://learn.microsoft.com/en-us/samples/azure-samples/functions-js-iot-hub-processing/processing-data-from-iot-hub-with-azure-functions/

And things are working fine, I have a device connected to the IoTHub called MyPythonDevice, so now in my code I would like to see this deviceId. In the examples given in the article above, we see a deviceId, but for me that is undefined if I log it.

So I was searching and found the following code snippet:

context.log(context.bindingData.systemProperties["iothub-connection-device-id"])

But this returns the follwing

Exception: TypeError: Cannot read property 'iothub-connection-device-id' of undefined

That means that systemProperties is undefined..

Any help on how to get the deviceId ?

Upvotes: 3

Views: 926

Answers (2)

Gowtham
Gowtham

Reputation: 767

This Works:

    public static string Run([EventHubTrigger("EventHubName", Connection = "EventHubConnectionAppSetting", ConsumerGroup = "xxxxx")] EventData eventMsg, DateTime enqueuedTimeUtc, ILogger log) {
var deviceId = eventMsg.SystemProperties["iothub-connection-device-id"].ToString();}

Upvotes: 0

kgalic
kgalic

Reputation: 2654

Try this:

context.bindingData.systemPropertiesArray[0]["iothub-connection-device-id"]

Upvotes: 6

Related Questions