问题
Use case
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.
Funtion
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);
}
Source: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-grid#use-an-http-trigger-as-an-event-grid-trigger
Event Subscription
In IoT Hub I created an Event Subscription which triggers the Function, using the Web Hook Endpoint type.
The problem
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="
}}
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?
回答1:
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",
...
and the data.body of the event will be a json formatted text.
More details here.
回答2:
The body is Base64 encoded, you can decode it with https://docs.microsoft.com/en-us/dotnet/api/system.convert.frombase64string?view=netframework-4.8
This is the readable body of your message: {"DateTime":"2019-10-05T10:09:29","ActualTarrif":1,"ActualPowerDelivered":271.0,"TotalElectricityDeliveredTarrif1":6015577.0,"TotalElectricityDeliveredTarrif2":6027952.0}
来源:https://stackoverflow.com/questions/58247124/read-azure-iot-hub-telemetry-from-httptrigger-function