Jason
Jason

Reputation: 3030

IOT Hub Events into Function

I have created a very simple Azure Function that wires into the IoT Hub to capture the built-in events endpoint (with a custom consumer group, but that's not the issue).

The issue I'm having is the message that's sent to the endpoint. It doesn't identify which device or which module, or even what the event is. Is it a device-twin update, a message from a module, a module-twin update - and where - from device code, module code? Am I missing something here? How can I determine which device/module, etc sent the event?

The screenshots show the canned messaging -- basically taking the event message and dumping it to logs (the default constructor for a function).

The canned code that came with it:

using System;
using System.Web.Http;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Shared;

public static void Run(string myIoTHubMessage, TraceWriter log) {

    log.Info($"C# IoT Hub trigger function processed a message: {myIoTHubMessage}");

}

function integrate

function log

Upvotes: 0

Views: 586

Answers (2)

Roman Kiss
Roman Kiss

Reputation: 8235

another option (no additional assemblies):

using System;

public static void Run(string myIoTHubMessage, IDictionary<string, object> properties, TraceWriter log)
{
   log.Info($"C# IoT Hub trigger function processed a message: {myIoTHubMessage}");
   foreach (var prop in properties)
      log.Info($"\t{prop.Key} = {prop.Value}");
}

Upvotes: 2

Mikhail Shilkov
Mikhail Shilkov

Reputation: 35134

You should change your function to accept EventData as input parameter instead of string. That will give you access to event metadata, e.g. device ID from SystemProperties:

public static async Task Run(EventData myIoTHubMessage, TraceWriter log)
{
    var deviceId = myIoTHubMessage.SystemProperties["iothub-connection-device-id"];
    log.Info($"C# IoT Hub trigger function processed a message from  {deviceId}");
}

See this link for a list of system properties.

Upvotes: 2

Related Questions