package com.whyc.webSocket; import com.whyc.config.WebSocketConfig; import com.whyc.dto.Response; import com.whyc.factory.ThreadPoolExecutorFactory; import com.whyc.pojo.Battinf; 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.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; /** * 苏州地铁更新版 * 首页大屏展示 Socket */ @Component @ServerEndpoint(value = "/screen_sz2",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class) public class ScreenForSZ2Socket { private Session session; private Thread thread; private static BattalarmDataService battAlarmDataService; private static BattInfService battInfService; private static BattRtstateService battRtStateService; @Autowired public void setBattAlarmDataService(BattalarmDataService battAlarmDataService) { ScreenForSZ2Socket.battAlarmDataService = battAlarmDataService; } @Autowired public void setBattInfService(BattInfService battInfService) { ScreenForSZ2Socket.battInfService = battInfService; } @Autowired public void setBattRtStateService(BattRtstateService battRtStateService) { ScreenForSZ2Socket.battRtStateService = battRtStateService; } @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; Thread thread = new Thread() { @Override public void run() { try { while (!currentThread().isInterrupted()) { Map res =getStatic(userId); session.getBasicRemote().sendObject(new Response().set(1, res)); sleep(4000); } } catch (Exception e) { this.interrupt(); } } }; thread.start(); this.thread = thread; } public Map getStatic(Integer userId) throws InterruptedException { Map res = new HashMap<>(); ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor(); CountDownLatch latch = new CountDownLatch(4); poolExecutor.execute(()->{ //1统计机房个数,设备个数,电池组个数 Response res_inf = battInfService.getAllInfInSz2(userId); res.put("res_inf", res_inf); latch.countDown(); }); poolExecutor.execute(()->{ //2.统计电池告警(119001,119002,119003,119004,119005,119006,119007) Response res_battAlarm = battAlarmDataService.getAllBattAlarmInSz2(userId); res.put("res_battAlarm", res_battAlarm); latch.countDown(); }); poolExecutor.execute(()->{ //3.统计:电池状态 Response res_battState = battRtStateService.getAllBattStateInSz2(userId); res.put("res_battState", res_battState); latch.countDown(); }); poolExecutor.execute(()->{ //4.中间地图 Response res_station = battInfService.getAllStationInSz2(userId); res.put("res_station", res_station); latch.countDown(); }); latch.await(10, TimeUnit.MINUTES); return res; } @OnClose public void onClose(CloseReason closeReason) throws IOException { //System.err.println("closeReason = " + closeReason); if(session.isOpen()){ session.close(); } } @OnError public void onError(Throwable error) throws IOException { //error.printStackTrace(); thread.isInterrupted(); if(session.isOpen()){ session.close(); } } }