Upload Measurement Points


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

操作权限

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

请求格式

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

注解

{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格式见下文表格。如果测点数据不包含文件类型数据,则不需要提供文件 form-data。
请求报文 form-data 的 header
名称 必需/可选 数据格式 描述
Content-Disposition 必需 Content-Disposition: form-data; name=”enos-message” name 为常量,表示请求消息体名称。


请求报文 form-data 的 body
名称 必需/可选 数据格式 描述
method 必需 String 请求方法。
id 可选 Int 请求消息 ID。
version 必需 String 版本号。
params 必需 测点数据结构体 Array 资产测点数据的列表,其格式见测点数据结构体表。
files Body 包含文件时必需,不包含文件时可选 文件映射 Map 文件与资产测点映射关系,内容为 “文件名:文件映射数据结构体” 格式的键值对,其格式见文件映射数据结构体表。
isRealtimeIntegration 可选 Boolean
  • true:(默认)实时消息集成。
  • false:离线消息集成。


注解

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

  • 在请求中单独包含 assetId 以指定一个设备或一个逻辑资产
  • 在请求中单独包含 productKeydeviceKey 以指定一个设备
测点数据结构体
名称 必需/可选 数据格式 描述
assetId 可选 String 资产的ID。
productKey 可选 String 设备的 product key。
deviceKey 可选 String 设备的 device key。
time 必需 Long 测点数据的时间戳。
measurepoints 必需 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 Int 请求返回状态值。0 表示请求成功,非 0 则表示请求失败。
msg String 对状态码的解释。请求成功则为 OK
requestId String 每次请求获取的 ID,用于唯一标识该次 API 请求。

示例

请求示例

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

请求报文form-data
Content-Disposition: form-data; name="enos-message"
{
  "method": "integration.measurepoint.post",
  "id": "123",
  "version": "1.1",
  "params":[
    {
      "productKey": "productKey1",
      "deviceKey": "deviceKey1",
      "time": 1579580182824,
      "measurepoints": {
        "intMesurepintId1": 123,
        "fileMeasurepointId1":"local://filename1"
      }
    },
    {
      "assetId": "assetId2",
      "time": 1579580182824,
      "measurepoints": {
        "intMesurepintId2": 123,
        "fileMeasurepointId2":"local://filename2"
      }
    }
  ],
  "files": {
    "filename1": {
      "featureId": "fileMeasurepointId1",
      "produckKey": "productKey1",
      "deviceKey": "deviceKey1",
      "originalFilename": "test.txt",
      "fileExt": ".txt",
      "md5": "0e202f9b67323d11df8a79b319a3d4f6"
    },
    "filename2": {
      "featureId": "fileMeasurepointId2",
      "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":"03902fcf-0d23-40f5-a8fd-0af0774cb533",
    "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.IntegrationMeasurepointPostRequest;
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 MeasurepointIntegrationSample {
    // 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 IntegrationMeasurepointPostRequest buildMeasurepointPostRequest() {
        DeviceInfo deviceInfo1 = new DeviceInfo().setAssetId(ASSET_ID);
        DeviceInfo deviceInfo2 = new DeviceInfo().setKey(PRODUCT_KEY, DEVICE_KEY);

        // Measurepoints are defined in ThingModel
        // FileMeasurePoint1 is a file-type measurepoint
        HashMap<String, Object> hashMap = Maps.newHashMap();
        hashMap.put("IntMeasurePoint1", 123);
        hashMap.put("FileMeasurePoint1", new File("sample1.txt"));
        return IntegrationMeasurepointPostRequest.builder()
                .addMeasurepoint(deviceInfo1, System.currentTimeMillis(), hashMap)
                .addMeasurepoint(deviceInfo2, System.currentTimeMillis(), 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();

        IntegrationMeasurepointPostRequest request = buildMeasurepointPostRequest();

        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 measurepoint post with file
        request = buildMeasurepointPostRequest();

        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();
        }
    }
}