azure - IOT 中心事件转化为函数

标签 azure azure-functions azure-iot-hub

我创建了一个非常简单的 Azure 函数,它连接到 IoT 中心以捕获内置事件端点(使用自定义消费者组,但这不是问题)。

我遇到的问题是发送到端点的消息。它无法识别哪个设备或哪个模块,甚至事件是什么。是设备孪生更新、来自模块的消息、模块孪生更新——以及在哪里——来自设备代码、模块代码?我在这里错过了什么吗?如何确定哪个设备/模块等发送了事件?

屏幕截图显示了预设的消息传递 - 基本上是获取事件消息并将其转储到日志(函数的默认构造函数)。

附带的预设代码:

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}");

}

function integrate

function log

最佳答案

您应该更改函数以接受 EventData 作为输入参数而不是字符串。这将使您能够访问事件元数据,例如来自 SystemProperties 的设备 ID:

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}");
}

参见this link获取系统属性列表。

关于azure - IOT 中心事件转化为函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49366586/

相关文章:

azure - IoT Edge 代理上的日志轮换

azure - 如何从移动应用程序配置 Azure IoT 设备

azure-iot-hub - Azure IoT Central 将遥测数据存储多长时间?

azure - 当用户在 Web 应用程序中进行身份验证时,对 azure 函数进行用户身份验证

将数据库链接到 MVC 项目和移动应用程序 Xamarin 的 API

c# - 从 Azure Active Directory 获取 MVC Web App 中的 samAccountName 名称

如果我想在我的nodejs函数中调用Linux可执行库,Azure函数: do I have to use Premium plan's custom linux image feature,?

azure - 使用脚本重新创建 cosmos DB

azure - 如何执行一次 Azure 函数?

用于文件迁移的 Azure 逻辑应用