和普通版阿里云物联网平台相比较,高级版多了很多支持,例如设备状态查询,设备属性定义,固件升级等等

首先在阿里云物联网平台创建高级版产品,然后创建设备,并定义功能


查看Topic类列表


在开发设备中


然后我们需要开发云端控制接口,这里我使用了Idea + Spring boot进行模拟测试

在idea中新建maven项目,在pom文件中集成以下插件

                                com.aliyun            aliyun-java-sdk-iot            5.0.0                            com.aliyun            aliyun-java-sdk-core            3.5.1                            commons-codec            commons-codec            1.11            

测试代码如下

具体的文档详见

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();        }    }    /**     * 设备状态枚举     *     * 1ONLINE    设备在线     * 2OFFLINE    设备离线     * 3UNACTIVE设备未激活     * 4DISABLE    设备已禁用     */    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   StringProductKey,全局唯一PK     *  ProductName    String产品名称     *  DeviceName  String设备名称     *  DeviceSecretString设备秘钥     *  IotId        String设备全局唯一Id,与productKey&deviceName同时作为设备的唯一标识符     *  GmtCreate    String创建时间     *  GmtActive    String激活时间     *  GmtOnline    String在线时间     *  Status      String设备状态     *  FirmwareVersionString固件版本号     *  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();    }

正常情况下,我们就可以在开发设备中看到以下日志


更多相关文章

  1. 阿里技术沙龙第17期·杭州站-无线技术专场 记录
  2. Android稳定性测试工具Monkey的使用
  3. Google 究竟是不是要用 Fuchsia OS 取代 Android?
  4. JRuby on Java ME/CDC
  5. Android(安卓)adb常用命令
  6. Android(安卓)OpenGL ES学习笔记之常用API
  7. android 陀螺仪简单使用,判读手机是否静止状态
  8. Awesome Adb——一份超全超详细的 ADB 用法大全
  9. android获取当前设备运行app的进程

随机推荐

  1. 关于Android SDK工具Lint的误报:Class ref
  2. android网络监听事件机制(kernel, c++, ja
  3. Kotlin 风险高、RxJava 已过时,Android 原
  4. Android实现EventBus登录界面与传值(粘性
  5. 浅析Android下的Android.mk文件
  6. Android中Math取整的三个方法
  7. android recovery 系统代码分析 -- 选择
  8. Activity、Bundle、请求码与结果码、Inte
  9. 深入理解dvm和jvm
  10. Android(安卓)2.3及以上版本支持自定义的