package com.whyc.webSocket; import com.whyc.config.WebSocketConfig; import com.whyc.constant.DevStateEnum; import com.whyc.constant.StopReasonEnum; import com.whyc.dto.Real.SticRealRtdataDto; import com.whyc.dto.Real.TopDto; import com.whyc.dto.RealTimeDto; import com.whyc.dto.Response; import com.whyc.factory.BattCapFactory; import com.whyc.pojo.db_batt_testdata.BattresdataInf; import com.whyc.pojo.db_batt_testdata.BatttestdataInf; import com.whyc.pojo.db_ram_db.BattRtdata; import com.whyc.pojo.db_ram_db.BattRtstate; import com.whyc.pojo.db_ram_db.DeviceState; import com.whyc.pojo.db_ram_db.PwrdevAcdcdata; import com.whyc.pojo.db_station.BattInf; import com.whyc.pojo.db_station.PowerInf; import com.whyc.pojo.db_station.StationInf; import com.whyc.pojo.db_user.User; import com.whyc.service.*; import com.whyc.util.ActionUtil; import com.whyc.util.ThreadLocalUtil; 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.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Component @ServerEndpoint(value = "/real",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class) public class RealTimeSocket { private Session session; private Thread thread; //实时数据 private static BattRtstateService battRtstateService; private static DeviceStateService deviceStateService; private static PwrdevAcdcdataService pwrdevAcdcdataService; //实时告警 private static BattalarmDataService battalarmDataService; private static DevalarmDataService devalarmDataService; private static PwrdevAlarmService pwrdevAlarmService; //放电记录 private static BatttestdataInfService batttestdataInfService; //电池组信息 private static BattInfService battInfService; //机房信息 private static StationInfService stationInfService; //电源信息 private static PowerInfService powerInfService; //单体信息 private static BattRtdataService rtdataService; //内阻测试信息 private static BattresdataInfService battresdataInfService; private volatile boolean runFlag = true; private static HttpSession httpSession; private volatile Map threadMap = new HashMap<>(); private volatile Map threadFlagMap = new HashMap<>(); @Autowired public void setBattRtstateService(BattRtstateService battRtstateService) { RealTimeSocket.battRtstateService = battRtstateService; } @Autowired public void setDeviceStateService(DeviceStateService deviceStateService) { RealTimeSocket.deviceStateService = deviceStateService; } @Autowired public void setPwrdevAcdcdataService(PwrdevAcdcdataService pwrdevAcdcdataService) { RealTimeSocket.pwrdevAcdcdataService = pwrdevAcdcdataService; } @Autowired public void setBattalarmDataService(BattalarmDataService battalarmDataService) { RealTimeSocket.battalarmDataService = battalarmDataService; } @Autowired public void setDevalarmDataService(DevalarmDataService devalarmDataService) { RealTimeSocket.devalarmDataService = devalarmDataService; } @Autowired public void setPwrdevAlarmService(PwrdevAlarmService pwrdevAlarmService) { RealTimeSocket.pwrdevAlarmService = pwrdevAlarmService; } @Autowired public void setBatttestdataInfService(BatttestdataInfService batttestdataInfService) { RealTimeSocket.batttestdataInfService = batttestdataInfService; } @Autowired public void setBattInfService(BattInfService battInfService) { RealTimeSocket.battInfService = battInfService; } @Autowired public void setStationInfService(StationInfService stationInfService) { RealTimeSocket.stationInfService = stationInfService; } @Autowired public void setPowerInfService(PowerInfService powerInfService) { RealTimeSocket.powerInfService = powerInfService; } @Autowired public void setBattRtdataService(BattRtdataService rtdataService) { RealTimeSocket.rtdataService = rtdataService; } @Autowired public void setBattresdataInfService(BattresdataInfService battresdataInfService) { RealTimeSocket.battresdataInfService = battresdataInfService; } @OnOpen public void onOpen(Session session, EndpointConfig config) { this.session = session; this.httpSession = (HttpSession) config.getUserProperties().get("httpSession"); } @OnMessage public void onMessage(Session session, String message) { RealTimeDto realDto= ActionUtil.getGson().fromJson(message,RealTimeDto.class); if(httpSession!=null){ User user = (User) httpSession.getAttribute("user"); realDto.setUid(user.getId()); }else{ realDto.setUid(99); } thread = new Thread("Thread_realTimeSocket") { @Override public void run() { while (runFlag && !isInterrupted()) { Thread thread = currentThread(); threadFlagMap.put(thread.getId(), true); try { Map map=new HashMap<>(); //获取头部信息 Response topRes=getTop(realDto); map.put("topRes",topRes); //根据pageType获取返回界面 Response realRes=getRes(realDto); map.put("realRes",realRes); if (session.isOpen()) { //推送信息 synchronized (session) { session.getBasicRemote().sendObject(new Response<>().setII(1,true,map,"实时页面数据推送")); } threadFlagMap.put(thread.getId(), false); } sleep(4000); } 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); } //获取头部信息 private Response getTop(RealTimeDto realDto) { TopDto topDto = new TopDto(); try { //实时获取电池组信息 BattRtstate battRtstate = battRtstateService.getBattRealInfo(realDto.getBattgroupId()); if (battRtstate != null) { topDto.setBattState(battRtstate.getBattState()); topDto.setVbusVol(battRtstate.getVbusVol()); topDto.setOnlineVol(battRtstate.getOnlineVol()); topDto.setRecordtime(ThreadLocalUtil.format(battRtstate.getRecDatetime(),1)); } //实时获取电池组信息 BattInf binf=battInfService.getBinfByBattgroupId(realDto.getBattgroupId()); if(binf!=null){ topDto.setBattGroupName(binf.getBattgroupName()); topDto.setDevName(binf.getDevName()); } //获取机房信息 StationInf stationInf = stationInfService.getStationInfById(binf.getStationId()); if(stationInf!=null){ topDto.setStationName(stationInf.getStationName()); topDto.setProvice(stationInf.getProvice()); topDto.setCity(stationInf.getCity()); topDto.setCounty(stationInf.getCountry()); topDto.setFullName(stationInf.getFullName()); } //获取电源信息 PowerInf powerInf = powerInfService.getPowerInfById(binf.getPowerId()); if(powerInf!=null){ topDto.setPowerName(powerInf.getPowerName()); } //实时获取设备信息 DeviceState deviceState = deviceStateService.getDevRealInfo(realDto.getDevId()); if (deviceState != null) { topDto.setDevState(deviceState.getDevWorkstate()); if (deviceState.getDevWorkstate() == DevStateEnum.DEVSTATE_2.getStateId()) {//核容放电 topDto.setCaptestGroupvol(deviceState.getDevCaptestGroupvol()); topDto.setCaptestCurr(deviceState.getDevCaptestCurr()); topDto.setCaptestTimelong(deviceState.getDevCaptestTimelong()); topDto.setCaptestCap(deviceState.getDevCaptestCap()); //剩余容量和剩余时间计算 Float restCap = batttestdataInfService.getLastTestDataRestCap(realDto.getBattgroupId()); topDto.setRestCap(restCap); //实时组端电流,剩余容量,标称容量 if(battRtstate!=null){ Float restTime= BattCapFactory.getTheoryTime(battRtstate.getLoadCurr(), restCap, binf.getMoncapstd()); topDto.setRestTime(restTime); } } //获取一级未处理实时告警数 Integer battALmNum = battalarmDataService.getBattAlmNum(realDto.getBattgroupId()); topDto.setBattALmNum(battALmNum); Integer devALmNum = devalarmDataService.getDevAlmNum(realDto.getDevId()); topDto.setDevALmNum(devALmNum); Integer pwrALmNum = pwrdevAlarmService.getPwrAlmNum(realDto.getPowerId()); topDto.setPwrALmNum(pwrALmNum); //实时获取电源信息(是否存在电源告警信息) //PwrdevAcdcdata pwrdevAcdcdata = pwrdevAcdcdataService.getPwrRealInfo(realDto.getPowerId()); topDto.setPwrState((pwrALmNum > 0 ? 1 : 0)); Integer allALmNum = battALmNum + devALmNum + pwrALmNum; topDto.setAllALmNum(allALmNum); topDto.setSystemState((allALmNum > 0 ? 1 : 0)); } return new Response().setII(1, true, topDto, "实时页面头部数据推送"); } catch (Exception e) { return new Response().set(1, false, "实时页面头部数据推送"); } } //根据pageType获取返回界面 private Response getRes(RealTimeDto realDto) { Response res=new Response(); switch (realDto.getPageType()){ case "system":res=getSystemPage(realDto);break; case "power":res=getPowerPage(realDto);break; case "vol":res=getVolPage(realDto);break; case "res":res=getResPage(realDto);break; case "tmp":res=getTmpPage(realDto);break; case "img":res=getImgPage(realDto);break; case "3D":res=get3DPage(realDto);break; case "self":res=getSelfPage(realDto);break; case "manage":res=getManagePage(realDto);break; default:res=new Response().set(1); } return res; } //获取管理界面信息 private Response getManagePage(RealTimeDto realDto) { Map map=new HashMap<>(); try { StationInf sinf=stationInfService.getStationInfById(realDto.getStaitonId()); map.put("sinf",sinf); PowerInf pinf=powerInfService.getPowerInfById(realDto.getPowerId()); map.put("pinf",pinf); BattInf binf=battInfService.getBinfByBattgroupId(realDto.getBattgroupId()); map.put("binf",binf); map.put("pageType",realDto.getPageType()); return new Response().setII(1,true,map,"管理界面信息推送"); }catch (Exception e){ return new Response().set(1,false,"管理界面信息推送"); } } //获取自愈能力界面信息 private Response getSelfPage(RealTimeDto realDto) { Map map=new HashMap<>(); map.put("pageType",realDto.getPageType()); return new Response().setII(1,true,map,"自愈能力界面信息推送"); } //获取3D界面信息 private Response get3DPage(RealTimeDto realDto) { Map map=new HashMap<>(); map.put("pageType",realDto.getPageType()); return new Response().setII(1,true,map,"3D界面信息信息推送"); } //获取图片界面信息 private Response getImgPage(RealTimeDto realDto) { Map map=new HashMap<>(); map.put("pageType",realDto.getPageType()); return new Response().setII(1,true,map,"图片界面信息信息推送"); } //获取温度界面信息 private Response getTmpPage(RealTimeDto realDto) { Map map=new HashMap<>(); Float maxData=0f; Float minData=0f; Float avgData=0f; try { //实时获取单体信息 List rtdataList = rtdataService.getRtdataRealInfo(realDto.getBattgroupId()); if(rtdataList!=null&&rtdataList.size()>0){ for(int i=0;i= maxData) { maxData = battRtdata.getMonTmp(); } if (battRtdata.getMonTmp() <= minData) { minData = battRtdata.getMonTmp(); } avgData += battRtdata.getMonTmp(); } avgData=avgData/rtdataList.size(); } map.put("rtdataList",rtdataList!=null?rtdataList:""); map.put("maxData",maxData); map.put("minData",minData); map.put("avgData",avgData); map.put("pageType",realDto.getPageType()); return new Response().setII(1,true,map,"温度界面信息推送"); }catch (Exception e){ return new Response().set(1,false,"温度界面信息推送"); } } //获取内阻界面信息 private Response getResPage(RealTimeDto realDto) { Map map=new HashMap<>(); Float maxData=0f; Float minData=0f; Float avgData=0f; try { //实时获取单体信息 List rtdataList = rtdataService.getRtdataRealInfo(realDto.getBattgroupId()); if(rtdataList!=null&&rtdataList.size()>0){ for(int i=0;i= maxData) { maxData = battRtdata.getMonRes(); } if (battRtdata.getMonRes() <= minData) { minData = battRtdata.getMonRes(); } avgData += battRtdata.getMonRes(); } avgData=avgData/rtdataList.size(); } map.put("rtdataList",rtdataList!=null?rtdataList:""); map.put("maxData",maxData); map.put("minData",minData); map.put("avgData",avgData); //上一次内阻测试数据 BattresdataInf rinfData= battresdataInfService.getLastTestData(realDto.getBattgroupId()); map.put("rinfData",rinfData!=null?rinfData:""); map.put("pageType",realDto.getPageType()); return new Response().setII(1,true,map,"内阻界面信息推送"); }catch (Exception e){ return new Response().set(1,false,"内阻界面信息推送"); } } //获取电压界面信息 private Response getVolPage(RealTimeDto realDto) { Map map=new HashMap<>(); Float maxData=0f; Float minData=0f; Float avgData=0f; try { //实时获取单体信息 List rtdataList = rtdataService.getRtdataRealInfo(realDto.getBattgroupId()); if(rtdataList!=null&&rtdataList.size()>0){ for(int i=0;i= maxData) { maxData = battRtdata.getMonVol(); } if (battRtdata.getMonVol() <= minData) { minData = battRtdata.getMonVol(); } avgData += battRtdata.getMonVol(); } avgData=avgData/rtdataList.size(); } map.put("rtdataList",rtdataList!=null?rtdataList:""); map.put("maxData",maxData); map.put("minData",minData); map.put("avgData",avgData); map.put("pageType",realDto.getPageType()); return new Response().setII(1,true,map,"电压界面信息推送"); }catch (Exception e){ return new Response().set(1,false,"电压界面信息推送"); } } //获取电源界面信息 private Response getPowerPage(RealTimeDto realDto) { Map map=new HashMap<>(); try { //实时获取电源信息 PwrdevAcdcdata pwrdevAcdcdata = pwrdevAcdcdataService.getPwrRealInfo(realDto.getPowerId()); map.put("pwrdevAcdcdata",pwrdevAcdcdata!=null?pwrdevAcdcdata:""); map.put("pageType",realDto.getPageType()); return new Response().setII(1,true,map,"电源界面信息推送"); }catch (Exception e){ return new Response().set(1,false,"电源界面信息推送"); } } //获取系统概述界面信息 private Response getSystemPage(RealTimeDto realDto) { Map map=new HashMap<>(); map.put("pageType",realDto.getPageType()); try { //实时获取电池组信息 BattRtstate battRtstate = battRtstateService.getBattRealInfo(realDto.getBattgroupId()); if(battRtstate!=null){ battRtstate.setLastCaptestStopTypeReason(StopReasonEnum.getValue(battRtstate.getLastCaptestStopType())); } map.put("battRtstate",battRtstate!=null?battRtstate:""); //实时获取设备信息 DeviceState deviceState = deviceStateService.getDevRealInfo(realDto.getDevId()); map.put("deviceState",deviceState!=null?deviceState:""); //获取上一次标准核容信息 BatttestdataInf tinfData= batttestdataInfService.getLastStandardTestData(realDto.getBattgroupId()); map.put("tinfData",tinfData!=null?tinfData:""); //获取近半小时电源数据,整流器数据,核容设备数据(推送实时,接口查询最近半小时数据) PwrdevAcdcdata pwrdevAcdcdata = pwrdevAcdcdataService.getPwrRealInfo(realDto.getPowerId()); map.put("pwrdevAcdcdata",pwrdevAcdcdata!=null?pwrdevAcdcdata:""); //电源对象(整流器使用情况) PowerInf powerInf = powerInfService.getPowerInfById(realDto.getPowerId()); map.put("powerInf",powerInf!=null?powerInf:""); //获取电池组的蓄电池信息 List battRtdataList = rtdataService.getRtdataRealInfo(realDto.getBattgroupId()); SticRealRtdataDto dto=setMaxMinData(battRtdataList); map.put("sticRtdata",dto); return new Response().setII(1,true,map,"系统概述推送"); }catch (Exception e) { e.printStackTrace(); } return new Response().set(1,false,"系统概述推送"); } //计算集合中单体电压,内阻,温度,容量的最大最小值 public SticRealRtdataDto setMaxMinData(List battRtdataList){ SticRealRtdataDto dto=new SticRealRtdataDto(); Float maxCap = 0.0f; Float minCap = 0.0f; Float maxVol = 0.0f; Float minVol = 0.0f; Float maxTmp = 0.0f; Float minTmp = 0.0f; Float maxRes = 0.0f; Float minRes = 0.0f; List maxCapNumList = new ArrayList<>(); List minCapNumList = new ArrayList<>(); List maxVolNumList = new ArrayList<>(); List minVolNumList = new ArrayList<>(); List maxTmpNumList = new ArrayList<>(); List minTmpNumList = new ArrayList<>(); List maxResNumList = new ArrayList<>(); List minResNumList = new ArrayList<>(); //计算集合中单体电压,内阻,温度,容量的最大最小值 for (BattRtdata rtdata:battRtdataList) { if(rtdata.getMonVol()>=maxVol){ maxVol = rtdata.getMonVol(); } if(rtdata.getMonVol()<=minVol){ minVol = rtdata.getMonVol(); } if(rtdata.getMonTmp()>=maxTmp){ maxTmp = rtdata.getMonTmp(); } if(rtdata.getMonTmp()<=minTmp){ minTmp = rtdata.getMonTmp(); } if(rtdata.getMonRes()>=maxRes){ maxRes = rtdata.getMonRes(); } if(rtdata.getMonRes()<=minRes){ minRes = rtdata.getMonRes(); } if(rtdata.getMonCap()>=maxCap){ maxCap = rtdata.getMonCap(); } if(rtdata.getMonCap()<=minCap){ minCap = rtdata.getMonCap(); } } dto.setMaxCap(maxCap); dto.setMinCap(minCap); dto.setMaxVol(maxVol); dto.setMinVol(minVol); dto.setMaxTmp(maxTmp); dto.setMinTmp(minTmp); dto.setMaxRes(maxRes); dto.setMinRes(minRes); for (BattRtdata rtdata:battRtdataList) { if(rtdata.getMonVol()==maxVol){ maxVolNumList.add(rtdata.getMonNum()); } if(rtdata.getMonVol()==minVol){ minVolNumList.add(rtdata.getMonNum()); } if(rtdata.getMonTmp()==maxTmp){ maxTmpNumList.add(rtdata.getMonNum()); } if(rtdata.getMonTmp()==minTmp){ minTmpNumList.add(rtdata.getMonNum()); } if(rtdata.getMonRes()==maxRes){ maxResNumList.add(rtdata.getMonNum()); } if(rtdata.getMonRes()==minRes){ minResNumList.add(rtdata.getMonNum()); } if(rtdata.getMonCap()==maxCap){ maxCapNumList.add(rtdata.getMonNum()); } if(rtdata.getMonCap()==minCap){ minCapNumList.add(rtdata.getMonNum()); } } dto.setMaxCapNumList(maxCapNumList); dto.setMinCapNumList(minCapNumList); dto.setMaxVolNumList(maxVolNumList); dto.setMinVolNumList(minVolNumList); dto.setMaxTmpNumList(maxTmpNumList); dto.setMinTmpNumList(minTmpNumList); dto.setMaxResNumList(maxResNumList); dto.setMinResNumList(minResNumList); return dto; } @OnClose 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) { error.printStackTrace(); if (thread != null && thread.isAlive()) { thread.interrupt(); } threadMap.remove(session.getId()); } }