|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using Amazon.IoT; |
| 5 | +using Amazon.IoT.Model; |
| 6 | + |
| 7 | +namespace IoTActions; |
| 8 | + |
| 9 | +// snippet-start:[iot.dotnetv4.Hello] |
| 10 | +/// <summary> |
| 11 | +/// Hello AWS IoT example. |
| 12 | +/// </summary> |
| 13 | +public class HelloIoT |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Main method to run the Hello IoT example. |
| 17 | + /// </summary> |
| 18 | + /// <param name="args">Command line arguments.</param> |
| 19 | + /// <returns>A Task object.</returns> |
| 20 | + public static async Task Main(string[] args) |
| 21 | + { |
| 22 | + var iotClient = new AmazonIoTClient(); |
| 23 | + |
| 24 | + try |
| 25 | + { |
| 26 | + Console.WriteLine("Hello AWS IoT! Let's list your IoT Things:"); |
| 27 | + Console.WriteLine(new string('-', 80)); |
| 28 | + |
| 29 | + var request = new ListThingsRequest |
| 30 | + { |
| 31 | + MaxResults = 10 |
| 32 | + }; |
| 33 | + |
| 34 | + var response = await iotClient.ListThingsAsync(request); |
| 35 | + |
| 36 | + if (response.Things.Count > 0) |
| 37 | + { |
| 38 | + Console.WriteLine($"Found {response.Things.Count} IoT Things:"); |
| 39 | + foreach (var thing in response.Things) |
| 40 | + { |
| 41 | + Console.WriteLine($"- Thing Name: {thing.ThingName}"); |
| 42 | + Console.WriteLine($" Thing ARN: {thing.ThingArn}"); |
| 43 | + Console.WriteLine($" Thing Type: {thing.ThingTypeName ?? "No type specified"}"); |
| 44 | + Console.WriteLine($" Version: {thing.Version}"); |
| 45 | + |
| 46 | + if (thing.Attributes?.Count > 0) |
| 47 | + { |
| 48 | + Console.WriteLine(" Attributes:"); |
| 49 | + foreach (var attr in thing.Attributes) |
| 50 | + { |
| 51 | + Console.WriteLine($" {attr.Key}: {attr.Value}"); |
| 52 | + } |
| 53 | + } |
| 54 | + Console.WriteLine(); |
| 55 | + } |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + Console.WriteLine("No IoT Things found in your account."); |
| 60 | + Console.WriteLine("You can create IoT Things using the IoT Basics scenario example."); |
| 61 | + } |
| 62 | + |
| 63 | + Console.WriteLine("Hello IoT completed successfully."); |
| 64 | + } |
| 65 | + catch (Exception ex) |
| 66 | + { |
| 67 | + Console.WriteLine($"Error: {ex.Message}"); |
| 68 | + } |
| 69 | + finally |
| 70 | + { |
| 71 | + iotClient.Dispose(); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | +// snippet-end:[iot.dotnetv4.Hello] |
0 commit comments