From 12988986f873e336c457be7cec84eabe8ec4dcc5 Mon Sep 17 00:00:00 2001
From: lxw <810412026@qq.com>
Date: 星期二, 22 八月 2023 15:15:24 +0800
Subject: [PATCH] 获取门禁

---
 src/main/java/com/whyc/webSocket/CarCameraWebSocket.java |   52 +++++++++++++++++++++++++++-------------------------
 1 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/src/main/java/com/whyc/webSocket/CarCameraWebSocket.java b/src/main/java/com/whyc/webSocket/CarCameraWebSocket.java
index d33f44d..8d8041d 100644
--- a/src/main/java/com/whyc/webSocket/CarCameraWebSocket.java
+++ b/src/main/java/com/whyc/webSocket/CarCameraWebSocket.java
@@ -1,7 +1,10 @@
 package com.whyc.webSocket;
 
+import com.google.gson.reflect.TypeToken;
 import com.whyc.dto.CarCamera;
 import com.whyc.dto.Login;
+import com.whyc.factory.ThreadPoolExecutorFactory;
+import com.whyc.pojo.Response;
 import com.whyc.service.AirconditionerStateService;
 import com.whyc.service.CarCameraService;
 import com.whyc.util.ActionUtil;
@@ -12,7 +15,11 @@
 import javax.websocket.*;
 import javax.websocket.server.ServerEndpoint;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
 
 @Component
 @ServerEndpoint(value = "/carCameraSocket",encoders = WebSocketEncoderUtil.class)
@@ -43,40 +50,35 @@
 
     @OnMessage
     public void onMessage(Session session, String message){
-        Login login= ActionUtil.getGson().fromJson(message,Login.class);
+        List<Login> list= ActionUtil.getGson().fromJson(message,new TypeToken<List<Login>>(){}.getType());
         thread = new Thread("Thread_airSocket") {
             @Override
             public void run() {
-                while (runFlag && !isInterrupted()) {
-                    Thread thread = currentThread();
-                    threadFlagMap.put(thread.getId(), true);
-                    try {
-                        if (session.isOpen()) {
-                            //鎺ㄩ�佷俊鎭�
-                            synchronized (session) {
-                                session.getBasicRemote().sendObject(service.getCarCamera(login));
-                            }
-                            threadFlagMap.put(thread.getId(), false);
+                try {
+                    Map<String, Object> map = new HashMap<>();
+                    while (!currentThread().isInterrupted()) {
+                        Thread thread = currentThread();
+                        threadFlagMap.put(thread.getId(), true);
+                        ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
+                        CountDownLatch latch = new CountDownLatch(list.size());
+                        for (Login login:list) {
+                            poolExecutor.execute(() -> {
+                                Response res=service.getCarCamera(login);
+                                map.put(login.getIp(),res);
+                                latch.countDown();
+                            });
                         }
-                        sleep(executeTime);
-                        //} catch (IOException | InterruptedException | EncodeException e) {
-                    } catch (Exception e) {
-                        interrupt();
+                        latch.await(10, TimeUnit.MINUTES);
+                        session.getBasicRemote().sendObject(new Response().set(1, map));
+                        sleep(5000);
                     }
+                } catch (Exception e) {
+                    this.interrupt();
                 }
             }
         };
         thread.start();
-        threadFlagMap.put(thread.getId(), true);
-        //鍋滄鑰佺殑socket绾跨▼
-        Thread threadBefore = threadMap.get(session.getId());
-        if (threadBefore != null && threadBefore.isAlive()) {
-            while (threadFlagMap.get(threadBefore.getId())) {
-            }
-            threadBefore.interrupt();
-        }
-        //灏嗙嚎绋嬪瓨鍌�,渚夸簬璋冪敤瀹氫綅
-        threadMap.put(session.getId(), this.thread);
+        this.thread = thread;
     }
 
     @OnClose

--
Gitblit v1.9.1