panlili2024
2024-11-14 b1569b789eef12cf5d4252620ca7e7d2d9c8b93a
HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java
@@ -1,5 +1,18 @@
package com.hdl.hdlsdk;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
@@ -8,39 +21,36 @@
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.reflect.TypeToken;
import com.hdl.hdlsdk.device.DevicesListActivity;
import com.hdl.sdk.common.config.TopicConstant;
import com.hdl.sdk.common.event.DeleteNetworkListener;
import com.hdl.sdk.common.event.EventListener;
import com.hdl.sdk.common.exception.HDLLinkException;
import com.hdl.sdk.common.utils.AllTopicManagerUtils;
import com.hdl.sdk.common.utils.IdUtils;
import com.hdl.sdk.common.utils.IpUtils;
import com.hdl.sdk.common.utils.LogUtils;
import com.hdl.sdk.common.utils.gson.GsonConvert;
import com.hdl.sdk.connect.HDLLink;
import com.hdl.sdk.connect.bean.LinkResponse;
import com.hdl.sdk.connect.bean.request.AttributesRequest;
import com.hdl.sdk.connect.bean.request.AuthenticateRequest;
import com.hdl.sdk.connect.bean.request.PropertyReadRequest;
import com.hdl.sdk.connect.bean.request.DeviceControlRequest;
import com.hdl.sdk.connect.bean.request.ListSidRequest;
import com.hdl.sdk.connect.bean.request.ListUploadRequest;
import com.hdl.sdk.connect.bean.response.BaseLocalResponse;
import com.hdl.sdk.connect.bean.response.DeviceInfoResponse;
import com.hdl.sdk.connect.bean.response.GatewaySearchBean;
import com.hdl.sdk.connect.bean.response.UpdateInfo;
import com.hdl.sdk.connect.callback.HDLLinkCallBack;
import com.hdl.sdk.connect.callback.HDLLinkResponseCallBack;
import com.hdl.sdk.connect.cloud.CallBackListener;
import com.hdl.sdk.connect.cloud.CheckAppVersionListener;
import com.hdl.sdk.connect.cloud.HDLException;
import com.hdl.sdk.connect.config.HDLLinkConfig;
import com.hdl.sdk.connect.socket.HDLAuthSocket;
import com.hdl.sdk.connect.socket.HDLSocket;
import com.hdl.sdk.connect.bean.request.DeviceControlRequest;
import com.hdl.sdk.connect.protocol.LinkMessageDecoder;
import com.hdl.sdk.connect.protocol.LinkMessageEncoder;
import com.hdl.sdk.socket.SocketOptions;
import com.hdl.sdk.socket.codec.MessagePipeLine;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
@@ -49,8 +59,6 @@
import java.util.List;
import java.util.Map;
import static com.hdl.sdk.common.config.TopicConstant.GATEWAY_SEARCH_REPLY;
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
@@ -58,28 +66,128 @@
    private RecyclerView rv;
    private TextView tv;
    private TextView responseTv;
    private TextView localSecret;
    private EditText editText;
    boolean isOn;
    private EventListener allTopicsListener;
    private String testLightSid = "0001010D48C71B02020100010101";
    private String testLightSid = "02010131D9C03D01020300010107";
    private String secret = "";
    @Override
    protected void onDestroy() {
        super.onDestroy();
        removeAllTopicsListener();
    void applyDeviceSecret() {
        tv.setText("开始申请设备密钥...");
        responseTv.setText("");
//        //正式服务器
//        String appKey = "i8hR07jzrIS";//appkey
//        String appSecret = "BmnJ8RWTtaVEBk24zPPF4UMwfYu0lAWU";//appsecret
        //测试服务器
        String appKey ="FcRyUJlLJFF";
        String appSecret = "wz8wn75ABidx8vXcFGUotqhwFkTaYvvJ";
//        String appKey = "L2OZliZRxHc";
//        String appSecret = "aCIWSvJDOukXfx3kivsKW11x9xdR3IbV";
        String supplier = "JINMAOYUN";//厂商
//        String mac = "AA00000000000100";//设备唯一MAC地址
        String mac = editText.getText().toString();
        String spk = "screen.touch";//产品spk
        if (TextUtils.isEmpty(mac)) {
            Toast.makeText(this, "mac不能为空!", Toast.LENGTH_SHORT).show();
            return;
        }
        HDLLink.getInstance().applyDeviceSecret(this, appKey, appSecret, supplier, mac, spk, new CallBackListener() {
            @Override
            public void onError(HDLException e) {
                tv.setText("申请失败");
                responseTv.setText(e.getMsg());
                secret = "";
            }
            @Override
            public void onSuccess(String msg) {
                tv.setText("申请成功");
                responseTv.setText(msg.toString());
                secret = msg.toString();
            }
        });
    }
    void checkAppVersion() {
        tv.setText("开始检测更新...");
        responseTv.setText("");
//        //正式服务器
        String appKey = "i8hR07jzrIS";//appkey
        String appSecret = "BmnJ8RWTtaVEBk24zPPF4UMwfYu0lAWU";//appsecret
        String appCode = "1697150870315999233";//appCode
        HDLLink.getInstance().checkAppVersion(this, appKey, appSecret, getAppVersionName(this), appCode, new CheckAppVersionListener() {
            @Override
            public void onSuccess(UpdateInfo info) {
                tv.setText("有新更新");
                responseTv.setText(info.toString());
            }
            @Override
            public void onError(HDLException e) {
                tv.setText("检测更新失败");
                responseTv.setText(e.getMsg());
            }
        });
    }
    public void UploadDeviceAuth(String mac, String result, String message, String auth_code) {
        //将认证结果上报给网关
        HDLLink.getInstance().UploadDeviceAuth(mac, result, message, auth_code, new HDLLinkCallBack() {
            @Override
            public void onSuccess(String s) {
                tv.setText("上报结果成功");
                responseTv.setText(s.toString());
            }
            @Override
            public void onError(HDLLinkException e) {
                tv.setText("上报结果失败");
                responseTv.setText(e.getMsg());
                Log.i(TAG, "onError: 上报结果失败");
            }
        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        responseTv = findViewById(R.id.response_tv);
        editText = findViewById(R.id.edittext);
        localSecret = findViewById(R.id.local_secret);
        tv = findViewById(R.id.state_tv);
        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
        selectnetwork();
        checkIfCertified();
        initDeviceInfo();//初始化
        registerAllTopicsListener();
        HDLLink.getInstance().setDeleteNetworkListener(new DeleteNetworkListener() {
            @Override
            public void onSuccess(Object msg) {
                LogUtils.i("setDeleteNetworkListener onSucceed = " + msg);
            }
            @Override
            public void onFailure() {
                LogUtils.i("setDeleteNetworkListener onFailure");
            }
        });
        UploadSidList();
        ActivityResultLauncher<String[]> launcher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {
            @Override
@@ -90,9 +198,11 @@
        launcher.launch(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE});
        final List<DemoBean> beans = new ArrayList<>();
        final List<DemoBean> beans = new ArrayList<DemoBean>();
        beans.add(new DemoBean("申请设备密钥"));
        beans.add(new DemoBean("入网认证"));
        beans.add(new DemoBean("搜索指定网关是否在线"));
        beans.add(new DemoBean("上报oid列表"));
        beans.add(new DemoBean("获取功能列表"));
        beans.add(new DemoBean("功能属性读取"));
        beans.add(new DemoBean("设备控制"));
@@ -100,6 +210,12 @@
        beans.add(new DemoBean("获取场景列表"));
        beans.add(new DemoBean("场景控制"));
        beans.add(new DemoBean("设备功能列表"));
        beans.add(new DemoBean("UDP发送"));
        beans.add(new DemoBean("TCP发送"));
        beans.add(new DemoBean("UDP发送,监听主题回复,带重发带回调"));
        beans.add(new DemoBean("TCP发送,监听主题回复,带重发带回调"));
        beans.add(new DemoBean("退网"));
        beans.add(new DemoBean("检测更新"));
        demoAdapter = new DemoAdapter(beans);
        rv.setAdapter(demoAdapter);
@@ -115,55 +231,93 @@
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                switch (position) {
                    case 0:
                        //申请设备密钥
                        applyDeviceSecret();
                        break;
                    case 1:
                        //入网认证
                        sendAuthenticateRequest();
                        break;
                    case 1:
                    case 2:
                        //搜索网关
                        searchGatewayBroadcast();
                        break;
                    case 2:
                       //获取功能列表
                        getFunctionList();
                        break;
                    case 3:
                        //功能属性读取
                       getFunctionAttribute();
                        //上报oid列表
                        UploadOidList();
                        break;
                    case 4:
                        //获取功能列表
                        getFunctionList();
                        break;
                    case 5:
                        //功能属性读取
                        getFunctionAttribute();
                        break;
                    case 6:
                        //设备控制
                        controlDecide();
                        break;
                    case 5:
                    case 7:
                        //读取状态
                        propertyRead();
                        break;
                    case 6:
                    case 8:
                        //获取场景列表
                        getSceneList();
                        break;
                    case 7:
                        //获取场景列表
                        controlScene();
                    case 9:
                        //场景控制
                        getScene();
                        break;
                    case 8:
                    case 10:
                        //功能列表
                        startDevicesListActivity();
                        break;
                    case 11:
                        //UDP发送
                        udpSend();
                        break;
                    case 12:
                        //TCP发送
                        tcpSend();
                        break;
                    case 13:
                        //UDP发送,监听主题回复,带重发带回调
                        udpSendWithCallback();
                        break;
                    case 14:
                        //TCP发送,监听主题回复,带重发带回调
                        tcpSendWithCallback();
                        break;
                    case 15:
                        //退网
                        deleteNetwork();
                        break;
                    case 16:
                        //检测更新
                        checkAppVersion();
                        break;
                }
            }
        });
    }
    public void showToast(String text) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }
    void selectnetwork() {
        HDLLink.getInstance().selectNetwork("wlan0");
    }
    /**
     * 检测是否认证过
     */
    void checkIfCertified(){
    void checkIfCertified() {
        boolean isCertified = HDLLink.getInstance().checkIfCertified();
        String mes = isCertified?"已经认证过":"未认证";
        String mes = isCertified ? "已经认证过" : "未认证";
        showToast(mes);
        tv.setText(mes);
    }
@@ -171,11 +325,16 @@
    /**
     * 注册所有主题数据的监听
     */
    void registerAllTopicsListener(){
    void registerAllTopicsListener() {
        allTopicsListener = new EventListener() {
            @Override
            public void onMessage(Object msg) {
                LinkResponse response = (LinkResponse)msg;
                LinkResponse response = (LinkResponse) msg;
                Log.i("onMessage", "response.getTopic():" + response.getTopic());
                if ("/user/all/custom/gateway/broadcast_reply".equals(response.getTopic())) {
                    localSecret.setText("密钥:" + HDLLinkConfig.getInstance().getLocalSecret());
                }
            }
        };
        HDLLink.getInstance().registerAllTopicsListener(allTopicsListener);
@@ -183,20 +342,21 @@
    /**
     * 处理收到的主题
     *
     * @param response
     */
    private void handleLinkResponse(LinkResponse response){
    private void handleLinkResponse(LinkResponse response) {
        //网关搜索回复
        if(response.getTopic().contains("/user/all/custom/gateway/search_reply")){
        if (response.getTopic().contains("/user/all/custom/gateway/search_reply")) {
            String data = response.getData();
            if (!TextUtils.isEmpty(data)) {
                Log.i("handleLinkResponse", "data:"+data);
                Log.i("handleLinkResponse", "data:" + data);
                final BaseLocalResponse<GatewaySearchBean> bean = GsonConvert.getGson().fromJson(data, new TypeToken<BaseLocalResponse<GatewaySearchBean>>() {
                }.getType());
                GatewaySearchBean searchBean = bean.getObjects();
                Log.i("handleLinkResponse", "GatewaySearchBean: "+searchBean.getGatewayId());
                Log.i("handleLinkResponse", "GatewaySearchBean: " + searchBean.getGatewayId());
            }
        }else{
        } else {
            //其它主题
        }
@@ -212,7 +372,6 @@
                }.getType());
                searchBean = response.getObjects();
            }
        }
        return searchBean;
    }
@@ -220,51 +379,63 @@
    /**
     * 移除所有主题数据的监听
     */
    void removeAllTopicsListener(){
    void removeAllTopicsListener() {
        HDLLink.getInstance().removeAllTopicsListener(allTopicsListener);
    }
    void initDeviceInfo() {
        AuthenticateRequest.AuthenticateDeviceInfoBean infoBean = new AuthenticateRequest.AuthenticateDeviceInfoBean();
        infoBean.setDeviceMAC("f2c5d8bad48f");
        infoBean.setIPMAC("f2c5d8bad48f");
        infoBean.setDeviceName("魔镜");//设备名字
        infoBean.setDeviceModel("HDLSDK");//设备型号
        infoBean.setAccessMode("WIFI");
        infoBean.setIPGateway("192.168.1.1");
        infoBean.setIPAddress("192.168.1.103");
        infoBean.setGateway_type("screen.touch");
        infoBean.setHw_version("HW2.0");
        infoBean.setFw_version("Fw1.0");
        infoBean.setOID("010105000000FE11");//每个设备oid都要不一样
        infoBean.setSid("110105000000FE08110100000011");//每个设备的sid都要不一样
        infoBean.setSupplier("JINMAOYUN");
        HDLLinkConfig.getInstance().setDeviceInfoBean(infoBean);
    }
    /**
     * 入网认证
     */
    void sendAuthenticateRequest(){
    void sendAuthenticateRequest() {
        tv.setText("开始入网认证...");
        //认证提交参数准备
        String spkStr = "ir.module";//产品spk
        String macStr = "AA000000000000BB";//设备唯一MAC地址
        String secret = "87ae414b7a853f65";//通过spk和mac提交云端认证后分配的secret
//        测试服务
//        String spkStr = "ir.module";//产品spk
//        String macStr = "AA000000000000AF";//设备唯一MAC地址
//        String secret = "44b360eb74b7ba64";//通过spk和mac提交云端认证后分配的secret
//        正式服务器
        String spkStr = "screen.mirror";//产品spk
        String macStr = "f2c5d8bad48f";//设备唯一MAC地址
//        String secret = "e186beeb7974998e";//通过spk和mac提交云端认证后分配的secret
        String mac_key = stringToMD5(stringToMD5(macStr + secret));
        String versionString = "HDL_V1.0.1";//
        String time = String.valueOf(System.currentTimeMillis());
        HDLLinkConfig.getInstance().getDeviceInfoBean().setDeviceMAC(macStr);
        //1.设置认证信息
        AuthenticateRequest.RequestBean requestBean = new AuthenticateRequest.RequestBean();
        requestBean.setMAC(macStr);
        requestBean.setSupplier("HDL");
        requestBean.setSupplier("JINMAOYUN");
        requestBean.setFirmwareVersion(versionString);
        requestBean.setHardwareModel("1956F");
        AuthenticateRequest.AuthBean authbean = new AuthenticateRequest.AuthBean();
        authbean.setSpk(spkStr);
        authbean.setMACKey(mac_key);
        authbean.setRequest(requestBean);
        //2.设置设备信息
        AuthenticateRequest.AuthenticateDeviceInfoBean infoBean = new AuthenticateRequest.AuthenticateDeviceInfoBean();
        infoBean.setDeviceMAC(macStr);
        infoBean.setIPMAC(macStr);
        infoBean.setDeviceName("红外宝");//设备名字
        infoBean.setDeviceModel("HDL");//
        infoBean.setAccessMode("WIFI");
        infoBean.setIPGateway("192.168.12.1");
        infoBean.setIPAddress(IpUtils.getIP(this));
        infoBean.setOID("010105000000FE08");
        infoBean.setSid("010105000000FE08110100000000");
//        infoBean.set
        AuthenticateRequest.VersionBean[] versionBeans = new AuthenticateRequest.VersionBean[]{new AuthenticateRequest.VersionBean("FW", versionString), new AuthenticateRequest.VersionBean("HW", "1956F")};
        infoBean.setVersions(versionBeans);
        AuthenticateRequest request = new AuthenticateRequest(IdUtils.getUUId(), time, infoBean, authbean);
        //HDLLinkConfig.getInstance().getDeviceInfoBean()这个初始化的时候要先设置好
        AuthenticateRequest request = new AuthenticateRequest(IdUtils.getUUId(), time, HDLLinkConfig.getInstance().getDeviceInfoBean(), authbean);
        HDLLink.getInstance().startAuthenticateRequest(request, new HDLLinkCallBack() {
            @Override
            public void onError(HDLLinkException e) {
@@ -307,18 +478,21 @@
     * 如果已经认证后,搜索指定网关是否在线
     * 广播搜索认证过的网关是否在线
     */
    void searchGatewayBroadcast(){
    void searchGatewayBroadcast() {
        tv.setText("搜索网关中...");
        responseTv.setText("");
        HDLLinkConfig.getInstance().getGatewayId();
        HDLLink.getInstance().searchGatewayBroadcast(new HDLAuthSocket.SearchGatewayCallBack() {
            @Override
            public void onError(HDLLinkException e) {
                tv.setText("网关不在线");
                responseTv.setText(e.getMsg());
            }
            @Override
            public void onSuccess(GatewaySearchBean gatewaySearchBean) {
                tv.setText("网关在线");
                responseTv.setText("搜索成功 网关id:"+gatewaySearchBean.getGatewayId());
                responseTv.setText("搜索成功 网关id:" + gatewaySearchBean.getGatewayId());
//                                LogUtils.i("TAG", "onSuccess: 搜索成功:"+gatewaySearchBean.getGatewayId());
            }
        });
@@ -327,7 +501,7 @@
    /**
     * 获取功能列表
     */
    void getFunctionList(){
    void getFunctionList() {
        tv.setText("获取功能列表中...");
        responseTv.setText("");
        HDLLink.getInstance().getFunctionList(new HDLLinkCallBack() {
@@ -348,8 +522,9 @@
     * 功能属性读取
     * 支持批量读取
     */
    void getFunctionAttribute(){
    void getFunctionAttribute() {
        tv.setText("功能属性读取");
        responseTv.setText("");
        List<String> sids = new ArrayList<>();
        sids.add(testLightSid);
        HDLLink.getInstance().getFunctionAttribute(sids, new HDLLinkCallBack() {
@@ -369,8 +544,9 @@
     * 读取设备状态
     * 支持批量读取
     */
    void propertyRead(){
    void propertyRead() {
        tv.setText("读取状态中...");
        responseTv.setText("");
        List<String> list = new ArrayList<>();
        list.add(testLightSid);//要读取设备的sid
        HDLLink.getInstance().propertyRead(list, new HDLLinkCallBack() {
@@ -379,6 +555,7 @@
                tv.setText("读取成功");
                responseTv.setText(data);
            }
            @Override
            public void onError(HDLLinkException e) {
                tv.setText("读取失败");
@@ -391,19 +568,21 @@
     * 控制失败
     * 回复响应code为200 代表执行成功
     */
    void controlDecide(){
    void controlDecide() {
        tv.setText("控制设备");
        responseTv.setText("");
        isOn = !isOn;
        List<DeviceControlRequest> requestList = new ArrayList<>();
        DeviceControlRequest request = new DeviceControlRequest();
        request.setSid(testLightSid);
        List<DeviceControlRequest.StatusBean>  statusBeanList= new ArrayList<>();
        DeviceControlRequest.StatusBean bean =  new DeviceControlRequest.StatusBean();
        List<DeviceControlRequest.StatusBean> statusBeanList = new ArrayList<>();
        DeviceControlRequest.StatusBean bean = new DeviceControlRequest.StatusBean();
        bean.setKey("on_off");
        bean.setValue(isOn ? "on" : "off");
        statusBeanList.add(bean);
        request.setStatus(statusBeanList);
        requestList.add(request);
        HDLLink.getInstance().propertyDown(requestList, new HDLLinkCallBack() {
            @Override
            public void onSuccess(String data) {
@@ -420,12 +599,14 @@
    /**
     * 获取场景列表
     */
    void getSceneList(){
    void getSceneList() {
        tv.setText("读取场景列表");
        responseTv.setText("");
        HDLLink.getInstance().getSceneList(new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                responseTv.setText(msg);
                Log.d("panlili", "MainActivity.java:getSceneList-----> " + msg);
            }
            @Override
@@ -437,21 +618,47 @@
    //场景列表
    // {"id":"8a5eaa143ce943b987b577df5a66759b","time_stamp":"1637040217235","objects":[{"sid":"04010560D2C7170A0A0100000000","name":"回家模式","status":"off","group":"255","delay":"0","modify_time":"1634871490"},{"sid":"04010560D2C76E0A0A0100010000","name":"离家模式","status":"off","group":"255","delay":"0","modify_time":"1634785823"}]}
    /**
     * 获取场景详情
     * 执行成功的话 响应code为200
     */
    void getScene() {
        tv.setText("场景读取");
        responseTv.setText("");
        //场景sid列表,支持批量读取
        List<String> sids = new ArrayList<>();
        sids.add("0201034C50B53F0A0A0100011100");
        sids.add("0201037F50B53F0A0A0100021100");
        HDLLink.getInstance().getScene(sids, new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                responseTv.setText(msg);
            }
            @Override
            public void onError(HDLLinkException e) {
                responseTv.setText(e.getMsg());
            }
        });
    }
    /**
     * 控制场景
     * 执行成功的话 响应code为200
     */
    void controlScene(){
    void controlScene() {
        tv.setText("场景控制");
        responseTv.setText("");
        //控制场景sid列表,支持批量控制
        List<String> sids = new ArrayList<>();
        isOn = !isOn;
        if(isOn){
        if (isOn) {
            sids.add("04010560D2C7170A0A0100000000");
        }else{
        } else {
            sids.add("04010560D2C76E0A0A0100010000");
        }
        HDLLink.getInstance().controlScene(sids,new HDLLinkCallBack() {
        HDLLink.getInstance().controlScene(sids, new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                responseTv.setText(msg);
@@ -465,8 +672,215 @@
    }
    void startDevicesListActivity(){
    void startDevicesListActivity() {
        Intent intent = new Intent(this, DevicesListActivity.class);
        startActivity(intent);
    }
    /**
     * TCP发送 只发一次,不监听回复,不重发
     */
    private void tcpSend() {
        tv.setText("TCP发送 只发一次,不监听回复,不重发");
        responseTv.setText("");
        String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId());
        String bodyStr = getPropertyDownBodyStr();
        HDLLink.getInstance().tcpSendMsg(propertyDownTopic, bodyStr);
    }
    /**
     * UDP只发一次,不监听回复,不重发
     */
    private void udpSend() {
        tv.setText("UDP发送 只发一次,不监听回复,不重发");
        responseTv.setText("");
        String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId());
        String bodyStr = getPropertyDownBodyStr();
        HDLLink.getInstance().udpSendMsg(propertyDownTopic, bodyStr);
    }
    private String getPropertyDownBodyStr() {
        isOn = !isOn;
        List<DeviceControlRequest> requestList = new ArrayList<>();
        DeviceControlRequest request = new DeviceControlRequest();
        request.setSid(testLightSid);
        List<DeviceControlRequest.StatusBean> statusBeanList = new ArrayList<>();
        DeviceControlRequest.StatusBean bean = new DeviceControlRequest.StatusBean();
        bean.setKey("on_off");
        bean.setValue(isOn ? "on" : "off");
        statusBeanList.add(bean);
        request.setStatus(statusBeanList);
        requestList.add(request);
        String time = String.valueOf(System.currentTimeMillis());
        final BaseLocalResponse<List<DeviceControlRequest>> data = new BaseLocalResponse<>();
        data.setId(IdUtils.getUUId());
        data.setTime_stamp(time);
        data.setObjects(requestList);
        return GsonConvert.getGson().toJson(data);
    }
    /**
     * UDP发送,监听主题回复,带重发带回调
     */
    private void udpSendWithCallback() {
        tv.setText("UDP发送,监听主题回复,带重发带回调");
        responseTv.setText("");
        String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId());
        String bodyStr = getPropertyDownBodyStr();
        HDLLink.getInstance().udpSendMsg(propertyDownTopic, bodyStr, new HDLLinkResponseCallBack() {
            @Override
            public void onSuccess(LinkResponse msg) {
                Log.i("udpSendWithCallback", "udpSendWithCallback");
                responseTv.setText(GsonConvert.getGson().toJson(msg));
            }
            @Override
            public void onError(HDLLinkException e) {
                responseTv.setText(e.getMsg());
            }
        });
    }
    /**
     * TCP发送,监听主题回复,带重发带回调
     */
    private void tcpSendWithCallback() {
        tv.setText("TCP发送,监听主题回复,带重发带回调");
        responseTv.setText("");
        String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId());
        String bodyStr = getPropertyDownBodyStr();
        HDLLink.getInstance().tcpSendMsg(propertyDownTopic, bodyStr, new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                Log.i("tcpSendWithCallback", "tcpSendWithCallback");
                responseTv.setText(msg);
            }
            @Override
            public void onError(HDLLinkException e) {
                responseTv.setText(e.getMsg());
            }
        });
    }
    private void deleteNetwork() {
        tv.setText("退网");
        responseTv.setText("");
        String oid = HDLLinkConfig.getInstance().getDeviceInfoBean().getOID();
        HDLLink.getInstance().deleteNetwork(oid, false, new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                responseTv.setText(msg);
                //退网成功,删除数据
            }
            @Override
            public void onError(HDLLinkException e) {
                responseTv.setText(e.getMsg());
            }
        });
    }
    /**
     * 上报oid列表
     */
    private void UploadOidList() {
        tv.setText("上报oid列表...");
        responseTv.setText("");
        //1.设置设备信息
        AuthenticateRequest.AuthenticateDeviceInfoBean authenticateDeviceInfoBean = HDLLinkConfig.getInstance().getDeviceInfoBean();
        ListUploadRequest request = new ListUploadRequest();
        request.setOid(authenticateDeviceInfoBean.getOID());
        request.setDevice_name(authenticateDeviceInfoBean.getDeviceName());
        request.setDevice_model(authenticateDeviceInfoBean.getDeviceModel());
        request.setDevice_mac(authenticateDeviceInfoBean.getDeviceMAC());
        request.setSrc("LINK");//来自哪个驱动,如Link设备没有则填"LINK"
        request.setFrom(authenticateDeviceInfoBean.getOID());//来自哪个网关,如果时Link网络设备,则填自身oid;如果是驱动则不需要此字段
        HDLLink.getInstance().UploadOidList(request, new HDLLinkCallBack() {
            @Override
            public void onError(HDLLinkException error) {
                tv.setText(error.getMsg());
            }
            @Override
            public void onSuccess(String data) {
                tv.setText("上报oid列表成功");
                responseTv.setText(data);
            }
        });
    }
    private void UploadSidList() {
        tv.setText("上报Sid列表...");
        responseTv.setText("");
        //1.设置设备信息
        AuthenticateRequest.AuthenticateDeviceInfoBean authenticateDeviceInfoBean = HDLLinkConfig.getInstance().getDeviceInfoBean();
        ListSidRequest request = new ListSidRequest();
        request.setOid(authenticateDeviceInfoBean.getOID());
        request.setName("sid名称");
        request.setOmodel(authenticateDeviceInfoBean.getDeviceModel());
        request.setSid("12345678");//当前sid的
        request.setSpk("light.switch");//当前spk名称,根据实际设备来定义
        request.setFrom(authenticateDeviceInfoBean.getOID());//来自哪个网关,如果时Link网络设备,则填自身oid;如果是驱动则不需要此字段
        AttributesRequest attributes = new AttributesRequest();
        attributes.setKey("on_off");
        attributes.setData_type("string");
        List<Object> valueList = new ArrayList<>();
        valueList.add("on");
        valueList.add("off");
        attributes.setValue(valueList);
        //添加支持的属性列表
        List<AttributesRequest> attributesList = new ArrayList<>();
        request.setAttributes(attributesList);
        List<ListSidRequest> sidRequestList = new ArrayList<>();
        sidRequestList.add(request);
        HDLLink.getInstance().UploadSidList(sidRequestList, new HDLLinkCallBack() {
            @Override
            public void onError(HDLLinkException error) {
                tv.setText(error.getMsg());
            }
            @Override
            public void onSuccess(String data) {
                tv.setText("上报sid列表成功");
                responseTv.setText(data);
            }
        });
    }
    /**
     * 获取APP版本
     *
     * @param context
     * @return
     */
    public static String getAppVersionName(Context context) {
        String versionName = "0";
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            versionName = packageInfo.versionName;
            if (TextUtils.isEmpty(versionName)) {
                versionName = "0";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionName;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        removeAllTopicsListener();
    }
}