whyclxw
2025-05-28 e16302f9d475c7cc4dd18c5abf1a23cb5502e362
src/main/java/com/whyc/webSocket/HomeAdminSocket.java
@@ -5,18 +5,15 @@
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.pojo.Battinf;
import com.whyc.pojo.PowerInf;
import com.whyc.service.BadBattMonService;
import com.whyc.service.BattInfService;
import com.whyc.service.BatttestdataInfService;
import com.whyc.service.PowerInfService;
import com.whyc.util.ActionUtil;
import com.whyc.pojo.StationInf;
import com.whyc.pojo.UserInf;
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.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -26,7 +23,7 @@
import java.util.stream.Collectors;
/**
 * 总首页
 * 总首页(领导层)
 */
@Component
@ServerEndpoint(value = "/homeAdmin", encoders = WebSocketEncoder.class, configurator = WebSocketConfig.class)
@@ -35,7 +32,17 @@
    private Thread thread;
    private volatile boolean runFlag = true;
    private volatile Map<String, Thread> threadMap = new HashMap<>();
    private volatile Map<Long, Boolean> threadFlagMap = new HashMap<>();
    private static final int executeTime = 5000;
    private static BattInfService battInfService;
    private static StationInfService stationInfService;
    private static BadBattMonService badBattMonService;
