HDLSDK/hdl-common/build.gradle
@@ -24,10 +24,8 @@ sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { api 'com.google.code.gson:gson:2.8.8' api 'com.android.support:support-annotations:28.0.0"' api 'com.android.support:collections:28.0.0' } HDLSDK/hdl-common/src/main/java/com/hdl/sdk/common/HDLSdk.java
@@ -13,13 +13,14 @@ /** * 获取当前版本 * * @return */ public String getVersion() { return version; } private String version="1.0.15"; private String version = "1.0.15"; private HDLSdk() { } @@ -43,9 +44,10 @@ /** * 设置打印是否开启 * * @param enable */ public void setLogEnabled(boolean enable){ public void setLogEnabled(boolean enable) { LogUtils.setEnabled(enable); } } HDLSDK/hdl-common/src/main/java/com/hdl/sdk/common/event/EventDispatcher.java
@@ -1,9 +1,7 @@ package com.hdl.sdk.common.event; import android.support.annotation.NonNull; import android.support.v4.util.ArrayMap; import android.util.ArrayMap; import com.hdl.sdk.common.utils.LogUtils; import com.hdl.sdk.common.utils.ThreadToolUtils; @@ -11,6 +9,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; @@ -22,9 +21,9 @@ private static final List<EventListener> ALL_TOPICS_EVENT = new ArrayList<>();//所有主题消息 private static final ArrayMap<Object, List<EventListener>> EVENT = new ArrayMap<>(); private static final ConcurrentHashMap<Object, List<EventListener>> EVENT = new ConcurrentHashMap<>(); private static final ArrayMap<EventListener, Integer> TYPE = new ArrayMap<>(); private static final ConcurrentHashMap<EventListener, Integer> TYPE = new ConcurrentHashMap<>(); private static final int MAIN_TYPE = 0; private static final int IO_TYPE = 1; @@ -63,7 +62,7 @@ } } public void registerIo(Object tag, EventListener listener) { public void registerIo(Object tag, EventListener listener) { synchronized (this) { LogUtils.i("注册主题:" + tag); if (tag == null) return; @@ -83,7 +82,7 @@ } } public void remove(Object tag) { public void remove(Object tag) { synchronized (this) { if (tag == null) { return; @@ -106,7 +105,7 @@ } } public void remove(Object tag, EventListener listener) { public void remove(Object tag, EventListener listener) { synchronized (this) { try { if (tag == null || listener == null) { @@ -126,7 +125,7 @@ } } public synchronized void post(Object tag, @NonNull Object o) { public synchronized void post(Object tag, Object o) { if (tag == null) { LogUtils.i("post tag为空"); return; @@ -174,17 +173,18 @@ }); } } }catch (Exception e){ } catch (Exception e) { LogUtils.e("post异常2:" + e.getMessage()); } } /** * 注册所有主题消息的监听 * * @param listener */ public synchronized void registerAllTopicsListener(EventListener listener) { if(listener==null){ if (listener == null) { return; } try { @@ -192,13 +192,14 @@ ALL_TOPICS_EVENT.add(listener); } TYPE.put(listener, MAIN_TYPE); }catch (Exception e){ } catch (Exception e) { LogUtils.e("registerAllTopicsListener:" + e.getMessage()); } } /** * 取消所有主题消息的监听 * * @param listener */ public synchronized void removeAllTopicsListener(EventListener listener) { @@ -207,7 +208,7 @@ @Override public void run() { try { if(listener==null){ if (listener == null) { return; } if (ALL_TOPICS_EVENT != null && !ALL_TOPICS_EVENT.isEmpty()) { HDLSDK/hdl-common/src/main/java/com/hdl/sdk/common/exception/HDLLinkException.java
@@ -1,7 +1,7 @@ package com.hdl.sdk.common.exception; import android.support.annotation.NonNull; /** * Created by Tong on 2021/11/11. @@ -40,7 +40,7 @@ this.code = code; } public @NonNull public String getMsg() { return msg; } HDLSDK/hdl-common/src/main/java/com/hdl/sdk/common/utils/LogUtils.java
@@ -57,7 +57,7 @@ public static void e(String msg) { if (msg != null && isEnabled) { Log.e(TAG, msg); Log.e(TAG, msg); } } } HDLSDK/hdl-common/src/main/java/com/hdl/sdk/common/utils/SPUtils.java
@@ -2,7 +2,6 @@ import android.content.Context; import android.content.SharedPreferences; import android.support.annotation.NonNull; import com.hdl.sdk.common.HDLSdk; @@ -26,87 +25,87 @@ //======通用存储======== public static void put(@NonNull final String key, final String value) { public static void put(final String key, final String value) { getAppPreference().edit().putString(key, value).apply(); } public static String getString(@NonNull final String key) { public static String getString(final String key) { return getString(key, ""); } public static String getString(@NonNull final String key, final String defaultValue) { public static String getString(final String key, final String defaultValue) { return getAppPreference().getString(key, defaultValue); } public static void put(@NonNull final String key, final int value) { public static void put(final String key, final int value) { put(key, value, false); } public static void put(@NonNull final String key, final int value, final boolean isCommit) { public static void put(final String key, final int value, final boolean isCommit) { getAppPreference().edit().putInt(key, value).apply(); } public static int getInt(@NonNull final String key) { public static int getInt(final String key) { return getInt(key, -1); } public static int getInt(@NonNull final String key, final int defaultValue) { public static int getInt(final String key, final int defaultValue) { return getAppPreference().getInt(key, defaultValue); } public static void put(@NonNull final String key, final long value) { public static void put(final String key, final long value) { getAppPreference().edit().putLong(key, value).apply(); } public static long getLong(@NonNull final String key) { public static long getLong(final String key) { return getLong(key, -1L); } public static long getLong(@NonNull final String key, final long defaultValue) { public static long getLong(final String key, final long defaultValue) { return getAppPreference().getLong(key, defaultValue); } public static void put(@NonNull final String key, final float value) { public static void put(final String key, final float value) { getAppPreference().edit().putFloat(key, value).apply(); } public static float getFloat(@NonNull final String key) { public static float getFloat(final String key) { return getFloat(key, -1f); } public static float getFloat(@NonNull final String key, final float defaultValue) { public static float getFloat(final String key, final float defaultValue) { return getAppPreference().getFloat(key, defaultValue); } public static void put(@NonNull final String key, final boolean value) { public static void put(final String key, final boolean value) { getAppPreference().edit().putBoolean(key, value).apply(); } public static boolean getBoolean(@NonNull final String key) { public static boolean getBoolean(final String key) { return getBoolean(key, false); } public static boolean getBoolean(@NonNull final String key, final boolean defaultValue) { public static boolean getBoolean(final String key, final boolean defaultValue) { return getAppPreference().getBoolean(key, defaultValue); } public static void put(@NonNull final String key, public static void put(final String key, final Set<String> value ) { @@ -115,12 +114,12 @@ } public static Set<String> getStringSet(@NonNull final String key) { public static Set<String> getStringSet(final String key) { return getStringSet(key, Collections.<String>emptySet()); } public static Set<String> getStringSet(@NonNull final String key, public static Set<String> getStringSet(final String key, final Set<String> defaultValue) { return getAppPreference().getStringSet(key, defaultValue); } @@ -131,12 +130,12 @@ } public static boolean contains(@NonNull final String key) { public static boolean contains(final String key) { return getAppPreference().contains(key); } public static void remove(@NonNull final String key) { public static void remove(final String key) { getAppPreference().edit().remove(key).apply(); } HDLSDK/hdl-common/src/main/java/com/hdl/sdk/common/utils/gson/ParameterizedTypeImpl.java
@@ -1,8 +1,6 @@ package com.hdl.sdk.common.utils.gson; import android.support.annotation.NonNull; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; @@ -25,14 +23,13 @@ return new ParameterizedTypeImpl(rawType, actualTypeArguments, null); } @NonNull @Override public Type[] getActualTypeArguments() { return actualTypeArguments; } @NonNull @Override public Type getRawType() { return rawType; HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/bean/LinkRequest.java
@@ -1,6 +1,6 @@ package com.hdl.sdk.connect.bean; import android.support.annotation.NonNull; import android.text.TextUtils; @@ -56,7 +56,6 @@ this.length = length; } @NonNull @Override public String toString() { return "Topic:" + HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/bean/LinkResponse.java
@@ -1,6 +1,6 @@ package com.hdl.sdk.connect.bean; import android.support.annotation.NonNull; import android.text.TextUtils; @@ -57,7 +57,7 @@ this.length = length; } @NonNull @Override public String toString() { return GsonConvert.getGson().toJson(this); HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/protocol/LinkMessageDecoder.java
@@ -1,19 +1,14 @@ package com.hdl.sdk.connect.protocol; import android.annotation.TargetApi; import android.os.Build; import android.util.Log; import com.google.gson.internal.bind.DateTypeAdapter; import com.hdl.sdk.common.utils.LogUtils; import com.hdl.sdk.connect.config.HDLLinkConfig; import com.hdl.sdk.common.event.EventDispatcher; import com.hdl.sdk.common.utils.ByteUtils; import com.hdl.sdk.common.utils.LogUtils; import com.hdl.sdk.connect.bean.LinkResponse; import com.hdl.sdk.connect.config.HDLLinkConfig; import com.hdl.sdk.connect.utils.AesUtil; import com.hdl.sdk.connect.utils.ProtocolParse; import com.hdl.sdk.socket.codec.ByteToMessageDecoder; import java.util.ArrayList; @@ -110,7 +105,7 @@ } list.clear(); for(int i=0;i<tempList.size();i++){ for (int i = 0; i < tempList.size(); i++) { list.add(tempList.get(i)); } } @@ -118,7 +113,7 @@ @Override protected synchronized LinkResponse decoder(Object msg,String ipaddress) throws Exception { protected synchronized LinkResponse decoder(Object msg, String ipaddress) throws Exception { if (msg instanceof byte[]) { bytes.addAll(ByteUtils.toByteList((byte[]) msg)); //如果多条命令打包在一条数据中,都需要处理完 @@ -180,10 +175,10 @@ String bodyString = new String(body, "utf-8"); response.setData(bodyString); LogUtils.i( "接收到数据:" + response.getTopic() + "\r\n" + response.getData()); LogUtils.i("接收到数据:" + response.getTopic() + "\r\n" + response.getData()); //非正常数据,返回 if (!((bodyString.startsWith("{") &&bodyString.endsWith("}")) || (bodyString.startsWith("[")&&bodyString.endsWith("]")))) { if (!((bodyString.startsWith("{") && bodyString.endsWith("}")) || (bodyString.startsWith("[") && bodyString.endsWith("]")))) { continue; } //解析完成,topic发送一次 HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/SocketBoot.java
@@ -1,13 +1,7 @@ package com.hdl.sdk.socket; import android.support.v4.util.ArrayMap; 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.utils.IdUtils; import com.hdl.sdk.common.utils.LogUtils; import com.hdl.sdk.common.utils.ThreadToolUtils; import com.hdl.sdk.socket.annotation.ConnectStatus; @@ -15,15 +9,11 @@ import com.hdl.sdk.socket.client.IHeartbeat; import com.hdl.sdk.socket.listener.SendListener; import java.net.ConnectException; import java.util.Date; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; /** * Created by Tong on 2021/9/26. @@ -38,18 +28,20 @@ private final IClient client; private IHeartbeat iHeartbeat; public void SetHeartbeat(IHeartbeat iHeartbeat){ this.iHeartbeat=iHeartbeat; public void SetHeartbeat(IHeartbeat iHeartbeat) { this.iHeartbeat = iHeartbeat; } /** * 当前接收到数据的时间 */ private static long time=System.currentTimeMillis(); private static long time = System.currentTimeMillis(); /** * tcp是否已经连接 */ private boolean connected=false; private boolean connected = false; public IClient getClient() { return client; @@ -57,7 +49,7 @@ private final BlockingQueue<SocketRequest> mMessageQueue = new LinkedBlockingDeque<>(); private final ArrayMap<String, SendListener> sendMap = new ArrayMap<>(); private final ConcurrentMap<String, SendListener> sendMap = new ConcurrentHashMap<>(); public SocketBoot(IClient client) { this.client = client; @@ -76,11 +68,11 @@ client.onConnectStatus(ConnectStatus.CONNECTING); Thread.sleep(1000); client.connect(); connected=true; connected = true; LogUtils.i("TCP连接成功"); client.onConnectStatus(ConnectStatus.CONNECTED); }catch(Exception e) { LogUtils.e("连接异常"+e.getMessage()); } catch (Exception e) { LogUtils.e("连接异常" + e.getMessage()); } } @@ -141,7 +133,7 @@ if (connected) { //读取数据 client.onHandleResponse(); time= System.currentTimeMillis(); time = System.currentTimeMillis(); } else { try { Thread.sleep(1000); @@ -174,7 +166,7 @@ if (!connected) { reconect(); } Thread.sleep(3*1000); Thread.sleep(3 * 1000); } catch (Exception e) { LogUtils.e("定时连接失败:" + e.getMessage()); } @@ -223,6 +215,7 @@ /** * 发送无需回调 * * @param msg 发送的数据 */ public void sendMsg(byte[] msg) { HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/annotation/ConnectStatus.java
@@ -1,32 +1,24 @@ package com.hdl.sdk.socket.annotation; import android.support.annotation.IntDef; import java.lang.annotation.ElementType; import java.lang.annotation.Target; /** * Created by Tong on 2021/9/22. */ @Target({ElementType.TYPE_PARAMETER, ElementType.PARAMETER}) @IntDef({ConnectStatus.CONNECTING, ConnectStatus.CONNECTED, ConnectStatus.DISCONNECT}) public @interface ConnectStatus { public class ConnectStatus { /** * 连接中 */ int CONNECTING = 0; public static final int CONNECTING = 0; /** * 连接成功 */ int CONNECTED = 1; public static final int CONNECTED = 1; /** * 连接关闭 */ int DISCONNECT = 2; public static final int DISCONNECT = 2; } HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/udp/UdpSocketBoot.java
@@ -1,26 +1,16 @@ package com.hdl.sdk.socket.udp; import android.support.v4.util.ArrayMap; import android.text.TextUtils; import com.hdl.sdk.common.utils.LogUtils; import com.hdl.sdk.common.utils.ThreadToolUtils; import com.hdl.sdk.socket.SocketRequest; import com.hdl.sdk.socket.annotation.ConnectStatus; import com.hdl.sdk.socket.client.IClient; import com.hdl.sdk.socket.client.IUdpClient; import com.hdl.sdk.socket.listener.SendListener; import java.net.ConnectException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -37,7 +27,7 @@ private ExecutorService receiveThread; private final ArrayMap<String, SendListener> sendMap = new ArrayMap<>(); private final ConcurrentMap<String, SendListener> sendMap = new ConcurrentHashMap<>(); public UdpSocketBoot(IUdpClient client) { this.client = client;