hxb
2022-02-17 af2cbc2a24b3724e7fc3e6b42e134b21a3144c75
HDLSDK_DEMO/app/src/main/java/com/hdl/hdlsdk/MainActivity.java
@@ -37,6 +37,7 @@
import com.hdl.sdk.connect.bean.response.GatewaySearchBean;
import com.hdl.sdk.connect.callback.HDLLinkCallBack;
import com.hdl.sdk.connect.callback.HDLLinkResponseCallBack;
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;
@@ -71,6 +72,25 @@
        removeAllTopicsListener();
    }
    void initDeviceInfo()
    {
        AuthenticateRequest.AuthenticateDeviceInfoBean infoBean = new AuthenticateRequest.AuthenticateDeviceInfoBean();
        infoBean.setDeviceMAC("123456789");
        infoBean.setIPMAC("12345678900");
        infoBean.setDeviceName("音乐播放器");//设备名字
        infoBean.setDeviceModel("MCLog.431");//设备型号
        infoBean.setAccessMode("WIFI");
        infoBean.setIPGateway("192.168.88.1");
        infoBean.setIPAddress("192.168.88.100");
        infoBean.setGateway_type("music.standard");
        infoBean.setHw_version("HW2.0");
        infoBean.setFw_version("Fw1.0");
        infoBean.setOID("010105000000FE08");
        infoBean.setSid("010105000000FE08110100000000");
        HDLLinkConfig.getInstance().setDeviceInfoBean(infoBean);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
@@ -81,7 +101,7 @@
        rv.setLayoutManager(new LinearLayoutManager(this));
        checkIfCertified();
        initDeviceInfo();
        registerAllTopicsListener();
        ActivityResultLauncher<String[]> launcher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {
@@ -103,10 +123,10 @@
        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("UDP发送,不回调不重发"));
        beans.add(new DemoBean("TCP发送,不回调不重发"));
        demoAdapter = new DemoAdapter(beans);
        rv.setAdapter(demoAdapter);
@@ -130,12 +150,12 @@
                        searchGatewayBroadcast();
                        break;
                    case 2:
                       //获取功能列表
                        //获取功能列表
                        getFunctionList();
                        break;
                    case 3:
                        //功能属性读取
                       getFunctionAttribute();
                        getFunctionAttribute();
                        break;
                    case 4:
                        //设备控制
@@ -159,25 +179,24 @@
                        break;
                    case 9:
                        //UDP发送
                        udpSendWithCallback();
                        udpSend();
                        break;
                    case 10:
                        //TCP发送
                        tcpSendWithCallback();
                        tcpSend();
                        break;
                    case 11:
                        //UDP发送
                        udpSend();
                        //UDP发送,监听主题回复,带重发带回调
                        udpSendWithCallback();
                        break;
                    case 12:
                        //TCP发送
                        tcpSend();
                        //TCP发送,监听主题回复,带重发带回调
                        tcpSendWithCallback();
                        break;
                }
            }
        });
    }
    public void showToast(String text) {
@@ -187,9 +206,9 @@
    /**
     * 检测是否认证过
     */
    void checkIfCertified(){
    void checkIfCertified() {
        boolean isCertified = HDLLink.getInstance().checkIfCertified();
        String mes = isCertified?"已经认证过":"未认证";
        String mes = isCertified ? "已经认证过" : "未认证";
        showToast(mes);
        tv.setText(mes);
    }
@@ -197,11 +216,11 @@
    /**
     * 注册所有主题数据的监听
     */
    void registerAllTopicsListener(){
    void registerAllTopicsListener() {
        allTopicsListener = new EventListener() {
            @Override
            public void onMessage(Object msg) {
                LinkResponse response = (LinkResponse)msg;
                LinkResponse response = (LinkResponse) msg;
            }
        };
        HDLLink.getInstance().registerAllTopicsListener(allTopicsListener);
@@ -209,20 +228,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 {
            //其它主题
        }
@@ -238,7 +258,6 @@
                }.getType());
                searchBean = response.getObjects();
            }
        }
        return searchBean;
    }
