whycxzp
2025-06-10 73bb5617e6aeb4ee8f7dfd4ad54b61d477969d86
src/main/java/com/whyc/webSocket/BattSocket.java
@@ -1,27 +1,18 @@
package com.whyc.webSocket;
import com.whyc.config.WebSocketConfig;
import com.whyc.dto.Response;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.pojo.RtState;
import com.whyc.pojo.StationInf;
import com.whyc.service.BattAlarmService;
import com.whyc.service.BattInfService;
import com.whyc.service.RtDataService;
import com.whyc.service.RtStateService;
import com.whyc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpSession;
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;
import java.util.stream.Collectors;
/**
 * packageName com.whyc.websocket
@@ -30,7 +21,7 @@
 * @version JDK 8
 * @className BattSocket (此处以class为例)
 * @date 2024/6/15
 * @description TODO
 * @description
 */
@Component
@ServerEndpoint(value = "/batt", encoders = WebSocketEncoder.class)
@@ -43,7 +34,6 @@
    private volatile Map<String, Thread> threadMap = new HashMap<>();
    private volatile Map<Long, Boolean> threadFlagMap = new HashMap<>();
    private static final int executeTime = 5000;
@@ -52,6 +42,10 @@
    private static RtDataService rtDataService;
    private static BattAlarmService battAlarmService;
    private static RtEnvirmentService rtEnvirmentService;
    private volatile Map<Long,Boolean> threadFlagMap = new HashMap<>();
    @Autowired
@@ -69,6 +63,11 @@
        BattSocket.battAlarmService = battAlarmService;
    }
    @Autowired
    public void setRtEnvirmentService(RtEnvirmentService rtEnvirmentService) {
        BattSocket.rtEnvirmentService = rtEnvirmentService;
    }
    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
@@ -77,14 +76,16 @@
    @OnMessage
    public void onMessage(Session session, String message) {
        int binfId=Integer.valueOf(message);
        Thread thread = new Thread() {
        thread = new Thread("Thread_BattSocket") {
            @Override
            public void run() {
                try {
                    Map<String, Object> res = new HashMap<>();
                    while (!currentThread().isInterrupted()) {
                Map<String, Object> res = new HashMap<>();
                while (runFlag && !isInterrupted()) {
                    Thread thread = currentThread();
                    threadFlagMap.put(thread.getId(), true);
                    try {
                        ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
                        CountDownLatch latch = new CountDownLatch(3);
                        CountDownLatch latch = new CountDownLatch(4);
                        poolExecutor.execute(() -> {
                            Response resRtState = rtStateService.getResRtState(binfId);
                            res.put("resRtState", resRtState);
@@ -100,21 +101,41 @@
                            res.put("resBattAlm", resBattAlm);
                            latch.countDown();
                        });
                        poolExecutor.execute(() -> {
                            Response resRtEnvir = rtEnvirmentService.getResRtEnvir();
                            res.put("resRtEnvir", resRtEnvir);
                            latch.countDown();
                        });
                        latch.await(10, TimeUnit.MINUTES);
                        session.getBasicRemote().sendObject(new Response().set(1, res));
                        if (session.isOpen()) {
                            //推送信息
                            synchronized (session) {
                                session.getBasicRemote().sendObject(new Response().set(1, res));
                            }
                            threadFlagMap.put(thread.getId(), false);
                        }
                        sleep(executeTime);
                    } catch (Exception e) {
                        interrupt();
                    }
                } catch (Exception e) {
                    this.interrupt();
                }
            }
        };
        thread.start();
        this.thread = thread;
        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);
    }
    @OnClose
    public void onClose(CloseReason closeReason) {
    public void onClose(CloseReason closeReason){
        System.err.println("closeReason = " + closeReason);
        runFlag = false;
        if (thread != null && thread.isAlive()) {