쿠키 기본 설정 선택

당사는 사이트와 서비스를 제공하는 데 필요한 필수 쿠키 및 유사한 도구를 사용합니다. 고객이 사이트를 어떻게 사용하는지 파악하고 개선할 수 있도록 성능 쿠키를 사용해 익명의 통계를 수집합니다. 필수 쿠키는 비활성화할 수 없지만 '사용자 지정' 또는 ‘거부’를 클릭하여 성능 쿠키를 거부할 수 있습니다.

사용자가 동의하는 경우 AWS와 승인된 제3자도 쿠키를 사용하여 유용한 사이트 기능을 제공하고, 사용자의 기본 설정을 기억하고, 관련 광고를 비롯한 관련 콘텐츠를 표시합니다. 필수가 아닌 모든 쿠키를 수락하거나 거부하려면 ‘수락’ 또는 ‘거부’를 클릭하세요. 더 자세한 내용을 선택하려면 ‘사용자 정의’를 클릭하세요.

AWS SDK를 AWS IoT SiteWise 사용하여의 기본 사항 알아보기 - AWS SDK 코드 예제

Doc AWS SDK 예제 GitHub 리포지토리에서 더 많은 SDK 예제를 사용할 수 있습니다. AWS

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

Doc AWS SDK 예제 GitHub 리포지토리에서 더 많은 SDK 예제를 사용할 수 있습니다. AWS

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

AWS SDK를 AWS IoT SiteWise 사용하여의 기본 사항 알아보기

다음 코드 예제에서는 AWS SDK AWS IoT SiteWise 사용을 위한 핵심 작업을 학습하는 방법을 보여줍니다.

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

AWS IoT SiteWise SDK 메서드의 래퍼 클래스입니다.

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; } }
JavaScript
SDK for JavaScript (v3)
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

