Skip to content

Asset Management Service – 使用 Asset Types 中的默认值

以下示例说明如何使用 Asset Management Service 创建 asset type 并为它的其中一个属性定义默认值。本示例中创建了一个 asset type Valve,它具有一个名为 color 的属性。默认情况下,新阀应为灰色,但如本示例所示,可在必要情下对默认值进行覆盖。

定义 Asset Type 的默认值

使用以下请求创建 asset type - 用您的租户 ID 替换 {tenantId}

PUT /assettypes/{tenantId}.Valve

将以下 JSON 结构加入有效载荷,以定义该 asset type 及其 color 属性的默认值:

{
 "name":"Valve",
 "description":"General purpose valve",
 "parentTypeId": "core.basicdevice",
 "instantiable":true,
 "scope": "private",
 "variables": [
 {
 "name": "color",
 "dataType":"STRING",
 "searchable":true,
 "length":10,
 "defaultValue": "gray"
 }
 ]
}

使用以下请求创建新建 asset type 的 asset 实例:

POST /assets/

将以下 JSON 结构加入有效载荷,以定义 asset - 用您的租户 ID 替换 {tenantID},用找出 Asset 的 parentID 替换 {parentAssetId}

{
 "name":"Valve-001",
 "externalId":"SN 123456-123-123456",
 "description":"Valve 001 installed somewhere",
 "typeId": "{tenantId}.Valve",
 "parentId": "{parentAssetId}",
 "timezone":"Europe/Berlin"
}

调用的响应结果包含新创建的 asset 实例,因此您可以验证创建是否正确:

{
 "assetId": "{assetId}",
 "tenantId": "{tenantId}",
 "name":"Valve-001",
 "etag": "{etagValue}",
 "externalId":"SN 123456-123-123456",
 "t2Tenant":null,
 "subTenant":null,
 "description":"Valve 001 installed somewhere",
 "timezone":"Europe/Berlin",
 "parentId": "{parentAssetId}",
 "typeId": "{tenantId}.Valve",
 "location":null,
 "fileAssignments": [],
 "variables": [
 {
 "name": "color",
 "value": "gray"
 }
 ],
 "aspects": [],
 "locks": [],
 "hierarchyPath": [
 {
 "assetId": "{assetId}",
 "name": "{tenantId}"
 }
 ],
 "deleted":null,
 "_links": {
 "self": {
 "href": "{link}"
 },
 "aspects": {
 "href": "{link}"
 },
 "variables": {
 "href": "{link}"
 },
 "location": {
 "href": "{link}"
 },
 "parent": {
 "href": "{link}"
 }
 }
}

响应结果显示 Valve-001color 属性设置为 gray

覆盖 Asset 的默认值

使用以下请求将 color 属性从 gray 更改为 white - 用 asset ID 替换 {id}

PATCH /assets/{id}

If-Match 参数提供 {CurrentEtagValue} 并提供以下 JSON 结构来定义 asset:

{
 "variables": [
 {
 "name": "color",
 "value": "white"
 }
 ]
}

验证响应文本中的结果:

{
 "assetId": "{assetId}",
 "tenantId": "{tenantId}",
 "name":"Valve-001",
 "etag": "{etagValue}",
 "externalId":"SN 123456-123-123456",
 "t2Tenant":null,
 "subTenant":null,
 "description":"Valve 001 installed somewhere",
 "timezone":"Europe/Berlin",
 "parentId": "{parentAssetId}",
 "typeId": "{tenantId}.Valve",
 "location":null,
 "fileAssignments": [],
 "variables": [
 {
 "name": "color",
 "value": "white"
 }
 ],
 "aspects": [],
 "locks": [],
 "hierarchyPath": [
 {
 "assetId": "{assetId}",
 "name": "{tenantId}"
 }
 ],
 "deleted":null,
 "_links": {
 "self": {
 "href": "{link}"
 },
 "aspects": {
 "href": "{link}"
 },
 "variables": {
 "href": "{link}"
 },
 "location": {
 "href": "{link}"
 },
 "parent": {
 "href": "{link}"
 }
 }
}

