Azure IoT Hub, EventHub and Functions

前端 未结 3 605
失恋的感觉
失恋的感觉 2021-01-12 03:20

I have an IoTHub with a route that points to an EventHub which triggers a Functions.

I\'m having problem getting the DeviceId and other IoT Hub properties

3条回答
  •  星月不相逢
    2021-01-12 04:09

    I missed a thing in the documentation for EventData. It has a method called GetBytes() and returns the body as a byte array. Example of getting both the IoT Hub properties and the body:

    public static async void Run(EventData telemetryMessage, TraceWriter log)
    {
        var deviceId = GetDeviceId(telemetryMessage);
        var payload = GetPayload(telemetryMessage.GetBytes());
    
        log.Info($"C# Event Hub trigger function processed a message.  deviceId: { deviceId }, payload: { JsonConvert.SerializeObject(payload) }");
    }
    
    private static Payload GetPayload(byte[] body)
    {
        var json = System.Text.Encoding.UTF8.GetString(body);
        return JsonConvert.DeserializeObject(json);
    }
    
    private static string GetDeviceId(EventData message)
    {
        return message.SystemProperties["iothub-connection-device-id"].ToString();
    }
    

提交回复
热议问题