Skip to content

Commit 1896347

Browse files
committed
Rolled in reviewers changes
1 parent 5f5c957 commit 1896347

File tree

3 files changed

+146
-186
lines changed

3 files changed

+146
-186
lines changed

javav2/example_code/iotsitewise/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@
106106
<groupId>software.amazon.awssdk</groupId>
107107
<artifactId>ssm</artifactId>
108108
</dependency>
109+
<dependency>
110+
<groupId>software.amazon.awssdk</groupId>
111+
<artifactId>sso</artifactId>
112+
</dependency>
113+
<dependency>
114+
<groupId>software.amazon.awssdk</groupId>
115+
<artifactId>ssooidc</artifactId>
116+
</dependency>
109117
<dependency>
110118
<groupId>org.apache.logging.log4j</groupId>
111119
<artifactId>log4j-core</artifactId>

javav2/example_code/iotsitewise/src/main/java/com/example/iotsitewise/scenario/SitewiseActions.java

Lines changed: 51 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
8+
import software.amazon.awssdk.services.iotsitewise.model.AssetModelPropertySummary;
89
import software.amazon.awssdk.services.iotsitewise.model.BatchPutAssetPropertyValueResponse;
910
import software.amazon.awssdk.services.iotsitewise.model.CreateGatewayRequest;
1011
import software.amazon.awssdk.services.iotsitewise.model.CreateGatewayResponse;
1112
import software.amazon.awssdk.services.iotsitewise.model.DeleteGatewayRequest;
13+
import software.amazon.awssdk.services.iotsitewise.model.DeleteGatewayResponse;
1214
import software.amazon.awssdk.services.iotsitewise.model.DescribeGatewayRequest;
1315
import software.amazon.awssdk.services.iotsitewise.model.DescribeGatewayResponse;
1416
import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
@@ -40,6 +42,7 @@
4042
import software.amazon.awssdk.services.iotsitewise.model.GetAssetPropertyValueRequest;
4143
import software.amazon.awssdk.services.iotsitewise.model.GetAssetPropertyValueResponse;
4244
import software.amazon.awssdk.services.iotsitewise.model.GreengrassV2;
45+
import software.amazon.awssdk.services.iotsitewise.model.ListAssetModelPropertiesRequest;
4346
import software.amazon.awssdk.services.iotsitewise.model.ListAssetModelsRequest;
4447
import software.amazon.awssdk.services.iotsitewise.model.Measurement;
4548
import software.amazon.awssdk.services.iotsitewise.model.PropertyDataType;
@@ -55,6 +58,8 @@
5558
import java.util.List;
5659
import java.util.Map;
5760
import java.util.concurrent.CompletableFuture;
61+
import java.util.concurrent.CompletionException;
62+
import java.util.stream.Collectors;
5863

