![](/img/trans.png)
[英]Reading & parsing Azure IoT Hub Telemetry from a storage account blob
[英]Read Azure IoT Hub Telemetry from HttpTrigger Function
用例
我有一個將遙測數據發送到 IoT 中心的 IoT 中心設備。 我想使用 Function 處理遙測,例如存儲到數據庫。
功能
我在VS2019中創建了如下function並發布到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 Hub 中,我使用 Web Hook Endpoint 類型創建了一個觸發 Function 的事件訂閱。
問題
事件數據的主體似乎已加密(?):
{{
"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="
}}
雖然在雲端 Shell 我可以看到可讀數據。 我還可以通過使用 EventHubClient in.Net 將設備讀取到雲消息來查看可讀數據。
我錯過了什么? 我怎樣才能解密身體?
您的設備在未指定content-type和content-encoding的情況下發送了遙測數據,請參閱systemProperties object 中缺少這些屬性。
設備在發送遙測數據時需要填充這些系統屬性,然后您將在事件消息中看到:
"systemProperties":{
"iothub-content-type":"application/json",
"iothub-content-encoding":"utf-8",
...
並且事件的data.body將是 json 格式的文本。
更多細節在這里。
主體為 Base64 編碼,您可以使用https://docs.microsoft.com/en-us/dotnet/api/system.convert.frombase64string?view=netframework-4.8對其進行解碼
這是您消息的可讀正文: {"DateTime":"2019-10-05T10:09:29","ActualTarrif":1,"ActualPowerDelivered":271.0,"TotalElectricityDeliveredTarrif1":6015577.0,"TotalElectricityDeliveredTarrif2":6027952.0}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.