562935844@qq.com
2022-05-13 a3f9e5ce8dce3d86a60a79b85ce44424a612c6fa
HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLSocket.java
@@ -1,8 +1,5 @@
package com.hdl.sdk.connect.socket;
import android.text.TextUtils;
import android.util.Log;
import com.google.gson.JsonObject;
import com.hdl.sdk.common.config.TopicConstant;
import com.hdl.sdk.common.event.EventDispatcher;
@@ -11,6 +8,7 @@
import com.hdl.sdk.common.exception.HDLLinkException;
import com.hdl.sdk.common.utils.IdUtils;
import com.hdl.sdk.common.utils.LogUtils;
import com.hdl.sdk.common.utils.TextUtils;
import com.hdl.sdk.common.utils.ThreadToolUtils;
import com.hdl.sdk.common.utils.gson.GsonConvert;
import com.hdl.sdk.connect.bean.request.BroadcastRequest;
@@ -32,6 +30,8 @@
import com.hdl.sdk.socket.codec.MessagePipeLine;
import com.hdl.sdk.socket.listener.ConnectStatusListener;
import com.hdl.sdk.socket.listener.SendListener;
import org.apache.commons.lang3.StringEscapeUtils;
import java.util.ArrayList;
import java.util.List;
@@ -110,7 +110,7 @@
        }).start();
    }
//    private static class SingletonInstance {
    //    private static class SingletonInstance {
//    }
    private static final HDLSocket instance = new HDLSocket();
@@ -119,8 +119,9 @@
    }
    SocketOptions options;
    private SocketOptions getTcpOptions() {
        if(null!=options){
        if (null != options) {
            return options;
        }
        options = new SocketOptions();
@@ -243,6 +244,7 @@
            final BaseLocalResponse<List<FunctionAttributeRequest>> data = new BaseLocalResponse<>();
            data.setId(IdUtils.getUUId());
            data.setTime_stamp(time);
            List<FunctionAttributeRequest> list = new ArrayList<>();
            for (String s : sids) {
@@ -423,13 +425,13 @@
            throw new RuntimeException("请搜索网关");
        }
        //如果没有初始化,或者网关IP更改了,就重新初始化
        if (tcpBoot == null||!getTcpOptions().getIp().equals(getTcpIp())) {
        if (tcpBoot == null || !getTcpOptions().getIp().equals(getTcpIp())) {
            tcpBoot = TcpClient.init(getTcpIp(), getTcpPort(), getTcpOptions());
            tcpBoot.SetHeartbeat(new IHeartbeat() {
                @Override
                public void heartbeat() {
                    if(HDLLinkConfig.getInstance().getDeviceInfoBean()==null||HDLLinkConfig.getInstance().getDeviceInfoBean().getOID()==null) {
                    if (HDLLinkConfig.getInstance().getDeviceInfoBean() == null || HDLLinkConfig.getInstance().getDeviceInfoBean().getOID() == null) {
                        LogUtils.i("DeviceInfoBean为空,需要设置才能正常心跳");
                        return;
                    }
@@ -493,8 +495,9 @@
    }
    /**
     *  场景控制
     * @param sids 场景sid列表
     * 场景控制
     *
     * @param sids     场景sid列表
     * @param callBack 回调
     */
    public void controlScene(List<String> sids, HDLLinkCallBack callBack) {
@@ -584,8 +587,8 @@
    /**
     * 通用TCP发送指令 只发一次,不监听回复,不重发
     *
     * @param topic    发送数据
     * @param bodyStr  回复的主题
     * @param topic   发送数据
     * @param bodyStr 回复的主题
     */
    public void tcpSendMsg(String topic, String bodyStr) {
        try {
@@ -596,7 +599,7 @@
            LinkRequest message = new LinkRequest(topic, bodyStr);
            getTcp().sendMsg(message.getSendBytes());
        } catch (Exception e) {
            LogUtils.e("tcpSendMsg", "发送失败 :"+e.getMessage());
            LogUtils.e("tcpSendMsg", "发送失败 :" + e.getMessage());
        }
    }
@@ -607,7 +610,7 @@
    public void sendMsg(byte[] data, String eventTag, HDLLinkCallBack callBack, SendListener sendListener) {
        try {
            if(eventTag!=null) {
            if (eventTag != null) {
                final AtomicInteger sendCount = new AtomicInteger(0);
                final ScheduledExecutorService threadPool = ThreadToolUtils.getInstance().newScheduledThreadPool(1);
@@ -615,12 +618,14 @@
                    @Override
                    public void onMessage(Object msg) {
                        if (msg instanceof LinkResponse) {
                            LogUtils.i("sendMsg onSuccess");
                            LogUtils.i("sendMsg onSuccess" + StringEscapeUtils.unescapeJava(msg.toString()));
//                            LogUtils.i("sendMsg onSuccess" + msg);
                            threadPool.shutdownNow();
                            LogUtils.i("sendMsg eventListener remove");
                            EventDispatcher.getInstance().remove(eventTag, this);
                            if (callBack != null) {
                                callBack.onSuccess(msg.toString());
                                callBack.onSuccess(StringEscapeUtils.unescapeJava(msg.toString()));
//                                callBack.onSuccess(msg.toString());
                            }
                        }
                    }
@@ -644,6 +649,8 @@
                                    }
                                }
                            });
                            LogUtils.i("HDLSocket sendMsg onError 发送失败");
                        }
                    }
                }, 1000, 500, TimeUnit.MILLISECONDS);
@@ -675,6 +682,8 @@
                    }
                }
            });
            LogUtils.i("HDLSocket sendMsg onError 发送失败");
        }
    }