文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
CreateAssetModel
搭配 a AWS SDK 或 CLI 使用
下列程式碼範例示範如何使用 CreateAssetModel
。
- CLI
-
- AWS CLI
-
建立資產模型
下列
create-asset-model
範例會建立資產模型,以定義具有下列屬性的風力發電機:序號 - wind turbineGenerated 電源的序號 - 從 wind turbineTemperature C 產生的電源資料串流 - 風力發電機 in CelsiusTemperature F 的溫度資料串流 - 從攝氏到華氏的映射溫度資料點
aws iotsitewise create-asset-model \ --cli-input-json
file://create-wind-turbine-model.json
create-wind-turbine-model.json
的內容:{ "assetModelName": "Wind Turbine Model", "assetModelDescription": "Represents a wind turbine", "assetModelProperties": [ { "name": "Serial Number", "dataType": "STRING", "type": { "attribute": {} } }, { "name": "Generated Power", "dataType": "DOUBLE", "unit": "kW", "type": { "measurement": {} } }, { "name": "Temperature C", "dataType": "DOUBLE", "unit": "Celsius", "type": { "measurement": {} } }, { "name": "Temperature F", "dataType": "DOUBLE", "unit": "Fahrenheit", "type": { "transform": { "expression": "temp_c * 9 / 5 + 32", "variables": [ { "name": "temp_c", "value": { "propertyId": "Temperature C" } } ] } } }, { "name": "Total Generated Power", "dataType": "DOUBLE", "unit": "kW", "type": { "metric": { "expression": "sum(power)", "variables": [ { "name": "power", "value": { "propertyId": "Generated Power" } } ], "window": { "tumbling": { "interval": "1h" } } } } } ] }
輸出:
{ "assetModelId": "a1b2c3d4-5678-90ab-cdef-11111EXAMPLE", "assetModelArn": "arn:aws:iotsitewise:us-west-2:123456789012:asset-model/a1b2c3d4-5678-90ab-cdef-11111EXAMPLE", "assetModelStatus": { "state": "CREATING" } }
如需詳細資訊,請參閱 AWS IoT SiteWise Word 使用者指南中的定義資產模型。
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateAssetModel
。
-
- Java
-
- Java 2.x 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 /** * Creates an asset model. * * @param name the name of the asset model to create. * @return a {@link CompletableFuture} that represents a {@link CreateAssetModelResponse} result. The calling code * can attach callbacks, then handle the result or exception by calling {@link CompletableFuture#join()} or * {@link CompletableFuture#get()}. * <p> * If any completion stage in this method throws an exception, the method logs the exception cause and keeps it * available to the calling code as a {@link CompletionException}. By calling * {@link CompletionException#getCause()}, the calling code can access the original exception. */ public CompletableFuture<CreateAssetModelResponse> createAssetModelAsync(String name) { PropertyType humidity = PropertyType.builder() .measurement(Measurement.builder().build()) .build(); PropertyType temperaturePropertyType = PropertyType.builder() .measurement(Measurement.builder().build()) .build(); AssetModelPropertyDefinition temperatureProperty = AssetModelPropertyDefinition.builder() .name("Temperature") .dataType(PropertyDataType.DOUBLE) .type(temperaturePropertyType) .build(); AssetModelPropertyDefinition humidityProperty = AssetModelPropertyDefinition.builder() .name("Humidity") .dataType(PropertyDataType.DOUBLE) .type(humidity) .build(); CreateAssetModelRequest createAssetModelRequest = CreateAssetModelRequest.builder() .assetModelName(name) .assetModelDescription("This is my asset model") .assetModelProperties(temperatureProperty, humidityProperty) .build(); return getAsyncClient().createAssetModel(createAssetModelRequest) .whenComplete((response, exception) -> { if (exception != null) { logger.error("Failed to create asset model: {} ", exception.getCause().getMessage()); } }); }
-
如需 API 詳細資訊,請參閱 CreateAssetModel AWS SDK for Java 2.x 參考中的 API。
-
CreateAsset
CreateGateway