本教程详细介绍如何使用Alibaba Cloud SDK for Java查询IOT设备的运行状态、影子信息。

前提条件

在使用本教程前,请确保已完成以下操作:
  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见 阿里云开发工具包(SDK)
     <dependencies>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
             <groupId>com.aliyun</groupId>
             <artifactId>aliyun-java-sdk-core</artifactId>
             <version>4.4.3</version>
         </dependency>
         <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-iot -->
         <dependency>
             <groupId>com.aliyun</groupId>
             <artifactId>aliyun-java-sdk-iot</artifactId>
             <version>7.0.1</version>
         </dependency>
     </dependencies>

代码示例

本文操作示例主要以代码形式体现,具体代码如下:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.iot.model.v20180120.*;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.List;

/**
 * GetDeviceStatus    查看指定设备的运行状态
 * QueryDevice    查询产品的设备列表
 * GetDeviceShadow    查询设备影子
 */
public class Demo {

    // 产品Key
    private static String productKey = "*****";

    public static void main(String[] args) {
        Gson gson = new Gson();
        IAcsClient client = initialization();
        try {
            // 查询产品的设备列表
            QueryDeviceResponse queryDeviceResponse = queryDevice(client, productKey);
            // 获取返回设备信息列表
            List<QueryDeviceResponse.DeviceInfo> data = queryDeviceResponse.getData();
            // 获取返回设备
            QueryDeviceResponse.DeviceInfo deviceInfo = data.get(0);
            // 获取设备的唯一标识符
            String iotId = deviceInfo.getIotId();
            // 获取设备名称
            String deviceName = deviceInfo.getDeviceName();
            System.out.println(gson.toJson(queryDeviceResponse));
            // 查看指定设备的运行状态
            GetDeviceStatusResponse deviceStatusResponse = getDeviceStatus(client, iotId);
            System.out.println(gson.toJson(deviceStatusResponse));
            // 查询设备影子
            GetDeviceShadowResponse deviceShadowResponse = getDeviceShadow(client, deviceName, productKey);
            System.out.println(gson.toJson(deviceShadowResponse));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

    /**
     * GetDeviceShadow    查询设备影子
     */
    private static GetDeviceShadowResponse getDeviceShadow(IAcsClient client, String deviceName, String productKey) throws ClientException {
        GetDeviceShadowRequest request = new GetDeviceShadowRequest();
        System.out.println("------------------getDeviceShadow-------------------");
        // 要查询的设备所隶属的产品Key
        request.setProductKey(productKey);
        // 要查询的设备名称
        request.setDeviceName(deviceName);
        GetDeviceShadowResponse response = client.getAcsResponse(request);
        return response;
    }

    /**
     * GetDeviceStatus    查看指定设备的运行状态
     */
    private static GetDeviceStatusResponse getDeviceStatus(IAcsClient client, String iotId) throws ClientException {
        GetDeviceStatusRequest request = new GetDeviceStatusRequest();
        System.out.println("------------------getDeviceStatus-------------------");
        // 要查看运行状态的设备ID
        request.setIotId(iotId);
        GetDeviceStatusResponse response = client.getAcsResponse(request);
        return response;
    }

    /**
     * QueryDevice    查询产品的设备列表
     */
    private static QueryDeviceResponse queryDevice(IAcsClient client, String productKey) throws ClientException {
        QueryDeviceRequest request = new QueryDeviceRequest();
        System.out.println("------------------queryDevice-------------------");
        // 要查询的设备所隶属的产品Key
        request.setProductKey(productKey);
        // 分页条件
        request.setCurrentPage(1);
        request.setPageSize(20);
        QueryDeviceResponse response = client.getAcsResponse(request);
        return response;
    }

    /**
     * Initialization  初始化公共请求参数
     */
    private static IAcsClient initialization() {
        // 初始化请求参数
        DefaultProfile profile = DefaultProfile.getProfile(
                "<your-region-id>", // 您的可用区ID
                "<your-access-key-id>", // 您的AccessKey ID
                "<your-access-key-secret>"); // 您的AccessKey Secret
        return new DefaultAcsClient(profile);
    }
}

执行结果

正确的执行结果类似如下:
------------------queryDevice-------------------
{
  "requestId": "E85B8ACE-321A-4C72-BC9B-432CDB1D0970",
  "success": true,
  "total": 1,
  "pageSize": 20,
  "pageCount": 1,
  "page": 1,
  "data": [
    {
      "deviceId": "Dzw9Is9******XPab0",
      "deviceSecret": "mBsRW3***Erw",
      "productKey": "a***Pr",
      "deviceStatus": "UNACTIVE",
      "deviceName": "Test_Doc",
      "gmtCreate": "Fri, 11-Oct-2019 11:25:25 GMT",
      "gmtModified": "Fri, 11-Oct-2019 11:25:25 GMT",
      "utcCreate": "2019-10-11T11:25:25.000Z",
      "utcModified": "2019-10-11T11:25:25.000Z",
      "iotId": "Dzw9I****0",
      "nickname": "文档测试"
    }
  ]
}
------------------getDeviceStatus-------------------
{
  "requestId": "2F723888-2379-45F7-A989-B10CE6F37293",
  "success": true,
  "data": {
    "status": "UNACTIVE"
  }
}
------------------getDeviceShadow-------------------
{
  "RequestId": "BB71E443-4447-4024-A000-EDE09922891E",
  "Success": true,
  "ShadowMessage": {
    "method": "update",
    "state": {
      "desired": {
        "color": "green"
      },
      "reported": ""
    }
    "version": 1
  }
}