将Azure功能连接到Iot Hub云到设备反馈端点

时间:2018-05-14 09:28:02

标签: c# azure-iot-hub

是否有可能以某种方式将Azure功能连接到Iot Hub云到设备反馈端点?看起来此端点与Azure事件中心不兼容。

编写自定义事件触发器?

我使用C#Azure功能。

2 个答案:

答案 0 :(得分:1)

目前,云到设备反馈端点不支持直接订阅Azure功能。

正常情况是我们应该在立即发送C2D消息后处理反馈。这是由IoT Hub Service SDK通过ServiceClient.GetFeedbackReceiver()实现的。有关处理C2D消息反馈的更多详细信息,请参阅Receive delivery feedback

如果您还想从Azure功能处理这些反馈,可以使用发送C2D消息的HTTP请求从 ServiceClient 转发它。然后,您可以使用Http触发器创建Azure函数以接收它。

如果您对Azure IoT Hub有任何想法和反馈,可以从here提交。

答案 1 :(得分:0)

是的,您可以为IoT Hub创建自定义功能。只要IoT Hub为Event Hub兼容端点发送新消息,就会运行此功能。 您可以按照以下步骤操作:

  1. 使用IoT Hub(事件中心)模板创建自定义功能。 enter image description here
  2. 使用以下内容创建名为 project.json 的json文件:

    {
      "frameworks": {
        "net46":{
          "dependencies": {
            "Microsoft.Azure.Devices": "1.4.1"
          }
        }
       }
    }
    
  3. 上传project.json文件,它用于引用Microsoft.Azure.Devices的程序集。您可以看到此document以获取更多信息。 enter image description here

  4. 将IoT Hub连接字符串添加到功能应用程序设置中。 enter image description here

  5. 将run.csx修改为以下代码:

    #r "Microsoft.ServiceBus"
    
    using System.Configuration;
    using System.Text;
    using System.Net;
    using Microsoft.Azure.Devices;
    using Microsoft.ServiceBus.Messaging;
    using Newtonsoft.Json;
    
    static Microsoft.Azure.Devices.ServiceClient client =     ServiceClient.CreateFromConnectionString(ConfigurationManager.AppSettings["iothubConnectionstring"]);
    
    public static async void Run(EventData myIoTHubMessage, TraceWriter log)
    {
        log.Info($"{myIoTHubMessage.SystemProperties["iothub-connection-device-id"]}");
        var deviceId = myIoTHubMessage.SystemProperties["iothub-connection-device-id"].ToString();
        var msg = JsonConvert.SerializeObject(new { temp = 20.5 });
        var c2dmsg = new Microsoft.Azure.Devices.Message(Encoding.ASCII.GetBytes(msg));
    
        await client.SendAsync(deviceId, c2dmsg);
    }
    
  6. 在保存并运行该功能之后,如果IoT Hub发送新消息,该功能将被触发,并且在该功能中它将发送云到设备消息。

相关问题