panlili2024
2025-03-05 db29d92c6974d50f0ebb9328834c44a69c6054bb
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,43 +21,45 @@
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.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.hdl.hdlsdk.bean.FunctionBean;
import com.hdl.hdlsdk.bean.RoomFunctionBean;
import com.hdl.hdlsdk.bean.SceneBean;
import com.hdl.hdlsdk.bean.SceneDetailBean;
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.IdUtils;
import com.hdl.sdk.common.utils.IpUtils;
import com.hdl.sdk.common.utils.LogUtils;
import com.hdl.sdk.common.utils.SPUtils;
import com.hdl.sdk.common.utils.gson.GsonConvert;
import com.hdl.sdk.connect.HDLLink;
import com.hdl.sdk.connect.bean.LinkRequest;
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.BindInfoBean;
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.cloud.bean.GatewayInfo;
import com.hdl.sdk.connect.cloud.listener.GatewayListener;
import com.hdl.sdk.connect.cloud.listener.SibichiListener;
import com.hdl.sdk.connect.cloud.bean.AiLoginInfo;
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 com.hdl.sdk.sourceos.utils.SPKey;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
@@ -53,8 +68,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";
@@ -62,28 +75,174 @@
    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 = "020101A19B485D02020100010101";
    private String secret = "";
    private List<FunctionBean> devicesList = new ArrayList<>();
    private List<FunctionBean> roomDevicesList = new ArrayList<>();
    private List<SceneBean> sceneList = new ArrayList<>();
    private List<SceneDetailBean> sceneDetailList = new ArrayList<>();
    private List<SceneDetailBean> roomSceneList = new ArrayList<>();
    private GatewayInfo gatewayInfo;
    @Override
    protected void onDestroy() {
        super.onDestroy();
        removeAllTopicsListener();
    void applyDeviceSecret() {
        tv.setText("开始申请设备密钥...");
        responseTv.setText("");
        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().initCloud();
        HDLLink.getInstance().applyDeviceSecret(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 appCode = "1697150870315999233";//appCode
        //调用云端接口需初始化HDLLink.getInstance().initCloud();
        HDLLink.getInstance().checkAppVersion(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());
            }
        });
    }
    void getSibichiToken() {
        tv.setText("获取思必驰token...");
        responseTv.setText("");
        BindInfoBean bindInfoBean = (BindInfoBean) SPUtils.getSerializableEntity(SPKey.BIND_HOME_INFO);
        String clientId = "4ED634B5A7AD97A770A52AC00FF43805";//思必驰clientId
        //调用云端接口需初始化HDLLink.getInstance().initCloud();
        HDLLink.getInstance().getSibichiToken(bindInfoBean.getHomeId(), clientId, new SibichiListener() {
            @Override
            public void onSuccess(AiLoginInfo info) {
                tv.setText("思必驰token");
                responseTv.setText(info.toString());
            }
            @Override
            public void onError(HDLException e) {
                tv.setText("获取思必驰token失败");
                responseTv.setText(e.getMsg());
            }
        });
    }
    void syncMainGateway() {
        tv.setText("获取主网关信息...");
        responseTv.setText("");
        BindInfoBean bindInfoBean = (BindInfoBean) SPUtils.getSerializableEntity(SPKey.BIND_HOME_INFO);
        if (bindInfoBean == null) {
            Toast.makeText(this, "请先扫码绑定住宅!", Toast.LENGTH_SHORT).show();
            return;
        }
        //调用云端接口需初始化HDLLink.getInstance().initCloud();
        HDLLink.getInstance().syncMainGateway(bindInfoBean.getHomeId(), new GatewayListener() {
            @Override
            public void onSuccess(GatewayInfo info) {
                tv.setText("获取主网关信息");
                responseTv.setText(info.toString());
                if (info != null) {
                    HDLLinkConfig.getInstance().setGatewayId(info.getGatewayId());
                }
            }
            @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();
        //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
@@ -94,77 +253,81 @@
        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("获取功能列表"));
        beans.add(new DemoBean("功能属性读取"));
        beans.add(new DemoBean("上报oid列表"));
        beans.add(new DemoBean("获取设备列表"));
        beans.add(new DemoBean("设备功能属性读取"));
        beans.add(new DemoBean("设备控制"));
        beans.add(new DemoBean("读取状态"));
        beans.add(new DemoBean("获取场景列表"));
        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("Source功能支持"));
        beans.add(new DemoBean("获取房间列表"));
        beans.add(new DemoBean("获取房间设备列表"));
        beans.add(new DemoBean("获取房间场景列表"));
        beans.add(new DemoBean("获取自动化列表"));
        beans.add(new DemoBean("⾃动化启⽤禁⽤"));
        beans.add(new DemoBean("生成二维码绑定住宅"));
        beans.add(new DemoBean("获取思必驰token"));
        beans.add(new DemoBean("获取网关信息"));
        demoAdapter = new DemoAdapter(beans);
        rv.setAdapter(demoAdapter);
//        final SocketOptions options = new SocketOptions();
//
//        MessagePipeLine pipeLine = new MessagePipeLine();
//        options.setHandleMessage(pipeLine);
//        options.setEnabledHeartbeat(false);
        demoAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            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();
                        //startDevicesListActivity();
                        break;
                    case 5:
                        //设备功能属性读取
                        getFunctionAttribute();
                        break;
                    case 6:
                        //设备控制
                        controlDecide();
                        break;
                    case 5:
                    case 7:
                        //读取状态
                        propertyRead();
                        break;
                    case 6:
                    case 8:
                        //获取场景列表
                        getSceneList();
                        break;
                    case 7:
                        //获取场景列表
                        controlScene();
                        break;
                    case 8:
                        //功能列表
                        startDevicesListActivity();
                        break;
                    case 9:
                        //UDP发送
                        udpSend();
                        //场景详情
                        getSceneDetail();
                        break;
                    case 10:
                        //TCP发送
                        tcpSend();
                        //场景控制
                        controlScene();
                        break;
                    case 11:
                        //UDP发送,监听主题回复,带重发带回调
@@ -174,6 +337,46 @@
                        //TCP发送,监听主题回复,带重发带回调
                        tcpSendWithCallback();
                        break;
                    case 13:
                        //退网
                        deleteNetwork();
                        break;
                    case 14:
                        //Source功能支持
                        startSourceTestActivity();
                        break;
                    case 15:
                        //获取房间列表
                        getRoomList();
                        break;
                    case 16:
                        //获取房间设备列表
                        getRoomDeviceList();
                        break;
                    case 17:
                        //获取房间场景列表
                        getRoomSceneList();
                        break;
                    case 18:
                        //获取自动化列表
                        getLogicList();
                        break;
                    case 19:
                        //⾃动化启⽤禁⽤
                        editEnableLogic();
                        break;
                    case 20:
                        //生成二维码绑定住宅
                        startSourceBindActivity();
                        break;
                    case 21:
                        //获取思必驰token
                        getSibichiToken();
                        break;
                    case 22:
                        //获取网关信息
                        syncMainGateway();
                        break;
                }
            }
        });
