阿里云物联网平台远程配置功能JAVA 示例参考

概述

很多场景下,开发者需要更新设备的配置信息,包括设备的系统参数、网络参数、本地策略等。通常情况下,是通过固件升级的方式更新设备的配置信息。但是,这将加大固件版本的维护工作,并且需要设备中断运行以完成更新。为了解决上述问题,物联网平台提供了远程配置更新的功能,设备无需重启或中断运行即可在线完成配置信息的更新。本文主要演示如何使用JAVA SDK实现两种应用场景。

原理说明

1、物联网平台提供的远程配置功能,支持:

  • 开启/关闭远程配置。
  • 在线编辑配置文件,并管理版本。
  • 批量更新设备配置信息。
  • 设备主动请求更新配置信息。

2、流程图

阿里云物联网平台远程配置功能JAVA 示例参考

3、远程配置大致分为三部分:

  • 生成配置文件:您在物联网平台控制台编辑并保存配置信息。
  • 推送配置文件:您在物联网平台控制台批量推送配置信息给设备。设备接收后,修改本地配置文件。
  • 设备主动请求配置信息:设备主动向云端请求新的配置文件,并进行更新。

远程配置使用分为两种场景,一种是云端推送配置信息给设备端,一种是设备端主动请求配置信息。根据场景的不同,远程配置的步骤也有所区别。

场景一:云端推送配置信息给设备端

1、设备端业务代码实现

import com.aliyun.alink.dm.api.DeviceInfo;
import com.aliyun.alink.dm.api.InitResult;
import com.aliyun.alink.dm.api.IoTApiClientConfig;
import com.aliyun.alink.linkkit.api.ILinkKitConnectListener;
import com.aliyun.alink.linkkit.api.IoTMqttClientConfig;
import com.aliyun.alink.linkkit.api.LinkKit;
import com.aliyun.alink.linkkit.api.LinkKitInitParams;
import com.aliyun.alink.linksdk.cmp.connect.channel.MqttSubscribeRequest;
import com.aliyun.alink.linksdk.cmp.core.base.AMessage;
import com.aliyun.alink.linksdk.cmp.core.base.ConnectState;
import com.aliyun.alink.linksdk.cmp.core.listener.IConnectNotifyListener;
import com.aliyun.alink.linksdk.cmp.core.listener.IConnectSubscribeListener;
import com.aliyun.alink.linksdk.tools.AError;

public class DeviceCOTADemo1 {

    public static void main(String[] args) {
        String regionId = "cn-shanghai";
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.productKey = "******";
        deviceInfo.deviceName = "******";
        deviceInfo.deviceSecret = "********";

        LinkKitInitParams params = new LinkKitInitParams();
        IoTMqttClientConfig config = new IoTMqttClientConfig();
        config.productKey = deviceInfo.productKey;
        config.deviceName = deviceInfo.deviceName;
        config.deviceSecret = deviceInfo.deviceSecret;
        config.channelHost = deviceInfo.productKey + ".iot-as-mqtt." + regionId + ".aliyuncs.com:1883";
        params.mqttClientConfig = config;
        params.connectConfig = new IoTApiClientConfig();
        params.deviceInfo = deviceInfo;

        /**建立连接**/
        LinkKit.getInstance().init(params, new ILinkKitConnectListener() {
            @Override
            public void onError(AError aError) {
                System.out.println("初始化失败 " + aError.getMsg());
            }

            @Override
            public void onInitDone(InitResult initResult) {

                System.out.println("初始化成功 " + initResult.tsl);

                String pd = deviceInfo.productKey + "/" + deviceInfo.deviceName;

                // 1、cota 请求 Topic
                String requestcotaTopic = "/sys/"+pd+"/thing/config/push";

                // 订阅Topic
                MqttSubscribeRequest upgradeTopicRequest = new MqttSubscribeRequest();
                upgradeTopicRequest.topic = requestcotaTopic;
                upgradeTopicRequest.isSubscribe = true;

                // 直接做Topic的订阅
                LinkKit.getInstance().subscribe(upgradeTopicRequest, new IConnectSubscribeListener(){
                    @Override
                    public void onSuccess() {
                        System.out.println("cota订阅成功");
                    }

                    @Override
                    public void onFailure(AError aError) {
                        System.out.println("直接订阅失败" + aError.getMsg());
                    }
                });

                // 注册ota下行监听
                LinkKit.getInstance().registerOnNotifyListener(new IConnectNotifyListener() {
                    @Override
                    public void onNotify(String s, String s1, AMessage aMessage) {

                        System.out.println("cota 监听响应:");
                        System.out.println(new String((byte[])aMessage.getData()));
                    }

                    @Override
                    public boolean shouldHandle(String s, String s1) {
                        return false;
                    }

                    @Override
                    public void onConnectStateChange(String s, ConnectState connectState) {
                        System.out.println("连接状态发生变化 :" + s + connectState);
                    }
                });

            }
        });
    }
}