5964
// snippet-start:[iotsitewise.java2.actions.main]
6065
public class SitewiseActions {
@@ -83,7 +88,7 @@ private static IoTSiteWiseAsyncClient getAsyncClient() {
8388
ioTSiteWiseAsyncClient = IoTSiteWiseAsyncClient.builder()
8489
.httpClient(httpClient)
8590
.overrideConfiguration(overrideConfig)
86-
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
91+
// .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
8792
.build();
8893
}
8994
return ioTSiteWiseAsyncClient;
@@ -101,14 +106,14 @@ public CompletableFuture<CreateAssetModelResponse> createAssetModelAsync(String
101106
.measurement(Measurement.builder().build())
102107
.build();
103108

104-
PropertyType propertyType = PropertyType.builder()
109+
PropertyType temperaturePropertyType = PropertyType.builder()
105110
.measurement(Measurement.builder().build())
106111
.build();
107112

108113
AssetModelPropertyDefinition temperatureProperty = AssetModelPropertyDefinition.builder()
109114
.name("Temperature")
110115
.dataType(PropertyDataType.DOUBLE)
111-
.type(propertyType)
116+
.type(temperaturePropertyType)
112117
.build();
113118

114119
AssetModelPropertyDefinition humidityProperty = AssetModelPropertyDefinition.builder()
@@ -117,16 +122,11 @@ public CompletableFuture<CreateAssetModelResponse> createAssetModelAsync(String
117122
.type(humidity)
118123
.build();
119124

120-
AssetModelPropertyDefinition measurementProperty = AssetModelPropertyDefinition.builder()
121-
.name("Temperature")
122-
.dataType(PropertyDataType.DOUBLE)
123-
.type(temperatureProperty.type())
124-
.build();
125125

126126
CreateAssetModelRequest createAssetModelRequest = CreateAssetModelRequest.builder()
127127
.assetModelName(name)
128128
.assetModelDescription("This is my asset model")
129-
.assetModelProperties(measurementProperty, humidityProperty)
129+
.assetModelProperties(temperatureProperty, humidityProperty)
130130
.build();
131131

132132
return getAsyncClient().createAssetModel(createAssetModelRequest)
@@ -165,8 +165,8 @@ public CompletableFuture<CreateAssetResponse> createAssetAsync(String assetName,
165165
}
166166
// snippet-end:[sitewise.java2_create_asset.main]
167167

168-
// snippet-start:[sitewise.java2_put_property.main]
169-
public CompletableFuture<BatchPutAssetPropertyValueResponse> sendDataToSiteWiseAsync(String assetId, String humPropId, String idHum) {
168+
// snippet-start:[sitewise.java2_put_batch_property.main]
169+
public CompletableFuture<BatchPutAssetPropertyValueResponse> sendDataToSiteWiseAsync(String assetId, String tempPropertyId, String humidityPropId) {
170170
Map<String, Double> sampleData = generateSampleData();
171171
long timestamp = Instant.now().toEpochMilli();
172172

@@ -180,7 +180,7 @@ public CompletableFuture<BatchPutAssetPropertyValueResponse> sendDataToSiteWiseA
180180
PutAssetPropertyValueEntry.builder()
181181
.entryId("entry-3")
182182
.assetId(assetId)
183-
.propertyId(humPropId)
183+
.propertyId(tempPropertyId)
184184
.propertyValues(Arrays.asList(
185185
AssetPropertyValue.builder()
186186
.value(Variant.builder()
@@ -193,7 +193,7 @@ public CompletableFuture<BatchPutAssetPropertyValueResponse> sendDataToSiteWiseA
193193
PutAssetPropertyValueEntry.builder()
194194
.entryId("entry-4")
195195
.assetId(assetId)
196-
.propertyId(idHum)
196+
.propertyId(humidityPropId)
197197
.propertyValues(Arrays.asList(
198198
AssetPropertyValue.builder()
199199
.value(Variant.builder()
@@ -209,13 +209,13 @@ public CompletableFuture<BatchPutAssetPropertyValueResponse> sendDataToSiteWiseA
209209
return getAsyncClient().batchPutAssetPropertyValue(request)
210210
.whenComplete((response, exception) -> {
211211
if (exception != null) {
212-
throw new RuntimeException("Failed to send data to SiteWise: " + exception.getMessage(), exception);
212+
logger.error("An exception occurred: {}", exception.getCause().getMessage());
213213
} else {
214214
logger.info("Data sent successfully.");
215215
}
216216
});
217217
}
218-
// snippet-end:[sitewise.java2_put_property.main]
218+
// snippet-end:[sitewise.java2_put_batch_property.main]
219219

220220
// snippet-start:[sitewise.java2_get_property.main]
221221
// TODO -- fix this including Javadoc
@@ -240,53 +240,21 @@ public void getAssetPropValueAsync(String propName, String propId, String assetI
240240

241241
// snippet-start:[sitewise.java2.describe.asset.model.main]
242242
/**
243-
* Finds the property ID of a given property name asynchronously.
244-
*
245-
* @param propertyName the name of the property to search for
246-
* @return a {@link CompletableFuture} that completes with the property ID if found, or null if not found
247-
* @throws RuntimeException if an exception occurs during the search
243+
* @param assetModelId The Id of the asset model.
244+
* @return A map of the asset model properties when the CompletableFuture completes.
248245
*/
249-
public CompletableFuture<String> findPropertyIdByNameAsync(String propertyName) {
250-
ListAssetModelsRequest listRequest = ListAssetModelsRequest.builder().build();
251-
252-
return getAsyncClient().listAssetModels(listRequest)
253-
.thenCompose(listResponse -> {
254-
List<CompletableFuture<String>> futures = new ArrayList<>();
255-
for (AssetModelSummary modelSummary : listResponse.assetModelSummaries()) {
256-
DescribeAssetModelRequest describeRequest = DescribeAssetModelRequest.builder()
257-
.assetModelId(modelSummary.id())
258-
.build();
259-
260-
CompletableFuture<String> future = getAsyncClient().describeAssetModel(describeRequest)
261-
.thenApply(describeResponse -> {
262-
for (AssetModelProperty property : describeResponse.assetModelProperties()) {
263-
if (property.name().equals(propertyName)) {
264-
return property.id();
265-
}
266-
}
267-
return null;
268-
});
269-
270-
futures.add(future);
271-
}
272-
273-
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
274-
.thenApply(v -> {
275-
for (CompletableFuture<String> future : futures) {
276-
String propertyId = future.join();
277-
if (propertyId != null) {
278-
return propertyId;
279-
}
280-
}
281-
return null;
282-
});
283-
})
284-
.handle((propertyId, exception) -> {
285-
if (exception != null) {
286-
throw new RuntimeException("Failed to find property by name: " + exception.getMessage(), exception);
287-
}
288-
return propertyId;
289-
});
246+
CompletableFuture<Map<String, String>> getPropertyIds(String assetModelId){
247+
ListAssetModelPropertiesRequest modelPropertiesRequest = ListAssetModelPropertiesRequest.builder().assetModelId(assetModelId).build();
248+
return getAsyncClient().listAssetModelProperties(modelPropertiesRequest)
249+
.handle( (response, throwable) -> {
250+
if (response != null){
251+
return response.assetModelPropertySummaries().stream()
252+
.collect(Collectors
253+
.toMap(AssetModelPropertySummary::name, AssetModelPropertySummary::id));
254+
} else {
255+
throw (CompletionException) throwable.getCause();
256+
}
257+
});
290258
}
291259
// snippet-end:[sitewise.java2.describe.asset.model.main]
292260

@@ -306,7 +274,9 @@ public CompletableFuture<DeleteAssetResponse> deleteAssetAsync(String assetId) {
306274
return getAsyncClient().deleteAsset(deleteAssetRequest)
307275
.whenComplete((response, exception) -> {
308276
if (exception != null) {
309-
throw new RuntimeException("Failed to delete asset with ID: " + assetId + ". Error: " + exception.getMessage(), exception);
277+
logger.error("An error occurred deleting asset with id: {}", assetId);
278+
} else {
279+
logger.info("Request to delete the asset completed successfully.");
310280
}
311281
});
312282
}
@@ -352,11 +322,12 @@ public CompletableFuture<String> createPortalAsync(String portalName, String iam
352322
.build();
353323

354324
return getAsyncClient().createPortal(createPortalRequest)
355-
.thenApply(CreatePortalResponse::portalId)
356-
.whenComplete((portalId, exception) -> {
325+
.handle ((response, exception) -> {
357326
if (exception != null) {
358-
throw new RuntimeException("Failed to create portal with name: " + portalName + ". Error: " + exception.getMessage(), exception);
327+
logger.error("Failed to create portal: {} ", exception.getCause().getMessage());
328+
throw (CompletionException) exception;
359329
}
330+
return response.portalId();
360331
});
361332
}
362333
// snippet-end:[sitewise.java2.create.portal.main]
@@ -438,7 +409,7 @@ public CompletableFuture<String> describePortalAsync(String portalId) {
438409
* @return a {@link CompletableFuture} containing the {@link CreateGatewayResponse} representing the created gateway
439410
* @throws RuntimeException if there was an error creating the gateway
440411
*/
441-
public CompletableFuture<CreateGatewayResponse> createGatewayAsync(String gatewayName, String myThing) {
412+
public CompletableFuture<String> createGatewayAsync(String gatewayName, String myThing) {
442413
GreengrassV2 gg = GreengrassV2.builder()
443414
.coreDeviceThingName(myThing)
444415
.build();
@@ -459,29 +430,29 @@ public CompletableFuture<CreateGatewayResponse> createGatewayAsync(String gatewa
459430
return getAsyncClient().createGateway(createGatewayRequest)
460431
.handle((response, exception) -> {
461432
if (exception != null) {
462-
throw new RuntimeException("Error creating gateway", exception);
433+
logger.error("Error creating the gateway.");
434+
throw (RuntimeException) exception;
463435
}
464436
System.out.println("The ARN of the gateway is " + response.gatewayArn());
465-
return response;
437+
return response.gatewayId();
466438
});
467439
}
468440
// snippet-end:[sitewise.java2.create.gateway.main]
469441

470442
// snippet-start:[sitewise.java2.delete.gateway.main]
471-
public CompletableFuture<DeleteAssetResponse> deleteGatewayAsync(String gatewayARN) {
443+
public CompletableFuture<DeleteGatewayResponse> deleteGatewayAsync(String gatewayARN) {
472444
DeleteGatewayRequest deleteGatewayRequest = DeleteGatewayRequest.builder()
473445
.gatewayId(gatewayARN)
474446
.build();
475447

476-
getAsyncClient().deleteGateway(deleteGatewayRequest)
448+
return getAsyncClient().deleteGateway(deleteGatewayRequest)
477449
.whenComplete((response, exception) -> {
478450
if (exception != null) {
479-
throw new RuntimeException("Error creating gateway", exception);
451+
logger.error("An error occurred during the deleteGateway method.");
480452
} else {
481-
logger.info("The Gateway was deleted successfully");
453+
logger.info("The Gateway was deleted successfully.");
482454
}
483455
});
484-
return null;
485456
}
486457
// snippet-end:[sitewise.java2.delete.gateway.main]
487458

@@ -492,15 +463,15 @@ public CompletableFuture<DescribeGatewayResponse> describeGatewayAsync(String ga
492463
.build();
493464

494465
return getAsyncClient().describeGateway(request)
495-
.handle((response, exception) -> {
466+
.whenComplete((response, exception) -> {
496467
if (exception != null) {
497-
throw new RuntimeException("Failed to describe the SiteWise gateway", exception);
468+
logger.error("An error occurred during the describeGateway method");
469+
} else {
470+
logger.info("Gateway Name: " + response.gatewayName());
471+
logger.info("Gateway ARN: " + response.gatewayArn());
472+
logger.info("Gateway Platform: " + response.gatewayPlatform().toString());
473+
logger.info("Gateway Creation Date: " + response.creationDate());
498474
}
499-
logger.info("Gateway Name: " + response.gatewayName());
500-
logger.info("Gateway ARN: " + response.gatewayArn());
501-
logger.info("Gateway Platform: " + response.gatewayPlatform().toString());
502-
logger.info("Gateway Creation Date: " + response.creationDate());
503-
return response;
504475
});
505476
}
506477
// snippet-end:[sitewise.java2.describe.gateway.main]

0 commit comments

Comments
 (0)