From 14de918a79943e4961b09fa01ed320c6cad41f2e Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期三, 28 六月 2023 17:14:51 +0800 Subject: [PATCH] Revert "Revert "Merge branch 'hxb' into wjc"" --- HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLTcpConnect.java | 143 +++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 143 insertions(+), 0 deletions(-) diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLTcpConnect.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLTcpConnect.java new file mode 100644 index 0000000..9ddf6ad --- /dev/null +++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/connect/HDLTcpConnect.java @@ -0,0 +1,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鍙婄鍙f壘鍑鸿繛鎺ュ鎴风锛屽鏋滃綋鍓嶆病鏈夎繛鎺ャ�備富瑕佹槸閽堝璋冭瘯杞欢浣跨敤锛屾敮鎸佽繛鎺ヤ富缃戝叧銆佷粠缃戝叧 + * + * @param ip 杩炴帴鐨勭綉鍏矷P + * @return + */ + public static synchronized TcpSocketBoot getTcpSocketBoot(String ip) { + return initTcp(ip); + } + + static int dddd; + /** + * 鍒濆鍖杢cp杩炴帴 + * + * @param ip 杩炴帴鐨勭綉鍏矷P + * @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; + } +} -- Gitblit v1.8.0