2、管理门户配置远程控制,详细步骤参考链接。

阿里云物联网平台远程配置功能JAVA 示例参考

3、启动设备端程序,管理门户批量更新,设备端获取信息(批量更新频率限制:一小时内仅允许操作一次):

 

{"method":"thing.config.push","id":"1296799269","params":{"getType":"file","configId":"92eb82a17a7441111f7dcb830e93ed79","sign":"568e9211117eedc9eadbc2907719fe2086348f572ddd3f373d6f297ba5aba4d1","configSize":12,"signMethod":"Sha256","url":"https://otx-devicecenter-thing-config-cn-shanghai-online.oss-cn-shanghai.aliyuncs.com/11zisMUtYDk/1567338668042_gz2Qs7hl?Expires=1567340568&OSSAccessKeyId=1111113rx5dg2JBm&Signature=S0LS9H%1111zJqTg%2Fphf19%2BfE%2F2bs%3D"},"version":"1.0"}

场景二:设备主动请求配置信息

1、设备端业务代码实现

import com.aliyun.alink.dm.api.DeviceInfo;
import com.aliyun.alink.dm.api.InitResult;
import com.aliyun.alink.dm.api.IoTApiClientConfig;
import com.aliyun.alink.linkkit.api.ILinkKitConnectListener;
import com.aliyun.alink.linkkit.api.IoTMqttClientConfig;
import com.aliyun.alink.linkkit.api.LinkKit;
import com.aliyun.alink.linkkit.api.LinkKitInitParams;
import com.aliyun.alink.linksdk.cmp.connect.channel.MqttPublishRequest;
import com.aliyun.alink.linksdk.cmp.connect.channel.MqttSubscribeRequest;
import com.aliyun.alink.linksdk.cmp.core.base.AMessage;
import com.aliyun.alink.linksdk.cmp.core.base.ARequest;
import com.aliyun.alink.linksdk.cmp.core.base.AResponse;
import com.aliyun.alink.linksdk.cmp.core.base.ConnectState;
import com.aliyun.alink.linksdk.cmp.core.listener.IConnectNotifyListener;
import com.aliyun.alink.linksdk.cmp.core.listener.IConnectSendListener;
import com.aliyun.alink.linksdk.cmp.core.listener.IConnectSubscribeListener;
import com.aliyun.alink.linksdk.tools.AError;

public class DeviceCOTADemo2 {

