簡體   English   中英

Azure 物聯網和事件中心?

[英]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 .NETEvent Hubs - Event Processor Host在 C# 中從 IoT 中心讀取事件。

否則,有兩個使用connection string的 NodeJS Azure IoT SDKAzure IoT Service SDKAPI 參考)和Azure IoT Device SDKAPI 參考)。

您可以在All settings中的Shared Access Policies選項卡的一個策略中找到它的connection string ,請參閱文檔Tutorial: Get started with Azure IoT Hub

在此處輸入圖像描述

根據您從 IoT 中心讀取事件的需要,您可以按照這些示例使用適用於 NodeJS 的 Azure IoT SDK 進行編碼。

  1. 使用Azure IoT Service SDK列出在您的 IoT 中心注冊的 deviceIds,請參閱示例https://github.com/Azure/azure-iot-sdks/blob/master/node/service/samples/registry_sample.js
  2. 使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM