本示例介绍使用Alibaba Cloud SDK for Java调用BatchGetDeviceState接口批量获取设备的运行状态。
前提条件
请在pom.xml文件中增加以下依赖,准确的SDK版本号,参见阿里云开发工具包(SDK)。
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>java.demo</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<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>6.11.0</version>
</dependency>
</dependencies>
</project>
限制说明
- 单阿里云账号调用该接口的每秒请求数(QPS)最大限制为50。
说明 子账号共享主账号配额。
- 单客户端出口IP的最大QPS限制为100,即来自单个客户端出口IP,调用阿里云接口的每秒请求总数不能超过100。
- 该接口用于查看一个产品下多个设备的运行状态,单次最多可查询50个设备。
示例代码
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.iot.model.v20180120.BatchGetDeviceStateRequest;
import com.aliyuncs.iot.model.v20180120.BatchGetDeviceStateResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
/**
* 调用该接口批量获取设备状态
*/
public class TestBatchGetDeviceState {
public static void main(String[] args) {
DefaultProfile profile = DefaultProfile.getProfile(
"<your-region-id>", // 地域ID
"<your-access-key-id>", // 您的AccessKey ID
"<your-access-key-secret>"); // 您的AccessKey Secret
IAcsClient client = new DefaultAcsClient(profile);
// 创建请求接口
BatchGetDeviceStateRequest request = new BatchGetDeviceStateRequest();
// 要查看运行状态的设备所隶属的产品Key。
// 说明 如果传入该参数,需同时传入 DeviceNames。
// request.setProductKey("yourProductKey");
// 组建DeviceNames参数 要查看运行状态的设备的名称
// 如果传入该参数,需同时传入ProductKey。单次查询最多50个设备。
List<String> deviceNameList = new ArrayList<String>();
deviceNameList.add("yourdeviceName_1");
deviceNameList.add("yourdeviceName_2");
deviceNameList.add("yourdeviceName_3");
deviceNameList.add("yourdeviceName_4");
deviceNameList.add("yourdeviceName_5");
System.out.println(deviceNameList.toString());
// request.setDeviceNames(deviceNameList);
// 组建IotIds参数 要查看运行状态的设备ID列表。
// 如果传入该参数,则无需传入 ProductKey和 DeviceName。
// IotId作为设备唯一标识符,与 ProductKey 和 DeviceName组合是一一对应的关系。
// 如果您同时传入 IotId和 ProductKey与 DeviceName组合,则以 IotId为准。
List<String> iotIdList = new ArrayList<String>();
iotIdList.add("yourIotIds_1");
iotIdList.add("yourIotIds_2");
iotIdList.add("yourIotIds_3");
iotIdList.add("yourIotIds_4");
iotIdList.add("yourIotIds_5");
request.setIotIds(iotIdList);
try {
// 发起请求并获取返回值
BatchGetDeviceStateResponse response = client.getAcsResponse(request);
// 处理业务逻辑
System.out.println(new Gson().toJson(response));
} 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());
}
}
}