[英]UWP app for recievering messages from Azure IoT hub
我想制作UWP應用來接收來自Azure IoT中心的消息。 我找到了控制台接收器應用程序的示例代碼,但不適用於UWP,因為UWP不支持此引用。
using Microsoft.ServiceBus.Messaging;
有人可以為UWP應用發布代碼以從Azure IoT中心接收消息嗎?
您可以嘗試使用AzureSBLite,也可以從NuGet程序包管理器中進行安裝。
https://github.com/ppatierno/azuresblite
接收的示例代碼:
string ConnectionString = "<EventHub-Compatible-EndPoint>;SharedAccessKeyName=iothubowner;SharedAccessKey<Key>
static string eventHubEntity = "<EventHub-Compatible-Name>";
string partitionId = "0";
DateTime startingDateTimeUtc;
EventHubConsumerGroup group;
EventHubClient client;
MessagingFactory factory;
EventHubReceiver receiver;
public async Task ReceiveDataFromCloud()
{
startingDateTimeUtc = DateTime.UtcNow;
ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(ConnectionString);
builder.TransportType = ppatierno.AzureSBLite.Messaging.TransportType.Amqp;
factory = MessagingFactory.CreateFromConnectionString(ConnectionString);
client = factory.CreateEventHubClient(eventHubEntity);
group = client.GetDefaultConsumerGroup();
receiver = group.CreateReceiver(partitionId.ToString(), startingDateTimeUtc);//startingDateTimeUtc
while (true)
{
EventData data = receiver.Receive();
if (data != null)
{
var receiveddata = Encoding.UTF8.GetString(data.GetBytes());
Debug.WriteLine("{0} {1} {2}", data.SequenceNumber, data.EnqueuedTimeUtc.ToLocalTime(), Encoding.UTF8.GetString(data.GetBytes()));
}
else
{
break;
}
await Task.Delay(2000);
}
receiver.Close();
client.Close();
factory.Close();
}
您可以從IoTHub設置->消息傳遞部分獲取EventHub-Compatible-Endpoint和EventHub-Compatible Name。 在“設備到雲”設置下。
我也面臨着同樣的問題。我無法收到消息Azure IOT集線器。因為UWP不支持AMQP。 我已經嘗試過以下示例。但是我沒有運氣來接收消息。 https://github.com/Azure/azure-iot-sdks/tree/master/csharp/device/samples/UWPSample
如果您收到UWP中的消息,請告訴我。 計划在UWP中支持AMQP嗎?何時發布此更新?
官方SDK使用HTTP端點支持接收雲發送到設備的消息。 看到:
https://github.com/Azure/azure-iot-sdks/tree/master/csharp/device/samples/UWPSample
接收設備發送到雲的消息比較棘手,因為它是AMQP端點,但是可以使用AMQPNetLite完成:
這是從物聯網接收消息的最佳方法。 有兩種方法可以實現此C2D
和D2C
...雲到設備和設備到雲。 這取決於您的要求。 我對代碼進行了完整的測試:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
//txtblkMessages.Text = "test";
Start();
}
private static int MESSAGE_COUNT = 1;
private const string DeviceConnectionString = "HostName=[your hostname];DeviceId=[your DeviceId];SharedAccessKey=[your shared key]";
public async Task Start()
{
try
{
DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Amqp);
//await SendEvent(deviceClient);
await ReceiveCommands(deviceClient);
}
catch (Exception ex)
{
Debug.WriteLine("Error in sample: {0}", ex.Message);
}
}
async Task SendEvent(DeviceClient deviceClient)
{
string dataBuffer;
//for (int count = 0; count < MESSAGE_COUNT; count++)
//{
dataBuffer = "Hello Iot";
Message eventMessage = new Message(Encoding.UTF8.GetBytes(dataBuffer));
await deviceClient.SendEventAsync(eventMessage);
// }
}
async Task ReceiveCommands(DeviceClient deviceClient)
{
Message receivedMessage;
string messageData;
while (true)
{
receivedMessage = await deviceClient.ReceiveAsync();
if (receivedMessage != null)
{
messageData = Encoding.ASCII.GetString(receivedMessage.GetBytes());
txtblkMessages.Text = messageData + "\n" + txtblkMessages.Text;
await deviceClient.CompleteAsync(receivedMessage);
}
// Note: In this sample, the polling interval is set to
// 10 seconds to enable you to see messages as they are sent.
// To enable an IoT solution to scale, you should extend this // interval. For example, to scale to 1 million devices, set
// the polling interval to 25 minutes.
// For further information, see
// https://azure.microsoft.com/documentation/articles/iot-hub-devguide/#messaging
await Task.Delay(TimeSpan.FromSeconds(1));
}
}
}
請檢查我創建的UWP應用程序的源代碼:
CloudDataService類包含兩種方法並使用IoT中心SDK:
這是一個如何使用UWP應用和IoT中心發送和接收消息的示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.