ListAssetModels与 Amazon SDK 或 CLI 配合使用 - Amazon IoT SiteWise
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

ListAssetModels与 Amazon SDK 或 CLI 配合使用

以下代码示例演示如何使用 ListAssetModels

CLI
Amazon CLI

列出所有资产模型

以下list-asset-models示例列出了您在当前地区的 Amazon 账户中定义的所有资产模型。

aws iotsitewise list-asset-models

输出:

{ "assetModelSummaries": [ { "id": "a1b2c3d4-5678-90ab-cdef-22222EXAMPLE", "arn": "arn:aws:iotsitewise:us-west-2:123456789012:asset-model/a1b2c3d4-5678-90ab-cdef-22222EXAMPLE", "name": "Wind Farm Model", "description": "Represents a wind farm that comprises many wind turbines", "creationDate": 1575671284.0, "lastUpdateDate": 1575671988.0, "status": { "state": "ACTIVE" } }, { "id": "a1b2c3d4-5678-90ab-cdef-11111EXAMPLE", "arn": "arn:aws:iotsitewise:us-west-2:123456789012:asset-model/a1b2c3d4-5678-90ab-cdef-11111EXAMPLE", "name": "Wind Turbine Model", "description": "Represents a wind turbine manufactured by Example Corp", "creationDate": 1575671207.0, "lastUpdateDate": 1575686273.0, "status": { "state": "ACTIVE" } } ] }

有关更多信息,请参阅《Amazon 物联网 SiteWise 用户指南》中的列出所有资产模型

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考ListAssetModels中的。

Java
适用于 Java 的 SDK 2.x
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/** * Retrieves the asset model ID for the given asset model name. * * @param assetModelName the name of the asset model for the ID. * @return a {@link CompletableFuture} that represents a {@link String} result of the asset model ID or null if the * asset model cannot be found. The calling code can attach callbacks, then handle the result or exception * by calling {@link CompletableFuture#join()} or {@link CompletableFuture#get()}. * <p> * If any completion stage in this method throws an exception, the method logs the exception cause and keeps * it available to the calling code as a {@link CompletionException}. By calling * {@link CompletionException#getCause()}, the calling code can access the original exception. */ public CompletableFuture<String> getAssetModelIdAsync(String assetModelName) { ListAssetModelsRequest listAssetModelsRequest = ListAssetModelsRequest.builder().build(); return getAsyncClient().listAssetModels(listAssetModelsRequest) .handle((listAssetModelsResponse, exception) -> { if (exception != null) { logger.error("Failed to retrieve Asset Model ID: {}", exception.getCause().getMessage()); throw (CompletionException) exception; } for (AssetModelSummary assetModelSummary : listAssetModelsResponse.assetModelSummaries()) { if (assetModelSummary.name().equals(assetModelName)) { return assetModelSummary.id(); } } return null; }); }
  • 有关 API 的详细信息,请参阅 Amazon SDK for Java 2.x API 参考ListAssetModels中的。

JavaScript
适用于 JavaScript (v3) 的软件开发工具包
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

import { ListAssetModelsCommand, IoTSiteWiseClient, } from "@aws-sdk/client-iotsitewise"; import { parseArgs } from "node:util"; /** * List asset models. * @param {{ assetModelTypes : array }} */ export const main = async ({ assetModelTypes = [] }) => { const client = new IoTSiteWiseClient({}); try { const result = await client.send( new ListAssetModelsCommand({ assetModelTypes: assetModelTypes, // The model types to list }), ); console.log("Asset model types retrieved successfully."); return result; } catch (caught) { if (caught instanceof Error && caught.name === "IoTSiteWiseError") { console.warn( `${caught.message}. There was a problem listing the asset model types.`, ); } else { throw caught; } } };
  • 有关 API 的详细信息,请参阅 适用于 JavaScript 的 Amazon SDK API 参考ListAssetModels中的。

Python
适用于 Python 的 SDK(Boto3)
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

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 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
  • 有关 API 的详细信息,请参阅适用ListAssetModelsPython 的Amazon SDK (Boto3) API 参考

有关 S Amazon DK 开发者指南和代码示例的完整列表,请参阅将此服务与 Amazon SDK 配合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。