whyclxw
2025-05-28 e16302f9d475c7cc4dd18c5abf1a23cb5502e362
src/main/java/com/whyc/webSocket/HomeAdminSocket.java
@@ -2,6 +2,7 @@
import com.whyc.config.WebSocketConfig;
import com.whyc.dto.Response;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.pojo.Battinf;
import com.whyc.pojo.PowerInf;
import com.whyc.pojo.StationInf;
@@ -16,10 +17,13 @@
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;
/**
 * 总首页
 * 总首页(领导层)
 */
@Component
@ServerEndpoint(value = "/homeAdmin", encoders = WebSocketEncoder.class, configurator = WebSocketConfig.class)
@@ -46,6 +50,8 @@
    private static PowerInfService powerInfService;
    private static BatttestdataInfService batttestdataInfService;
    private HttpSession httpSession;
    @Autowired
    public void setBattInfService(BattInfService battInfService) {
@@ -75,11 +81,15 @@
    @OnOpen
    public void onOpen(Session session, EndpointConfig config) {
        this.session = session;
        HttpSession httpSession = (HttpSession) config.getUserProperties().get("httpSession");
        UserInf user = (UserInf) httpSession.getAttribute("user");
        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 = 1001;
        /*Thread thread = new Thread() {
        //final int userId = 1087;
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
@@ -88,9 +98,8 @@
                        ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
                        CountDownLatch latch = new CountDownLatch(7);
                        poolExecutor.execute(() -> {
                            //蓄电池优劣分析
                            Response resQualityAnalysis = badBattMonService.getQualityAnalysis(userId);
                            res.put("resQualityAnalysis", resQualityAnalysis);
                            Response resGroupAnalysis = batttestdataInfService.getGroupAnalysis3(userId);
                            res.put("resGroupAnalysis", resGroupAnalysis);
                            latch.countDown();
                        });
                        poolExecutor.execute(() -> {
@@ -103,13 +112,14 @@
                            //站点信息
                            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());
                                }
@@ -182,9 +192,9 @@
                            res.put("resTestdataInfAnalysis", resTestdataInfAnalysis);
                            latch.countDown();
                        });
                        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();
@@ -192,128 +202,7 @@
            }
        };
        thread.start();
        this.thread = thread;*/
        thread = new Thread("Thread_homeAdmin") {
            @Override
            public void run() {
                while (runFlag && !isInterrupted()) {
                    Thread thread = currentThread();
                    threadFlagMap.put(thread.getId(), true);
                    try {
                        if (session.isOpen()) {
                            //推送信息
                            synchronized (session) {
                                session.getBasicRemote().sendObject(getAnalysis(userId));
                            }
                            threadFlagMap.put(thread.getId(), false);
                        }
                        sleep(executeTime);
                        //} catch (IOException | InterruptedException | EncodeException e) {
                    } catch (Exception e) {
                        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);
    }
    //实时页面推送
    public Response getAnalysis(int userId) {
        Map<String, Object> res = new HashMap<>();
        //蓄电池优劣分析
        Response resQualityAnalysis = badBattMonService.getQualityAnalysis(userId);
        res.put("resQualityAnalysis", resQualityAnalysis);
        //基础资源信息
        Response resBattInfoAnalysis = battInfService.getBattInfoAnalysis(userId);
        res.put("resBattInfoAnalysis", resBattInfoAnalysis);
        //站点信息
        Response resStationAnalysis = new Response();
        try {
            //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 = sinfList.size();
            for (Integer node : nodeStationMap.keySet()) {
                nodeMap.put(node, nodeStationMap.get(node).size());
            }
            for (String station : stationTypeMap.keySet()) {
                stationMap.put(station, stationTypeMap.get(station).size());
            }
            battGroupInfoMap.put("node", nodeMap);
            battGroupInfoMap.put("stationType", stationMap);
            battGroupInfoMap.put("totalNum", battSize);
            resStationAnalysis.setII(1, true, battGroupInfoMap, null);
        } catch (Exception e) {
            resStationAnalysis.set(1, false, "发生异常:" + e.getCause());
        }
        res.put("resStationAnalysis", resStationAnalysis);
        //不同品牌同一时间品质和同品牌同时间品质
        Response resProductQuaAnalysis = badBattMonService.getProductQuaAnalysis(userId);
        res.put("resProductQuaAnalysis", resProductQuaAnalysis);
        //电池信息
        Response res_battGroupInfo = new Response();
        try {
            List<Battinf> battinfList = battInfService.getBattGroupList(userId);
            Map<String, List<Battinf>> battProducerMap = battinfList.stream().collect(Collectors.groupingBy(Battinf::getBattProducer));
            Map<Float, List<Battinf>> battMonVolStdMap = battinfList.stream().collect(Collectors.groupingBy(Battinf::getMonVolStd));
            Map<String, Object> battGroupProducerMap = new HashMap<>();
            Map<String, Object> battGroupMonVolMap = new HashMap<>();
            Map<String, Object> battGroupInfoMap = new HashMap<>();
            int battSize = battinfList.size();
            for (String producer : battProducerMap.keySet()) {
                battGroupProducerMap.put(producer, battProducerMap.get(producer).size());
            }
            for (Float monVolStd : battMonVolStdMap.keySet()) {
                battGroupMonVolMap.put(Float.toString(monVolStd), battMonVolStdMap.get(monVolStd).size());
            }
            battGroupInfoMap.put("producer", battGroupProducerMap);
            battGroupInfoMap.put("monVol", battGroupMonVolMap);
            battGroupInfoMap.put("totalNum", battSize);
            res_battGroupInfo.setII(1, true, battGroupInfoMap, null);
        } catch (Exception e) {
            res_battGroupInfo.set(1, false, "发生异常:" + e.getCause());
        }
        res.put("battGroupInfo", res_battGroupInfo);
        //电源信息
        Response resPwrdevInfAnalysis = new Response();
        try {
            List<PowerInf> powerInfList = powerInfService.getPwrdevInfAnalysis(userId);
            Map<String, List<PowerInf>> powerProducerMap = powerInfList.stream().collect(Collectors.groupingBy(PowerInf::getPowerProducer));
            Map<String, Object> AnalysisMap = new HashMap<>();
            for (String producer : powerProducerMap.keySet()) {
                AnalysisMap.put(producer, powerProducerMap.get(producer).size());
            }
            resPwrdevInfAnalysis.setII(1, true, AnalysisMap, "电源信息");
        } catch (Exception e) {
            resPwrdevInfAnalysis.set(1, false, "发生异常:" + e.getCause());
        }
        res.put("resPwrdevInfAnalysis", resPwrdevInfAnalysis);
        //测试信息
        Response resTestdataInfAnalysis = batttestdataInfService.getTestdataInfAnalysis(userId);
        res.put("resTestdataInfAnalysis", resTestdataInfAnalysis);
        return new Response().set(1, res);
    }
    @OnMessage
    public void onMessage(Session session, String message) {
        this.thread = thread;
    }
    @OnClose