hxb
2024-02-22 d451118d4be05f180cfe1f99bc5904f74db8cc3f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.hdl.sdk.link.core.connect;
 
import com.google.gson.JsonObject;
import com.hdl.sdk.link.common.config.TopicConstant;
import com.hdl.sdk.link.common.exception.HDLLinkException;
import com.hdl.sdk.link.common.utils.IdUtils;
import com.hdl.sdk.link.common.utils.LogUtils;
import com.hdl.sdk.link.common.utils.gson.GsonConvert;
import com.hdl.sdk.link.core.bean.LinkRequest;
import com.hdl.sdk.link.core.bean.LinkResponse;
import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
import com.hdl.sdk.link.core.bean.request.BroadcastRequest;
import com.hdl.sdk.link.core.callback.HDLLinkResponseCallBack;
import com.hdl.sdk.link.core.config.HDLLinkConfig;
import com.hdl.sdk.link.core.protocol.LinkMessageDecoder;
import com.hdl.sdk.link.core.protocol.LinkMessageEncoder;
import com.hdl.sdk.link.gateway.HDLLinkLocalGateway;
import com.hdl.sdk.link.socket.TcpSocketBoot;
import com.hdl.sdk.link.socket.SocketOptions;
import com.hdl.sdk.link.socket.client.IHeartbeat;
import com.hdl.sdk.link.socket.client.TcpClient;
import com.hdl.sdk.link.socket.codec.MessagePipeLine;
import com.hdl.sdk.link.socket.listener.ConnectStatusListener;
 
/**
 * Created by Tong on 2021/9/26.
 * 1、通过Udp 组播或者广播搜索网关
 * 2、通过Udp 获取Tcp ip 端口统一8586
 */
public class HDLTcpConnect {
 
    /**
     * tcp默认端口
     */
    private static final int TCP_PORT = 8586;
 
 
    private ConnectStatusListener statusListener;
 
    private HDLTcpConnect() {
        statusListener = new ConnectStatusListener() {
            @Override
            public void onConnecting() {
                broadcastRequest();
            }
 
            @Override
            public void onConnected() {
 
            }
 
            @Override
            public void onConnectFailed() {
 
            }
        };
    }
 
    /**
     * 广播自身信息给主网关
     */
    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");
        HDLUdpConnect.getInstance().udpSendMsg(TopicConstant.BROADCAST, GsonConvert.getGson().toJson(request), true);
        HDLUdpConnect.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) {
 
            }
        });
        HDLUdpConnect.getInstance().udpSendMsg(TopicConstant.BROADCAST, GsonConvert.getGson().toJson(request), true);
    }
 
    private static class SingletonInstance {
        private static final HDLTcpConnect INSTANCE = new HDLTcpConnect();
    }
 
    public static HDLTcpConnect getInstance() {
        return SingletonInstance.INSTANCE;
    }
 
    /**
     * 通过IP及端口找出连接客户端,如果当前没有连接。主要是针对调试软件使用,支持连接主网关、从网关
     *
     * @param ip 连接的网关IP
     * @return
     */
    public static synchronized TcpSocketBoot getTcpSocketBoot(String ip) {
        return initTcp(ip);
    }
 
    static  int dddd;
    /**
     * 初始化tcp连接
     *
     * @param ip 连接的网关IP
     * @return
     */
    public static synchronized TcpSocketBoot initTcp(String ip) {
        int port = TCP_PORT;
        TcpSocketBoot tcpSocketBoot = TcpSocketBoot.getByEndPoint(ip, port);
 
        if (null == tcpSocketBoot) {
            final SocketOptions options = new SocketOptions();
            final MessagePipeLine pipeLine = new MessagePipeLine();
            pipeLine.add(new LinkMessageDecoder());
            pipeLine.add(new LinkMessageEncoder());
            options.setHandleMessage(pipeLine);
            tcpSocketBoot = TcpClient.init(ip, port, options);
            tcpSocketBoot.SetHeartbeat(new IHeartbeat() {
                @Override
                public void heartbeat() {
 
                    String time = String.valueOf(System.currentTimeMillis());
 
                    JsonObject jsonObject = new JsonObject();
                    jsonObject.addProperty("id", IdUtils.getUUId());
                    jsonObject.addProperty("time_stamp", time);
 
                    GatewayBean gatewayBean= HDLLinkLocalGateway.getInstance().getGatewayByOidOrGatewayId(ip);
                    if(gatewayBean==null){
                        return;
                    }
                    String topic = String.format(TopicConstant.HEARTBEAT, gatewayBean.getOid());
 
                    LinkRequest request = new LinkRequest(topic, jsonObject.toString(), false);
                    new HDLConnectHelper(ip, request, null, true).send();
                }
            });
        }
        return tcpSocketBoot;
    }
}