New file |
| | |
| | | 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;
|
| | | }
|
| | | }
|