全部产品

应用集成

本篇主要是对集成商对数据模型中数据的获取、服务模型的调用介绍。

一、数据模型

1.1 数据查看

1.登录数字园区引擎平台,查看当前实例的Appkey和AppSecret,用于数据模型数据的获取。

07

2.在录数字园区引擎平台中,单击数据集成,可以查看当前实例具有的数据模型。

3.单击数据集成,看到添加在项目中的数据模型。

4.单击授权,开通项目应用对数据模型的权限。

其中Scope ID为项目的唯一标识,在数据模型调用中作为scopeid的入参值使用。

08

5.单击管理,可以查看当前数据模型中的全部数据。

1.2 数据获取

根据上面获取到的Appkey和AppSecret进行数据模型数据的获取。

API开发链接:https://help.aliyun.com/document_detail/114862.html?spm=a2c4g.11174283.6.642.13e065bfMiWsvF

代码示例:

/*** 订阅数据 ---拿到dataIds后----数据查询*/public class TestTopicAndQuery {//    private final static String MODEL_ID = "CustomerFlowBasicData";//    private final static String appKey = "29386793";//    private final static String appSecret = "8627a6fb3eae1592e697c742cd0f5404";private final static String MODEL_ID = "CustomerFlowBasicData";private final static String appKey = "你的Appkey";private final static String appSecret = "你的appSecret";private final static String iotGatewayHost = "api.link.aliyun.com";private final static String QueryPath = "/data/model/data/query";private final static String endPoint = "https://%s.iot-as-http2.cn-shanghai.aliyuncs.com:443";public static void main(String[] args) {/*** 数据订阅* 返回示例;Message{payload={"dataIds":[3099],"modelId":"CustomerFlowBasicData","operateType":"insert"}, topic='/sys/appkey/29388982/dop/model/data/change', messageId='1253554752688804352', qos=1, generateTime=1587705717619}*/String endpoint = String.format(endPoint, appKey);// 连接配置Profile profile = Profile.getAppKeyProfile(endpoint, appKey, appSecret);// 构造客户端MessageClient client = MessageClientFactory.messageClient(profile);// 数据接收client.connect(messageToken -> {System.out.println(messageToken.getMessage());byte[] bytes = messageToken.getMessage().getPayload();String Str = new String(bytes);TopicPayload payload = JSON.parseObject(Str, TopicPayload.class);List<Integer> dataIdsList = payload.getDataIds();if (StringUtils.isNoneBlank(payload.getOperateType())) {Integer dataIds = dataIdsList.get(0);System.out.println(dataIds);/***根据dataids进行 数据查询**/try {IoTApiClientBuilderParams ioTApiClientBuilderParams = new IoTApiClientBuilderParams();ioTApiClientBuilderParams.setAppKey(appKey);ioTApiClientBuilderParams.setAppSecret(appSecret);SyncApiClient syncClient = new SyncApiClient(ioTApiClientBuilderParams);IoTApiRequest request = new IoTApiRequest();//设置api的版本request.setApiVer("0.0.3");request.setId(UUID.randomUUID().toString().replaceAll("-", ""));request.putParam("modelId", MODEL_ID);DataQueryDTO dataQueryDTO = createQueryDTO(dataIds);request.putParam("returnFields", dataQueryDTO.getReturnFields());request.putParam("conditions", dataQueryDTO.getConditions());request.setVersion("1.0");// 网关协议版本ApiResponse apiResponse = syncClient.postBody(iotGatewayHost,QueryPath, request, true);System.out.println("response code = " + apiResponse.getCode() + " response = " + new String(apiResponse.getBody(), "UTF-8"));} catch (UnsupportedEncodingException e) {e.printStackTrace();}}return MessageCallback.Action.CommitSuccess;});try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}MessageCallback messageCallback = new MessageCallback() {@Overridepublic MessageCallback.Action consume(MessageToken messageToken) {return Action.CommitSuccess;}};String topic = String.format("/sys/%s/dop/model/data/change", appKey);client.setMessageListener(topic, messageCallback);}private static DataQueryDTO createQueryDTO(Integer id) {DataQueryDTO dataQueryDTO = new DataQueryDTO();dataQueryDTO.setModelId(MODEL_ID);if (null != id) {ConditionDTO conditionDTO = new ConditionDTO("id", id, "eq");dataQueryDTO.setConditions(Lists.newArrayList(conditionDTO));}dataQueryDTO.setReturnFields(Lists.newArrayList("*"));return dataQueryDTO;}

2 服务模型

服务模型的开发API链接:https://help.aliyun.com/document_detail/114863.html?spm=a2c4g.11186623.6.640.70fe54086Pc8Mx

代码示例:

public static void main(String[] args) throws UnsupportedEncodingException {IoTApiClientBuilderParams ioTApiClientBuilderParams =new IoTApiClientBuilderParams();ioTApiClientBuilderParams.setAppKey("31366007");ioTApiClientBuilderParams.setAppSecret("f9e3a7c75a54daccce4004fda186a0d9");SyncApiClient syncClient = new SyncApiClient(ioTApiClientBuilderParams);IoTApiRequest request = new IoTApiRequest();//设置api的版本request.setApiVer("1.0");request.setVersion("1.2");// 网关协议版本//request.setId("A33202C1032946C0-B12B66C272AAFD1C");// 接口参数request.putParam("spaceId","111");request.putParam("startTime",12313L);request.putParam("endTime",322223L);request.putParam("matchType","1");request.putParam("matchName","11213");//请求参数域名、path、requestApiResponse response = syncClient.postBody("service-mesh.api-iot.cn-shanghai.aliyuncs.com","/VideoClipService/addMatch", request, false);System.out.println( "response code = " + response.getCode()+ " response = " + new String(response.getBody(), "UTF-8"));}