@@ -182,6 +385,10 @@
    public void showToast(String text) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }
    void selectnetwork() {
        HDLLink.getInstance().selectNetwork("wlan0");
    }
    /**
@@ -202,7 +409,11 @@
            @Override
            public void onMessage(Object msg) {
                LinkResponse response = (LinkResponse) msg;
                handleLinkResponse(response);
                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);
@@ -251,22 +462,37 @@
        HDLLink.getInstance().removeAllTopicsListener(allTopicsListener);
    }
    void initDeviceInfo()
    {
    void initDeviceInfo() {
        AuthenticateRequest.AuthenticateDeviceInfoBean infoBean = new AuthenticateRequest.AuthenticateDeviceInfoBean();
        infoBean.setDeviceMAC("123456789");
        infoBean.setIPMAC("12345678900");
        infoBean.setDeviceName("音乐播放器");//设备名字
        infoBean.setDeviceModel("MCLog.431");//设备型号
        infoBean.setDeviceMAC("f2c5d8bad48f");
        infoBean.setIPMAC("f2c5d8bad48f");
        infoBean.setDeviceName("魔镜");//设备名字
        infoBean.setDeviceModel("HDLSDK");//设备型号
        infoBean.setAccessMode("WIFI");
        infoBean.setIPGateway("192.168.88.1");
        infoBean.setIPAddress("192.168.88.100");
        infoBean.setGateway_type("music.standard");
        infoBean.setIPGateway("192.168.31.1");
        infoBean.setIPAddress("192.168.31.103");
        infoBean.setGateway_type("screen.touch");
        infoBean.setHw_version("HW2.0");
        infoBean.setFw_version("Fw1.0");
        infoBean.setOID("010105000000FE08");
        infoBean.setSid("010105000000FE08110100000000");
        infoBean.setOID("010105000000FE11");//每个设备oid都要不一样
        infoBean.setSid("110105000000FE08110100000011");//每个设备的sid都要不一样
        infoBean.setSupplier("JINMAOYUN");
        HDLLinkConfig.getInstance().setDeviceInfoBean(infoBean);
    }
    void initLink() {
        //step1:先生成二维码,用onpro扫码绑定住宅获取住宅信息
        BindInfoBean bindInfoBean = (BindInfoBean) SPUtils.getSerializableEntity(SPKey.BIND_HOME_INFO);
        if (bindInfoBean != null) {
            HDLLinkConfig.getInstance().setHomeId(bindInfoBean.getHomeId());
            HDLLinkConfig.getInstance().setLocalSecret(bindInfoBean.getLocalSecret());
        }
        //step2:再获取网关信息
        if (gatewayInfo != null) {
            HDLLinkConfig.getInstance().setGatewayId(gatewayInfo.getGatewayId());//当前主网关id
        }
    }
    /**
@@ -276,48 +502,27 @@
        tv.setText("开始入网认证...");
        //认证提交参数准备
//        //测试服务
//        String spkStr = "ir.module";//产品spk
//        String macStr = "AA000000000000BB";//设备唯一MAC地址
//        String secret = "87ae414b7a853f65";//通过spk和mac提交云端认证后分配的secret
        //正式服务器
        String spkStr = "screen.mirror";//产品spk
        String macStr = "AA000000000000AC";//设备唯一MAC地址
        String secret = "ee62124c151b737c";//通过spk和mac提交云端认证后分配的secret
        String macStr = "f2c5d8bad48f";//设备唯一MAC地址
        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("raysgem");
        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.88.1");
        infoBean.setIPAddress(IpUtils.getIP(this));//根据真实的IP地址填写
        infoBean.setOID("010105000000FE08");
        infoBean.setSid("010105000000FE08110100000000");
        infoBean.setHw_version("1.0");
        infoBean.setFw_version("1.0");
//        infoBean.set
        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) {
@@ -348,8 +553,7 @@
        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10)
                hex.append("0");
            if ((b & 0xFF) < 0x10) hex.append("0");
            hex.append(Integer.toHexString(b & 0xFF));
        }
@@ -362,6 +566,8 @@
     */
    void searchGatewayBroadcast() {
        tv.setText("搜索网关中...");
        responseTv.setText("");
        HDLLinkConfig.getInstance().getGatewayId();
        HDLLink.getInstance().searchGatewayBroadcast(new HDLAuthSocket.SearchGatewayCallBack() {
            @Override
            public void onError(HDLLinkException e) {
@@ -379,10 +585,10 @@
    }
    /**
     * 获取功能列表
     * 获取设备列表
     */
    void getFunctionList() {
        tv.setText("获取功能列表中...");
        tv.setText("获取设备列表中...");
        responseTv.setText("");
        HDLLink.getInstance().getFunctionList(new HDLLinkCallBack() {
            @Override
@@ -392,20 +598,45 @@
            @Override
            public void onSuccess(String data) {
                tv.setText("获取功能列表成功");
                tv.setText("获取设备列表成功");
                responseTv.setText(data);
                handelFunctionList(data);
            }
        });
    }
    void handelFunctionList(String data) {
        try {
            final LinkResponse linkResponse = GsonConvert.getGson().fromJson(data, new TypeToken<LinkResponse>() {
            }.getType());
            final BaseLocalResponse<List<FunctionBean>> bean = GsonConvert.getGson().fromJson(linkResponse.getData(), new TypeToken<BaseLocalResponse<List<FunctionBean>>>() {
            }.getType());
            devicesList.clear();
            devicesList.addAll(bean.getObjects());
            Log.d(TAG, "handelFunList-----> " + devicesList.toString());
        } catch (Exception e) {
            Log.e(TAG, "handelFunList: " + e.getMessage());
        }
    }
    /**
     * 功能属性读取
     * 设备功能属性读取
     * 支持批量读取
     */
    void getFunctionAttribute() {
        tv.setText("功能属性读取");
        tv.setText("设备功能属性读取");
        responseTv.setText("");
        List<String> sids = new ArrayList<>();
        sids.add(testLightSid);
        if (devicesList.size() != 0) {
            sids.add(devicesList.get(0).getSid());
        } else {
            sids.add(testLightSid);
        }
        HDLLink.getInstance().getFunctionAttribute(sids, new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
@@ -425,8 +656,13 @@
     */
    void propertyRead() {
        tv.setText("读取状态中...");
        responseTv.setText("");
        List<String> list = new ArrayList<>();
        list.add(testLightSid);//要读取设备的sid
        if (devicesList.size() != 0) {
            list.add(devicesList.get(0).getSid());
        } else {
            list.add(testLightSid);//要读取设备的sid
        }
        HDLLink.getInstance().propertyRead(list, new HDLLinkCallBack() {
            @Override
            public void onSuccess(String data) {
@@ -448,10 +684,15 @@
     */
    void controlDecide() {
        tv.setText("控制设备");
        responseTv.setText("");
        isOn = !isOn;
        List<DeviceControlRequest> requestList = new ArrayList<>();
        DeviceControlRequest request = new DeviceControlRequest();
        request.setSid(testLightSid);
        if (devicesList.size() != 0) {
            request.setSid(devicesList.get(0).getSid());
        } else {
            request.setSid(testLightSid);//要读取设备的sid
        }
        List<DeviceControlRequest.StatusBean> statusBeanList = new ArrayList<>();
        DeviceControlRequest.StatusBean bean = new DeviceControlRequest.StatusBean();
        bean.setKey("on_off");
@@ -459,6 +700,7 @@
        statusBeanList.add(bean);
        request.setStatus(statusBeanList);
        requestList.add(request);
        HDLLink.getInstance().propertyDown(requestList, new HDLLinkCallBack() {
            @Override
            public void onSuccess(String data) {
@@ -477,10 +719,13 @@
     */
    void getSceneList() {
        tv.setText("读取场景列表");
        responseTv.setText("");
        HDLLink.getInstance().getSceneList(new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                responseTv.setText(msg);
                handelSceneList(msg);
            }
            @Override
@@ -490,8 +735,73 @@
        });
    }
    void handelSceneList(String data) {
        try {
            final LinkResponse linkResponse = GsonConvert.getGson().fromJson(data, new TypeToken<LinkResponse>() {
            }.getType());
            final BaseLocalResponse<List<SceneBean>> bean = GsonConvert.getGson().fromJson(linkResponse.getData(), new TypeToken<BaseLocalResponse<List<SceneBean>>>() {
            }.getType());
            sceneList.clear();
            sceneList.addAll(bean.getObjects());
            Log.d(TAG, "handelSceneList-----> " + sceneList.toString());
        } catch (Exception e) {
            Log.e(TAG, "handelSceneList: " + e.getMessage());
        }
    }
    //场景列表
    // {"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"}]}
//    {"id":"98a683eca5384f2db2ca1ed688953439","time_stamp":"1740024067320","code":"0","msg":"Success","objects":[{"sid":"0201039965BF5D0A0A0100011100","name":"场景-1","status":"off","group":"255","delay":"0","modify_time":"1735293636","local":"false"},{"sid":"030105016B84AA0A0A0100020000","name":"大厅灯开","status":"off","group":"255","delay":"0","modify_time":"1740023916","local":"false"},{"sid":"030105016D14240A0A0100030000","name":"大厅灯关","status":"off","group":"255","delay":"0","modify_time":"1740023969","local":"false"},{"sid":"020103D465BF5D0A0A0100041100","name":"场景-4","status":"off","group":"255","delay":"0","modify_time":"1735200096","local":"false"}]}
    /**
     * 获取场景详情
     * 执行成功的话 响应code为200
     */
    void getSceneDetail() {
        tv.setText("场景读取");
        responseTv.setText("");
        //场景sid列表,支持批量读取
        if (sceneList != null && sceneList.size() != 0) {
            List<String> sids = new ArrayList<>();
            for (SceneBean s : sceneList) {
                sids.add(s.getSid());//遍历所有场景拿到详情信息
            }
            HDLLink.getInstance().getScene(sids, new HDLLinkCallBack() {
                @Override
                public void onSuccess(String msg) {
                    responseTv.setText(msg);
                    handelSceneDetailList(msg);
                }
                @Override
                public void onError(HDLLinkException e) {
                    responseTv.setText(e.getMsg());
                }
            });
        } else {
            Toast.makeText(MainActivity.this, "请先获取场景列表", Toast.LENGTH_SHORT).show();
        }
    }
    void handelSceneDetailList(String data) {
        try {
            final LinkResponse linkResponse = GsonConvert.getGson().fromJson(data, new TypeToken<LinkResponse>() {
            }.getType());
            final BaseLocalResponse<List<SceneDetailBean>> bean = GsonConvert.getGson().fromJson(linkResponse.getData(), new TypeToken<BaseLocalResponse<List<SceneDetailBean>>>() {
            }.getType());
            sceneDetailList.clear();
            sceneDetailList.addAll(bean.getObjects());
            Log.d(TAG, "handelSceneDetailList-----> " + sceneDetailList.toString());
        } catch (Exception e) {
            Log.e(TAG, "handelSceneDetailList: " + e.getMessage());
        }
    }
    /**
     * 控制场景
@@ -499,13 +809,14 @@
     */
    void controlScene() {
        tv.setText("场景控制");
        responseTv.setText("");
        //控制场景sid列表,支持批量控制
        List<String> sids = new ArrayList<>();
        isOn = !isOn;
        if (isOn) {
            sids.add("04010560D2C7170A0A0100000000");
            sids.add("030105016D14240A0A0100030000");
        } else {
            sids.add("04010560D2C76E0A0A0100010000");
            sids.add("030105016B84AA0A0A0100020000");
        }
        HDLLink.getInstance().controlScene(sids, new HDLLinkCallBack() {
            @Override
@@ -520,9 +831,166 @@
        });
    }
    /**
     * 获取房间列表
     */
    void getRoomList() {
        tv.setText("读取房间列表");
        responseTv.setText("");
        HDLLink.getInstance().getRoomList(new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                responseTv.setText(msg);
            }
            @Override
            public void onError(HDLLinkException e) {
                responseTv.setText(e.getMsg());
            }
        });
    }
    /**
     * 获取房间设备列表
     * 先获取房间绑定关系列表,通过sid和设备列表匹配筛选
     */
    void getRoomDeviceList() {
        tv.setText("获取房间设备列表");
        responseTv.setText("");
        //房间uid列表,支持批量读取
        List<String> uid = new ArrayList<>();
        uid.add("64a4fc6f-4eae-4d1d-82e4-40140c802c48");
        HDLLink.getInstance().getRoomBindList(uid, new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                if (!TextUtils.isEmpty(msg)) {
                    handelRoomFunctionList(msg);
                }
            }
            @Override
            public void onError(HDLLinkException e) {
                responseTv.setText(e.getMsg());
            }
        });
    }
    void handelRoomFunctionList(String data) {
        try {
            final LinkResponse linkResponse = GsonConvert.getGson().fromJson(data, new TypeToken<LinkResponse>() {
            }.getType());
            final BaseLocalResponse<List<RoomFunctionBean>> bean = GsonConvert.getGson().fromJson(linkResponse.getData(), new TypeToken<BaseLocalResponse<List<RoomFunctionBean>>>() {
            }.getType());
            List<RoomFunctionBean> roomFunctionBeans = bean.getObjects();
            List<RoomFunctionBean.Functions> functions = roomFunctionBeans.get(0).getFunctions();
            roomDevicesList.clear();
            if (devicesList != null && devicesList.size() != 0) {
                for (int i = 0; i < functions.size(); i++) {
                    for (int j = 0; j < devicesList.size(); j++) {
                        //与设备列表的数据匹配,sid对应
                        if (functions.get(i).getSid().equals(devicesList.get(j).getSid())) {
                            roomDevicesList.add(devicesList.get(j));
                        }
                    }
                }
                responseTv.setText(roomDevicesList.toString());
                Log.d(TAG, "handelRoomFunctionList-----> " + roomDevicesList.toString());
            } else {
                Toast.makeText(MainActivity.this, "请先获取设备列表", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            Log.e(TAG, "handelRoomFunctionList: " + e.getMessage());
        }
    }
    /**
     * 获取自动化列表
     */
    void getLogicList() {
        tv.setText("读取自动化列表");
        responseTv.setText("");
        HDLLink.getInstance().getLogicList(new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                responseTv.setText(msg);
            }
            @Override
            public void onError(HDLLinkException e) {
                responseTv.setText(e.getMsg());
            }
        });
    }
    /**
     * ⾃动化启⽤禁⽤
     */
    void editEnableLogic() {
        tv.setText("⾃动化启⽤禁⽤");
        responseTv.setText("");
        String sid = "0301050041800C161501000A0000";//自动化sid
        boolean enable = true;//true=启⽤ false=禁⽤
        HDLLink.getInstance().editEnableLogic(sid, enable, new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                responseTv.setText(msg);
            }
            @Override
            public void onError(HDLLinkException e) {
                responseTv.setText(e.getMsg());
            }
        });
    }
    /**
     * 获取房间场景列表(需要先拿到场景详情)
     */
    void getRoomSceneList() {
        tv.setText("获取房间场景列表");
        responseTv.setText("");
        String uid = "64a4fc6f-4eae-4d1d-82e4-40140c802c48";//房间uid
        roomSceneList.clear();
        if (sceneDetailList != null && sceneDetailList.size() != 0) {
            for (SceneDetailBean s : sceneDetailList) {
                List<String> roomUids = s.getUids();
                if (roomUids != null && roomUids.size() != 0) {
                    for (int i = 0; i < roomUids.size(); i++) {
                        if (uid.equals(roomUids.get(i))) {
                            roomSceneList.add(s);
                        }
                    }
                }
            }
            responseTv.setText(roomSceneList.toString());
            Log.d(TAG, "getRoomSceneList-----> " + roomSceneList.toString());
        } else {
            Toast.makeText(MainActivity.this, "请先获取场景列表详情", Toast.LENGTH_SHORT).show();
        }
    }
    void startDevicesListActivity() {
        Intent intent = new Intent(this, DevicesListActivity.class);
        startActivity(intent);
    }
    void startSourceTestActivity() {
        Intent intent = new Intent(this, SourceTestActivity.class);
        startActivity(intent);
    }
    void startSourceBindActivity() {
        Intent intent = new Intent(this, SourceBindActivity.class);
        startActivity(intent);
    }
@@ -531,6 +999,7 @@
     */
    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);
@@ -541,6 +1010,7 @@
     */
    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);
@@ -573,6 +1043,7 @@
     */
    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() {
@@ -594,6 +1065,7 @@
     */
    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() {
@@ -609,4 +1081,123 @@
            }
        });
    }
    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();
    }
}