@@ -246,23 +265,31 @@
    /**
     * 移除所有主题数据的监听
     */
    void removeAllTopicsListener(){
    void removeAllTopicsListener() {
        HDLLink.getInstance().removeAllTopicsListener(allTopicsListener);
    }
    /**
     * 入网认证
     */
    void sendAuthenticateRequest(){
    void sendAuthenticateRequest() {
        tv.setText("开始入网认证...");
        //认证提交参数准备
        String spkStr = "screen.mirror";//产品spk
        String macStr = "AA000000000000AC";//设备唯一MAC地址
        String secret = "ee62124c151b737c";//通过spk和mac提交云端认证后分配的secret
        //测试服务
        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 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();
@@ -274,23 +301,8 @@
        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));
        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) {
@@ -333,7 +345,7 @@
     * 如果已经认证后,搜索指定网关是否在线
     * 广播搜索认证过的网关是否在线
     */
    void searchGatewayBroadcast(){
    void searchGatewayBroadcast() {
        tv.setText("搜索网关中...");
        HDLLink.getInstance().searchGatewayBroadcast(new HDLAuthSocket.SearchGatewayCallBack() {
            @Override
@@ -341,10 +353,11 @@
                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());
            }
        });
@@ -353,7 +366,7 @@
    /**
     * 获取功能列表
     */
    void getFunctionList(){
    void getFunctionList() {
        tv.setText("获取功能列表中...");
        responseTv.setText("");
        HDLLink.getInstance().getFunctionList(new HDLLinkCallBack() {
@@ -374,7 +387,7 @@
     * 功能属性读取
     * 支持批量读取
     */
    void getFunctionAttribute(){
    void getFunctionAttribute() {
        tv.setText("功能属性读取");
        List<String> sids = new ArrayList<>();
        sids.add(testLightSid);
@@ -395,7 +408,7 @@
     * 读取设备状态
     * 支持批量读取
     */
    void propertyRead(){
    void propertyRead() {
        tv.setText("读取状态中...");
        List<String> list = new ArrayList<>();
        list.add(testLightSid);//要读取设备的sid
@@ -405,6 +418,7 @@
                tv.setText("读取成功");
                responseTv.setText(data);
            }
            @Override
            public void onError(HDLLinkException e) {
                tv.setText("读取失败");
@@ -417,14 +431,14 @@
     * 控制失败
     * 回复响应code为200 代表执行成功
     */
    void controlDecide(){
    void controlDecide() {
        tv.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);
@@ -446,7 +460,7 @@
    /**
     * 获取场景列表
     */
    void getSceneList(){
    void getSceneList() {
        tv.setText("读取场景列表");
        HDLLink.getInstance().getSceneList(new HDLLinkCallBack() {
            @Override
@@ -463,21 +477,22 @@
    //场景列表
    // {"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 controlScene(){
    void controlScene() {
        tv.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);
@@ -491,74 +506,92 @@
    }
    void startDevicesListActivity(){
    void startDevicesListActivity() {
        Intent intent = new Intent(this, DevicesListActivity.class);
        startActivity(intent);
    }
    /**
     * TCP发送,监听主题回复,带重发带回调
     */
    private void tcpSendWithCallback() {
        String time = String.valueOf(System.currentTimeMillis());
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("id", IdUtils.getUUId());
        jsonObject.addProperty("time_stamp", time);
        HDLLink.getInstance().tcpSendMsg(TopicConstant.GATEWAY_SEARCH, jsonObject.toString(), new HDLLinkCallBack() {
            @Override
            public void onSuccess(String data) {
                responseTv.setText(data);
            }
            @Override
            public void onError(HDLLinkException e) {
                responseTv.setText(e.getMsg());
            }
        });
    }
    /**
     * UDP发送,监听主题回复,带重发带回调
     */
    private void udpSendWithCallback() {
        String time = String.valueOf(System.currentTimeMillis());
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("id", IdUtils.getUUId());
        jsonObject.addProperty("time_stamp", time);
        HDLLink.getInstance().udpSendMsg(TopicConstant.GATEWAY_SEARCH, jsonObject.toString(), new HDLLinkResponseCallBack() {
            @Override
            public void onSuccess(LinkResponse linkResponse) {
                if(linkResponse != null){
                    responseTv.setText(linkResponse.getData());
                }
            }
            @Override
            public void onError(HDLLinkException e) {
                responseTv.setText(e.getMsg());
            }
        });
    }
    /**
     * TCP发送 只发一次,不监听回复,不重发
     */
    private void tcpSend() {
        String time = String.valueOf(System.currentTimeMillis());
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("id", IdUtils.getUUId());
        jsonObject.addProperty("time_stamp", time);
        HDLLink.getInstance().tcpSendMsg(TopicConstant.GATEWAY_SEARCH, jsonObject.toString());
        tv.setText("TCP发送 只发一次,不监听回复,不重发");
        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发送 只发一次,不监听回复,不重发");
        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());
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("id", IdUtils.getUUId());
        jsonObject.addProperty("time_stamp", time);
        HDLLink.getInstance().udpSendMsg(TopicConstant.GATEWAY_SEARCH, jsonObject.toString());
        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发送,监听主题回复,带重发带回调");
        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发送,监听主题回复,带重发带回调");
        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());
            }
        });
    }
}