Upload Attributes


上传设备或逻辑资产属性数据。属性数据可以包含文件类型。

操作权限

需授权的资源 所需操作权限
资产 Write

请求格式

POST https://{integration-address}/connect-service/v2.1/integration?action=postAttribute

注解

{integration-address}:消息集成服务网关地址。该信息可登陆 EnOS 管理控制台 ,在 帮助 > 环境信息 > HTTP消息集成通道 中获取。

请求参数(URI)

名称 位置(Path/Query) 必需/可选 数据类型 描述
orgId Query 必需 String 资产所属的组织 ID。如何获取 orgId 信息>>

请求参数 (Body)

Body 格式为 multipart/form-data。其中包含了多个 form-data,其中:

  • 必须包含且仅包含一个请求报文。请求报文占用一个 form-data,其格式为 “请求报文 form-data”。请求报文 form-data 的 header 和 body 格式参见下文表格。
  • 可以包含一个或多个文件 form-data 。如果 body 中包含了文件类型数据,每个文件占用一个 form-data,文件数据格式为 “文件 form-data”。“文件 form-data” 类型的数据 header 格式见下文表格。如果 body 不包含文件类型数据,则不需要提供文件 form-data。
请求报文 form-data 的 header
名称 必需/可选 数据格式 描述
Content-Disposition 必需 Content-Disposition: form-data; name=”enos-message” name 为常量,表示请求消息体名称。


请求报文 form-data 的 body
名称 必需/可选 数据格式 描述
method 必需 String 请求方法。
id 必需 Integer 请求消息 ID。
version 必需 String 版本号。
params 必需 属性数据结构体数组 资产属性数据的列表,其格式见属性数据结构体表。
files body包含文件时必需,不包含文件时可选 文件映射 Map 文件与资产属性映射关系,内容为“文件名:文件映射数据结构体”格式的键值对,其格式见文件映射数据结构体表。


注解

以下非必选字段中,使用以下任意一种方法:

  • 在请求中单独包含 assetId 以指定一个设备或一个逻辑资产。
  • 在请求中单独包含 productKeydeviceKey 以指定一个设备。
属性数据结构体
名称 必需/可选 数据格式 描述
assetId 可选 String 资产的 ID。单独使用以指定一个设备或一个逻辑资产。 如何获取 Asset ID>>
productKey 可选 String 设备的 product key。与 deviceKey 一起使用,用于指定一个设备。
deviceKey 可选 String 设备的 device key。与 productKey 一起使用,用于指定一个设备。
attributes 必需 Map 内容为 “属性 ID:属性值” 格式的键值对。如果属性 ID 是文件类型,其值为 local://filename local:// 为固定格式开头。 filename 为文件名称。


文件映射数据结构体
名称 必需/可选 数据格式 描述
featureId 必需 String 测点 ID。
assetId 可选 String 资产的 ID。
productKey 可选 String 设备的 product key。
deviceKey 可选 String 设备的 device key。
md5 可选 String 文件 MD5。
originalFilename 可选 String 原始文件的名称,将在下载时返回。如果未指定,文件名为随机文件标识符。
fileExt 可选 String 文件后缀,如 .zip 和 .gpg。
fileLength 可选 Long 文件大小,单位字节。文件大小的值将以文件 form-data 的 header 里的 Content-Length 为准,只有在获取不到其值时才会获取 fileLength 的值。
文件 form-data 的 header
名称 必需/可选 数据格式 描述
Content-Disposition 必需 Content-Disposition: form-data; name=”enos-file”; filename=”yourFileName” name 为固定值,filename 为对应文件名。
Content-Length 必需 Long 文件大小,单位字节。

响应参数

名称 数据类型 描述
code Integer 请求返回状态值。0 表示请求成功,非 0 则表示请求失败。
msg String 对状态码的解释和说明。请求成功为 “OK”。
requestId String 每次请求获取的 ID,用于唯一标识一次 API 请求。

示例

请求示例

url: https://{integration-address}/connect-service/v2.1/integration?action=postAttribute&orgId=yourOrgId
method: POST
requestHeader:
{
  "apim-accesstoken":"yourAccessToken"
}
requestBody:

请求报文form-data
Content-Disposition: form-data; name="enos-message"
{
  "method": "integration.attribute.post",
  "id": "123",
  "version": "1.1",
  "params":[
    {
      "productKey": "productKey1",
      "deviceKey": "deviceKey1",
      "attributes": {
        "intAttibuteId1": 123,
        "fileAttibuteId1":"local://filename1"
      }
    },
    {
      "assetId": "assetId2",
      "attributes": {
        "intAttibuteId2": 123,
        "fileAttibuteId2": "local://filename2"
      }
    }
  ],
  "files": {
    "filename1": {
      "featureId": "fileAttibuteId1",
      "productKey": "productKey1",
      "deviceKey": "deviceKey1",
      "originalFilename": "test.txt",
      "fileExt": ".txt",
      "md5": "0e202f9b67323d11df8a79b319a3d4f6"
    },
    "filename2": {
      "featureId": "fileAttibuteId2",
      "assetId": "assetId2",
      "md5": "aa8ea05bdbcbadfcda7300c65c40859f",
      "fileLength": 1024000
    }
  }
}

文件form-data
Content-Disposition: form-data; name="enos-file"; filename="filename1"
Content-Length: 1024000
<文件filename1的内容>

文件form-data
Content-Disposition: form-data; name="enos-file"; filename="filename2"
Content-Length: 1024000
<文件filename2的内容>

返回示例

{
    "code":0,
    "msg":"OK",
    "requestId":"861bb9f0-f9db-4adf-bf5e-976e6943338e",
    "data":{}
}

Java SDK 调用示例

import com.envisioniot.enos.iot_http_integration.HttpConnection;
import com.envisioniot.enos.iot_http_integration.message.IIntegrationCallback;
import com.envisioniot.enos.iot_http_integration.message.IntegrationAttributePostRequest;
import com.envisioniot.enos.iot_http_integration.message.IntegrationResponse;
import com.envisioniot.enos.iot_mqtt_sdk.core.exception.EnvisionException;
import com.envisioniot.enos.sdk.data.DeviceInfo;
import com.google.common.collect.Maps;
import com.google.gson.GsonBuilder;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

public class AttributeIntegrationSample {
    // EnOS Token Server URL (which is the same as the API gateway address) and HTTP Broker URL, which can be obtained from Environment Information page in EnOS Management Console
    static final String TOKEN_SERVER_URL = "https://token_server_url";
    static final String BROKER_URL = "http://broker_url";

    // EnOS Application AccessKey and SecretKey, which can be obtain in Application Registration page in EnOS Console
    static final String APP_KEY = "appKey";
    static final String APP_SECRET = "appSecret";

    // Device credentials, which can be obtained from Device Details page in EnOS Console
    static final String ORG_ID = "orgId";
    static final String ASSET_ID = "assetId";
    static final String PRODUCT_KEY = "productKey";
    static final String DEVICE_KEY = "deviceKey";

    private static IntegrationAttributePostRequest buildAttributePostRequest() {
        DeviceInfo deviceInfo1 = new DeviceInfo().setAssetId(ASSET_ID);
        DeviceInfo deviceInfo2 = new DeviceInfo().setKey(PRODUCT_KEY, DEVICE_KEY);

        // Attributes are defined in ThingModel
        // FileAttribute1 is a file-type attribute
        HashMap < String, Object > hashMap = Maps.newHashMap();
        hashMap.put("IntAttribute1", 123);
        hashMap.put("FileAttribute1", new File("sample1.txt"));
        return IntegrationAttributePostRequest.builder()
            .addAttribute(deviceInfo1, hashMap)
            .addAttribute(deviceInfo2, hashMap)
            .build();
    }

    public static void main(String[] args) {
        // Construct a http connection
        HttpConnection connection = new HttpConnection.Builder(
                BROKER_URL, TOKEN_SERVER_URL, APP_KEY, APP_SECRET, ORG_ID)
            .build();

        IntegrationAttributePostRequest request = buildAttributePostRequest();

        try {
            IntegrationResponse response = connection.publish(request, (bytes, length) ->
                System.out.println(String.format("Progress: %.2f %%", (float) bytes / length * 100.0)));
            System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(response));
        } catch (EnvisionException | IOException e) {
            e.printStackTrace();
        }

        // Asynchronously call the attribute post with file
        request = buildAttributePostRequest();

        try {
            connection.publish(request, new IIntegrationCallback() {
                    @Override
                    public void onResponse(IntegrationResponse response) {
                        System.out.println("receive response asynchronously");
                        System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(response));
                    }

                    @Override
                    public void onFailure(Exception failure) {
                        failure.printStackTrace();
                    }
                }, (bytes, length) ->
                System.out.println(String.format("Progress: %.2f %%", (float) bytes / length * 100.0))
            );
        } catch (IOException | EnvisionException e) {
            e.printStackTrace();
        }
    }
}