panlili2024
2025-02-25 cc0421a5d9b1b7f19f6c8e39e4756b95345f1623
HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java
@@ -24,12 +24,15 @@
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.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.AllTopicManagerUtils;
import com.hdl.sdk.common.utils.IdUtils;
import com.hdl.sdk.common.utils.LogUtils;
import com.hdl.sdk.common.utils.gson.GsonConvert;
@@ -41,7 +44,6 @@
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;
@@ -70,20 +72,25 @@
    private EditText editText;
    boolean isOn;
    private EventListener allTopicsListener;
    private String testLightSid = "02010131D9C03D01020300010107";
    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<>();
    void applyDeviceSecret() {
        tv.setText("开始申请设备密钥...");
        responseTv.setText("");
//        //正式服务器
        String appKey = "i8hR07jzrIS";//appkey
        String appSecret = "BmnJ8RWTtaVEBk24zPPF4UMwfYu0lAWU";//appsecret
//        String appKey = "i8hR07jzrIS";//appkey
//        String appSecret = "BmnJ8RWTtaVEBk24zPPF4UMwfYu0lAWU";//appsecret
        //测试服务器
//        String appKey ="FcRyUJlLJFF";
//        String appSecret = "wz8wn75ABidx8vXcFGUotqhwFkTaYvvJ";
        String appKey = "FcRyUJlLJFF";
        String appSecret = "wz8wn75ABidx8vXcFGUotqhwFkTaYvvJ";
//        String appKey = "L2OZliZRxHc";
//        String appSecret = "aCIWSvJDOukXfx3kivsKW11x9xdR3IbV";
@@ -203,28 +210,24 @@
        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("场景控制"));
        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("检测更新"));
        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("⾃动化启⽤禁⽤"));
        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
@@ -247,11 +250,12 @@
                        UploadOidList();
                        break;
                    case 4:
                        //获取功能列表
                        //获取设备列表
                        getFunctionList();
                        //startDevicesListActivity();
                        break;
                    case 5:
                        //功能属性读取
                        //设备功能属性读取
                        getFunctionAttribute();
                        break;
                    case 6:
@@ -267,36 +271,48 @@
                        getSceneList();
                        break;
                    case 9:
                        //场景控制
                        getScene();
                        //场景详情
                        getSceneDetail();
                        break;
                    case 10:
                        //功能列表
                        startDevicesListActivity();
                        //场景控制
                        controlScene();
                        break;
                    case 11:
                        //UDP发送
                        udpSend();
                        break;
                    case 12:
                        //TCP发送
                        tcpSend();
                        break;
                    case 13:
                        //UDP发送,监听主题回复,带重发带回调
                        udpSendWithCallback();
                        break;
                    case 14:
                    case 12:
                        //TCP发送,监听主题回复,带重发带回调
                        tcpSendWithCallback();
                        break;
                    case 15:
                    case 13:
                        //退网
                        deleteNetwork();
                        break;
                    case 14:
                        //Source功能支持
                        startSourceTestActivity();
                        break;
                    case 15:
                        //获取房间列表
                        getRoomList();
                        break;
                    case 16:
                        //检测更新
                        checkAppVersion();
                        //获取房间设备列表
                        getRoomDeviceList();
                        break;
                    case 17:
                        //获取房间场景列表
                        getRoomSceneList();
                        break;
                    case 18:
                        //获取自动化列表
                        getLogicList();
                        break;
                    case 19:
                        //⾃动化启⽤禁⽤
                        editEnableLogic();
                        break;
                }
            }
@@ -390,8 +406,8 @@
        infoBean.setDeviceName("魔镜");//设备名字
        infoBean.setDeviceModel("HDLSDK");//设备型号
        infoBean.setAccessMode("WIFI");
        infoBean.setIPGateway("192.168.1.1");
        infoBean.setIPAddress("192.168.1.103");
        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");
@@ -466,8 +482,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));
        }
@@ -481,6 +496,7 @@
    void searchGatewayBroadcast() {
        tv.setText("搜索网关中...");
        responseTv.setText("");
        HDLLinkConfig.getInstance().getGatewayId();
        HDLLink.getInstance().searchGatewayBroadcast(new HDLAuthSocket.SearchGatewayCallBack() {
            @Override
            public void onError(HDLLinkException e) {
@@ -498,10 +514,10 @@
    }
    /**
     * 获取功能列表
     * 获取设备列表
     */
    void getFunctionList() {
        tv.setText("获取功能列表中...");
        tv.setText("获取设备列表中...");
        responseTv.setText("");
        HDLLink.getInstance().getFunctionList(new HDLLinkCallBack() {
            @Override
@@ -511,18 +527,37 @@
            @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);
@@ -605,7 +640,8 @@
            @Override
            public void onSuccess(String msg) {
                responseTv.setText(msg);
                Log.d("panlili", "MainActivity.java:getSceneList-----> " + msg);
                handelSceneList(msg);
            }
            @Override
@@ -615,31 +651,72 @@
        });
    }
    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 getScene() {
    void getSceneDetail() {
        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);
        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);
            @Override
            public void onError(HDLLinkException e) {
                responseTv.setText(e.getMsg());
            }
        });
                    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());
        }
    }
    /**
@@ -653,9 +730,9 @@
        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
@@ -670,12 +747,164 @@
        });
    }
    /**
     * 获取房间列表
     */
    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);
    }
    /**
     * TCP发送 只发一次,不监听回复,不重发
     */