Java对阿里云物联网平台设备如何操作–保姆级教程
如果创建物联网平台和产品实例就不多说了,创建好之后直接把下面的代码copy然后替换相应配置就行
步骤:
- 添加阿里云依赖
- 编写配置文件
- 工具类
1、首先导入阿里云依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>iot20180120</artifactId>
<version>3.0.8</version>
</dependency>
2、yml配置文件
aliyah:
accessKeyId: LTAI5tG1H11WWj1t2V4SAoB
accessKeySecret: B2W6i11jkL2hM9plOEf3
productKey: k0ou1123MCt
region: iot.cn-shanghai.aliyuncs.com
iotInstanceId: iot-060a9qjm
package com.location.web.ams.aliyun;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration
public class AppConfig {
/**
* 密钥id
*/
@Value("${aliyah.accessKeyId}")
private String accessKeyId;
/**
* 密钥串
*/
@Value("${aliyah.accessKeySecret}")
private String accessKeySecret;
/**
* 产品id
*/
@Value("${aliyah.productKey}")
private String productKey;
/**
* 地区编号
*/
@Value("${aliyah.region}")
private String regionId;
/**
* 实例编号
*/
@Value("${aliyah.iotInstanceId}")
private String iotInstanceId;
}
accessKeyId 和accessKeySecret是账号申请的,阿里云物联网控制台右上角图标点击就能看到
productKey 产品id,添加产品后会自动生成
region 选择服务器的地方区域
iotInstanceId 实例的id
3、调用工具类
package com.location.web.ams.aliyun;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyun.iot20180120.Client;
import com.aliyun.iot20180120.models.*;
import com.aliyun.teautil.models.RuntimeOptions;
import com.location.core.common.core.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class IotUtils {
@Autowired
AppConfig appConfig;
/*** 使用AK&SK初始化账号Client
* @return Client
* @throws Exception
*/
public static com.aliyun.iot20180120.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
config.endpoint = "iot.cn-shanghai.aliyuncs.com";
return new com.aliyun.iot20180120.Client(config);
}
/**
* 创建设备
// * @param deviceName 设备名称不可以是中文
* @throws Exception
*/
public String addDevice() throws Exception {
com.aliyun.iot20180120.Client client = IotUtils.createClient(appConfig.getAccessKeyId(),appConfig.getAccessKeySecret());
com.aliyun.iot20180120.models.RegisterDeviceRequest registerDeviceRequest = new com.aliyun.iot20180120.models.RegisterDeviceRequest()
.setProductKey(appConfig.getProductKey()).setIotInstanceId(appConfig.getIotInstanceId());
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
RegisterDeviceResponse registerDeviceResponse = client.registerDeviceWithOptions(registerDeviceRequest, runtime);
if (null != registerDeviceResponse && 200==registerDeviceResponse.getStatusCode()){
RegisterDeviceResponseBody body = registerDeviceResponse.getBody();
return body.getData().iotId;
}else {
throw new BusinessException("添加设备失败,请联系管理员");
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* 检查设备
* @param deviceId
* @return
* @throws Exception
*/
public String checkDevice(String deviceId) throws Exception {
com.aliyun.iot20180120.Client client = IotUtils.createClient(appConfig.getAccessKeyId(), appConfig.getAccessKeySecret());
com.aliyun.iot20180120.models.GetDeviceStatusRequest getDeviceStatusRequest = new com.aliyun.iot20180120.models.GetDeviceStatusRequest()
.setIotInstanceId(appConfig.getIotInstanceId())
.setProductKey(appConfig.getProductKey())
.setIotId(deviceId);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
GetDeviceStatusResponse deviceStatusWithOptions = client.getDeviceStatusWithOptions(getDeviceStatusRequest, runtime);
if (ObjectUtil.isNotEmpty(deviceStatusWithOptions) && 200==deviceStatusWithOptions.getStatusCode()){
GetDeviceStatusResponseBody body = deviceStatusWithOptions.getBody();
String status = body.getData().getStatus();
return transitionStatus(status);
}
}catch (Exception e) {
e.printStackTrace();
}
return "设备异常";
}
public String transitionStatus(String status){
if (StrUtil.isNotEmpty(status)){
switch (status) {
case "ONLINE" -> {
return "设备在线";
}
case "OFFLINE" -> {
return "设备离线";
}
case "UNACTIVE" -> {
return "设备未激活";
}
case "DISABLE" -> {
return "设备已禁用";
}
}
}
return "设备故障";
}
/**
* 添加实例
* @param type
* @return
* @throws Exception
*/
public String addtype(String type) throws Exception {
RegisterDeviceResponse resp=null;
Client client = IotUtils.createClient(appConfig.getProductKey(),appConfig.getAccessKeySecret());
RegisterDeviceRequest request = new RegisterDeviceRequest();
CreateProductRequest createProductRequest = new CreateProductRequest();
createProductRequest.setProductName(type);
RuntimeOptions options = new RuntimeOptions();
try{
resp = client.registerDeviceWithOptions(request, options);
return resp.toString();
}catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* 开启设备
* @param deviceId
* @return
* @throws Exception
*/
public Boolean openDev(String deviceId) throws Exception {
com.aliyun.iot20180120.Client client = IotUtils.createClient(appConfig.getAccessKeyId(),appConfig.getAccessKeySecret());
EnableThingRequest enableThingRequest = new EnableThingRequest()
.setProductKey(appConfig.getProductKey()).setIotId(deviceId).setIotInstanceId(appConfig.getIotInstanceId());
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try{
EnableThingResponse enableThingResponse = client.enableThingWithOptions(enableThingRequest, runtime);
if (null != enableThingResponse && 200==enableThingResponse.getStatusCode()){
return true;
}
}catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 关闭设备
* @param deviceId
* @return
* @throws Exception
*/
public Boolean banDev(String deviceId) throws Exception {
com.aliyun.iot20180120.Client client = IotUtils.createClient(appConfig.getAccessKeyId(), appConfig.getAccessKeySecret());
DisableThingRequest disableThingRequest = new DisableThingRequest()
.setProductKey(appConfig.getProductKey()).setIotId(deviceId).setIotInstanceId(appConfig.getIotInstanceId());
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try{
DisableThingResponse disableThingResponse = client.disableThingWithOptions(disableThingRequest, runtime);
if (null != disableThingResponse && 200==disableThingResponse.getStatusCode()){
return true;
}
}catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 删除设备
* @param deviceId
* @return
* @throws Exception
*/
public Boolean deledev(String deviceId) throws Exception{
Client client = IotUtils.createClient(appConfig.getAccessKeyId(), appConfig.getAccessKeySecret());
DeleteDeviceRequest deleteDeviceRequest = new DeleteDeviceRequest()
.setIotInstanceId(appConfig.getIotInstanceId())
.setProductKey(appConfig.getProductKey())
.setIotId(deviceId);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
DeleteDeviceResponse deleteDeviceResponse = client.deleteDeviceWithOptions(deleteDeviceRequest, runtime);
if (null != deleteDeviceResponse && 200==deleteDeviceResponse.getStatusCode()){
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public Map<String,Object>getAccordingStatistics() throws Exception {
Map<String, Object> map = new HashMap<>();
Client client = IotUtils.createClient(appConfig.getAccessKeyId(), appConfig.getAccessKeySecret());
QueryDeviceStatisticsRequest queryDeviceStatisticsRequest = new QueryDeviceStatisticsRequest()
.setIotInstanceId(appConfig.getIotInstanceId())
.setProductKey(appConfig.getProductKey());
RuntimeOptions runtime = new RuntimeOptions();
try {
QueryDeviceStatisticsResponse response = client.queryDeviceStatisticsWithOptions(queryDeviceStatisticsRequest, runtime);
if (null != response && 200 == response.getStatusCode()) {
QueryDeviceStatisticsResponseBody.QueryDeviceStatisticsResponseBodyData data = response.getBody().data;
map = data.toMap();
return map;
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}
成功了就帮我一件三连吧!!!