[英]Azure ioT and Event Hub?
我正在尝试读取我的设备发送的事件。 我正在使用 azure npm lib 来阅读我认为正确的内容。
好的,首先,在我用于该服务的 Azure IoT Hub 帐户下,有一个选项卡调用消息。 有一种叫做“事件中心兼容名称”和“事件中心兼容端点”的东西。 我是否必须创建一个名为“Event Hub-compatible name”或什么的新事件中心? 我有点困惑:D 如果不是,连接字符串和主题等是什么?
这是代码现在的样子......
var azure = require('azure');
var serviceBusService = azure.createServiceBusService("Endpoint=XXXXXXXXXX.servicebus.windows.net/");
var isWaiting = false;
function waitForMessages(){
console.log("Checking Queue...");
isWaiting = true;
serviceBusService.receiveQueueMessage("messages","events",function (error, receivedMessage){
console.log(error);
console.log(receivedMessage);
isWaiting = false;
});
}
// Start messages listener
setInterval(function () {
if(!isWaiting){
waitForMessages();
}
}, 200);
与Event Hub-compatible name
并不意味着您必须创建具有相同名称的事件中心。
IOT Hub 提供了一个向后兼容 Event Hub API 的端点。 我认为实际实现稍微复杂一些,但您可以将 IOT Hub 视为继承自事件中心或至少是事件中心的实现。 将此事件中心兼容名称与任何事件中心 SDK 或代码示例一起用作连接字符串的一部分。
关于Event Hub-compatible name
& Event Hub-compatible endpoint
的概念的解释,可以参考官方文档Azure IoT Hub 开发者指南的How to read from Event Hubs-compatible endpoints
一节。 可以使用Azure Service Bus SDK for .NET
或Event Hubs - Event Processor Host
在 C# 中从 IoT 中心读取事件。
否则,有两个使用connection string
的 NodeJS Azure IoT SDK : Azure IoT Service SDK
( API 参考)和Azure IoT Device SDK
( API 参考)。
您可以在All settings
中的Shared Access Policies
选项卡的一个策略中找到它的connection string
,请参阅文档Tutorial: Get started with Azure IoT Hub
。
根据您从 IoT 中心读取事件的需要,您可以按照这些示例使用适用于 NodeJS 的 Azure IoT SDK 进行编码。
Azure IoT Service SDK
列出在您的 IoT 中心注册的 deviceIds,请参阅示例https://github.com/Azure/azure-iot-sdks/blob/master/node/service/samples/registry_sample.js 。Azure IoT Device SDK
监控来自 IoT Hub 的事件,请参阅示例https://github.com/Azure/azure-iot-sdks/blob/master/node/device/samples/remote_monitoring.js 。希望能帮助到你。 如有任何疑虑,请随时告诉我。
您可以使用适用于 Node.js 的事件中心 SDK 查看您的设备发送到 IoT 中心的事件/消息:
https://www.npmjs.com/package/azure-event-hubs
事件中心 SDK 的客户端对象可以接受 IoT 中心连接字符串,因此你不需要使用事件中心连接字符串。
如果你只是想调试你的设备并想验证它是否真的在发送消息,你可以使用 Azure IoT Hub SDK 提供的名为 iothub-explorer 的工具:
https://github.com/Azure/azure-iot-sdks/tree/master/tools/iothub-explorer
还有一些关于先前答案的澄清:服务 SDK 允许向设备发送消息,并读取设备发送的“反馈”消息,该消息用于了解设备是否接受或拒绝命令消息但不包含数据。 它无助于读取设备发送的数据事件。
连接到 IoT 中心以接收数据:
var protocol = 'amqps';
var eventHubHost = '{your event hub-compatible namespace}';
var sasName = 'iothubowner';
var sasKey = '{your iot hub key}';
var eventHubName = '{your event hub-compatible name}';
var numPartitions = 2;
协议
var protocol = 'amqps';
它是与事件中心兼容的终结点:sb://abcdefnamespace.servicebus.windows.net/ 但没有 sb:// and.service windows.net/
像这样:abcdefnamespace
var eventHubHost = '{your event hub-compatible namespace}';
太好了
var sasName = 'iothubowner';
主键,像这样:83wSUdsSdl6iFM4huqiLGFPVI27J2AlAkdCCNvQ==
var sasKey = '{your iot hub key}';
像这样命名:iothub-ehub-testsss-12922-ds333s var eventHubName = '{your event hub-compatible name}';
太好了
var numPartitions = 2;
每个 Iot Hub 实例都带有一个与事件中心兼容的内置终结点。 你在 Azure 门户中看到的事件中心兼容端点将是指向事件中心实例的连接字符串,你可以从中读取发送到 Iot 中心实例的所有消息。
您可以使用此连接字符串从@azure/event-hubs
库中实例化EventHubConsumerClient
类并阅读您的消息。
如果发生故障转移,据说这个支持内置端点会发生变化。 因此,您将不得不获取新的连接字符串并重新启动该过程。 另一种选择是使用azure-iot-samples-node 存储库中的示例代码,通过传入 Iot Hub 连接字符串来获取与事件中心兼容的连接字符串,然后使用@azure/event-hubs
库读取您的消息。 在从内置端点读取中查看更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.