This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from Azure-Samples/drwill/ReadD2cMessages
Cleanup ReadD2cMessages sample
- Loading branch information
Showing
13 changed files
with
269 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
// This application uses the Azure IoT Hub service SDK for .NET | ||
// For samples see: https://github.com/Azure/azure-iot-sdk-csharp/tree/master/iothub/service | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Devices; | ||
|
||
namespace InvokeDeviceMethod | ||
{ | ||
/// <summary> | ||
/// This sample illustrates the very basics of a service app invoking a method on a device. | ||
/// </summary> | ||
internal class Program | ||
{ | ||
private static ServiceClient s_serviceClient; | ||
|
||
// Connection string for your IoT Hub | ||
// az iot hub show-connection-string --hub-name {your iot hub name} --policy-name service | ||
private static string s_connectionString = "{Your service connection string here}"; | ||
|
||
private static async Task Main(string[] args) | ||
{ | ||
Console.WriteLine("IoT Hub Quickstarts #2 - InvokeDeviceMethod application."); | ||
|
||
// This sample accepts the service connection string as a parameter, if present | ||
ValidateConnectionString(args); | ||
|
||
// Create a ServiceClient to communicate with service-facing endpoint on your hub. | ||
s_serviceClient = ServiceClient.CreateFromConnectionString(s_connectionString); | ||
|
||
await InvokeMethodAsync(); | ||
|
||
s_serviceClient.Dispose(); | ||
|
||
Console.WriteLine("\nPress Enter to exit."); | ||
Console.ReadLine(); | ||
} | ||
|
||
// Invoke the direct method on the device, passing the payload | ||
private static async Task InvokeMethodAsync() | ||
{ | ||
var methodInvocation = new CloudToDeviceMethod("SetTelemetryInterval") | ||
{ | ||
ResponseTimeout = TimeSpan.FromSeconds(30), | ||
}; | ||
methodInvocation.SetPayloadJson("10"); | ||
|
||
// Invoke the direct method asynchronously and get the response from the simulated device. | ||
var response = await s_serviceClient.InvokeDeviceMethodAsync("MyDotnetDevice", methodInvocation); | ||
|
||
Console.WriteLine($"\nResponse status: {response.Status}, payload:\n\t{response.GetPayloadAsJson()}"); | ||
} | ||
|
||
private static void ValidateConnectionString(string[] args) | ||
{ | ||
if (args.Any()) | ||
{ | ||
try | ||
{ | ||
var cs = IotHubConnectionStringBuilder.Create(args[0]); | ||
s_connectionString = cs.ToString(); | ||
} | ||
catch (Exception) | ||
{ | ||
Console.WriteLine($"Error: Unrecognizable parameter '{args[0]}' as connection string."); | ||
Environment.Exit(1); | ||
} | ||
} | ||
else | ||
{ | ||
try | ||
{ | ||
_ = IotHubConnectionStringBuilder.Create(s_connectionString); | ||
} | ||
catch (Exception) | ||
{ | ||
Console.WriteLine("This sample needs a device connection string to run. Program.cs can be edited to specify it, or it can be included on the command-line as the only parameter."); | ||
Environment.Exit(1); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using CommandLine; | ||
|
||
namespace ReadD2cMessages | ||
{ | ||
/// <summary> | ||
/// Parameters for the application | ||
/// </summary> | ||
internal class Parameters | ||
{ | ||
internal const string IotHubSharedAccessKeyName = "service"; | ||
|
||
[Option( | ||
'e', | ||
"EventHubCompatibleEndpoint", | ||
HelpText = "The event hub-compatible endpoint from your IoT Hub instance. Use `az iot hub show --query properties.eventHubEndpoints.events.endpoint --name {your IoT Hub name}` to fetch via the Azure CLI.")] | ||
public string EventHubCompatibleEndpoint { get; set; } | ||
|
||
[Option( | ||
'n', | ||
"EventHubName", | ||
HelpText = "The event hub-compatible name of your IoT Hub instance. Use `az iot hub show --query properties.eventHubEndpoints.events.path --name {your IoT Hub name}` to fetch via the Azure CLI.")] | ||
public string EventHubName { get; set; } | ||
|
||
[Option( | ||
's', | ||
"SharedAccessKey", | ||
HelpText = "A primary or shared access key from your IoT Hub instance, with the 'service' permission. Use `az iot hub policy show --name service --query primaryKey --hub-name {your IoT Hub name}` to fetch via the Azure CLI.")] | ||
public string SharedAccessKey { get; set; } | ||
|
||
[Option( | ||
'c', | ||
"EventHubConnectionString", | ||
HelpText = "The connection string to the event hub-compatible endpoint. Use the Azure portal to get this parameter. If this value is provided, all the others are not necessary.")] | ||
public string EventHubConnectionString { get; set; } | ||
|
||
internal string GetEventHubConnectionString() | ||
{ | ||
return EventHubConnectionString ?? $"Endpoint={EventHubCompatibleEndpoint};SharedAccessKeyName={IotHubSharedAccessKeyName};SharedAccessKey={SharedAccessKey}"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
// This application uses the Azure Event Hubs Client for .NET | ||
// For samples see: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/eventhub/Azure.Messaging.EventHubs/samples/README.md | ||
// For documentation see: https://docs.microsoft.com/azure/event-hubs/ | ||
|
||
using Azure.Messaging.EventHubs.Consumer; | ||
using CommandLine; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace ReadD2cMessages | ||
{ | ||
/// <summary> | ||
/// A sample to illustrate reading Device-to-Cloud messages from a service app. | ||
/// </summary> | ||
internal class Program | ||
{ | ||
private static Parameters _parameters; | ||
|
||
public static async Task Main(string[] args) | ||
{ | ||
// Parse application parameters | ||
ParserResult<Parameters> result = Parser.Default.ParseArguments<Parameters>(args) | ||
.WithParsed(parsedParams => | ||
{ | ||
_parameters = parsedParams; | ||
}) | ||
.WithNotParsed(errors => | ||
{ | ||
Environment.Exit(1); | ||
}); | ||
|
||
// Either the connection string must be supplied, or the set of endpoint, name, and shared access key must be. | ||
if (string.IsNullOrWhiteSpace(_parameters.EventHubConnectionString) | ||
&& (string.IsNullOrWhiteSpace(_parameters.EventHubCompatibleEndpoint) | ||
|| string.IsNullOrWhiteSpace(_parameters.EventHubName) | ||
|| string.IsNullOrWhiteSpace(_parameters.SharedAccessKey))) | ||
{ | ||
Console.WriteLine(CommandLine.Text.HelpText.AutoBuild(result, null, null)); | ||
Environment.Exit(1); | ||
} | ||
|
||
Console.WriteLine("IoT Hub Quickstarts - Read device to cloud messages. Ctrl-C to exit.\n"); | ||
|
||
// Set up a way for the user to gracefully shutdown | ||
using var cts = new CancellationTokenSource(); | ||
Console.CancelKeyPress += (sender, eventArgs) => | ||
{ | ||
eventArgs.Cancel = true; | ||
cts.Cancel(); | ||
Console.WriteLine("Exiting..."); | ||
}; | ||
|
||
// Run the sample | ||
await ReceiveMessagesFromDeviceAsync(cts.Token); | ||
|
||
Console.WriteLine("Cloud message reader finished."); | ||
} | ||
|
||
// Asynchronously create a PartitionReceiver for a partition and then start | ||
// reading any messages sent from the simulated client. | ||
private static async Task ReceiveMessagesFromDeviceAsync(CancellationToken ct) | ||
{ | ||
string connectionString = _parameters.GetEventHubConnectionString(); | ||
|
||
// Create the consumer using the default consumer group using a direct connection to the service. | ||
// Information on using the client with a proxy can be found in the README for this quick start, here: | ||
// https://github.com/Azure-Samples/azure-iot-samples-csharp/tree/master/iot-hub/Quickstarts/ReadD2cMessages/README.md#websocket-and-proxy-support | ||
await using var consumer = new EventHubConsumerClient( | ||
EventHubConsumerClient.DefaultConsumerGroupName, | ||
connectionString, | ||
_parameters.EventHubName); | ||
|
||
Console.WriteLine("Listening for messages on all partitions."); | ||
|
||
try | ||
{ | ||
// Begin reading events for all partitions, starting with the first event in each partition and waiting indefinitely for | ||
// events to become available. Reading can be canceled by breaking out of the loop when an event is processed or by | ||
// signaling the cancellation token. | ||
// | ||
// The "ReadEventsAsync" method on the consumer is a good starting point for consuming events for prototypes | ||
// and samples. For real-world production scenarios, it is strongly recommended that you consider using the | ||
// "EventProcessorClient" from the "Azure.Messaging.EventHubs.Processor" package. | ||
// | ||
// More information on the "EventProcessorClient" and its benefits can be found here: | ||
// https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/eventhub/Azure.Messaging.EventHubs.Processor/README.md | ||
await foreach (PartitionEvent partitionEvent in consumer.ReadEventsAsync(ct)) | ||
{ | ||
Console.WriteLine($"\nMessage received on partition {partitionEvent.Partition.PartitionId}:"); | ||
|
||
string data = Encoding.UTF8.GetString(partitionEvent.Data.Body.ToArray()); | ||
Console.WriteLine($"\tMessage body: {data}"); | ||
|
||
Console.WriteLine("\tApplication properties (set by device):"); | ||
foreach (KeyValuePair<string, object> prop in partitionEvent.Data.Properties) | ||
{ | ||
Console.WriteLine($"\t\t{prop.Key}: {prop.Value}"); | ||
} | ||
|
||
Console.WriteLine("\tSystem properties (set by IoT Hub):"); | ||
foreach (KeyValuePair<string, object> prop in partitionEvent.Data.SystemProperties) | ||
{ | ||
Console.WriteLine($"\t\t{prop.Key}: {prop.Value}"); | ||
} | ||
} | ||
} | ||
catch (TaskCanceledException) | ||
{ | ||
// This is expected when the token is signaled; it should not be considered an | ||
// error in this scenario. | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
44 changes: 0 additions & 44 deletions
44
iot-hub/Quickstarts/back-end-application/BackEndApplication.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.