    public static void main(String[] args) {
        String regionId = "cn-shanghai";
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.productKey = "******";
        deviceInfo.deviceName = "******";
        deviceInfo.deviceSecret = "********";

        LinkKitInitParams params = new LinkKitInitParams();
        IoTMqttClientConfig config = new IoTMqttClientConfig();
        config.productKey = deviceInfo.productKey;
        config.deviceName = deviceInfo.deviceName;
        config.deviceSecret = deviceInfo.deviceSecret;
        config.channelHost = deviceInfo.productKey + ".iot-as-mqtt." + regionId + ".aliyuncs.com:1883";
        params.mqttClientConfig = config;
        params.connectConfig = new IoTApiClientConfig();
        params.deviceInfo = deviceInfo;

        /**建立连接**/
        LinkKit.getInstance().init(params, new ILinkKitConnectListener() {
            @Override
            public void onError(AError aError) {
                System.out.println("初始化失败 " + aError.getMsg());
            }

            @Override
            public void onInitDone(InitResult initResult) {

                System.out.println("初始化成功 " + initResult.tsl);

                String pd = deviceInfo.productKey + "/" + deviceInfo.deviceName;

                // 1、设备端订阅Topic:/sys/${productKey}/${deviceName}/thing/config/get_reply
                String requestcotaTopic = "/sys/"+pd+"/thing/config/get_reply";
                MqttSubscribeRequest upgradeTopicRequest = new MqttSubscribeRequest();
                upgradeTopicRequest.topic = requestcotaTopic;
                upgradeTopicRequest.isSubscribe = true;
                LinkKit.getInstance().subscribe(upgradeTopicRequest, new IConnectSubscribeListener(){
                    @Override
                    public void onSuccess() {
                        System.out.println("ota订阅成功");
                    }

                    @Override
                    public void onFailure(AError aError) {
                        System.out.println("直接订阅失败" + aError.getMsg());
                    }
                });

                // 注册下行监听
                LinkKit.getInstance().registerOnNotifyListener(new IConnectNotifyListener() {
                    @Override
                    public void onNotify(String s, String s1, AMessage aMessage) {

                        System.out.println("cota 监听响应:");
                        System.out.println(new String((byte[])aMessage.getData()));
                    }

                    @Override
                    public boolean shouldHandle(String s, String s1) {
                        return false;
                    }

                    @Override
                    public void onConnectStateChange(String s, ConnectState connectState) {
                        System.out.println("连接状态发生变化 :" + s + connectState);
                    }
                });

                // 4、设备通过Topic /sys/${productKey}/${deviceName}/thing/config/get主动查询最新的配置信息
                String requestTopic = "/sys/"+pd+"/thing/config/get";

                System.out.println("发送消息" + requestTopic);

                // 订阅Topic
                MqttPublishRequest request = new MqttPublishRequest();
                request.topic = requestTopic;
                request.payloadObj = "{"id":"123","method":"thing.config.get","params":{"getType":"file","configScope":"product"},"version":"1.0"}";

                // 直接做Topic的发送
                LinkKit.getInstance().publish(request, new IConnectSendListener() {
                    @Override
                    public void onResponse(ARequest aRequest, AResponse aResponse) {
                        System.out.println("请求发送成功 " + aResponse.getData());
                    }

                    @Override
                    public void onFailure(ARequest aRequest, AError aError) {
                        System.out.println("请求发送成功 " + aError.getMsg());
                    }
                });
            }
        });
    }
}

2、运行结果(/sys/${productKey}/${deviceName}/thing/config/get_reply 订阅获取的内容)

{"code":200,"data":{"configId":"a9415f2d6e444d7f9e60a3d46f11d566","configSize":18,"getType":"file","sign":"112f38e7345a2ac46d9f80a401886727f27de96ebf1c82b6825df4b31116ab02","signMethod":"Sha256","url":"https://otx-devicecenter-thing-config-cn-shanghai-online.oss-cn-shanghai.aliyuncs.com/11D8NwXCe56/1567337893929_gpUFnE69?Expires=1567339979&OSSAccessKeyId=11AIRY3rx5dg2JBm&Signature=1112qy8bOsa48elOQFt4qHQGaqk%3D"},"id":"123","method":"thing.config.get","version":"1.0"}

原文链接
本文为云栖社区原创内容,未经允许不得转载。

本文章来源于互联网,如有侵权,请联系删除!