从HttpTrigger函数阅读Azure IoT中心遥测 [英] Read Azure IoT Hub Telemetry from HttpTrigger Function

查看:79
本文介绍了从HttpTrigger函数阅读Azure IoT中心遥测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用例

我有一个Iot Hub设备,它将遥测数据发送到IoT Hub. 我想使用功能来处理遥测,例如将其存储到数据库中.

I have an Iot Hub device which sends telemetry data to IoT Hub. I want to process the telemetry, e.g storing to a database, using a Function.

功能

我在VS2019中创建了以下功能,并将其发布到Azure:

I created the following function in VS2019 and published it to Azure:

[FunctionName("HttpTrigger")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")]HttpRequestMessage req,
ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    var messages = await req.Content.ReadAsAsync<JArray>();

    // If the request is for subscription validation, send back the validation code.
    if (messages.Count > 0 && string.Equals((string)messages[0]["eventType"],
        "Microsoft.EventGrid.SubscriptionValidationEvent",
        System.StringComparison.OrdinalIgnoreCase))
    {
        log.LogInformation("Validate request received");
        return req.CreateResponse<object>(new
        {
            validationResponse = messages[0]["data"]["validationCode"]
        });
    }

    // The request is not for subscription validation, so it's for one or more events.
    foreach (JObject message in messages)
    {
        // Handle one event.
        EventGridEvent eventGridEvent = message.ToObject<EventGridEvent>();
        log.LogInformation($"Subject: {eventGridEvent.Subject}");
        log.LogInformation($"Time: {eventGridEvent.EventTime}");
        log.LogInformation($"Event data: {eventGridEvent.Data.ToString()}");
    }

    return req.CreateResponse(HttpStatusCode.OK);
}

来源: 事件订阅

在IoT中心中,我创建了一个事件订阅,该事件订阅使用Web Hook端点类型触发了功能.

In IoT Hub I created an Event Subscription which triggers the Function, using the Web Hook Endpoint type.

问题

事件数据的主体似乎已加密(?):

The body of the event data seems to be encrypted (?):

{{
  "properties": {},
  "systemProperties": {
    "iothub-connection-device-id": "smartmeter",
    "iothub-connection-auth-method": "{\"scope\":\"device\",\"type\":\"sas\",\"issuer\":\"iothub\",\"acceptingIpFilterRule\":null}",
    "iothub-connection-auth-generation-id": "637057961942743477",
    "iothub-enqueuedtime": "2019-10-05T08:09:17.973Z",
    "iothub-message-source": "Telemetry"
  },
  "body": "eyJEYXRlVGltZSI6IjIwMTktMTAtMDVUMTA6MDk6MjkiLCJBY3R1YWxUYXJyaWYiOjEsIkFjdHVhbFBvd2VyRGVsaXZlcmVkIjoyNzEuMCwiVG90YWxFbGVjdHJpY2l0eURlbGl2ZXJlZFRhcnJpZjEiOjYwMTU1NzcuMCwiVG90YWxFbGVjdHJpY2l0eURlbGl2ZXJlZFRhcnJpZjIiOjYwMjc5NTIuMH0="
}}

尽管在Cloud Shell中我可以看到可读数据.我还可以通过使用.Net中的EventHubClient读取设备以将消息云化的消息来查看可读数据.

though in the Cloud Shell I can see the readable data. I can also see the readable data by reading the device to cloud messages with the EventHubClient in .Net.

我想念什么?我该如何解密尸体?

What am I missing? How can I decrypt the body?

推荐答案

您的设备发送遥测数据时未指定 content-type content-encoding ,看到丢失这些属性在 systemProperties 对象中.

your device sent the telemetry data without specifying the content-type and content-encoding, see missing these properties in the systemProperties object.

在发送遥测数据时,设备需要填充这些系统属性,然后您将在事件消息中看到:

The device needs to populate those system properties when sending a telemetry data, then you will see in the event message:

 "systemProperties":{
    "iothub-content-type":"application/json",
    "iothub-content-encoding":"utf-8",
    ...

事件的

data.body 将是json格式的文本.

and the data.body of the event will be a json formatted text.

更多详细信息这里.

这篇关于从HttpTrigger函数阅读Azure IoT中心遥测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