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_sz",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class) public class ScreenForSZSocket { private Session session; private Thread thread; private static PwrdevAlarmService powerAlarmService; private static BattalarmDataService battAlarmDataService; private static Fbs9100StateService fbs9100StateService; private static DevalarmDataService devAlarmDataService; private static BattInfService battInfService; private static BattEnduranceService battEnduranceService; private static StationInfService stationService; private static PowerInfService powerInfService; //private static BatttestdataInfService testInfService; private static BatttestdataInfService batttestdataInfService; private static BattRtstateService battRtStateService; @Autowired public void setPowerAlarmService(PwrdevAlarmService powerAlarmService) { ScreenForSZSocket.powerAlarmService = powerAlarmService; } @Autowired public void setBattAlarmDataService(BattalarmDataService battAlarmDataService) { ScreenForSZSocket.battAlarmDataService = battAlarmDataService; } @Autowired public void setFbs9100StateService(Fbs9100StateService fbs9100StateService) { ScreenForSZSocket.fbs9100StateService = fbs9100StateService; } @Autowired public void setDevAlarmDataService(DevalarmDataService devAlarmDataService) { ScreenForSZSocket.devAlarmDataService = devAlarmDataService; } @Autowired public void setBattInfService(BattInfService battInfService) { ScreenForSZSocket.battInfService = battInfService; } @Autowired public void setBattEnduranceService(BattEnduranceService battEnduranceService) { ScreenForSZSocket.battEnduranceService = battEnduranceService; } @Autowired public void setStationService(StationInfService stationService) { ScreenForSZSocket.stationService = stationService; } @Autowired public void setPowerInfService(PowerInfService powerInfService) { ScreenForSZSocket.powerInfService = powerInfService; } @Autowired public void setBatttestdataInfService(BatttestdataInfService batttestdataInfService) { ScreenForSZSocket.batttestdataInfService = batttestdataInfService; } @Autowired public void setBattRtStateService(BattRtstateService battRtStateService) { ScreenForSZSocket.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 { Map res = new HashMap<>(); while (!currentThread().isInterrupted()) { ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor(); CountDownLatch latch = new CountDownLatch(5); /*poolExecutor.execute(()->{ //1.统计:电池统计 分为品牌和标称电压 Response res_battGroupInfo = new Response(); try { List battinfList = battInfService.getBattGroupList(userId); Map> battProducerMap = battinfList.stream().collect(Collectors.groupingBy(Battinf::getBattProducer)); Map> battMonVolStdMap = battinfList.stream().collect(Collectors.groupingBy(Battinf::getMonVolStd)); Map battGroupProducerMap = new HashMap<>(); Map battGroupMonVolMap = new HashMap<>(); Map 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); latch.countDown(); });*/ poolExecutor.execute(()->{ //2.统计:设备状态 Response res_devStates = fbs9100StateService.getBTSEquipStatus2(userId,2); res.put("devStates", res_devStates); latch.countDown(); }); poolExecutor.execute(()->{ //3.统计:组端容量 //Response res_monCapacityLowAnalysis = battAlarmDataService.getMonCapacityLowAnalysis(userId); Response res_groupCap = battEnduranceService.getGroupCapList(userId); res.put("groupCap", res_groupCap); latch.countDown(); }); poolExecutor.execute(()->{ //4.统计:各种设备类型总数 电池 设备 电源 站点 新增电池单体数 Response res_devCountMap = battInfService.getDevCountMapForSZ(userId); res.put("devCountMap", res_devCountMap); latch.countDown(); }); /*//5.统计:每个省的机房数统计 poolExecutor.execute(()->{ Response res_stationCount = stationService.getProvinceCountMap(userId); res.put("stationCount", res_stationCount); latch.countDown(); });*/ /*poolExecutor.execute(()->{ //6.统计:实时放电信息 正在放电的电池信息展示 Response dischargingList = battRtStateService.getDischargingList(userId); res.put("dischargingBattery", dischargingList); latch.countDown(); });*/ /*poolExecutor.execute(()->{ //7.统计:电源统计 Response res_powerProducer = new Response(); try { Response producersCount = powerInfService.getProducerCount(userId); res_powerProducer.setII(1,true,producersCount,null); }catch (Exception e){ res_powerProducer.set(1,false,"发生异常:"+e.getCause()); } res.put("powerProducerCount", res_powerProducer); latch.countDown(); });*/ /*poolExecutor.execute(()->{ //9.统计:告警统计 电源,设备,电池 Response res_devAlarmNum = new Response(); Map devAlarmNumMap = new HashMap<>(); try { int battAlarmNum = battAlarmDataService.getRealTimeWithLevel1(userId).getCode(); int devAlarmNum = devAlarmDataService.getRealTimeWithLevel1(userId); int powerAlarmNum = powerAlarmService.getRealTimeWithLevel1(userId); devAlarmNumMap.put("battAlarmNum",battAlarmNum); devAlarmNumMap.put("devAlarmNum",devAlarmNum); devAlarmNumMap.put("powerAlarmNum",powerAlarmNum); res_devAlarmNum.setII(1,true,devAlarmNumMap,null); }catch (Exception e){ res_devAlarmNum.set(1,false,"发生异常:"+e.getCause()); } res.put("devAlarmNum", res_devAlarmNum); latch.countDown(); });*/ poolExecutor.execute(()->{ //9-1.统计:区县(StationName5)的 告警统计 级别及类型统计 //查询出所有的区县,并按照区县来进行告警的分类统计 Response res_alarmLevel = new Response(); try { Response alarmsCountByLevel = battAlarmDataService.getByLevelAndStationName5(userId); res_alarmLevel.setII(1,true,alarmsCountByLevel,null); }catch (Exception e){ res_alarmLevel.set(1,false,"发生异常:"+e.getCause()); } res.put("alarmsLevelWithStationName5", res_alarmLevel); latch.countDown(); }); poolExecutor.execute(()->{ //9-2.统计:告警统计 级别及类型统计 Response res_alarmLevel = new Response(); Map devAlarmNumMap = new HashMap<>(); try { Response alarmsCountByLevel = battAlarmDataService.getByLevel(userId); res_alarmLevel.setII(1,true,alarmsCountByLevel,null); }catch (Exception e){ res_alarmLevel.set(1,false,"发生异常:"+e.getCause()); } res.put("alarmsLevel", res_alarmLevel); latch.countDown(); }); latch.await(10, TimeUnit.MINUTES); session.getBasicRemote().sendObject(new Response().set(1, res)); sleep(4000); } } catch (Exception e) { this.interrupt(); } } }; thread.start(); this.thread = thread; } @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(); } } }