和普通版阿里云物联网平台相比较,高级版多了很多支持,例如设备状态查询,设备属性定义,固件升级等等
首先在阿里云物联网平台创建高级版产品,然后创建设备,并定义功能
查看Topic类列表
在开发设备中
然后我们需要开发云端控制接口,这里我使用了Idea + Spring boot进行模拟测试
在idea中新建maven项目,在pom文件中集成以下插件
<dependencies> <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-iot --> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-iot</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.11</version> </dependency> </dependencies>
测试代码如下
具体的文档详见
https://help.aliyun.com/document_detail/69584.html?spm=a2c4g.11186623.6.650.okYZLm
但是文档中写的并不是很友好,新手肯定是需要一番折腾,所以可以用以下我简单封装的测试类进行开发测试
import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.iot.model.v20180120.*; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import java.util.Locale; /** * 阿里云物联网平台服务端 * Created by cc_want on 2018/4/11. */ public class IOT { private String TAG = "AlibabaIOT"; public static final String accessKey = "<填写>"; public static final String accessSecret = "<填写>"; public static final String productKey = "<填写>"; //这个是设备模版productkey public static final String deviceName = "device001";//这个是设备名称 private DefaultAcsClient mClient; private static IOT mIntance; public static IOT getIntance() { if (mIntance == null) { mIntance = new IOT(); } return mIntance; } public void init() { try { DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Iot", "iot.cn-shanghai.aliyuncs.com"); } catch (ClientException e) { e.printStackTrace(); } IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret); mClient = new DefaultAcsClient(profile); //初始化SDK客户端 } /** * 获取设备状态 */ public void getDeviceState() { GetDeviceStatusRequest request = new GetDeviceStatusRequest(); request.setDeviceName(deviceName); request.setProductKey(productKey); //request.setIotId("Zp2tLuUqHIgLHiISX8Nb0010****"); try { GetDeviceStatusResponse response = mClient.getAcsResponse(request); //ONLINE OFFLINE if (response.getSuccess()) { DeviceStatus status = DeviceStatus.create(response.getData().getStatus()); System.out.println("设备 = "+ status.getDesc()); } else { System.out.println("error:" + response.getErrorMessage()); } } catch (ClientException e) { e.printStackTrace(); } } /** * 调用服务 */ public void invokeThingService() { InvokeThingServiceRequest request = new InvokeThingServiceRequest(); request.setDeviceName(deviceName); request.setProductKey(productKey); request.setIdentifier("cmd"); request.setArgs("{\"LightStatus\":1,\"LightNo\":2}"); try { InvokeThingServiceResponse response = mClient.getAcsResponse(request); if (response.getSuccess()) { System.out.println("调用成功"); } else { System.out.println("error:" + response.getErrorMessage()); } } catch (ClientException e) { e.printStackTrace(); } } /** * 获取设备详情 */ public void queryDeviceDetail() { QueryDeviceDetailRequest request = new QueryDeviceDetailRequest(); request.setDeviceName(deviceName); request.setProductKey(productKey); try { QueryDeviceDetailResponse response = mClient.getAcsResponse(request); if (response.getSuccess()) { Device device = new Device(response.getData()); System.out.println(device.toString()); } else { System.out.println("error:" + response.getErrorMessage()); } } catch (ClientException e) { e.printStackTrace(); } } /** * 设备状态枚举 * * 1 ONLINE 设备在线 * 2 OFFLINE 设备离线 * 3 UNACTIVE 设备未激活 * 4 DISABLE 设备已禁用 */ public enum DeviceStatus{ ONLINE(1,"设备在线"), OFFLINE(2,"设备离线"), UNACTIVE(3,"设备未激活"), DISABLE(4,"设备已禁用"); private int id; private String desc; DeviceStatus(int id, String desc){ this.id = id; this.desc = desc; } public int getId() { return id; } public String getDesc() { return desc; } public static DeviceStatus create(String status) { for (DeviceStatus c : DeviceStatus.values()) { if (c.name().equals(status)) { return c; } } return null; } } /** * 设备信息 * * ProductKey String ProductKey,全局唯一PK * ProductName String 产品名称 * DeviceName String 设备名称 * DeviceSecret String 设备秘钥 * IotId String 设备全局唯一Id,与productKey&deviceName同时作为设备的唯一标识符 * GmtCreate String 创建时间 * GmtActive String 激活时间 * GmtOnline String 在线时间 * Status String 设备状态 * FirmwareVersion String 固件版本号 * IpAddress String 设备IP地址 * NodeType Integer 节点类型, 0 — 设备,1 — 网关 * Region String 所在地区 */ public static class Device{ private String iotId; private String productKey; private String productName; private String deviceName; private String deviceSecret; private String firmwareVersion; private String gmtCreate; private String gmtActive; private String gmtOnline; private DeviceStatus status; private String ipAddress; private Integer nodeType; private String region; public Device(QueryDeviceDetailResponse.Data data){ iotId = data.getIotId(); productKey = data.getProductKey(); productName = data.getProductName(); deviceName = data.getDeviceName(); deviceSecret = data.getDeviceSecret(); firmwareVersion = data.getFirmwareVersion(); gmtCreate = data.getGmtCreate(); gmtActive = data.getGmtActive(); gmtOnline = data.getGmtOnline(); status = DeviceStatus.create(data.getStatus()); ipAddress = data.getIpAddress(); nodeType = data.getNodeType(); region = data.getRegion(); } public String getIotId() { return iotId; } public void setIotId(String iotId) { this.iotId = iotId; } public String getProductKey() { return productKey; } public void setProductKey(String productKey) { this.productKey = productKey; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getDeviceName() { return deviceName; } public void setDeviceName(String deviceName) { this.deviceName = deviceName; } public String getDeviceSecret() { return deviceSecret; } public void setDeviceSecret(String deviceSecret) { this.deviceSecret = deviceSecret; } public String getFirmwareVersion() { return firmwareVersion; } public void setFirmwareVersion(String firmwareVersion) { this.firmwareVersion = firmwareVersion; } public String getGmtCreate() { return gmtCreate; } public void setGmtCreate(String gmtCreate) { this.gmtCreate = gmtCreate; } public String getGmtActive() { return gmtActive; } public void setGmtActive(String gmtActive) { this.gmtActive = gmtActive; } public String getGmtOnline() { return gmtOnline; } public void setGmtOnline(String gmtOnline) { this.gmtOnline = gmtOnline; } public DeviceStatus getStatus() { return status; } public void setStatus(DeviceStatus status) { this.status = status; } public String getIpAddress() { return ipAddress; } public void setIpAddress(String ipAddress) { this.ipAddress = ipAddress; } public Integer getNodeType() { return nodeType; } public void setNodeType(Integer nodeType) { this.nodeType = nodeType; } public String getRegion() { return region; } public void setRegion(String region) { this.region = region; } @Override public String toString() { return "Device{" + "iotId='" + iotId + '\'' + ", productKey='" + productKey + '\'' + ", productName='" + productName + '\'' + ", deviceName='" + deviceName + '\'' + ", deviceSecret='" + deviceSecret + '\'' + ", firmwareVersion='" + firmwareVersion + '\'' + ", gmtCreate='" + gmtCreate + '\'' + ", gmtActive='" + gmtActive + '\'' + ", gmtOnline='" + gmtOnline + '\'' + ", status='" + status + '\'' + ", ipAddress='" + ipAddress + '\'' + ", nodeType=" + nodeType + ", region='" + region + '\'' + '}'; } } }
然后测试调用是否成功
public static void main(String args[]){ IOT.getIntance().init(); //IOT.getIntance().open(1); //IOT.getIntance().getDeviceState(); IOT.getIntance().invokeThingService(); //IOT.getIntance().queryDeviceDetail(); }
正常情况下,我们就可以在开发设备中看到以下日志