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;
@@ -78,38 +78,50 @@
     * 广播自身信息给主网关
     */
    private void broadcastRequest() {
        String time = String.valueOf(System.currentTimeMillis());
        if (null == HDLLinkConfig.getInstance().getDeviceInfoBean()) {
            LogUtils.i("DeviceInfoBean为空,请设置当前对象");
            return;
        }
        BroadcastRequest request = new BroadcastRequest(IdUtils.getUUId(), time, HDLLinkConfig.getInstance().getDeviceInfoBean(), "200");
        HDLAuthSocket.getInstance().udpSendMsg(TopicConstant.BROADCAST, GsonConvert.getGson().toJson(request),true);
        HDLAuthSocket.getInstance().udpSendMsg(TopicConstant.BROADCAST, GsonConvert.getGson().toJson(request), true, new HDLLinkResponseCallBack() {
        new Thread(new Runnable() {
            @Override
            public void onSuccess(LinkResponse msg) {
                LogUtils.i("广播信息给主网关成功!");
            }
            public void run() {
                try {
                    LogUtils.i("广播设备本身信息");
            @Override
            public void onError(HDLLinkException e) {
                    String time = String.valueOf(System.currentTimeMillis());
                    if (null == HDLLinkConfig.getInstance().getDeviceInfoBean()) {
                        LogUtils.i("DeviceInfoBean为空,请设置当前对象");
                        return;
                    }
                    BroadcastRequest request = new BroadcastRequest(IdUtils.getUUId(), time, HDLLinkConfig.getInstance().getDeviceInfoBean(), "200");
                    HDLAuthSocket.getInstance().udpSendMsg(TopicConstant.BROADCAST, GsonConvert.getGson().toJson(request), true, new HDLLinkResponseCallBack() {
                        @Override
                        public void onSuccess(LinkResponse msg) {
                            LogUtils.i("广播信息给主网关成功!");
                        }
                        @Override
                        public void onError(HDLLinkException e) {
                            LogUtils.i("广播信息给主网关失败!");
                        }
                    });
                    HDLAuthSocket.getInstance().udpSendMsg(TopicConstant.BROADCAST, GsonConvert.getGson().toJson(request), true);
                    HDLAuthSocket.getInstance().udpSendMsg(TopicConstant.BROADCAST, GsonConvert.getGson().toJson(request), true);
                } catch (Exception e) {
                    LogUtils.e("广播设备本身信息失败,失败信息:" + e.getMessage());
                }
            }
        });
        HDLAuthSocket.getInstance().udpSendMsg(TopicConstant.BROADCAST, GsonConvert.getGson().toJson(request),true);
        }).start();
    }
    private static class SingletonInstance {
        private static final HDLSocket INSTANCE = new HDLSocket();
    }
    //    private static class SingletonInstance {
//    }
    private static final HDLSocket instance = new HDLSocket();
    public static HDLSocket getInstance() {
        return SingletonInstance.INSTANCE;
        return instance;
    }
    SocketOptions options;
    private SocketOptions getTcpOptions() {
        if(null!=options){
        if (null != options) {
            return options;
        }
        options = new SocketOptions();
@@ -232,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) {
@@ -412,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;
                    }
@@ -482,8 +495,9 @@
    }
    /**
     *  场景控制
     * @param sids 场景sid列表
     * 场景控制
     *
     * @param sids     场景sid列表
     * @param callBack 回调
     */
    public void controlScene(List<String> sids, HDLLinkCallBack callBack) {
@@ -573,8 +587,8 @@
    /**
     * 通用TCP发送指令 只发一次,不监听回复,不重发
     *
     * @param topic    发送数据
     * @param bodyStr  回复的主题
     * @param topic   发送数据
     * @param bodyStr 回复的主题
     */
    public void tcpSendMsg(String topic, String bodyStr) {
        try {
@@ -585,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());
        }
    }
@@ -596,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);
@@ -604,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());
                            }
                        }
                    }
@@ -633,6 +649,8 @@
                                    }
                                }
                            });
                            LogUtils.i("HDLSocket sendMsg onError 发送失败");
                        }
                    }
                }, 1000, 500, TimeUnit.MILLISECONDS);
@@ -664,6 +682,8 @@
                    }
                }
            });
            LogUtils.i("HDLSocket sendMsg onError 发送失败");
        }
    }