阿里雲物聯網管理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列表

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章