MQTT 设备订阅的 Azure C2D 消息
Azure C2D message for MQTT device to subscribe to
我已成功将两个实际设备连接到 Azure IoTHub(在同一个 Iot Hub 中),并希望第二个设备接收第一个设备发送的消息。
因此,在普通的 MQTT 代理中,第二个设备只是订阅了该主题,但 Azure 没有普通的 MQTT 代理。
我现在要做的是编写一个 Azure 函数,每当通过事件中心触发器在 IoTHub 中接收到来自第一台设备的消息时,该函数就会触发;并向第二个设备发送带有接收到的消息(字符串)的 C2D 消息。为了实现第二个设备订阅这个主题:devices/secondDevice/messages/devicebound
这是我的函数
#r "Microsoft.Azure.EventHubs"
using System;
using System.Text;
using Microsoft.Azure.EventHubs;
using Microsoft.Azure.Devices;
static ServiceClient serviceClient;
static string connectionString = ".........pXH9WI.....";
static string targetDevice = "secondDevice";
public static async Task Run(EventData[] events, ILogger log)
{
var exceptions = new List<Exception>();
serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
foreach (EventData eventData in events)
{
try
{
string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
// Replace these two lines with your processing logic.
log.LogInformation($"C# Event Hub trigger function processed a message: {messageBody}");
await Task.Yield();
var commandMessage = new Message(Encoding.ASCII.GetBytes(messageBody));
await serviceClient.SendAsync(targetDevice, commandMessage); // send the message to the second device that the first device sent to IoTHub
}
catch (Exception e)
{
// We need to keep processing the rest of the batch - capture this exception and continue.
// Also, consider capturing details of the message that failed processing so it can be processed again later.
exceptions.Add(e);
}
}
// Once processing of the batch is complete, if any messages in the batch failed processing throw an exception so that there is a record of the failure.
if (exceptions.Count > 1)
throw new AggregateException(exceptions);
if (exceptions.Count == 1)
throw exceptions.Single();
}
这是 .json 代码:
{
"bindings": [
{
"type": "eventHubTrigger",
"name": "events",
"direction": "inout",
"eventHubName": "samples-workitems",
"cardinality": "many",
"connection": "myIoTHub_events_IOTHUB",
"consumerGroup": "$Default"
}
]
}
我正在接收第一台设备的消息,但我没有在第二台设备订阅的主题中看到消息。
有什么想法吗?我正在通过门户网站完成所有这些工作,因为我目前在使用 VSCode 时遇到很多问题,我想尽快解决这个 C2D 问题。
谢谢
第二台设备应根据 docs 订阅主题过滤器 #
。所以题目会变成:
devices/secondDevice/messages/devicebound/#
我已成功将两个实际设备连接到 Azure IoTHub(在同一个 Iot Hub 中),并希望第二个设备接收第一个设备发送的消息。 因此,在普通的 MQTT 代理中,第二个设备只是订阅了该主题,但 Azure 没有普通的 MQTT 代理。
我现在要做的是编写一个 Azure 函数,每当通过事件中心触发器在 IoTHub 中接收到来自第一台设备的消息时,该函数就会触发;并向第二个设备发送带有接收到的消息(字符串)的 C2D 消息。为了实现第二个设备订阅这个主题:devices/secondDevice/messages/devicebound
这是我的函数
#r "Microsoft.Azure.EventHubs"
using System;
using System.Text;
using Microsoft.Azure.EventHubs;
using Microsoft.Azure.Devices;
static ServiceClient serviceClient;
static string connectionString = ".........pXH9WI.....";
static string targetDevice = "secondDevice";
public static async Task Run(EventData[] events, ILogger log)
{
var exceptions = new List<Exception>();
serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
foreach (EventData eventData in events)
{
try
{
string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
// Replace these two lines with your processing logic.
log.LogInformation($"C# Event Hub trigger function processed a message: {messageBody}");
await Task.Yield();
var commandMessage = new Message(Encoding.ASCII.GetBytes(messageBody));
await serviceClient.SendAsync(targetDevice, commandMessage); // send the message to the second device that the first device sent to IoTHub
}
catch (Exception e)
{
// We need to keep processing the rest of the batch - capture this exception and continue.
// Also, consider capturing details of the message that failed processing so it can be processed again later.
exceptions.Add(e);
}
}
// Once processing of the batch is complete, if any messages in the batch failed processing throw an exception so that there is a record of the failure.
if (exceptions.Count > 1)
throw new AggregateException(exceptions);
if (exceptions.Count == 1)
throw exceptions.Single();
}
这是 .json 代码:
{
"bindings": [
{
"type": "eventHubTrigger",
"name": "events",
"direction": "inout",
"eventHubName": "samples-workitems",
"cardinality": "many",
"connection": "myIoTHub_events_IOTHUB",
"consumerGroup": "$Default"
}
]
}
我正在接收第一台设备的消息,但我没有在第二台设备订阅的主题中看到消息。
有什么想法吗?我正在通过门户网站完成所有这些工作,因为我目前在使用 VSCode 时遇到很多问题,我想尽快解决这个 C2D 问题。
谢谢
第二台设备应根据 docs 订阅主题过滤器 #
。所以题目会变成:
devices/secondDevice/messages/devicebound/#