Pelajari dasar-dasar AWS IoT SiteWise dengan AWS SDK - AWS SDKContoh Kode

Ada lebih banyak AWS SDK contoh yang tersedia di GitHub repo SDKContoh AWS Dokumen.

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Pelajari dasar-dasar AWS IoT SiteWise dengan AWS SDK

Contoh kode berikut menunjukkan cara Mempelajari operasi inti untuk AWS IoT SiteWise menggunakan file AWS SDK.

Java
SDKuntuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Jalankan skenario interaktif yang menunjukkan AWS IoT SiteWise fitur.

public class SitewiseScenario { public static final String DASHES = new String(new char[80]).replace("\0", "-"); private static final Logger logger = LoggerFactory.getLogger(SitewiseScenario.class); static Scanner scanner = new Scanner(System.in); private static final String ROLES_STACK = "RoleSitewise"; static SitewiseActions sitewiseActions = new SitewiseActions(); public static void main(String[] args) throws Throwable { Scanner scanner = new Scanner(System.in); String contactEmail = "user@mydomain.com"; // Change email address. String assetModelName = "MyAssetModel1"; String assetName = "MyAsset1" ; String portalName = "MyPortal1" ; String gatewayName = "MyGateway1" ; String myThing = "MyThing1" ; logger.info(""" AWS IoT SiteWise is a fully managed software-as-a-service (SaaS) that makes it easy to collect, store, organize, and monitor data from industrial equipment and processes. It is designed to help industrial and manufacturing organizations collect data from their equipment and processes, and use that data to make informed decisions about their operations. One of the key features of AWS IoT SiteWise is its ability to connect to a wide range of industrial equipment and systems, including programmable logic controllers (PLCs), sensors, and other industrial devices. It can collect data from these devices and organize it into a unified data model, making it easier to analyze and gain insights from the data. AWS IoT SiteWise also provides tools for visualizing the data, setting up alarms and alerts, and generating reports. Another key feature of AWS IoT SiteWise is its ability to scale to handle large volumes of data. It can collect and store data from thousands of devices and process millions of data points per second, making it suitable for large-scale industrial operations. Additionally, AWS IoT SiteWise is designed to be secure and compliant, with features like role-based access controls, data encryption, and integration with other AWS services for additional security and compliance features. Let's get started... """); waitForInputToContinue(scanner); logger.info(DASHES); try { runScenario(assetModelName, assetName, portalName, contactEmail, gatewayName, myThing); } catch (RuntimeException e) { logger.info(e.getMessage()); } } public static void runScenario(String assetModelName, String assetName, String portalName, String contactEmail, String gatewayName, String myThing) throws Throwable { logger.info("Use AWS CloudFormation to create an IAM role that is required for this scenario."); CloudFormationHelper.deployCloudFormationStack(ROLES_STACK); Map<String, String> stackOutputs = CloudFormationHelper.getStackOutputsAsync(ROLES_STACK).join(); String iamRole = stackOutputs.get("SitewiseRoleArn"); logger.info("The ARN of the IAM role is {}",iamRole); logger.info(DASHES); logger.info(DASHES); logger.info("1. Create an AWS SiteWise Asset Model"); logger.info(""" An AWS IoT SiteWise Asset Model is a way to represent the physical assets, such as equipment, processes, and systems, that exist in an industrial environment. This model provides a structured and hierarchical representation of these assets, allowing users to define the relationships and properties of each asset. This scenario creates two asset model properties: temperature and humidity. """); waitForInputToContinue(scanner); String assetModelId = null; try { CreateAssetModelResponse response = sitewiseActions.createAssetModelAsync(assetModelName).join(); assetModelId = response.assetModelId(); logger.info("Asset Model successfully created. Asset Model ID: {}. ", assetModelId); } catch (CompletionException ce) { Throwable cause = ce.getCause(); if (cause instanceof ResourceAlreadyExistsException) { try { assetModelId = sitewiseActions.getAssetModelIdAsync(assetModelName).join(); logger.info("The Asset Model {} already exists. The id of the existing model is {}. Moving on...", assetModelName, assetModelId); } catch (CompletionException cex) { logger.error("Exception thrown acquiring the asset model id: {}", cex.getCause().getCause(), cex); return; } } else { logger.info("An unexpected error occurred: " + cause.getMessage(), cause); return; } } waitForInputToContinue(scanner); logger.info(DASHES); logger.info("2. Create an AWS IoT SiteWise Asset"); logger.info(""" The IoT SiteWise model that we just created defines the structure and metadata for your physical assets. Now we create an asset from the asset model. """); logger.info("Let's wait 30 seconds for the asset to be ready."); countdown(30); waitForInputToContinue(scanner); String assetId; try { CreateAssetResponse response = sitewiseActions.createAssetAsync(assetName, assetModelId).join(); assetId = response.assetId(); logger.info("Asset created with ID: {}", assetId); } catch (CompletionException ce) { Throwable cause = ce.getCause(); if (cause instanceof ResourceNotFoundException) { logger.info("The asset model id was not found: {}", cause.getMessage(), cause); } else { logger.info("An unexpected error occurred: {}", cause.getMessage(), cause); } return; } waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("3. Retrieve the property ID values"); logger.info(""" To send data to an asset, we need to get the property ID values. In this scenario, we access the temperature and humidity property ID values. """); waitForInputToContinue(scanner); Map<String, String> propertyIds = null; try { propertyIds = sitewiseActions.getPropertyIds(assetModelId).join(); } catch (CompletionException ce) { Throwable cause = ce.getCause(); if (cause instanceof IoTSiteWiseException) { logger.error("IoTSiteWiseException occurred: {}", cause.getMessage(), ce); } else { logger.error("An unexpected error occurred: {}", cause.getMessage(), ce); } return; } String humPropId = propertyIds.get("Humidity"); logger.info("The Humidity property Id is {}", humPropId); String tempPropId = propertyIds.get("Temperature"); logger.info("The Temperature property Id is {}", tempPropId); waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("4. Send data to an AWS IoT SiteWise Asset"); logger.info(""" By sending data to an IoT SiteWise Asset, you can aggregate data from multiple sources, normalize the data into a standard format, and store it in a centralized location. This makes it easier to analyze and gain insights from the data. In this example, we generate sample temperature and humidity data and send it to the AWS IoT SiteWise asset. """); waitForInputToContinue(scanner); try { sitewiseActions.sendDataToSiteWiseAsync(assetId, tempPropId, humPropId).join(); logger.info("Data sent successfully."); } catch (CompletionException ce) { Throwable cause = ce.getCause(); if (cause instanceof ResourceNotFoundException) { logger.error("The AWS resource was not found: {}", cause.getMessage(), cause); } else { logger.error("An unexpected error occurred: {}", cause.getMessage(), cause); } return; } waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("5. Retrieve the value of the IoT SiteWise Asset property"); logger.info(""" IoT SiteWise is an AWS service that allows you to collect, process, and analyze industrial data from connected equipment and sensors. One of the key benefits of reading an IoT SiteWise property is the ability to gain valuable insights from your industrial data. """); waitForInputToContinue(scanner); try { Double assetVal = sitewiseActions.getAssetPropValueAsync(tempPropId, assetId).join(); logger.info("The property name is: {}", "Temperature"); logger.info("The value of this property is: {}", assetVal); waitForInputToContinue(scanner); assetVal = sitewiseActions.getAssetPropValueAsync(humPropId, assetId).join(); logger.info("The property name is: {}", "Humidity"); logger.info("The value of this property is: {}", assetVal); } catch (CompletionException ce) { Throwable cause = ce.getCause(); if (cause instanceof ResourceNotFoundException) { logger.info("The AWS resource was not found: {}", cause.getMessage(), cause); } else { logger.info("An unexpected error occurred: {}", cause.getMessage(), cause); } return; } waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("6. Create an IoT SiteWise Portal"); logger.info(""" An IoT SiteWise Portal allows you to aggregate data from multiple industrial sources, such as sensors, equipment, and control systems, into a centralized platform. """); waitForInputToContinue(scanner); String portalId; try { portalId = sitewiseActions.createPortalAsync(portalName, iamRole, contactEmail).join(); logger.info("Portal created successfully. Portal ID {}", portalId); } catch (CompletionException ce) { Throwable cause = ce.getCause(); if (cause instanceof IoTSiteWiseException siteWiseEx) { logger.error("IoT SiteWise error occurred: Error message: {}, Error code {}", siteWiseEx.getMessage(), siteWiseEx.awsErrorDetails().errorCode(), siteWiseEx); } else { logger.error("An unexpected error occurred: {}", cause.getMessage()); } return; } waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("7. Describe the Portal"); logger.info(""" In this step, we get a description of the portal and display the portal URL. """); waitForInputToContinue(scanner); try { String portalUrl = sitewiseActions.describePortalAsync(portalId).join(); logger.info("Portal URL: {}", portalUrl); } catch (CompletionException ce) { Throwable cause = ce.getCause(); if (cause instanceof ResourceNotFoundException notFoundException) { logger.error("A ResourceNotFoundException occurred: Error message: {}, Error code {}", notFoundException.getMessage(), notFoundException.awsErrorDetails().errorCode(), notFoundException); } else { logger.error("An unexpected error occurred: {}", cause.getMessage()); } return; } waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("8. Create an IoT SiteWise Gateway"); logger.info( """ IoT SiteWise Gateway serves as the bridge between industrial equipment, sensors, and the cloud-based IoT SiteWise service. It is responsible for securely collecting, processing, and transmitting data from various industrial assets to the IoT SiteWise platform, enabling real-time monitoring, analysis, and optimization of industrial operations. """); waitForInputToContinue(scanner); String gatewayId = ""; try { gatewayId = sitewiseActions.createGatewayAsync(gatewayName, myThing).join(); logger.info("Gateway creation completed successfully. id is {}", gatewayId ); } catch (CompletionException ce) { Throwable cause = ce.getCause(); if (cause instanceof IoTSiteWiseException siteWiseEx) { logger.error("IoT SiteWise error occurred: Error message: {}, Error code {}", siteWiseEx.getMessage(), siteWiseEx.awsErrorDetails().errorCode(), siteWiseEx); } else { logger.error("An unexpected error occurred: {}", cause.getMessage()); } return; } logger.info(DASHES); logger.info(DASHES); logger.info("9. Describe the IoT SiteWise Gateway"); waitForInputToContinue(scanner); try { sitewiseActions.describeGatewayAsync(gatewayId) .thenAccept(response -> { logger.info("Gateway Name: {}", response.gatewayName()); logger.info("Gateway ARN: {}", response.gatewayArn()); logger.info("Gateway Platform: {}", response.gatewayPlatform()); logger.info("Gateway Creation Date: {}", response.creationDate()); }).join(); } catch (CompletionException ce) { Throwable cause = ce.getCause(); if (cause instanceof ResourceNotFoundException notFoundException) { logger.error("A ResourceNotFoundException occurred: Error message: {}, Error code {}", notFoundException.getMessage(), notFoundException.awsErrorDetails().errorCode(), notFoundException); } else { logger.error("An unexpected error occurred: {}", cause.getMessage(), cause); } return; } logger.info(DASHES); logger.info(DASHES); logger.info("10. Delete the AWS IoT SiteWise Assets"); logger.info( """ Before you can delete the Asset Model, you must delete the assets. """); logger.info("Would you like to delete the IoT SiteWise Assets? (y/n)"); String delAns = scanner.nextLine().trim(); if (delAns.equalsIgnoreCase("y")) { logger.info("You selected to delete the SiteWise assets."); waitForInputToContinue(scanner); try { sitewiseActions.deletePortalAsync(portalId).join(); logger.info("Portal {} was deleted successfully.", portalId); } catch (CompletionException ce) { Throwable cause = ce.getCause(); if (cause instanceof ResourceNotFoundException notFoundException) { logger.error("A ResourceNotFoundException occurred: Error message: {}, Error code {}", notFoundException.getMessage(), notFoundException.awsErrorDetails().errorCode(), notFoundException); } else { logger.error("An unexpected error occurred: {}", cause.getMessage()); } } try { sitewiseActions.deleteGatewayAsync(gatewayId).join(); logger.info("Gateway {} was deleted successfully.", gatewayId); } catch (CompletionException ce) { Throwable cause = ce.getCause(); if (cause instanceof ResourceNotFoundException notFoundException) { logger.error("A ResourceNotFoundException occurred: Error message: {}, Error code {}", notFoundException.getMessage(), notFoundException.awsErrorDetails().errorCode(), notFoundException); } else { logger.error("An unexpected error occurred: {}", cause.getMessage()); } } try { sitewiseActions.deleteAssetAsync(assetId).join(); logger.info("Request to delete asset {} sent successfully", assetId); } catch (CompletionException ce) { Throwable cause = ce.getCause(); if (cause instanceof ResourceNotFoundException notFoundException) { logger.error("A ResourceNotFoundException occurred: Error message: {}, Error code {}", notFoundException.getMessage(), notFoundException.awsErrorDetails().errorCode(), notFoundException); } else { logger.error("An unexpected error occurred: {}", cause.getMessage()); } } logger.info("Let's wait 1 minute for the asset to be deleted."); countdown(60); waitForInputToContinue(scanner); logger.info("Delete the AWS IoT SiteWise Asset Model"); try { sitewiseActions.deleteAssetModelAsync(assetModelId).join(); logger.info("Asset model deleted successfully."); } catch (CompletionException ce) { Throwable cause = ce.getCause(); if (cause instanceof ResourceNotFoundException notFoundException) { logger.error("A ResourceNotFoundException occurred: Error message: {}, Error code {}", notFoundException.getMessage(), notFoundException.awsErrorDetails().errorCode(), notFoundException); } else { logger.error("An unexpected error occurred: {}", cause.getMessage()); } } waitForInputToContinue(scanner); } else { logger.info("The resources will not be deleted."); } logger.info(DASHES); logger.info(DASHES); CloudFormationHelper.destroyCloudFormationStack(ROLES_STACK); logger.info("This concludes the AWS IoT SiteWise Scenario"); logger.info(DASHES); } private static void waitForInputToContinue(Scanner scanner) { while (true) { logger.info(""); logger.info("Enter 'c' followed by <ENTER> to continue:"); String input = scanner.nextLine(); if (input.trim().equalsIgnoreCase("c")) { logger.info("Continuing with the program..."); logger.info(""); break; } else { logger.info("Invalid input. Please try again."); } } } public static void countdown(int totalSeconds) throws InterruptedException { for (int i = totalSeconds; i >= 0; i--) { int displayMinutes = i / 60; int displaySeconds = i % 60; System.out.printf("\r%02d:%02d", displayMinutes, displaySeconds); Thread.sleep(1000); // Wait for 1 second } System.out.println(); // Move to the next line after countdown logger.info("Countdown complete!"); } }

Kelas pembungkus untuk AWS IoT SiteWise SDK metode.

public class SitewiseActions { private static final Logger logger = LoggerFactory.getLogger(SitewiseActions.class); private static IoTSiteWiseAsyncClient ioTSiteWiseAsyncClient; private static IoTSiteWiseAsyncClient getAsyncClient() { if (ioTSiteWiseAsyncClient == null) { SdkAsyncHttpClient httpClient = NettyNioAsyncHttpClient.builder() .maxConcurrency(100) .connectionTimeout(Duration.ofSeconds(60)) .readTimeout(Duration.ofSeconds(60)) .writeTimeout(Duration.ofSeconds(60)) .build(); ClientOverrideConfiguration overrideConfig = ClientOverrideConfiguration.builder() .apiCallTimeout(Duration.ofMinutes(2)) .apiCallAttemptTimeout(Duration.ofSeconds(90)) .retryStrategy(RetryMode.STANDARD) .build(); ioTSiteWiseAsyncClient = IoTSiteWiseAsyncClient.builder() .httpClient(httpClient) .overrideConfiguration(overrideConfig) .build(); } return ioTSiteWiseAsyncClient; } /** * Creates an asset model. * * @param name the name of the asset model to create. * @return a {@link CompletableFuture} that represents a {@link CreateAssetModelResponse} result. The calling code * can attach callbacks, then handle the result or exception by calling {@link CompletableFuture#join()} or * {@link CompletableFuture#get()}. * <p> * If any completion stage in this method throws an exception, the method logs the exception cause and keeps it * available to the calling code as a {@link CompletionException}. By calling * {@link CompletionException#getCause()}, the calling code can access the original exception. */ public CompletableFuture<CreateAssetModelResponse> createAssetModelAsync(String name) { PropertyType humidity = PropertyType.builder() .measurement(Measurement.builder().build()) .build(); PropertyType temperaturePropertyType = PropertyType.builder() .measurement(Measurement.builder().build()) .build(); AssetModelPropertyDefinition temperatureProperty = AssetModelPropertyDefinition.builder() .name("Temperature") .dataType(PropertyDataType.DOUBLE) .type(temperaturePropertyType) .build(); AssetModelPropertyDefinition humidityProperty = AssetModelPropertyDefinition.builder() .name("Humidity") .dataType(PropertyDataType.DOUBLE) .type(humidity) .build(); CreateAssetModelRequest createAssetModelRequest = CreateAssetModelRequest.builder() .assetModelName(name) .assetModelDescription("This is my asset model") .assetModelProperties(temperatureProperty, humidityProperty) .build(); return getAsyncClient().createAssetModel(createAssetModelRequest) .whenComplete((response, exception) -> { if (exception != null) { logger.error("Failed to create asset model: {} ", exception.getCause().getMessage()); } }); } /** * Creates an asset with the specified name and asset model Id. * * @param assetName the name of the asset to create. * @param assetModelId the Id of the asset model to associate with the asset. * @return a {@link CompletableFuture} that represents a {@link CreateAssetResponse} result. The calling code can * attach callbacks, then handle the result or exception by calling {@link CompletableFuture#join()} or * {@link CompletableFuture#get()}. * <p> * If any completion stage in this method throws an exception, the method logs the exception cause and keeps it * available to the calling code as a {@link CompletionException}. By calling * {@link CompletionException#getCause()}, the calling code can access the original exception. */ public CompletableFuture<CreateAssetResponse> createAssetAsync(String assetName, String assetModelId) { CreateAssetRequest createAssetRequest = CreateAssetRequest.builder() .assetModelId(assetModelId) .assetDescription("Created using the AWS SDK for Java") .assetName(assetName) .build(); return getAsyncClient().createAsset(createAssetRequest) .whenComplete((response, exception) -> { if (exception != null) { logger.error("Failed to create asset: {}", exception.getCause().getMessage()); } }); } /** * Sends data to the SiteWise service. * * @param assetId the ID of the asset to which the data will be sent. * @param tempPropertyId the ID of the temperature property. * @param humidityPropId the ID of the humidity property. * @return a {@link CompletableFuture} that represents a {@link BatchPutAssetPropertyValueResponse} result. The * calling code can attach callbacks, then handle the result or exception by calling * {@link CompletableFuture#join()} or {@link CompletableFuture#get()}. * <p> * If any completion stage in this method throws an exception, the method logs the exception cause and keeps it * available to the calling code as a {@link CompletionException}. By calling * {@link CompletionException#getCause()}, the calling code can access the original exception. */ public CompletableFuture<BatchPutAssetPropertyValueResponse> sendDataToSiteWiseAsync(String assetId, String tempPropertyId, String humidityPropId) { Map<String, Double> sampleData = generateSampleData(); long timestamp = Instant.now().toEpochMilli(); TimeInNanos time = TimeInNanos.builder() .timeInSeconds(timestamp / 1000) .offsetInNanos((int) ((timestamp % 1000) * 1000000)) .build(); BatchPutAssetPropertyValueRequest request = BatchPutAssetPropertyValueRequest.builder() .entries(Arrays.asList( PutAssetPropertyValueEntry.builder() .entryId("entry-3") .assetId(assetId) .propertyId(tempPropertyId) .propertyValues(Arrays.asList( AssetPropertyValue.builder() .value(Variant.builder() .doubleValue(sampleData.get("Temperature")) .build()) .timestamp(time) .build() )) .build(), PutAssetPropertyValueEntry.builder() .entryId("entry-4") .assetId(assetId) .propertyId(humidityPropId) .propertyValues(Arrays.asList( AssetPropertyValue.builder() .value(Variant.builder() .doubleValue(sampleData.get("Humidity")) .build()) .timestamp(time) .build() )) .build() )) .build(); return getAsyncClient().batchPutAssetPropertyValue(request) .whenComplete((response, exception) -> { if (exception != null) { logger.error("An exception occurred: {}", exception.getCause().getMessage()); } }); } /** * Fetches the value of an asset property. * * @param propId the ID of the asset property to fetch. * @param assetId the ID of the asset to fetch the property value for. * @return a {@link CompletableFuture} that represents a {@link Double} result. The calling code can attach * callbacks, then handle the result or exception by calling {@link CompletableFuture#join()} or * {@link CompletableFuture#get()}. * <p> * If any completion stage in this method throws an exception, the method logs the exception cause and keeps * it available to the calling code as a {@link CompletionException}. By calling * {@link CompletionException#getCause()}, the calling code can access the original exception. */ public CompletableFuture<Double> getAssetPropValueAsync(String propId, String assetId) { GetAssetPropertyValueRequest assetPropertyValueRequest = GetAssetPropertyValueRequest.builder() .propertyId(propId) .assetId(assetId) .build(); return getAsyncClient().getAssetPropertyValue(assetPropertyValueRequest) .handle((response, exception) -> { if (exception != null) { logger.error("Error occurred while fetching property value: {}.", exception.getCause().getMessage()); throw (CompletionException) exception; } return response.propertyValue().value().doubleValue(); }); } /** * Retrieves the property IDs associated with a specific asset model. * * @param assetModelId the ID of the asset model that defines the properties. * @return a {@link CompletableFuture} that represents a {@link Map} result that associates the property name to the * propert ID. The calling code can attach callbacks, then handle the result or exception by calling * {@link CompletableFuture#join()} or {@link CompletableFuture#get()}. * <p> * If any completion stage in this method throws an exception, the method logs the exception cause and keeps * it available to the calling code as a {@link CompletionException}. By calling * {@link CompletionException#getCause()}, the calling code can access the original exception. */ public CompletableFuture<Map<String, String>> getPropertyIds(String assetModelId) { ListAssetModelPropertiesRequest modelPropertiesRequest = ListAssetModelPropertiesRequest.builder().assetModelId(assetModelId).build(); return getAsyncClient().listAssetModelProperties(modelPropertiesRequest) .handle((response, throwable) -> { if (response != null) { return response.assetModelPropertySummaries().stream() .collect(Collectors .toMap(AssetModelPropertySummary::name, AssetModelPropertySummary::id)); } else { logger.error("Error occurred while fetching property IDs: {}.", throwable.getCause().getMessage()); throw (CompletionException) throwable; } }); } /** * Deletes an asset. * * @param assetId the ID of the asset to be deleted. * @return a {@link CompletableFuture} that represents a {@link DeleteAssetResponse} result. The calling code can * attach callbacks, then handle the result or exception by calling {@link CompletableFuture#join()} or * {@link CompletableFuture#get()}. * <p> * If any completion stage in this method throws an exception, the method logs the exception cause and keeps * it available to the calling code as a {@link CompletionException}. By calling * {@link CompletionException#getCause()}, the calling code can access the original exception. */ public CompletableFuture<DeleteAssetResponse> deleteAssetAsync(String assetId) { DeleteAssetRequest deleteAssetRequest = DeleteAssetRequest.builder() .assetId(assetId) .build(); return getAsyncClient().deleteAsset(deleteAssetRequest) .whenComplete((response, exception) -> { if (exception != null) { logger.error("An error occurred deleting asset with id: {}", assetId); } }); } /** * Deletes an Asset Model with the specified ID. * * @param assetModelId the ID of the Asset Model to delete. * @return a {@link CompletableFuture} that represents a {@link DeleteAssetModelResponse} result. The calling code * can attach callbacks, then handle the result or exception by calling {@link CompletableFuture#join()} or * {@link CompletableFuture#get()}. * <p> * If any completion stage in this method throws an exception, the method logs the exception cause and keeps * it available to the calling code as a {@link CompletionException}. By calling * {@link CompletionException#getCause()}, the calling code can access the original exception. */ public CompletableFuture<DeleteAssetModelResponse> deleteAssetModelAsync(String assetModelId) { DeleteAssetModelRequest deleteAssetModelRequest = DeleteAssetModelRequest.builder() .assetModelId(assetModelId) .build(); return getAsyncClient().deleteAssetModel(deleteAssetModelRequest) .whenComplete((response, exception) -> { if (exception != null) { logger.error("Failed to delete asset model with ID:{}.", exception.getMessage()); } }); } /** * Creates a new IoT SiteWise portal. * * @param portalName the name of the portal to create. * @param iamRole the IAM role ARN to use for the portal. * @param contactEmail the email address of the portal contact. * @return a {@link CompletableFuture} that represents a {@link String} result of the portal ID. The calling code * can attach callbacks, then handle the result or exception by calling {@link CompletableFuture#join()} or * {@link CompletableFuture#get()}. * <p> * If any completion stage in this method throws an exception, the method logs the exception cause and keeps * it available to the calling code as a {@link CompletionException}. By calling * {@link CompletionException#getCause()}, the calling code can access the original exception. */ public CompletableFuture<String> createPortalAsync(String portalName, String iamRole, String contactEmail) { CreatePortalRequest createPortalRequest = CreatePortalRequest.builder() .portalName(portalName) .portalDescription("This is my custom IoT SiteWise portal.") .portalContactEmail(contactEmail) .roleArn(iamRole) .build(); return getAsyncClient().createPortal(createPortalRequest) .handle((response, exception) -> { if (exception != null) { logger.error("Failed to create portal: {} ", exception.getCause().getMessage()); throw (CompletionException) exception; } return response.portalId(); }); } /** * Deletes a portal. * * @param portalId the ID of the portal to be deleted. * @return a {@link CompletableFuture} that represents a {@link DeletePortalResponse}. The calling code can attach * callbacks, then handle the result or exception by calling {@link CompletableFuture#join()} or * {@link CompletableFuture#get()}. * <p> * If any completion stage in this method throws an exception, the method logs the exception cause and keeps * it available to the calling code as a {@link CompletionException}. By calling * {@link CompletionException#getCause()}, the calling code can access the original exception. */ public CompletableFuture<DeletePortalResponse> deletePortalAsync(String portalId) { DeletePortalRequest deletePortalRequest = DeletePortalRequest.builder() .portalId(portalId) .build(); return getAsyncClient().deletePortal(deletePortalRequest) .whenComplete((response, exception) -> { if (exception != null) { logger.error("Failed to delete portal with ID: {}. Error: {}", portalId, exception.getCause().getMessage()); } }); } /** * Retrieves the asset model ID for the given asset model name. * * @param assetModelName the name of the asset model for the ID. * @return a {@link CompletableFuture} that represents a {@link String} result of the asset model ID or null if the * asset model cannot be found. The calling code can attach callbacks, then handle the result or exception * by calling {@link CompletableFuture#join()} or {@link CompletableFuture#get()}. * <p> * If any completion stage in this method throws an exception, the method logs the exception cause and keeps * it available to the calling code as a {@link CompletionException}. By calling * {@link CompletionException#getCause()}, the calling code can access the original exception. */ public CompletableFuture<String> getAssetModelIdAsync(String assetModelName) { ListAssetModelsRequest listAssetModelsRequest = ListAssetModelsRequest.builder().build(); return getAsyncClient().listAssetModels(listAssetModelsRequest) .handle((listAssetModelsResponse, exception) -> { if (exception != null) { logger.error("Failed to retrieve Asset Model ID: {}", exception.getCause().getMessage()); throw (CompletionException) exception; } for (AssetModelSummary assetModelSummary : listAssetModelsResponse.assetModelSummaries()) { if (assetModelSummary.name().equals(assetModelName)) { return assetModelSummary.id(); } } return null; }); } /** * Retrieves a portal's description. * * @param portalId the ID of the portal to describe. * @return a {@link CompletableFuture} that represents a {@link String} result of the portal's start URL * (see: {@link DescribePortalResponse#portalStartUrl()}). The calling code can attach callbacks, then handle the * result or exception by calling {@link CompletableFuture#join()} or {@link CompletableFuture#get()}. * <p> * If any completion stage in this method throws an exception, the method logs the exception cause and keeps * it available to the calling code as a {@link CompletionException}. By calling * {@link CompletionException#getCause()}, the calling code can access the original exception. */ public CompletableFuture<String> describePortalAsync(String portalId) { DescribePortalRequest request = DescribePortalRequest.builder() .portalId(portalId) .build(); return getAsyncClient().describePortal(request) .handle((response, exception) -> { if (exception != null) { logger.error("An exception occurred retrieving the portal description: {}", exception.getCause().getMessage()); throw (CompletionException) exception; } return response.portalStartUrl(); }); } /** * Creates a new IoT Sitewise gateway. * * @param gatewayName The name of the gateway to create. * @param myThing The name of the core device thing to associate with the gateway. * @return a {@link CompletableFuture} that represents a {@link String} result of the gateways ID. The calling code * can attach callbacks, then handle the result or exception by calling {@link CompletableFuture#join()} or * {@link CompletableFuture#get()}. * <p> * If any completion stage in this method throws an exception, the method logs the exception cause and keeps * it available to the calling code as a {@link CompletionException}. By calling * {@link CompletionException#getCause()}, the calling code can access the original exception. */ public CompletableFuture<String> createGatewayAsync(String gatewayName, String myThing) { GreengrassV2 gg = GreengrassV2.builder() .coreDeviceThingName(myThing) .build(); GatewayPlatform platform = GatewayPlatform.builder() .greengrassV2(gg) .build(); Map<String, String> tag = new HashMap<>(); tag.put("Environment", "Production"); CreateGatewayRequest createGatewayRequest = CreateGatewayRequest.builder() .gatewayName(gatewayName) .gatewayPlatform(platform) .tags(tag) .build(); return getAsyncClient().createGateway(createGatewayRequest) .handle((response, exception) -> { if (exception != null) { logger.error("Error creating the gateway."); throw (CompletionException) exception; } logger.info("The ARN of the gateway is {}" , response.gatewayArn()); return response.gatewayId(); }); } /** * Deletes the specified gateway. * * @param gatewayId the ID of the gateway to delete. * @return a {@link CompletableFuture} that represents a {@link DeleteGatewayResponse} result.. The calling code * can attach callbacks, then handle the result or exception by calling {@link CompletableFuture#join()} or * {@link CompletableFuture#get()}. * <p> * If any completion stage in this method throws an exception, the method logs the exception cause and keeps * it available to the calling code as a {@link CompletionException}. By calling * {@link CompletionException#getCause()}, the calling code can access the original exception. */ public CompletableFuture<DeleteGatewayResponse> deleteGatewayAsync(String gatewayId) { DeleteGatewayRequest deleteGatewayRequest = DeleteGatewayRequest.builder() .gatewayId(gatewayId) .build(); return getAsyncClient().deleteGateway(deleteGatewayRequest) .whenComplete((response, exception) -> { if (exception != null) { logger.error("Failed to delete gateway: {}", exception.getCause().getMessage()); } }); } /** * Describes the specified gateway. * * @param gatewayId the ID of the gateway to describe. * @return a {@link CompletableFuture} that represents a {@link DescribeGatewayResponse} result. The calling code * can attach callbacks, then handle the result or exception by calling {@link CompletableFuture#join()} or * {@link CompletableFuture#get()}. * <p> * If any completion stage in this method throws an exception, the method logs the exception cause and keeps * it available to the calling code as a {@link CompletionException}. By calling * {@link CompletionException#getCause()}, the calling code can access the original exception. */ public CompletableFuture<DescribeGatewayResponse> describeGatewayAsync(String gatewayId) { DescribeGatewayRequest request = DescribeGatewayRequest.builder() .gatewayId(gatewayId) .build(); return getAsyncClient().describeGateway(request) .whenComplete((response, exception) -> { if (exception != null) { logger.error("An error occurred during the describeGateway method: {}", exception.getCause().getMessage()); } }); } private static Map<String, Double> generateSampleData() { Map<String, Double> data = new HashMap<>(); data.put("Temperature", 23.5); data.put("Humidity", 65.0); return data; } }