重置 Asset 的默认值

从实例中删除默认属性时,会自动将其重置为默认值。

使用以下请求删除 asset 的属性 - 用 asset ID 替换 {id}

PATCH /asset/{id}

If-Match 参数提供 {CurrentEtagValue} 并提供以下 JSON 结构来定义 asset:

{
 "variables": [
 ]
}

返回结果可验证默认值已在 asset 实例中恢复:

{
 "assetId": "{assetId}",
 "tenantId": "{tenantId}",
 "name":"Valve-001",
 "etag": "{etagValue}",
 "externalId":"SN 123456-123-123456",
 "t2Tenant":null,
 "subTenant":null,
 "description":"Valve 001 installed somewhere",
 "timezone":"Europe/Berlin",
 "parentId": "{parentAssetId}",
 "typeId": "{tenantId}.Valve",
 "location":null,
 "fileAssignments": [],
 "variables": [
 {
 "name": "color",
 "value": "gray"
 }
 ],
 "aspects": [],
 "locks": [],
 "hierarchyPath": [
 {
 "assetId": "{assetId}",
 "name": "{tenantId}"
 }
 ],
 "deleted":null,
 "_links": {
 "self": {
 "href": "{link}"
 },
 "aspects": {
 "href": "{link}"
 },
 "variables": {
 "href": "{link}"
 },
 "location": {
 "href": "{link}"
 },
 "parent": {
 "href": "{link}"
 }
 }
}

更改多个 Assets 的默认值

可通过更新 asset type 为同一 asset type 的现有 assets 更改默认值。这将自动覆盖派生 assets 的默认值。

使用以下请求 - 用 {tenantId}.Valve 替换 {id},其中 {tenantID} 是您的租户 ID:

PATCH /assettypes/{id}

If-Match 参数提供 {currentEtagValue} 并提供以下 JSON 结构来定义 asset type:

{
 "variables": [
 {
 "name": "color",
 "dataType":"STRING",
 "searchable":true,
 "length":10,
 "defaultValue": "black"
 }
 ]
}

说明

需要为 PATCH 添加完整的 "variables" 实例。

使用以下请求 - 用 asset ID 替换 {assetID}

GET /assets/{assetID}

在响应结果中,颜色更改为黑色:

{
 "assetId": "{assetId}",
 "tenantId": "{tenantId}",
 "name":"Valve-001",
 "etag": "{etagValue}",
 "externalId":"SN 123456-123-123456",
 "t2Tenant":null,
 "subTenant":null,
 "description":"Valve 001 installed somewhere",
 "timezone":"Europe/Berlin",
 "parentId": "{parentAssetId}",
 "typeId": "{tenantId}.Valve",
 "location":null,
 "fileAssignments": [],
 "variables": [
 {
 "name": "color",
 "value": "black"
 }
 ],
 "aspects": [],
 "locks": [],
 "hierarchyPath": [
 {
 "assetId": "{assetId}",
 "name": "{tenantId}"
 }
 ],
 "deleted":null,
 "_links": {
 "self": {
 "href": "{link}"
 },
 "aspects": {
 "href": "{link}"
 },
 "variables": {
 "href": "{link}"
 },
 "location": {
 "href": "{link}"
 },
 "parent": {
 "href": "{link}"
 }
 }
}

找出 Asset 的 parentID

parentId 是高于实际 asset 一个层级的 asset 的 ID。在上述示例中,根 asset 可用作父 asset。使用以下请求查询根 asset 的 ID:

GET /assets/root

如果父项不是根 asset,那么您可以在 `"hierarchyPath" 列表中查看 asset 的直接父项。父项始终是列表中的最后一项。这也被称为“面包屑路径”。

创建新 asset 时,需要在有效载荷中添加父 asset 的 ID。如果您不记得该 ID,您可以使用如下所示的端点进行查询,并对您所知的任何属性进行过滤。

GET /assets

还有问题?

向社区提问


除非另行声明,该网站内容遵循MindSphere开发许可协议.


Last update: July 11, 2019