選取您的 Cookie 偏好設定

我們使用提供自身網站和服務所需的基本 Cookie 和類似工具。我們使用效能 Cookie 收集匿名統計資料,以便了解客戶如何使用我們的網站並進行改進。基本 Cookie 無法停用,但可以按一下「自訂」或「拒絕」以拒絕效能 Cookie。

如果您同意,AWS 與經核准的第三方也會使用 Cookie 提供實用的網站功能、記住您的偏好設定,並顯示相關內容,包括相關廣告。若要接受或拒絕所有非必要 Cookie,請按一下「接受」或「拒絕」。若要進行更詳細的選擇,請按一下「自訂」。

AWS IoT SiteWise 使用適用於 Java 的 SDK 2.x 的範例 - AWS SDK 程式碼範例

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

AWS IoT SiteWise 使用適用於 Java 的 SDK 2.x 的範例

下列程式碼範例示範如何使用 AWS SDK for Java 2.x 搭配 來執行動作和實作常見案例 AWS IoT SiteWise。

基本概念是程式碼範例,這些範例說明如何在服務內執行基本操作。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

開始使用

下列程式碼範例示範如何開始使用 AWS IoT SiteWise。

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

public class HelloSitewise { private static final Logger logger = LoggerFactory.getLogger(HelloSitewise.class); public static void main(String[] args) { fetchAssetModels(); } /** * Fetches asset models using the provided {@link IoTSiteWiseAsyncClient}. */ public static void fetchAssetModels() { IoTSiteWiseAsyncClient siteWiseAsyncClient = IoTSiteWiseAsyncClient.create(); ListAssetModelsRequest assetModelsRequest = ListAssetModelsRequest.builder() .assetModelTypes(AssetModelType.ASSET_MODEL) .build(); // Asynchronous paginator - process paginated results. ListAssetModelsPublisher listModelsPaginator = siteWiseAsyncClient.listAssetModelsPaginator(assetModelsRequest); CompletableFuture<Void> future = listModelsPaginator.subscribe(response -> { response.assetModelSummaries().forEach(assetSummary -> logger.info("Asset Model Name: {} ", assetSummary.name()) ); }); // Wait for the asynchronous operation to complete future.join(); } }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 ListAssetModels

下列程式碼範例示範如何開始使用 AWS IoT SiteWise。

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

public class HelloSitewise { private static final Logger logger = LoggerFactory.getLogger(HelloSitewise.class); public static void main(String[] args) { fetchAssetModels(); } /** * Fetches asset models using the provided {@link IoTSiteWiseAsyncClient}. */ public static void fetchAssetModels() { IoTSiteWiseAsyncClient siteWiseAsyncClient = IoTSiteWiseAsyncClient.create(); ListAssetModelsRequest assetModelsRequest = ListAssetModelsRequest.builder() .assetModelTypes(AssetModelType.ASSET_MODEL) .build(); // Asynchronous paginator - process paginated results. ListAssetModelsPublisher listModelsPaginator = siteWiseAsyncClient.listAssetModelsPaginator(assetModelsRequest); CompletableFuture<Void> future = listModelsPaginator.subscribe(response -> { response.assetModelSummaries().forEach(assetSummary -> logger.info("Asset Model Name: {} ", assetSummary.name()) ); }); // Wait for the asynchronous operation to complete future.join(); } }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 ListAssetModels

基本概念

下列程式碼範例說明如何學習 AWS IoT SiteWise 使用 AWS SDK 的核心操作。

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

執行互動式案例,示範 AWS IoT SiteWise 功能。

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!"); } }

SDK AWS IoT SiteWise 方法的包裝函式類別。

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; } }

下列程式碼範例說明如何學習 AWS IoT SiteWise 使用 AWS SDK 的核心操作。

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

執行互動式案例,示範 AWS IoT SiteWise 功能。

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!"); } }

SDK AWS IoT SiteWise 方法的包裝函式類別。

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; } }

動作

下列程式碼範例示範如何使用 BatchPutAssetPropertyValue

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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()); } }); }

下列程式碼範例示範如何使用 BatchPutAssetPropertyValue

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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()); } }); }

下列程式碼範例示範如何使用 CreateAsset

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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()); } }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 CreateAsset

下列程式碼範例示範如何使用 CreateAsset

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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()); } }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 CreateAsset

下列程式碼範例示範如何使用 CreateAssetModel

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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()); } }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 CreateAssetModel

下列程式碼範例示範如何使用 CreateAssetModel

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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()); } }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 CreateAssetModel

下列程式碼範例示範如何使用 CreateGateway

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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(); }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 CreateGateway

下列程式碼範例示範如何使用 CreateGateway

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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(); }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 CreateGateway

下列程式碼範例示範如何使用 CreatePortal

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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(); }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 CreatePortal

下列程式碼範例示範如何使用 CreatePortal

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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(); }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 CreatePortal

下列程式碼範例示範如何使用 DeleteAsset

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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); } }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 DeleteAsset

下列程式碼範例示範如何使用 DeleteAsset

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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); } }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 DeleteAsset

下列程式碼範例示範如何使用 DeleteAssetModel

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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()); } }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 DeleteAssetModel

下列程式碼範例示範如何使用 DeleteAssetModel

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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()); } }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 DeleteAssetModel

下列程式碼範例示範如何使用 DeleteGateway

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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()); } }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 DeleteGateway

下列程式碼範例示範如何使用 DeleteGateway

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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()); } }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 DeleteGateway

下列程式碼範例示範如何使用 DeletePortal

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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()); } }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 DeletePortal

下列程式碼範例示範如何使用 DeletePortal

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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()); } }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 DeletePortal

下列程式碼範例示範如何使用 DescribeAssetModel

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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; } }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 DescribeAssetModel

下列程式碼範例示範如何使用 DescribeAssetModel

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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; } }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 DescribeAssetModel

下列程式碼範例示範如何使用 DescribeGateway

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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()); } }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 DescribeGateway

下列程式碼範例示範如何使用 DescribeGateway

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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()); } }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 DescribeGateway

下列程式碼範例示範如何使用 DescribePortal

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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(); }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 DescribePortal

下列程式碼範例示範如何使用 DescribePortal

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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(); }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 DescribePortal

下列程式碼範例示範如何使用 GetAssetPropertyValue

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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(); }); }

下列程式碼範例示範如何使用 GetAssetPropertyValue

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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(); }); }

下列程式碼範例示範如何使用 ListAssetModels

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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; }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 ListAssetModels

下列程式碼範例示範如何使用 ListAssetModels

SDK for Java 2.x
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * 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; }); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 ListAssetModels

下一個主題:

Amazon Keyspaces

上一個主題:

AWS IoT data
隱私權網站條款Cookie 偏好設定
© 2025, Amazon Web Services, Inc.或其附屬公司。保留所有權利。