Skip to content

Commit fa37e46

Browse files
committed
Update for listing and pagination.
1 parent bf912ea commit fa37e46

File tree

4 files changed

+125
-52
lines changed

4 files changed

+125
-52
lines changed

dotnetv4/IoT/Actions/HelloIoT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static async Task Main(string[] args)
2323

2424
try
2525
{
26-
Console.WriteLine("Hello AWS IoT! Let's list your IoT Things:");
26+
Console.WriteLine("Hello AWS IoT! Let's list a few of your IoT Things:");
2727
Console.WriteLine(new string('-', 80));
2828

2929
var request = new ListThingsRequest

dotnetv4/IoT/Actions/IoTWrapper.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,11 +605,33 @@ public async Task<List<ThingAttribute>> ListThingsAsync()
605605
{
606606
try
607607
{
608-
var request = new ListThingsRequest();
608+
// Use pages of 10.
609+
var request = new ListThingsRequest()
610+
{
611+
MaxResults = 10
612+
};
609613
var response = await _amazonIoT.ListThingsAsync(request);
610614

611-
_logger.LogInformation($"Retrieved {response.Things.Count} Things");
612-
return response.Things;
615+
// Since there is not a built-in paginator, use the NextMarker to paginate.
616+
bool hasMoreResults = true;
617+
618+
var things = new List<ThingAttribute>();
619+
while (hasMoreResults)
620+
{
621+
things.AddRange(response.Things);
622+
623+
// If NextMarker is not null, there are more results. Get the next page of results.
624+
if (!String.IsNullOrEmpty(response.NextMarker))
625+
{
626+
request.Marker = response.NextMarker;
627+
response = await _amazonIoT.ListThingsAsync(request);
628+
}
629+
else
630+
hasMoreResults = false;
631+
}
632+
633+
_logger.LogInformation($"Retrieved {things.Count} Things");
634+
return things;
613635
}
614636
catch (Amazon.IoT.Model.ThrottlingException ex)
615637
{

dotnetv4/IoT/Scenarios/IoTBasics.cs

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace IoTBasics;
1818

1919
// snippet-start:[iot.dotnetv4.IoTScenario]
2020
/// <summary>
21-
/// Scenario class for AWS IoT basics workflow.
21+
/// Scenario class for AWS IoT basics.
2222
/// </summary>
2323
public class IoTBasics
2424
{
@@ -72,7 +72,7 @@ public static async Task Main(string[] args)
7272
_amazonCloudFormation = CloudFormationClient;
7373

7474
Console.WriteLine(new string('-', 80));
75-
Console.WriteLine("Welcome to the AWS IoT example workflow.");
75+
Console.WriteLine("Welcome to the AWS IoT example scenario.");
7676
Console.WriteLine("This example program demonstrates various interactions with the AWS Internet of Things (IoT) Core service.");
7777
Console.WriteLine();
7878
if (IsInteractive)
@@ -93,7 +93,7 @@ public static async Task Main(string[] args)
9393
}
9494

9595
Console.WriteLine(new string('-', 80));
96-
Console.WriteLine("The AWS IoT workflow has successfully completed.");
96+
Console.WriteLine("The AWS IoT scenario has successfully completed.");
9797
Console.WriteLine(new string('-', 80));
9898
}
9999

@@ -123,7 +123,7 @@ private static async Task RunScenarioInternalAsync(IoTWrapper iotWrapper, IAmazo
123123
string thingName = $"iot-thing-{Guid.NewGuid():N}";
124124
string certificateArn = "";
125125
string certificateId = "";
126-
string ruleName = $"iot-rule-{Guid.NewGuid():N}";
126+
string ruleName = $"iotruledefault";
127127
string snsTopicArn = "";
128128

129129
try
@@ -150,9 +150,39 @@ private static async Task RunScenarioInternalAsync(IoTWrapper iotWrapper, IAmazo
150150
Console.WriteLine($"{thingName} was successfully created. The ARN value is {thingArn}");
151151
Console.WriteLine(new string('-', 80));
152152

153+
// Step 1.1: List AWS IoT Things
154+
Console.WriteLine(new string('-', 80));
155+
Console.WriteLine("2. List AWS IoT Things.");
156+
Console.WriteLine("Now let's list the IoT Things to see the Thing we just created.");
157+
Console.WriteLine();
158+
if (IsInteractive)
159+
{
160+
Console.WriteLine("Press Enter to continue...");
161+
Console.ReadLine();
162+
}
163+
164+
var things = await iotWrapper.ListThingsAsync();
165+
Console.WriteLine($"Found {things.Count} IoT Things:");
166+
foreach (var thing in things.Take(10)) // Show first 10 things
167+
{
168+
Console.WriteLine($"Thing Name: {thing.ThingName}");
169+
Console.WriteLine($"Thing ARN: {thing.ThingArn}");
170+
if (thing.Attributes != null && thing.Attributes.Any())
171+
{
172+
Console.WriteLine("Attributes:");
173+
foreach (var attr in thing.Attributes)
174+
{
175+
Console.WriteLine($" {attr.Key}: {attr.Value}");
176+
}
177+
}
178+
Console.WriteLine("--------------");
179+
}
180+
Console.WriteLine();
181+
Console.WriteLine(new string('-', 80));
182+
153183
// Step 2: Generate a Device Certificate
154184
Console.WriteLine(new string('-', 80));
155-
Console.WriteLine("2. Generate a device certificate.");
185+
Console.WriteLine("3. Generate a device certificate.");
156186
Console.WriteLine("A device certificate performs a role in securing the communication between devices (Things) and the AWS IoT platform.");
157187
Console.WriteLine();
158188

@@ -216,7 +246,7 @@ private static async Task RunScenarioInternalAsync(IoTWrapper iotWrapper, IAmazo
216246

217247
// Step 4: Update an AWS IoT Thing with Attributes
218248
Console.WriteLine(new string('-', 80));
219-
Console.WriteLine("3. Update an AWS IoT Thing with Attributes.");
249+
Console.WriteLine("4. Update an AWS IoT Thing with Attributes.");
220250
Console.WriteLine("IoT Thing attributes, represented as key-value pairs, offer a pivotal advantage in facilitating efficient data");
221251
Console.WriteLine("management and retrieval within the AWS IoT ecosystem.");
222252
Console.WriteLine();
@@ -239,8 +269,7 @@ private static async Task RunScenarioInternalAsync(IoTWrapper iotWrapper, IAmazo
239269

240270
// Step 5: Return a unique endpoint specific to the Amazon Web Services account
241271
Console.WriteLine(new string('-', 80));
242-
Console.WriteLine("4. Return a unique endpoint specific to the Amazon Web Services account.");
243-
Console.WriteLine("An IoT Endpoint refers to a specific URL or Uniform Resource Locator that serves as the entry point for communication between IoT devices and the AWS IoT service.");
272+
Console.WriteLine("5. Return a unique endpoint specific to the Amazon Web Services account.");
244273
Console.WriteLine();
245274
if (IsInteractive)
246275
{
@@ -263,7 +292,7 @@ private static async Task RunScenarioInternalAsync(IoTWrapper iotWrapper, IAmazo
263292

264293
// Step 6: List your AWS IoT certificates
265294
Console.WriteLine(new string('-', 80));
266-
Console.WriteLine("5. List your AWS IoT certificates");
295+
Console.WriteLine("6. List your AWS IoT certificates");
267296
if (IsInteractive)
268297
{
269298
Console.WriteLine("Press Enter to continue...");
@@ -281,10 +310,7 @@ private static async Task RunScenarioInternalAsync(IoTWrapper iotWrapper, IAmazo
281310

282311
// Step 7: Create an IoT shadow
283312
Console.WriteLine(new string('-', 80));
284-
Console.WriteLine("6. Create an IoT shadow that refers to a digital representation or virtual twin of a physical IoT device");
285-
Console.WriteLine("A Thing Shadow refers to a feature that enables you to create a virtual representation, or \"shadow,\"");
286-
Console.WriteLine("of a physical device or thing. The Thing Shadow allows you to synchronize and control the state of a device between");
287-
Console.WriteLine("the cloud and the device itself. and the AWS IoT service. For example, you can write and retrieve JSON data from a Thing Shadow.");
313+
Console.WriteLine("7. Create an IoT shadow that refers to a digital representation or virtual twin of a physical IoT device");
288314
Console.WriteLine();
289315
if (IsInteractive)
290316
{
@@ -310,7 +336,7 @@ private static async Task RunScenarioInternalAsync(IoTWrapper iotWrapper, IAmazo
310336

311337
// Step 8: Write out the state information, in JSON format
312338
Console.WriteLine(new string('-', 80));
313-
Console.WriteLine("7. Write out the state information, in JSON format.");
339+
Console.WriteLine("8. Write out the state information, in JSON format.");
314340
if (IsInteractive)
315341
{
316342
Console.WriteLine("Press Enter to continue...");
@@ -323,14 +349,12 @@ private static async Task RunScenarioInternalAsync(IoTWrapper iotWrapper, IAmazo
323349

324350
// Step 9: Set up resources (SNS topic and IAM role) and create a rule
325351
Console.WriteLine(new string('-', 80));
326-
Console.WriteLine("8. Set up resources and create a rule");
327-
Console.WriteLine("Creates a rule that is an administrator-level action.");
328-
Console.WriteLine("Any user who has permission to create rules will be able to access data processed by the rule.");
352+
Console.WriteLine("9. Set up resources and create a rule");
329353
Console.WriteLine();
330354

331355
if (IsInteractive)
332356
{
333-
Console.Write("Enter Rule name: ");
357+
Console.Write($"Enter Rule name (press Enter for default '{ruleName}'):: ");
334358
var userRuleName = Console.ReadLine();
335359
if (!string.IsNullOrEmpty(userRuleName))
336360
ruleName = userRuleName;
@@ -391,7 +415,7 @@ private static async Task RunScenarioInternalAsync(IoTWrapper iotWrapper, IAmazo
391415

392416
// Step 10: List your rules
393417
Console.WriteLine(new string('-', 80));
394-
Console.WriteLine("9. List your rules.");
418+
Console.WriteLine("10. List your rules.");
395419
if (IsInteractive)
396420
{
397421
Console.WriteLine("Press Enter to continue...");
@@ -411,7 +435,7 @@ private static async Task RunScenarioInternalAsync(IoTWrapper iotWrapper, IAmazo
411435

412436
// Step 11: Search things using the Thing name
413437
Console.WriteLine(new string('-', 80));
414-
Console.WriteLine("10. Search things using the Thing name.");
438+
Console.WriteLine("11. Search things using the Thing name.");
415439
if (IsInteractive)
416440
{
417441
Console.WriteLine("Press Enter to continue...");
@@ -446,7 +470,7 @@ private static async Task RunScenarioInternalAsync(IoTWrapper iotWrapper, IAmazo
446470

447471
if (deleteCert?.ToLower() == "y")
448472
{
449-
Console.WriteLine("11. You selected to detach and delete the certificate.");
473+
Console.WriteLine("12. You selected to detach and delete the certificate.");
450474
if (IsInteractive)
451475
{
452476
Console.WriteLine("Press Enter to continue...");
@@ -464,7 +488,7 @@ private static async Task RunScenarioInternalAsync(IoTWrapper iotWrapper, IAmazo
464488

465489
// Step 13: Delete the AWS IoT Thing
466490
Console.WriteLine(new string('-', 80));
467-
Console.WriteLine("12. Delete the AWS IoT Thing.");
491+
Console.WriteLine("13. Delete the AWS IoT Thing.");
468492
var deleteThing = "y";
469493
if (IsInteractive)
470494
{
@@ -487,7 +511,7 @@ private static async Task RunScenarioInternalAsync(IoTWrapper iotWrapper, IAmazo
487511
if (!string.IsNullOrEmpty(snsTopicArn))
488512
{
489513
Console.WriteLine(new string('-', 80));
490-
Console.WriteLine("13. Clean up CloudFormation stack.");
514+
Console.WriteLine("14. Clean up CloudFormation stack.");
491515
Console.WriteLine("Deleting the CloudFormation stack and all resources...");
492516

493517
var cleanup = !IsInteractive || GetYesNoResponse("Do you want to delete the CloudFormation stack and all resources? (y/n) ");

0 commit comments

Comments
 (0)