Reputation: 3030
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}");
}
Upvotes: 0
Views: 586
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
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