From 99bc815e07e39354f51421b77f4012ffd35594d8 Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期三, 28 六月 2023 18:03:00 +0800
Subject: [PATCH] 2023年06月28日18:02:58
---
HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/QueueUtils.java | 134 ++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 134 insertions(+), 0 deletions(-)
diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/QueueUtils.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/QueueUtils.java
new file mode 100644
index 0000000..2dda1ee
--- /dev/null
+++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/core/utils/QueueUtils.java
@@ -0,0 +1,134 @@
+package com.hdl.sdk.link.core.utils;
+
+import com.hdl.sdk.link.common.event.EventDispatcher;
+import com.hdl.sdk.link.common.utils.LogUtils;
+import com.hdl.sdk.link.common.utils.ThreadToolUtils;
+import com.hdl.sdk.link.core.bean.LinkPacket;
+import com.hdl.sdk.link.core.bean.LinkResponse;
+import com.hdl.sdk.link.core.config.HDLLinkConfig;
+import com.hdl.sdk.link.core.connect.HDLConnectHelper;
+
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
+import java.util.LinkedList;
+import java.util.Queue;
+import java.util.concurrent.ExecutorService;
+
+/**
+ * Created by hxb on 2022/8/4.
+ */
+public class QueueUtils {
+
+ //杩樻病瑙e瘑鐨勬暟鎹寘
+ private final Queue<LinkPacket> linkPackets;
+ private final ExecutorService executorService;
+ //鏄惁宸茬粡鍚姩
+ private boolean started;
+ /**
+ * instance
+ */
+ private volatile static QueueUtils instance;
+
+ private QueueUtils() {
+ linkPackets = new LinkedList<>();
+ executorService = ThreadToolUtils.getInstance().newFixedThreadPool(1);
+ }
+
+ /**
+ * getInstance
+ *
+ * @return AuthenticateConfig
+ */
+ public static synchronized QueueUtils getInstance() {
+ if (instance == null) {
+ synchronized (QueueUtils.class) {
+ if (instance == null) {
+ instance = new QueueUtils();
+ }
+ }
+ }
+ return instance;
+ }
+
+ /**
+ * 澧炲姞鎺ユ敹鍒扮殑姣忔潯鏁版嵁
+ *
+ * @param linkPacket
+ */
+ public void add(LinkPacket linkPacket) {
+ synchronized (linkPackets) {
+ linkPackets.add(linkPacket);
+ }
+ }
+
+ public LinkPacket poll() {
+ synchronized (linkPackets) {
+ return linkPackets.poll();
+ }
+ }
+
+ /**
+ * 鍚姩澶勭悊鎺ユ敹鍒扮殑鏁版嵁
+ */
+ public synchronized void start() {
+ if (started) {
+ return;
+ }
+ started = true;
+ executorService.execute(new Runnable() {
+ @Override
+ public void run() {
+ while (true) {
+ try {
+ LinkPacket linkPacket = poll();
+ if (linkPacket == null) {
+ Thread.sleep(10);
+ continue;
+ }
+ manager(linkPacket);
+ } catch (Exception e) {
+ LogUtils.e("澶勭悊鎺ユ敹鍒扮殑鏁版嵁寮傚父:\r\n" + e.getMessage());
+ }
+ }
+ }
+ });
+ }
+
+ private void manager(LinkPacket linkPacket) throws UnsupportedEncodingException {
+
+ LinkResponse response = new LinkResponse();
+ response.setTopic(linkPacket.getTopic());
+// GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayByIpAddress(linkPacket.isIpAddress());
+// boolean isEncrypt = false;
+// if (gatewayBean != null) {
+// isEncrypt = gatewayBean.getIsLocalEncrypt();
+// }
+// if (EncryptUtil.ifNeedEncrypt(response.getTopic(), isEncrypt)) {
+// if (gatewayBean != null && gatewayBean.getIsLocalEncrypt() && encrypt(linkPacket.getBody())) {
+ if (!linkPacket.isCloudPacket() && encrypt(linkPacket.getBody())) {
+ //闇�瑕佽В瀵�
+ byte[] bodyBytes = AesUtil.aesDecrypt(linkPacket.getBody(), HDLLinkConfig.getInstance().getLocalSecret());
+ if (bodyBytes != null) {
+ response.setData(new String(bodyBytes, StandardCharsets.UTF_8));
+ } else {
+ LogUtils.e("瑙e瘑澶辫触\r\n" + linkPacket.getTopic() + "\r\n" + ByteUtils.encodeHexString(linkPacket.getBody()));
+ response.setData(new String(linkPacket.getBody(), "utf-8"));
+ }
+ } else {
+ response.setData(new String(linkPacket.getBody(), "utf-8"));
+ }
+ if (HDLConnectHelper.isLocal()) {
+ LogUtils.i("鏈湴鎺ユ敹鍒版暟鎹�:\r\n" + response.getTopic() + "\r\n" + response.getData());
+ }
+ //瑙f瀽瀹屾垚,topic鍙戦�佷竴娆�
+ EventDispatcher.getInstance().post(response.getTopic(), response);
+ }
+
+ //鏄惁鍔犲瘑
+ private boolean encrypt(byte[] bytes) {
+ if (bytes[0] == '{' && bytes[bytes.length - 1] == '}' || (bytes[0] == '[' && bytes[bytes.length - 1] == ']')) {
+ return false;
+ }
+ return true;
+ }
+}
--
Gitblit v1.8.0