@@ -43,6 +50,8 @@
    private static PowerInfService powerInfService;
    private static BatttestdataInfService batttestdataInfService;
    private HttpSession httpSession;
    @Autowired
    public void setBattInfService(BattInfService battInfService) {
@@ -64,13 +73,22 @@
        HomeAdminSocket.batttestdataInfService = batttestdataInfService;
    }
    @Autowired
    public void setStationInfService(StationInfService stationInfService) {
        HomeAdminSocket.stationInfService = stationInfService;
    }
    @OnOpen
    public void onOpen(Session session, EndpointConfig config) {
        this.session = session;
         /*HttpSession httpSession = (HttpSession) config.getUserProperties().get("httpSession");
        UserInf user = (UserInf) httpSession.getAttribute("user");
        final int userId = user.getUId().intValue();*/
        final int userId = 1001;
        this.httpSession = (HttpSession) config.getUserProperties().get("httpSession");
    }
    @OnMessage
    public void onMessage(Session session, String message) {
        UserInf user = (UserInf) this.httpSession.getAttribute("user");
        final int userId = user.getUId().intValue();
        //final int userId = 1087;
        Thread thread = new Thread() {
            @Override
            public void run() {
@@ -79,32 +97,29 @@
                    while (!currentThread().isInterrupted()) {
                        ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
                        CountDownLatch latch = new CountDownLatch(7);
                        Date date = new Date();
                        poolExecutor.execute(() -> {
                            //蓄电池优劣分析
                            Response resQualityAnalysis = badBattMonService.getQualityAnalysis(userId);
                            res.put("resQualityAnalysis", resQualityAnalysis);
                            Response resGroupAnalysis = batttestdataInfService.getGroupAnalysis3(userId);
                            res.put("resGroupAnalysis", resGroupAnalysis);
                            latch.countDown();
                            System.out.println(ActionUtil.sdf.format(date) + " :t1: " + latch.getCount());
                        });
                        poolExecutor.execute(() -> {
                            //基础资源信息
                            Response resBattInfoAnalysis = battInfService.getBattInfoAnalysis(userId);
                            res.put("resBattInfoAnalysis", resBattInfoAnalysis);
                            latch.countDown();
                            System.out.println(ActionUtil.sdf.format(date) + " :t2: " + latch.getCount());
                        });
                        poolExecutor.execute(() -> {
                            //站点信息
                            Response resStationAnalysis = new Response();
                            try {
                                List<Battinf> battinfList = battInfService.getSateAnalysis(userId);
                                Map<Integer, List<Battinf>> nodeStationMap = battinfList.stream().collect(Collectors.groupingBy(Battinf::getNodeStation));
                                Map<String, List<Battinf>> stationTypeMap = battinfList.stream().collect(Collectors.groupingBy(Battinf::getStationType));
                                //List<Battinf> battinfList = battInfService.getSateAnalysis(userId);
                                List<StationInf> sinfList = stationInfService.getSateAnalysis(userId);
                                Map<Integer, List<StationInf>> nodeStationMap = sinfList.stream().collect(Collectors.groupingBy(StationInf::getNodeStation));
                                Map<String, List<StationInf>> stationTypeMap = sinfList.stream().collect(Collectors.groupingBy(StationInf::getStationType));
                                Map<Integer, Object> nodeMap = new HashMap<>();
                                Map<String, Object> stationMap = new HashMap<>();
                                Map<String, Object> battGroupInfoMap = new HashMap<>();
                                int battSize = battinfList.size();
                                int battSize = sinfList.size();
                                for (Integer node : nodeStationMap.keySet()) {
                                    nodeMap.put(node, nodeStationMap.get(node).size());
                                }
@@ -120,14 +135,12 @@
                            }
                            res.put("resStationAnalysis", resStationAnalysis);
                            latch.countDown();
                            System.out.println(ActionUtil.sdf.format(date) + " :t3: " + latch.getCount());
                        });
                        poolExecutor.execute(() -> {
                            //不同品牌同一时间品质和同品牌同时间品质
                            Response resProductQuaAnalysis = badBattMonService.getProductQuaAnalysis(userId);
                            res.put("resProductQuaAnalysis", resProductQuaAnalysis);
                            latch.countDown();
                            System.out.println(ActionUtil.sdf.format(date) + " :t4: " + latch.getCount());
                        });
                        poolExecutor.execute(() -> {
                            //电池信息
@@ -155,7 +168,6 @@
                            }
                            res.put("battGroupInfo", res_battGroupInfo);
                            latch.countDown();
                            System.out.println(ActionUtil.sdf.format(date) + " :t5: " + latch.getCount());
                        });
                        poolExecutor.execute(() -> {
                            //电源信息
@@ -173,19 +185,16 @@
                            }
                            res.put("resPwrdevInfAnalysis", resPwrdevInfAnalysis);
                            latch.countDown();
                            System.out.println(ActionUtil.sdf.format(date) + " :t6: " + latch.getCount());
                        });
                        poolExecutor.execute(() -> {
                            //测试信息
                            Response resTestdataInfAnalysis = batttestdataInfService.getTestdataInfAnalysis(userId);
                            res.put("resTestdataInfAnalysis", resTestdataInfAnalysis);
                            latch.countDown();
                            System.out.println(ActionUtil.sdf.format(date) + " :t7:  " + latch.getCount());
                        });
                        System.out.println(ActionUtil.sdf.format(date) + " :all: " + latch.getCount());
                        latch.await(10, TimeUnit.SECONDS);
                        latch.await(10, TimeUnit.MINUTES);
                        session.getBasicRemote().sendObject(new Response().set(1, res));
                        sleep(4000);
                        sleep(5000);
                    }
                } catch (Exception e) {
                    this.interrupt();
@@ -197,20 +206,22 @@
    }
    @OnClose
    public void onClose(CloseReason closeReason) throws IOException {
        //System.err.println("closeReason = " + closeReason);
        if (session.isOpen()) {
            session.close();
    public void onClose(CloseReason closeReason) {
        System.err.println("closeReason = " + closeReason);
        runFlag = false;
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
        threadMap.remove(session.getId());
    }
    @OnError
    public void onError(Throwable error) throws IOException {
        //error.printStackTrace();
        thread.isInterrupted();
        if (session.isOpen()) {
            session.close();
    public void onError(Throwable error) {
        error.printStackTrace();
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
        threadMap.remove(session.getId());
    }
}