0

I am trying to read device Id from Event Hub (on the back of IoTHub) but my syntax in JS seems wrong.

module.exports = function (context, IoTHubMessages) {
context.log(`JavaScript eventhub trigger function called for message array: ${IoTHubMessages}`);

var deviceId = IoTHubMessages.SystemProperties["iothub-connection-device-id"];

The function returns an error: Exception: TypeError: Cannot read property 'iothub-connection-device-id' of undefined

I'm not entirely sure if "iothub-connection-device-id" is the correct name of the attribute on Event Hub but the problem seems to be with SystemProperties.

Appreciate any help.

Mark
  • 1
  • 2

4 Answers4

2

iothub-connection-device-id is the right key to use, you just have to use it on the right property bag. An unrelated GitHub issue https://github.com/Azure/azure-sdk-for-js/issues/7801 shows how this key is indeed available on each message.

Depending on the cardinality in your functions.json file, IotHubMessages will either be an array of messages or a single message. See IOTHubMessage.forEach is not a function? for more details.

If it is an array of messages, accessing SystemProperties directly on it will not work. You will need to loop through to access each message separately.

Do you see systemProperties on the individual messages? If yes, then message.systemProperties["iothub-connection-device-id"] should work.

Ramya Rao
  • 111
  • 5
1

You should be reading messages this way. Read this for more about regarding the topic - https://learn.microsoft.com/en-us/samples/azure-samples/functions-js-iot-hub-processing/processing-data-from-iot-hub-with-azure-functions/

    IoTHubMessages.forEach(message => {
    context.log(`Processed message: ${message}`);
    count++;
    totalTemperature += message.temperature;
    totalHumidity += message.humidity;
    deviceId = message.deviceId;
});
Serkant Karaca
  • 1,896
  • 9
  • 8
  • Thanks, I used this tutorial initially but it only shows how to retrieve an attribute from a string passed as the message. That's not a problem. What I want to do is to get the deviceId directly from the Hub so that I don't need to have it embedded in the payload. – Mark Feb 28 '20 at 16:21
  • I don't get what you mean by "getting it from hub". Hub won't have any direct affinity with the device however message will do. – Serkant Karaca Mar 02 '20 at 17:50
  • Every device must authenticate to the IotHub so it should always know which device sent the payload. It's a metadata property. I know how to query it from Stream Analytics but not from FunctionApp. – Mark Mar 04 '20 at 13:23
0

First, use the JSON.stringify to print you the payload received. Secondly i believe you should be able to access your device id by doing the following: message.annotations["iothub-connection-device-id"]. For more info, please reference to the Quickstart examples you have available in the Microsoft's Github repos. Navigate to the iot-hub\Quickstarts\read-d2c-messages folder and you should find the example of processing the message payload and printing the output.

Hugo Barona
  • 1,303
  • 9
  • 21
  • Thanks Hugo. Sadly, "annotations" is not recognized. – Mark Feb 28 '20 at 16:23
  • I also checked the repos but again they do not say anything about retrieving deviceId from the Hub and not from the payload. – Mark Feb 28 '20 at 16:24
0

I landed on this question when looking for the deviceId when a deviceTwinChange happens on on Azure Iot hub and the message it routed through event hub to my Azure function. In IotHubMessage I wave only getting reported or desired information. I was looking for the deviceId so i knew what device it came from

properties: { reported: { //everything in my reported section } }

But i found out this:

module.exports = function (context, IoTHubMessages) {

The device Id is in the context variable. I just did not on Azure Iot hub using an Azure function.

var deviceId = context.bindingData.systemProperties["iothub-connection-device-id"];

A bit annoying that the metadata is kept in context and there is no documentation about this.

Extra points: There is no application properties in the context. Does anyone know how to get application properties in the information send to the azure function? This is for when you enrich the data from azure iot hub

This is the information in azure iot hub when doing the routing. Just havent seen the information in my azure function come through.

Add up to 10 message enrichments per IoT Hub. These are added as application properties to messages sent to chosen endpoint(s).

Martin Naughton
  • 1,476
  • 17
  • 14