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

概述

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

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

注意

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

参考链接

IoT SDK Demo

API列表

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值