阿里云物联网管理SDK使用示例

概述

目前阿里云的绝大部分服务都提供了管理资源的Rest API,针对相应的API也做了SDK的封装,且版本也在不断的迭代更新。管理API SDK的统一GitHub地址链接。关于IoT SDK的使用,之前官方也给出了示例参考,但是该示例使用的SDK版本为5.0.0,且这个版本的SDK中没有DeleteProductRequest类的实现,下面演示使用目前最新版本:6.5.0关于产品的创建及删除操作,新建Topic间的消息路由关系、查询向指定Topic订阅消息的目标Topic操作,其它操作类似。

pom依赖

        <!--核心依赖-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>3.5.0</version>
        </dependency>

        <!--管理API SDK调用问题-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-iot</artifactId>
            <version>6.5.0</version>
        </dependency>

Java示例代码

import com.aliyuncs.AcsResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.RpcAcsRequest;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.iot.model.v20180120.CreateProductRequest;
import com.aliyuncs.iot.model.v20180120.CreateProductResponse;
import com.aliyuncs.iot.model.v20180120.DeleteProductRequest;
import com.aliyuncs.iot.model.v20180120.DeleteProductResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class IoTDemo {

    public static void main(String[] args) {

        String productName = "iottestdemo123456";
        String productKey = "******";
        Integer nodeType = 0;
//      createProductTest(productName,nodeType);
        deleteProductTest(productKey);
        System.out.println("删除成功");
    }


    /**
     * 创建产品
     * @param productName 产品名称
     * @param nodeType 产品描述
     * @return 产品的PK
     */
    public static String createProductTest(String productName, Integer nodeType) {
        CreateProductRequest request = new CreateProductRequest();
        request.setProductName(productName);

        request.setNodeType(nodeType);//节点的类型,0标识设备,1标识网关
        CreateProductResponse response = (CreateProductResponse)executeTest1(request);
        if (response != null && response.getSuccess() != false) {
            System.out.println("创建产品成功!productKey:" + response.getProductKey());
            return response.getProductKey();
        } else {
            System.out.println("创建产品失败!requestId:" + response.getRequestId() + "原因:" + response.getErrorMessage());
        }
        return null;
    }

    /**
     * 删除产品
     * @param productKey 产品key
     * @return 产品的PK,可以在管理门户获取
     */
    public static Boolean deleteProductTest(String productKey) {
        DeleteProductRequest request = new DeleteProductRequest();
        request.setProductKey(productKey);
        DeleteProductResponse response = (DeleteProductResponse)executeTest1(request);
        if (response != null && response.getSuccess() != false) {
            System.out.println("删除产品成功!productKey:" + response.getSuccess());
            return response.getSuccess();
        } else {
            System.out.println("创建产品失败!requestId:" + response.getRequestId() + "原因:" + response.getErrorMessage());
        }
        return false;
    }
    
    public static AcsResponse executeTest1(RpcAcsRequest request) {

        DefaultAcsClient client = null;
        String regionId = "cn-shanghai";
        String accessKeyID = "********";
        String accessKeySecret = "********";
        String productCode = "Iot";
        String domain = "iot.cn-shanghai.aliyuncs.com";
        IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyID, accessKeySecret);
        try {
            DefaultProfile.addEndpoint(regionId, regionId, productCode, domain);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        // 初始化client
        client = new DefaultAcsClient(profile);

        System.out.println("client 初始化完成!");

        AcsResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (Exception e) {
            System.out.println("执行失败:e:" + e.getMessage());
        }
        return response;
    }

    /**
     * 新建Topic间的消息路由关系
     */
    public static void CreateTopicRouteTable() {
        CreateTopicRouteTableRequest request = new CreateTopicRouteTableRequest();
        request.setSrcTopic("/a1D8NwXCe56/device1/user/TopicToTopicDemo"); // 源Topic,即被订阅的Topic。
        List<String> dstTopics = new ArrayList<String>();
        dstTopics.add("/a1D8NwXCe56/ceshi/user/TopicToTopicDemo"); // 目标Topic列表,即从SrcTopic订阅消息的Topic列表。
        request.setDstTopics(dstTopics);
        CreateTopicRouteTableResponse response = (CreateTopicRouteTableResponse)executeTest1(request);
        if (response != null && response.getSuccess() != false) {
            System.out.println("CreateTopicRouteTable 成功:" + response.getSuccess());
        } else {
            System.out.println("CreateTopicRouteTable, requestId:" + response.getRequestId() + "原因:" + response.getErrorMessage());
        }
    }

    /**
     *  查询向指定Topic订阅消息的目标Topic
     */
    public static void QueryTopicRouteTable() {
        QueryTopicRouteTableRequest request = new QueryTopicRouteTableRequest();
        request.setActionName("QueryTopicRouteTable");
        request.setTopic("/a1D8NwXCe56/device1/user/TopicToTopicDemo"); // 要查询的源Topic,即发布消息的Topic。
        QueryTopicRouteTableResponse response = (QueryTopicRouteTableResponse)executeTest1(request);
        if (response != null && response.getSuccess() != false) {
            System.out.println("QueryTopicRouteTable 成功:" + response.getSuccess());
            System.out.println(response.getDstTopics());
        } else {
            System.out.println("CreateTopicRouteTable, requestId:" + response.getRequestId() + "原因:" + response.getErrorMessage());
        }
    }
}

注意

当前阶段国内只有上海地区支持IoT服务,用户只需要根据需求替换AK及IoT的信息即可。删除产品的前提是产品中没有设备,管理门户目前删除产品需要短信验证码,直接使用SDK调用不需要验证码。

参考链接

IoT SDK Demo

API列表

上一篇:直播预告 | 全站加速DCDN重磅升级发布会


下一篇:阿里云李克:边缘云技术发展与实践