import { Scenario, ScenarioAction, ScenarioInput, ScenarioOutput, //} from "@aws-doc-sdk-examples/lib/scenario/index.js"; } from "../../libs/scenario/index.js"; import { IoTSiteWiseClient, CreateAssetModelCommand, CreateAssetCommand, ListAssetModelPropertiesCommand, BatchPutAssetPropertyValueCommand, GetAssetPropertyValueCommand, CreatePortalCommand, DescribePortalCommand, CreateGatewayCommand, DescribeGatewayCommand, DeletePortalCommand, DeleteGatewayCommand, DeleteAssetCommand, DeleteAssetModelCommand, DescribeAssetModelCommand, } from "@aws-sdk/client-iotsitewise"; import { CloudFormationClient, CreateStackCommand, DeleteStackCommand, DescribeStacksCommand, waitUntilStackExists, waitUntilStackCreateComplete, waitUntilStackDeleteComplete, } from "@aws-sdk/client-cloudformation"; import { wait } from "@aws-doc-sdk-examples/lib/utils/util-timers.js"; import { parseArgs } from "node:util"; import { readFileSync } from "node:fs"; import { fileURLToPath } from "node:url"; import { dirname } from "node:path"; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const stackName = "SiteWiseBasicsStack"; /** * @typedef {{ * iotSiteWiseClient: import('@aws-sdk/client-iotsitewise').IotSiteWiseClient, * cloudFormationClient: import('@aws-sdk/client-cloudformation').CloudFormationClient, * stackName, * stack, * askToDeleteResources: true, * asset: {assetName: "MyAsset1"}, * assetModel: {assetModelName: "MyAssetModel1"}, * portal: {portalName: "MyPortal1"}, * gateway: {gatewayName: "MyGateway1"}, * propertyIds: [], * contactEmail: "user@mydomain.com", * thing: "MyThing1", * sampleData: { temperature: 23.5, humidity: 65.0} * }} State */ /** * Used repeatedly to have the user press enter. * @type {ScenarioInput} */ const pressEnter = new ScenarioInput("continue", "Press Enter to continue", { type: "confirm", }); const greet = new ScenarioOutput( "greet", `AWS IoT SiteWise is a fully managed industrial 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...`, { header: true }, ); const displayBuildCloudFormationStack = new ScenarioOutput( "displayBuildCloudFormationStack", "This scenario uses AWS CloudFormation to create an IAM role that is required for this scenario. The stack will now be deployed.", ); const sdkBuildCloudFormationStack = new ScenarioAction( "sdkBuildCloudFormationStack", async (/** @type {State} */ state) => { try { const data = readFileSync( `${__dirname}/../../../../resources/cfn/iotsitewise_basics/SitewiseRoles-template.yml`, "utf8", ); await state.cloudFormationClient.send( new CreateStackCommand({ StackName: stackName, TemplateBody: data, Capabilities: ["CAPABILITY_IAM"], }), ); await waitUntilStackExists( { client: state.cloudFormationClient }, { StackName: stackName }, ); await waitUntilStackCreateComplete( { client: state.cloudFormationClient }, { StackName: stackName }, ); const stack = await state.cloudFormationClient.send( new DescribeStacksCommand({ StackName: stackName, }), ); state.stack = stack.Stacks[0].Outputs[0]; console.log(`The ARN of the IAM role is ${state.stack.OutputValue}`); } catch (caught) { console.error(caught.message); throw caught; } }, ); const displayCreateAWSSiteWiseAssetModel = new ScenarioOutput( "displayCreateAWSSiteWiseAssetModel", `1. Create an AWS SiteWise Asset Model 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.`, ); const sdkCreateAWSSiteWiseAssetModel = new ScenarioAction( "sdkCreateAWSSiteWiseAssetModel", async (/** @type {State} */ state) => { let assetModelResponse; try { assetModelResponse = await state.iotSiteWiseClient.send( new CreateAssetModelCommand({ assetModelName: state.assetModel.assetModelName, assetModelProperties: [ { name: "Temperature", dataType: "DOUBLE", type: { measurement: {}, }, }, { name: "Humidity", dataType: "DOUBLE", type: { measurement: {}, }, }, ], }), ); state.assetModel.assetModelId = assetModelResponse.assetModelId; console.log( `Asset Model successfully created. Asset Model ID: ${state.assetModel.assetModelId}`, ); } catch (caught) { if (caught.name === "ResourceAlreadyExistsException") { console.log( `The Asset Model ${state.assetModel.assetModelName} already exists.`, ); throw caught; } console.error(`${caught.message}`); throw caught; } }, ); const displayCreateAWSIoTSiteWiseAssetModel = new ScenarioOutput( "displayCreateAWSIoTSiteWiseAssetModel", `2. Create an AWS IoT SiteWise Asset 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. Let's wait 30 seconds for the asset to be ready.`, ); const waitThirtySeconds = new ScenarioAction("waitThirtySeconds", async () => { await wait(30); // wait 30 seconds console.log("Time's up! Let's check the asset's status."); }); const sdkCreateAWSIoTSiteWiseAssetModel = new ScenarioAction( "sdkCreateAWSIoTSiteWiseAssetModel", async (/** @type {State} */ state) => { try { const assetResponse = await state.iotSiteWiseClient.send( new CreateAssetCommand({ assetModelId: state.assetModel.assetModelId, assetName: state.asset.assetName, }), ); state.asset.assetId = assetResponse.assetId; console.log(`Asset created with ID: ${state.asset.assetId}`); } catch (caught) { if (caught.name === "ResourceNotFoundException") { console.log( `The Asset ${state.assetModel.assetModelName} was not found.`, ); throw caught; } console.error(`${caught.message}`); throw caught; } }, ); const displayRetrievePropertyId = new ScenarioOutput( "displayRetrievePropertyId", `3. Retrieve the property ID values 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.`, ); const sdkRetrievePropertyId = new ScenarioAction( "sdkRetrievePropertyId", async (state) => { try { const retrieveResponse = await state.iotSiteWiseClient.send( new ListAssetModelPropertiesCommand({ assetModelId: state.assetModel.assetModelId, }), ); for (const retrieveResponseKey in retrieveResponse.assetModelPropertySummaries) { if ( retrieveResponse.assetModelPropertySummaries[retrieveResponseKey] .name === "Humidity" ) { state.propertyIds.Humidity = retrieveResponse.assetModelPropertySummaries[ retrieveResponseKey ].id; } if ( retrieveResponse.assetModelPropertySummaries[retrieveResponseKey] .name === "Temperature" ) { state.propertyIds.Temperature = retrieveResponse.assetModelPropertySummaries[ retrieveResponseKey ].id; } } console.log(`The Humidity propertyId is ${state.propertyIds.Humidity}`); console.log( `The Temperature propertyId is ${state.propertyIds.Temperature}`, ); } catch (caught) { if (caught.name === "IoTSiteWiseException") { console.log( `There was a problem retrieving the properties: ${caught.message}`, ); throw caught; } console.error(`${caught.message}`); throw caught; } }, ); const displaySendDataToIoTSiteWiseAsset = new ScenarioOutput( "displaySendDataToIoTSiteWiseAsset", `4. Send data to an AWS IoT SiteWise Asset 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.`, ); const sdkSendDataToIoTSiteWiseAsset = new ScenarioAction( "sdkSendDataToIoTSiteWiseAsset", async (state) => { try { const sendResponse = await state.iotSiteWiseClient.send( new BatchPutAssetPropertyValueCommand({ entries: [ { entryId: "entry-3", assetId: state.asset.assetId, propertyId: state.propertyIds.Humidity, propertyValues: [ { value: { doubleValue: state.sampleData.humidity, }, timestamp: { timeInSeconds: Math.floor(Date.now() / 1000), }, }, ], }, { entryId: "entry-4", assetId: state.asset.assetId, propertyId: state.propertyIds.Temperature, propertyValues: [ { value: { doubleValue: state.sampleData.temperature, }, timestamp: { timeInSeconds: Math.floor(Date.now() / 1000), }, }, ], }, ], }), ); console.log("The data was sent successfully."); } catch (caught) { if (caught.name === "ResourceNotFoundException") { console.log(`The Asset ${state.asset.assetName} was not found.`); throw caught; } console.error(`${caught.message}`); throw caught; } }, ); const displayRetrieveValueOfIoTSiteWiseAsset = new ScenarioOutput( "displayRetrieveValueOfIoTSiteWiseAsset", `5. Retrieve the value of the IoT SiteWise Asset property 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.`, ); const sdkRetrieveValueOfIoTSiteWiseAsset = new ScenarioAction( "sdkRetrieveValueOfIoTSiteWiseAsset", async (/** @type {State} */ state) => { try { const temperatureResponse = await state.iotSiteWiseClient.send( new GetAssetPropertyValueCommand({ assetId: state.asset.assetId, propertyId: state.propertyIds.Temperature, }), ); const humidityResponse = await state.iotSiteWiseClient.send( new GetAssetPropertyValueCommand({ assetId: state.asset.assetId, propertyId: state.propertyIds.Humidity, }), ); console.log( `The property value for Temperature is ${temperatureResponse.propertyValue.value.doubleValue}`, ); console.log( `The property value for Humidity is ${humidityResponse.propertyValue.value.doubleValue}`, ); } catch (caught) { if (caught.name === "ResourceNotFoundException") { console.log(`The Asset ${state.asset.assetName} was not found.`); throw caught; } console.error(`${caught.message}`); throw caught; } }, ); const displayCreateIoTSiteWisePortal = new ScenarioOutput( "displayCreateIoTSiteWisePortal", `6. Create an IoT SiteWise Portal An IoT SiteWise Portal allows you to aggregate data from multiple industrial sources, such as sensors, equipment, and control systems, into a centralized platform.`, ); const sdkCreateIoTSiteWisePortal = new ScenarioAction( "sdkCreateIoTSiteWisePortal", async (/** @type {State} */ state) => { try { const createPortalResponse = await state.iotSiteWiseClient.send( new CreatePortalCommand({ portalName: state.portal.portalName, portalContactEmail: state.contactEmail, roleArn: state.stack.OutputValue, }), ); state.portal = { ...state.portal, ...createPortalResponse }; await wait(5); // Allow the portal to properly propagate. console.log( `Portal created successfully. Portal ID ${createPortalResponse.portalId}`, ); } catch (caught) { if (caught.name === "IoTSiteWiseException") { console.log( `There was a problem creating the Portal: ${caught.message}.`, ); throw caught; } console.error(`${caught.message}`); throw caught; } }, ); const displayDescribePortal = new ScenarioOutput( "displayDescribePortal", `7. Describe the Portal In this step, we get a description of the portal and display the portal URL.`, ); const sdkDescribePortal = new ScenarioAction( "sdkDescribePortal", async (/** @type {State} */ state) => { try { const describePortalResponse = await state.iotSiteWiseClient.send( new DescribePortalCommand({ portalId: state.portal.portalId, }), ); console.log(`Portal URL: ${describePortalResponse.portalStartUrl}`); } catch (caught) { if (caught.name === "ResourceNotFoundException") { console.log(`The Portal ${state.portal.portalName} was not found.`); throw caught; } console.error(`${caught.message}`); throw caught; } }, ); const displayCreateIoTSiteWiseGateway = new ScenarioOutput( "displayCreateIoTSiteWiseGateway", `8. Create an IoT SiteWise Gateway 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.`, ); const sdkCreateIoTSiteWiseGateway = new ScenarioAction( "sdkCreateIoTSiteWiseGateway", async (/** @type {State} */ state) => { try { const createGatewayResponse = await state.iotSiteWiseClient.send( new CreateGatewayCommand({ gatewayName: state.gateway.gatewayName, gatewayPlatform: { greengrassV2: { coreDeviceThingName: state.thing, }, }, }), ); console.log( `Gateway creation completed successfully. ID is ${createGatewayResponse.gatewayId}`, ); state.gateway.gatewayId = createGatewayResponse.gatewayId; } catch (caught) { if (caught.name === "IoTSiteWiseException") { console.log( `There was a problem creating the gateway: ${caught.message}.`, ); throw caught; } console.error(`${caught.message}`); throw caught; } }, ); const displayDescribeIoTSiteWiseGateway = new ScenarioOutput( "displayDescribeIoTSiteWiseGateway", "9. Describe the IoT SiteWise Gateway", ); const sdkDescribeIoTSiteWiseGateway = new ScenarioAction( "sdkDescribeIoTSiteWiseGateway", async (/** @type {State} */ state) => { try { const describeGatewayResponse = await state.iotSiteWiseClient.send( new DescribeGatewayCommand({ gatewayId: state.gateway.gatewayId, }), ); console.log("Gateway creation completed successfully."); console.log(`Gateway Name: ${describeGatewayResponse.gatewayName}`); console.log(`Gateway ARN: ${describeGatewayResponse.gatewayArn}`); console.log( `Gateway Platform: ${Object.keys(describeGatewayResponse.gatewayPlatform)}`, ); console.log( `Gateway Creation Date: ${describeGatewayResponse.creationDate}`, ); } catch (caught) { if (caught.name === "ResourceNotFoundException") { console.log(`The Gateway ${state.gateway.gatewayId} was not found.`); throw caught; } console.error(`${caught.message}`); throw caught; } }, ); const askToDeleteResources = new ScenarioInput( "askToDeleteResources", `10. Delete the AWS IoT SiteWise Assets Before you can delete the Asset Model, you must delete the assets.`, { type: "confirm" }, ); const displayConfirmDeleteResources = new ScenarioAction( "displayConfirmDeleteResources", async (/** @type {State} */ state) => { if (state.askToDeleteResources) { return "You selected to delete the SiteWise assets."; } return "The resources will not be deleted. Please delete them manually to avoid charges."; }, ); const sdkDeleteResources = new ScenarioAction( "sdkDeleteResources", async (/** @type {State} */ state) => { await wait(10); // Give the portal status time to catch up. try { await state.iotSiteWiseClient.send( new DeletePortalCommand({ portalId: state.portal.portalId, }), ); console.log( `Portal ${state.portal.portalName} was deleted successfully.`, ); } catch (caught) { if (caught.name === "ResourceNotFoundException") { console.log(`The Portal ${state.portal.portalName} was not found.`); } else { console.log(`When trying to delete the portal: ${caught.message}`); } } try { await state.iotSiteWiseClient.send( new DeleteGatewayCommand({ gatewayId: state.gateway.gatewayId, }), ); console.log( `Gateway ${state.gateway.gatewayName} was deleted successfully.`, ); } catch (caught) { if (caught.name === "ResourceNotFoundException") { console.log(`The Gateway ${state.gateway.gatewayId} was not found.`); } else { console.log(`When trying to delete the gateway: ${caught.message}`); } } try { await state.iotSiteWiseClient.send( new DeleteAssetCommand({ assetId: state.asset.assetId, }), ); await wait(5); // Allow the delete to finish. console.log(`Asset ${state.asset.assetName} was deleted successfully.`); } catch (caught) { if (caught.name === "ResourceNotFoundException") { console.log(`The Asset ${state.asset.assetName} was not found.`); } else { console.log(`When deleting the asset: ${caught.message}`); } } await wait(30); // Allow asset deletion to finish. try { await state.iotSiteWiseClient.send( new DeleteAssetModelCommand({ assetModelId: state.assetModel.assetModelId, }), ); console.log( `Asset Model ${state.assetModel.assetModelName} was deleted successfully.`, ); } catch (caught) { if (caught.name === "ResourceNotFoundException") { console.log( `The Asset Model ${state.assetModel.assetModelName} was not found.`, ); } else { console.log(`When deleting the asset model: ${caught.message}`); } } try { await state.cloudFormationClient.send( new DeleteStackCommand({ StackName: stackName, }), ); await waitUntilStackDeleteComplete( { client: state.cloudFormationClient }, { StackName: stackName }, ); console.log("The stack was deleted successfully."); } catch (caught) { console.log( `${caught.message}. The stack was NOT deleted. Please clean up the resources manually.`, ); } }, { skipWhen: (/** @type {{}} */ state) => !state.askToDeleteResources }, ); const goodbye = new ScenarioOutput( "goodbye", "This concludes the IoT Sitewise Basics scenario for the AWS Javascript SDK v3. Thank you!", ); const myScenario = new Scenario( "IoTSiteWise Basics", [ greet, pressEnter, displayBuildCloudFormationStack, sdkBuildCloudFormationStack, pressEnter, displayCreateAWSSiteWiseAssetModel, sdkCreateAWSSiteWiseAssetModel, displayCreateAWSIoTSiteWiseAssetModel, pressEnter, waitThirtySeconds, sdkCreateAWSIoTSiteWiseAssetModel, pressEnter, displayRetrievePropertyId, sdkRetrievePropertyId, pressEnter, displaySendDataToIoTSiteWiseAsset, sdkSendDataToIoTSiteWiseAsset, pressEnter, displayRetrieveValueOfIoTSiteWiseAsset, sdkRetrieveValueOfIoTSiteWiseAsset, pressEnter, displayCreateIoTSiteWisePortal, sdkCreateIoTSiteWisePortal, pressEnter, displayDescribePortal, sdkDescribePortal, pressEnter, displayCreateIoTSiteWiseGateway, sdkCreateIoTSiteWiseGateway, pressEnter, displayDescribeIoTSiteWiseGateway, sdkDescribeIoTSiteWiseGateway, pressEnter, askToDeleteResources, displayConfirmDeleteResources, sdkDeleteResources, goodbye, ], { iotSiteWiseClient: new IoTSiteWiseClient({}), cloudFormationClient: new CloudFormationClient({}), asset: { assetName: "MyAsset1" }, assetModel: { assetModelName: "MyAssetModel1" }, portal: { portalName: "MyPortal1" }, gateway: { gatewayName: "MyGateway1" }, propertyIds: [], contactEmail: "user@mydomain.com", thing: "MyThing1", sampleData: { temperature: 23.5, humidity: 65.0 }, }, ); /** @type {{ stepHandlerOptions: StepHandlerOptions }} */ export const main = async (stepHandlerOptions) => { await myScenario.run(stepHandlerOptions); }; // Invoke main function if this file was run directly. if (process.argv[1] === fileURLToPath(import.meta.url)) { const { values } = parseArgs({ options: { yes: { type: "boolean", short: "y", }, }, }); main({ confirmAll: values.yes }); }
Python
SDK for Python (Boto3)
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

명령 프롬프트에서 대화형 시나리오를 실행합니다.

class IoTSitewiseGettingStarted: """ A scenario that demonstrates how to use Boto3 to manage IoT physical assets using the AWS IoT SiteWise. """ def __init__( self, iot_sitewise_wrapper: IoTSitewiseWrapper, cloud_formation_resource: ServiceResource, ): self.iot_sitewise_wrapper = iot_sitewise_wrapper self.cloud_formation_resource = cloud_formation_resource self.stack = None self.asset_model_id = None self.asset_id = None self.portal_id = None self.gateway_id = None def run(self) -> None: """ Runs the scenario. """ print( """ 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... """ ) press_enter_to_continue() print_dashes() print(f"") print( f"Use AWS CloudFormation to create an IAM role that is required for this scenario." ) template_file = IoTSitewiseGettingStarted.get_template_as_string() self.stack = self.deploy_cloudformation_stack( "python-iot-sitewise-basics", template_file ) outputs = self.stack.outputs iam_role = None for output in outputs: if output.get("OutputKey") == "SitewiseRoleArn": iam_role = output.get("OutputValue") if iam_role is None: error_string = f"Failed to retrieve iam_role from CloudFormation stack." logger.error(error_string) raise ValueError(error_string) print(f"The ARN of the IAM role is {iam_role}") print_dashes() print_dashes() print(f"1. Create an AWS SiteWise Asset Model") print( """ 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 values of each asset. This scenario creates two asset model values: temperature and humidity. """ ) press_enter_to_continue() asset_model_name = "MyAssetModel1" temperature_property_name = "temperature" humidity_property_name = "humidity" try: properties = [ { "name": temperature_property_name, "dataType": "DOUBLE", "type": { "measurement": {}, }, }, { "name": humidity_property_name, "dataType": "DOUBLE", "type": { "measurement": {}, }, }, ] self.asset_model_id = self.iot_sitewise_wrapper.create_asset_model( asset_model_name, properties ) print( f"Asset Model successfully created. Asset Model ID: {self.asset_model_id}. " ) except ClientError as err: if err.response["Error"]["Code"] == "ResourceAlreadyExistsException": self.asset_model_id = self.get_model_id_for_model_name(asset_model_name) print( f"Asset Model {asset_model_name} already exists. Asset Model ID: {self.asset_model_id}. " ) else: raise press_enter_to_continue() print_dashes() print(f"2. Create an AWS IoT SiteWise Asset") print( """ 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. """ ) press_enter_to_continue() self.asset_id = self.iot_sitewise_wrapper.create_asset( "MyAsset1", self.asset_model_id ) print(f"Asset created with ID: {self.asset_id}") press_enter_to_continue() print_dashes() print_dashes() print(f"3. Retrieve the property ID values") print( """ 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. """ ) press_enter_to_continue() property_ids = self.iot_sitewise_wrapper.list_asset_model_properties( self.asset_model_id ) humidity_property_id = None temperature_property_id = None for property_id in property_ids: if property_id.get("name") == humidity_property_name: humidity_property_id = property_id.get("id") elif property_id.get("name") == temperature_property_name: temperature_property_id = property_id.get("id") if humidity_property_id is None or temperature_property_id is None: error_string = f"Failed to retrieve property IDs from Asset Model." logger.error(error_string) raise ValueError(error_string) print(f"The Humidity property Id is {humidity_property_id}") print(f"The Temperature property Id is {temperature_property_id}") press_enter_to_continue() print_dashes() print_dashes() print(f"4. Send data to an AWS IoT SiteWise Asset") print( """ 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. """ ) press_enter_to_continue() values = [ { "propertyId": humidity_property_id, "valueType": "doubleValue", "value": 65.0, }, { "propertyId": temperature_property_id, "valueType": "doubleValue", "value": 23.5, }, ] self.iot_sitewise_wrapper.batch_put_asset_property_value(self.asset_id, values) print(f"Data sent successfully.") press_enter_to_continue() print_dashes() print_dashes() print(f"5. Retrieve the value of the IoT SiteWise Asset property") print( """ 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. """ ) press_enter_to_continue() property_value = self.iot_sitewise_wrapper.get_asset_property_value( self.asset_id, temperature_property_id ) print(f"The property name is '{temperature_property_name}'.") print( f"The value of this property is: {property_value['value']['doubleValue']}" ) press_enter_to_continue() property_value = self.iot_sitewise_wrapper.get_asset_property_value( self.asset_id, humidity_property_id ) print(f"The property name is '{humidity_property_name}'.") print( f"The value of this property is: {property_value['value']['doubleValue']}" ) press_enter_to_continue() print_dashes() print_dashes() print(f"6. Create an IoT SiteWise Portal") print( """ An IoT SiteWise Portal allows you to aggregate data from multiple industrial sources, such as sensors, equipment, and control systems, into a centralized platform. """ ) press_enter_to_continue() contact_email = q.ask("Enter a contact email for the portal:", q.non_empty) print("Creating the portal. The portal may take a while to become active.") self.portal_id = self.iot_sitewise_wrapper.create_portal( "MyPortal1", iam_role, contact_email ) print(f"Portal created successfully. Portal ID {self.portal_id}") press_enter_to_continue() print_dashes() print_dashes() print(f"7. Describe the Portal") print( """ In this step, we get a description of the portal and display the portal URL. """ ) press_enter_to_continue() portal_description = self.iot_sitewise_wrapper.describe_portal(self.portal_id) print(f"Portal URL: {portal_description['portalStartUrl']}") press_enter_to_continue() print_dashes() print_dashes() print(f"8. Create an IoT SiteWise Gateway") press_enter_to_continue() self.gateway_id = self.iot_sitewise_wrapper.create_gateway( "MyGateway1", "MyThing1" ) print(f"Gateway creation completed successfully. id is {self.gateway_id}") print_dashes() print_dashes() print(f"9. Describe the IoT SiteWise Gateway") press_enter_to_continue() gateway_description = self.iot_sitewise_wrapper.describe_gateway( self.gateway_id ) print(f"Gateway Name: {gateway_description['gatewayName']}") print(f"Gateway ARN: {gateway_description['gatewayArn']}") print(f"Gateway Platform:\n{gateway_description['gatewayPlatform']}") print(f"Gateway Creation Date: {gateway_description['gatewayArn']}") print_dashes() print_dashes() print(f"10. Delete the AWS IoT SiteWise Assets") if q.ask("Would you like to delete the IoT SiteWise Assets? (y/n)", q.is_yesno): self.cleanup() else: print(f"The resources will not be deleted.") print_dashes() print_dashes() print(f"This concludes the AWS IoT SiteWise Scenario") def cleanup(self) -> None: """ Deletes the CloudFormation stack and the resources created for the demo. """ if self.gateway_id is not None: self.iot_sitewise_wrapper.delete_gateway(self.gateway_id) print(f"Deleted gateway with id {self.gateway_id}.") self.gateway_id = None if self.portal_id is not None: self.iot_sitewise_wrapper.delete_portal(self.portal_id) print(f"Deleted portal with id {self.portal_id}.") self.portal_id = None if self.asset_id is not None: self.iot_sitewise_wrapper.delete_asset(self.asset_id) print(f"Deleted asset with id {self.asset_id}.") self.iot_sitewise_wrapper.wait_asset_deleted(self.asset_id) self.asset_id = None if self.asset_model_id is not None: self.iot_sitewise_wrapper.delete_asset_model(self.asset_model_id) print(f"Deleted asset model with id {self.asset_model_id}.") self.asset_model_id = None if self.stack is not None: stack = self.stack self.stack = None self.destroy_cloudformation_stack(stack) def deploy_cloudformation_stack( self, stack_name: str, cfn_template: str ) -> ServiceResource: """ Deploys prerequisite resources used by the scenario. The resources are defined in the associated `SitewiseRoles-template.yaml` AWS CloudFormation script and are deployed as a CloudFormation stack, so they can be easily managed and destroyed. :param stack_name: The name of the CloudFormation stack. :param cfn_template: The CloudFormation template as a string. :return: The CloudFormation stack resource. """ print(f"Deploying CloudFormation stack: {stack_name}.") stack = self.cloud_formation_resource.create_stack( StackName=stack_name, TemplateBody=cfn_template, Capabilities=["CAPABILITY_NAMED_IAM"], ) print(f"CloudFormation stack creation started: {stack_name}") print("Waiting for CloudFormation stack creation to complete...") waiter = self.cloud_formation_resource.meta.client.get_waiter( "stack_create_complete" ) waiter.wait(StackName=stack.name) stack.load() print("CloudFormation stack creation complete.") return stack def destroy_cloudformation_stack(self, stack: ServiceResource) -> None: """ Destroys the resources managed by the CloudFormation stack, and the CloudFormation stack itself. :param stack: The CloudFormation stack that manages the example resources. """ print( f"CloudFormation stack '{stack.name}' is being deleted. This may take a few minutes." ) stack.delete() waiter = self.cloud_formation_resource.meta.client.get_waiter( "stack_delete_complete" ) waiter.wait(StackName=stack.name) print(f"CloudFormation stack '{stack.name}' has been deleted.") @staticmethod def get_template_as_string() -> str: """ Returns a string containing this scenario's CloudFormation template. """ template_file_path = os.path.join(script_dir, "SitewiseRoles-template.yaml") file = open(template_file_path, "r") return file.read() def get_model_id_for_model_name(self, model_name: str) -> str: """ Returns the model ID for the given model name. :param model_name: The name of the model. :return: The model ID. """ model_id = None asset_models = self.iot_sitewise_wrapper.list_asset_models() for asset_model in asset_models: if asset_model["name"] == model_name: model_id = asset_model["id"] break return model_id

AWS IoT SiteWise 작업을 래핑하는 IoTSitewiseWrapper 클래스입니다.

class IoTSitewiseWrapper: """Encapsulates AWS IoT SiteWise actions using the client interface.""" def __init__(self, iotsitewise_client: client) -> None: """ Initializes the IoTSitewiseWrapper with an AWS IoT SiteWise client. :param iotsitewise_client: A Boto3 AWS IoT SiteWise client. This client provides low-level access to AWS IoT SiteWise services. """ self.iotsitewise_client = iotsitewise_client self.entry_id = 0 # Incremented to generate unique entry IDs for batch_put_asset_property_value. @classmethod def from_client(cls) -> "IoTSitewiseWrapper": """ Creates an IoTSitewiseWrapper instance with a default AWS IoT SiteWise client. :return: An instance of IoTSitewiseWrapper initialized with the default AWS IoT SiteWise client. """ iotsitewise_client = boto3.client("iotsitewise") return cls(iotsitewise_client) def create_asset_model( self, asset_model_name: str, properties: List[Dict[str, Any]] ) -> str: """ Creates an AWS IoT SiteWise Asset Model. :param asset_model_name: The name of the asset model to create. :param properties: The property definitions of the asset model. :return: The ID of the created asset model. """ try: response = self.iotsitewise_client.create_asset_model( assetModelName=asset_model_name, assetModelDescription="This is a sample asset model description.", assetModelProperties=properties, ) asset_model_id = response["assetModelId"] waiter = self.iotsitewise_client.get_waiter("asset_model_active") waiter.wait(assetModelId=asset_model_id) return asset_model_id except ClientError as err: if err.response["Error"]["Code"] == "ResourceAlreadyExistsException": logger.error("Asset model %s already exists.", asset_model_name) else: logger.error( "Error creating asset model %s. Here's why %s", asset_model_name, err.response["Error"]["Message"], ) raise def create_asset(self, asset_name: str, asset_model_id: str) -> str: """ Creates an AWS IoT SiteWise Asset. :param asset_name: The name of the asset to create. :param asset_model_id: The ID of the asset model to associate with the asset. :return: The ID of the created asset. """ try: response = self.iotsitewise_client.create_asset( assetName=asset_name, assetModelId=asset_model_id ) asset_id = response["assetId"] waiter = self.iotsitewise_client.get_waiter("asset_active") waiter.wait(assetId=asset_id) return asset_id except ClientError as err: if err.response["Error"] == "ResourceNotFoundException": logger.error("Asset model %s does not exist.", asset_model_id) else: logger.error( "Error creating asset %s. Here's why %s", asset_name, err.response["Error"]["Message"], ) raise def list_asset_models(self) -> List[Dict[str, Any]]: """ Lists all AWS IoT SiteWise Asset Models. :return: A list of dictionaries containing information about each asset model. """ try: asset_models = [] paginator = self.iotsitewise_client.get_paginator("list_asset_models") pages = paginator.paginate() for page in pages: asset_models.extend(page["assetModelSummaries"]) return asset_models except ClientError as err: logger.error( "Error listing asset models. Here's why %s", err.response["Error"]["Message"], ) raise def list_asset_model_properties(self, asset_model_id: str) -> List[Dict[str, Any]]: """ Lists all AWS IoT SiteWise Asset Model Properties. :param asset_model_id: The ID of the asset model to list values for. :return: A list of dictionaries containing information about each asset model property. """ try: asset_model_properties = [] paginator = self.iotsitewise_client.get_paginator( "list_asset_model_properties" ) pages = paginator.paginate(assetModelId=asset_model_id) for page in pages: asset_model_properties.extend(page["assetModelPropertySummaries"]) return asset_model_properties except ClientError as err: logger.error( "Error listing asset model values. Here's why %s", err.response["Error"]["Message"], ) raise def batch_put_asset_property_value( self, asset_id: str, values: List[Dict[str, str]] ) -> None: """ Sends data to an AWS IoT SiteWise Asset. :param asset_id: The asset ID. :param values: A list of dictionaries containing the values in the form {propertyId : property_id, valueType : [stringValue|integerValue|doubleValue|booleanValue], value : the_value}. """ try: entries = self.properties_to_values(asset_id, values) self.iotsitewise_client.batch_put_asset_property_value(entries=entries) except ClientError as err: if err.response["Error"]["Code"] == "ResourceNotFoundException": logger.error("Asset %s does not exist.", asset_id) else: logger.error( "Error sending data to asset. Here's why %s", err.response["Error"]["Message"], ) raise def properties_to_values( self, asset_id: str, values: list[dict[str, Any]] ) -> list[dict[str, Any]]: """ Utility function to convert a values list to the entries parameter for batch_put_asset_property_value. :param asset_id : The asset ID. :param values : A list of dictionaries containing the values in the form {propertyId : property_id, valueType : [stringValue|integerValue|doubleValue|booleanValue], value : the_value}. :return: An entries list to pass as the 'entries' parameter to batch_put_asset_property_value. """ entries = [] for value in values: epoch_ns = time.time_ns() self.entry_id += 1 if value["valueType"] == "stringValue": property_value = {"stringValue": value["value"]} elif value["valueType"] == "integerValue": property_value = {"integerValue": value["value"]} elif value["valueType"] == "booleanValue": property_value = {"booleanValue": value["value"]} elif value["valueType"] == "doubleValue": property_value = {"doubleValue": value["value"]} else: raise ValueError("Invalid valueType: %s", value["valueType"]) entry = { "entryId": f"{self.entry_id}", "assetId": asset_id, "propertyId": value["propertyId"], "propertyValues": [ { "value": property_value, "timestamp": { "timeInSeconds": int(epoch_ns / 1000000000), "offsetInNanos": epoch_ns % 1000000000, }, } ], } entries.append(entry) return entries def get_asset_property_value( self, asset_id: str, property_id: str ) -> Dict[str, Any]: """ Gets the value of an AWS IoT SiteWise Asset Property. :param asset_id: The ID of the asset. :param property_id: The ID of the property. :return: A dictionary containing the value of the property. """ try: response = self.iotsitewise_client.get_asset_property_value( assetId=asset_id, propertyId=property_id ) return response["propertyValue"] except ClientError as err: if err.response["Error"]["Code"] == "ResourceNotFoundException": logger.error( "Asset %s or property %s does not exist.", asset_id, property_id ) else: logger.error( "Error getting asset property value. Here's why %s", err.response["Error"]["Message"], ) raise def create_portal( self, portal_name: str, iam_role_arn: str, portal_contact_email: str ) -> str: """ Creates an AWS IoT SiteWise Portal. :param portal_name: The name of the portal to create. :param iam_role_arn: The ARN of an IAM role. :param portal_contact_email: The contact email of the portal. :return: The ID of the created portal. """ try: response = self.iotsitewise_client.create_portal( portalName=portal_name, roleArn=iam_role_arn, portalContactEmail=portal_contact_email, ) portal_id = response["portalId"] waiter = self.iotsitewise_client.get_waiter("portal_active") waiter.wait(portalId=portal_id, WaiterConfig={"MaxAttempts": 40}) return portal_id except ClientError as err: if err.response["Error"]["Code"] == "ResourceAlreadyExistsException": logger.error("Portal %s already exists.", portal_name) else: logger.error( "Error creating portal %s. Here's why %s", portal_name, err.response["Error"]["Message"], ) raise def describe_portal(self, portal_id: str) -> Dict[str, Any]: """ Describes an AWS IoT SiteWise Portal. :param portal_id: The ID of the portal to describe. :return: A dictionary containing information about the portal. """ try: response = self.iotsitewise_client.describe_portal(portalId=portal_id) return response except ClientError as err: logger.error( "Error describing portal %s. Here's why %s", portal_id, err.response["Error"]["Message"], ) raise def create_gateway(self, gateway_name: str, my_thing: str) -> str: """ Creates an AWS IoT SiteWise Gateway. :param gateway_name: The name of the gateway to create. :param my_thing: The core device thing name. :return: The ID of the created gateway. """ try: response = self.iotsitewise_client.create_gateway( gatewayName=gateway_name, gatewayPlatform={ "greengrassV2": {"coreDeviceThingName": my_thing}, }, tags={"Environment": "Production"}, ) gateway_id = response["gatewayId"] return gateway_id except ClientError as err: if err.response["Error"]["Code"] == "ResourceAlreadyExistsException": logger.error("Gateway %s already exists.", gateway_name) else: logger.error( "Error creating gateway %s. Here's why %s", gateway_name, err.response["Error"]["Message"], ) raise def describe_gateway(self, gateway_id: str) -> Dict[str, Any]: """ Describes an AWS IoT SiteWise Gateway. :param gateway_id: The ID of the gateway to describe. :return: A dictionary containing information about the gateway. """ try: response = self.iotsitewise_client.describe_gateway(gatewayId=gateway_id) return response except ClientError as err: if err.response["Error"]["Code"] == "ResourceNotFoundException": logger.error("Gateway %s does not exist.", gateway_id) else: logger.error( "Error describing gateway %s. Here's why %s", gateway_id, err.response["Error"]["Message"], ) raise def delete_gateway(self, gateway_id: str) -> None: """ Deletes an AWS IoT SiteWise Gateway. :param gateway_id: The ID of the gateway to delete. """ try: self.iotsitewise_client.delete_gateway(gatewayId=gateway_id) except ClientError as err: if err.response["Error"]["Code"] == "ResourceNotFoundException": logger.error("Gateway %s does not exist.", gateway_id) else: logger.error( "Error deleting gateway %s. Here's why %s", gateway_id, err.response["Error"]["Message"], ) raise def delete_portal(self, portal_id: str) -> None: """ Deletes an AWS IoT SiteWise Portal. :param portal_id: The ID of the portal to delete. """ try: self.iotsitewise_client.delete_portal(portalId=portal_id) except ClientError as err: if err.response["Error"]["Code"] == "ResourceNotFoundException": logger.error("Portal %s does not exist.", portal_id) else: logger.error( "Error deleting portal %s. Here's why %s", portal_id, err.response["Error"]["Message"], ) raise def delete_asset(self, asset_id: str) -> None: """ Deletes an AWS IoT SiteWise Asset. :param asset_id: The ID of the asset to delete. """ try: self.iotsitewise_client.delete_asset(assetId=asset_id) except ClientError as err: logger.error( "Error deleting asset %s. Here's why %s", asset_id, err.response["Error"]["Message"], ) raise def delete_asset_model(self, asset_model_id: str) -> None: """ Deletes an AWS IoT SiteWise Asset Model. :param asset_model_id: The ID of the asset model to delete. """ try: self.iotsitewise_client.delete_asset_model(assetModelId=asset_model_id) except ClientError as err: logger.error( "Error deleting asset model %s. Here's why %s", asset_model_id, err.response["Error"]["Message"], ) raise def wait_asset_deleted(self, asset_id: str) -> None: """ Waits for an AWS IoT SiteWise Asset to be deleted. :param asset_id: The ID of the asset to wait for. """ try: waiter = self.iotsitewise_client.get_waiter("asset_not_exists") waiter.wait(assetId=asset_id) except ClientError as err: logger.error( "Error waiting for asset %s to be deleted. Here's why %s", asset_id, err.response["Error"]["Message"], ) raise
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!"); } }

AWS IoT SiteWise SDK 메서드의 래퍼 클래스입니다.

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; } }
프라이버시사이트 이용 약관쿠키 기본 설정
© 2025, Amazon Web Services, Inc. 또는 계열사. All rights reserved.