New file |
| | |
| | | package com.whyc.controller; |
| | | |
| | | import com.whyc.dto.RealDataStatic.SticRealDataAc; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.service.BattRealdataIdService; |
| | | import com.whyc.service.PwrdevHistorydataIdService; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import java.text.ParseException; |
| | | |
| | | @RestController |
| | | @Api(tags = "实时数据曲线统计") |
| | | @RequestMapping("realStatic") |
| | | public class RealDataStaticController { |
| | | @Autowired |
| | | private PwrdevHistorydataIdService pwrdevHistorydataIdService; |
| | | |
| | | @Autowired |
| | | private BattRealdataIdService battRealdataIdService; |
| | | |
| | | @ApiOperation(value = "交流单元") |
| | | @PostMapping("getPwrHisAcData") |
| | | public Response getPwrHisAcData(@RequestBody SticRealDataAc stic) throws ParseException, InterruptedException { |
| | | return pwrdevHistorydataIdService.getPwrHisAcData(stic); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.whyc.dto.RealDataStatic; |
| | | |
| | | import com.fasterxml.jackson.annotation.JsonFormat; |
| | | import lombok.Data; |
| | | import lombok.Value; |
| | | |
| | | import java.util.Date; |
| | | import java.util.Map; |
| | | |
| | | @Data |
| | | public class ResRealDataAc { |
| | | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") |
| | | private Date recordDatetime; |
| | | |
| | | private Map<String,Map<String, Float>> propertyInfo; |
| | | } |
New file |
| | |
| | | package com.whyc.dto.RealDataStatic; |
| | | |
| | | import lombok.Data; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @Data |
| | | public class SticRealDataAc { |
| | | private Integer powerId; |
| | | private Map<String,List<String>> propertyInfo; |
| | | } |
| | |
| | | @ApiModelProperty(value = "文件名") |
| | | private String fileName; |
| | | |
| | | @ApiModelProperty(value = "属性字段名称") |
| | | private String propertyName; |
| | | |
| | | |
| | | } |
| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.whyc.dto.Real.PwrHisRealAcInDto; |
| | | import com.whyc.dto.Real.PwrHisRealDcoutInDto; |
| | | import com.whyc.dto.RealDataStatic.ResRealDataAc; |
| | | import com.whyc.dto.RealDataStatic.SticRealDataAc; |
| | | import com.whyc.mapper.PwrdevAcdcdataMapper; |
| | | import com.whyc.pojo.db_data_history.PwrdevHistorydataId; |
| | | import com.whyc.pojo.db_ram_db.BattRtstate; |
| | | import com.whyc.pojo.db_ram_db.PwrdevAcdcdata; |
| | | import com.whyc.util.ActionUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @Service |
| | | public class PwrdevAcdcdataService { |
| | |
| | | PwrHisRealDcoutInDto dcoutDto=mapper.getPwrRealDcOut(powerId); |
| | | return dcoutDto; |
| | | } |
| | | |
| | | //实时数据统计曲线-交流单元 |
| | | public ResRealDataAc getSticRealDataAc(SticRealDataAc realDataAc) throws NoSuchFieldException, IllegalAccessException { |
| | | Integer powerId=realDataAc.getPowerId(); |
| | | Map<String,List<String>> propertyInfo=realDataAc.getPropertyInfo(); |
| | | QueryWrapper wrapper=new QueryWrapper(); |
| | | wrapper.eq("power_id",powerId); |
| | | wrapper.last("limit 1"); |
| | | PwrdevAcdcdata pwr=mapper.selectOne(wrapper); |
| | | ResRealDataAc data=new ResRealDataAc(); |
| | | if(pwr!=null){ |
| | | data.setRecordDatetime(pwr.getRecordDatetime()); |
| | | Map<String, Map<String,Float>>dataMap=new HashMap<>(); |
| | | for (Map.Entry<String, List<String>> entry : propertyInfo.entrySet()) { |
| | | String key = entry.getKey(); |
| | | List<String> valueList = entry.getValue(); |
| | | Map<String,Float> propertyMap=new HashMap<>(); |
| | | for (String value : valueList) { |
| | | String propertyName = ActionUtil.toCamelCase(value); |
| | | Field field = PwrdevAcdcdata.class.getDeclaredField(propertyName); |
| | | field.setAccessible(true); // 如果字段是私有的,需要设置为可访问 |
| | | Float propertyValue = (Float) field.get(pwr); |
| | | propertyMap.put(value,propertyValue); |
| | | } |
| | | dataMap.put(key,propertyMap); |
| | | } |
| | | data.setPropertyInfo(dataMap); |
| | | } |
| | | return data; |
| | | } |
| | | } |
| | |
| | | import com.whyc.dto.Real.PwrHisRealAcInDto; |
| | | import com.whyc.dto.Real.PwrHisRealDcoutInDto; |
| | | import com.whyc.dto.Real.QuarterDto; |
| | | import com.whyc.dto.RealDataStatic.ResRealDataAc; |
| | | import com.whyc.dto.RealDataStatic.SticRealDataAc; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.dto.Statistic.QuarterPwr7Res; |
| | | import com.whyc.factory.ThreadPoolExecutorFactory; |
| | |
| | | List dataListSorted = dataList.stream().sorted(Comparator.comparing(PwrHisRealDcoutInDto::getRecordDatetime)).collect(Collectors.toList()); |
| | | return new Response().setII(1,dataList.size()>0,dataListSorted,"获取半小时内直流输出统计"); |
| | | } |
| | | //实时数据数据曲线统计-交流单元 |
| | | public Response getPwrHisAcData(SticRealDataAc stic) throws ParseException, InterruptedException { |
| | | //获取前笔数*时间间隔 |
| | | PowerheartParam heartParam=heartService.getHeartParamByPowerId(stic.getPowerId()); |
| | | Integer interverCount=100*5;//默认值 |
| | | Integer granularity=5;//默认值 |
| | | if(heartParam!=null){ |
| | | interverCount=heartParam.getAcinInterverCfg()*heartParam.getAcinCountCfg(); |
| | | granularity=heartParam.getAcinInterverCfg(); |
| | | } |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | String dateTime = now.format(formatter); |
| | | Date endTime=ActionUtil.sdf.parse(dateTime); |
| | | // 计算笔数*时间间隔的时间点 |
| | | LocalDateTime halfHourAgo = now.minusMinutes(interverCount); |
| | | // 格式化输出 |
| | | String halfHourAgoTime=halfHourAgo.format(formatter); |
| | | Date startTime=ActionUtil.sdf.parse(halfHourAgoTime); |
| | | //获取两个时间分割多少张表 |
| | | List<List<Date>> monthTimeList = DateUtil.getMonthTime(startTime,endTime); |
| | | List<ResRealDataAc> dataList = new LinkedList<>(); |
| | | ThreadPoolExecutor pool = ThreadPoolExecutorFactory.getPoolExecutor(); |
| | | CountDownLatch latch = new CountDownLatch(monthTimeList.size()); |
| | | for (int i = 0; i < monthTimeList.size(); i++) { |
| | | int finalI = i; |
| | | Integer finalGranularity = granularity; |
| | | pool.execute(() -> { |
| | | int finalII = finalI; |
| | | Date recordDatetime=monthTimeList.get(finalII).get(0); |
| | | Date recordDatetime1=monthTimeList.get(finalII).get(1); |
| | | String table = stic.getPowerId() + "_" + ThreadLocalUtil.format(recordDatetime,2); |
| | | String tableName="db_data_history.tb_pwrdev_historydata_"+table; |
| | | //判断表是否存在 |
| | | int tableNum = subTablePageInfoService.judgeTable_pwrhis(table); |
| | | List<ResRealDataAc> list = new ArrayList(); |
| | | if (tableNum > 0) { |
| | | //获取指定时间段内最大最小recordNum确保数据的完整 |
| | | List recordNums= subTablePageInfoService.getPwrMaxAndMinRecordNum(tableName,recordDatetime,recordDatetime1); |
| | | Integer maxRecordNum= 0; |
| | | Integer minRecordNum= 0; |
| | | if(recordNums.size()>0){ |
| | | maxRecordNum=(Integer)recordNums.get(0); |
| | | minRecordNum=(Integer)recordNums.get(1); |
| | | } |
| | | list=subTablePageInfoService.getPwrHisAcData(tableName, finalGranularity,recordDatetime,recordDatetime1,maxRecordNum,minRecordNum,stic.getPropertyInfo()); |
| | | } |
| | | dataList.addAll(list); |
| | | latch.countDown(); |
| | | }); |
| | | sleep(200); |
| | | } |
| | | latch.await(10, TimeUnit.MINUTES); |
| | | List dataListSorted = dataList.stream().sorted(Comparator.comparing(ResRealDataAc::getRecordDatetime)).collect(Collectors.toList()); |
| | | return new Response().setII(1,dataList.size()>0,dataListSorted,"获取半小时内交流输入统计"); |
| | | } |
| | | } |
| | |
| | | import com.whyc.dto.AlmHis.PwrAlmPar; |
| | | import com.whyc.dto.AnalysisAlm.ResAnalysis; |
| | | import com.whyc.dto.Real.*; |
| | | import com.whyc.dto.RealDataStatic.ResRealDataAc; |
| | | import com.whyc.dto.Statistic.ComPareChangeCurve; |
| | | import com.whyc.dto.Statistic.ComPareChart; |
| | | import com.whyc.dto.Statistic.QuarterPwr7Res; |
| | |
| | | }); |
| | | return list; |
| | | } |
| | | //系统概览获取半小时核容设备信息(组端信息取一个单体信息即可) |
| | | public List<BattHisRealDto> getHalfHourBattDevData(String tableName,Integer granularity,String halfHourAgoTime) { |
| | | String sql="select distinct mon_num,record_time,group_vol,online_vol,group_curr,group_tmp,load_curr " + |
| | | "from (select a.*, (@i:= @i+1) as number " + |
| | | " from (select * from "+tableName+" "+ |
| | | " where record_time>='"+halfHourAgoTime+"' and mon_num=1) a, " + |
| | | " (select @i:=0) b) c "+ |
| | | " where c.number%"+granularity+"=0 or c.number=1 "; |
| | | sql+=" order by record_time asc"; |
| | | List<BattHisRealDto> list = sqlExecuteService.executeQuery_call(sql, new CallBack() { |
| | | @Override |
| | | public List getResults(ResultSet rs) throws SQLException { |
| | | List<BattHisRealDto> list=new ArrayList<>(); |
| | | while (rs.next()){ |
| | | BattHisRealDto data=new BattHisRealDto(); |
| | | data.setRecordTime(rs.getTimestamp("record_time")); |
| | | data.setGroupVol(rs.getFloat("group_vol")); |
| | | data.setOnlineVol(rs.getFloat("online_vol")); |
| | | data.setGroupCurr(rs.getFloat("group_curr")); |
| | | data.setGroupTmp(rs.getFloat("group_tmp")); |
| | | data.setLoadCurr(rs.getFloat("load_curr")); |
| | | list.add(data); |
| | | } |
| | | return list; |
| | | } |
| | | }); |
| | | return list; |
| | | } |
| | | //系统概览获取半小时交流输入统计 |
| | | public List<PwrHisRealAcInDto> getHalfHourPwrHisAcinData(String tableName,Integer granularity,Date recordDatetime,Date recordDatetime1,Integer maxRecordNum,Integer minRecordNum) { |
| | | String sql=" select distinct record_datetime,acin1_vola,acin1_volb,acin1_volc,acin2_vola,acin2_volb,acin2_volc " + |
| | |
| | | }); |
| | | return list; |
| | | } |
| | | |
| | | //系统概览获取半小时交流输出统计 |
| | | public List<PwrHisRealDcoutInDto> getHalfHourPwrHisDcoutData(String tableName,Integer granularity,Date recordDatetime,Date recordDatetime1,Integer maxRecordNum,Integer minRecordNum) { |
| | | String sql="select distinct * " + |
| | | " from "+tableName+" where record_datetime>='"+ActionUtil.sdf.format(recordDatetime)+"'" + |
| | |
| | | data.setM14OutVol(rs.getFloat("m14_out_vol")); |
| | | data.setM15OutVol(rs.getFloat("m15_out_vol")); |
| | | data.setM16OutVol(rs.getFloat("m16_out_vol")); |
| | | list.add(data); |
| | | } |
| | | return list; |
| | | } |
| | | }); |
| | | return list; |
| | | } |
| | | //实时数据数据曲线统计-交流单元 |
| | | public List<ResRealDataAc> getPwrHisAcData(String tableName, Integer granularity, Date recordDatetime, Date recordDatetime1 |
| | | , Integer maxRecordNum, Integer minRecordNum, Map<String, List<String>> propertyInfo) { |
| | | String propertyStr = propertyInfo.values().stream() |
| | | .flatMap(List::stream) |
| | | .collect(Collectors.joining(",")); |
| | | String sql=" select distinct record_datetime,"+propertyStr+" "+ |
| | | " from "+tableName+" where record_datetime>='"+ActionUtil.sdf.format(recordDatetime)+"'" + |
| | | " and record_datetime<='"+ActionUtil.sdf.format(recordDatetime1)+ |
| | | "' and (record_num-"+minRecordNum+")%"+granularity+"=0 or record_num="+minRecordNum+" or record_num="+maxRecordNum; |
| | | sql+=" order by record_datetime asc"; |
| | | List<ResRealDataAc> list = sqlExecuteService.executeQuery_call(sql, new CallBack() { |
| | | @Override |
| | | public List getResults(ResultSet rs) throws SQLException { |
| | | List<ResRealDataAc> list=new ArrayList<>(); |
| | | while (rs.next()){ |
| | | ResRealDataAc data=new ResRealDataAc(); |
| | | data.setRecordDatetime(rs.getTimestamp("record_datetime")); |
| | | Map<String,Map<String,Float>>dataMap=new HashMap<>(); |
| | | for (Map.Entry<String, List<String>> entry : propertyInfo.entrySet()) { |
| | | String key = entry.getKey(); |
| | | List<String> valueList = entry.getValue(); |
| | | Map<String,Float> propertyMap=new HashMap<>(); |
| | | for (String value : valueList) { |
| | | Float propertyValue = rs.getFloat(value); |
| | | propertyMap.put(value,propertyValue); |
| | | } |
| | | dataMap.put(key,propertyMap); |
| | | } |
| | | data.setPropertyInfo(dataMap); |
| | | list.add(data); |
| | | } |
| | | return list; |
| | |
| | | }); |
| | | return list; |
| | | } |
| | | |
| | | } |
| | |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.Stream; |
| | | |
| | | import com.whyc.pojo.db_user.User; |
| | | |
| | | public class ActionUtil { |
| | |
| | | System.out.println(date); |
| | | }*/ |
| | | //System.out.println(toCamelCase("user_name_and_age")); |
| | | Date time1=getDateAdd(new Date(),-10); |
| | | System.out.println(ActionUtil.sdf.format(time1)); |
| | | /*Date time1=getDateAdd(new Date(),-10); |
| | | System.out.println(ActionUtil.sdf.format(time1));*/ |
| | | Map<String, List<String>> map = new HashMap<>(); |
| | | map.put("key1", Stream.of("value1", "value2").collect(Collectors.toList())); |
| | | map.put("key2", Stream.of("value3", "value4").collect(Collectors.toList())); |
| | | String result = map.values().stream() |
| | | .flatMap(List::stream) |
| | | .collect(Collectors.joining(",")); |
| | | System.out.println( result); |
| | | } |
| | | } |
New file |
| | |
| | | package com.whyc.webSocket; |
| | | |
| | | import com.whyc.config.WebSocketConfig; |
| | | import com.whyc.dto.Real.PwrHisRealAcInDto; |
| | | import com.whyc.dto.RealDataStatic.ResRealDataAc; |
| | | import com.whyc.dto.RealDataStatic.SticRealDataAc; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.pojo.db_param.PowerheartParam; |
| | | import com.whyc.service.PowerheartParamService; |
| | | import com.whyc.service.PwrdevAcdcdataService; |
| | | import com.whyc.util.ActionUtil; |
| | | 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.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 实时数据统计推送交流单元,更改频率 |
| | | */ |
| | | @Component |
| | | @ServerEndpoint(value = "/realsticAc",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class) |
| | | public class RealDataStaticAcSocket { |
| | | private Session session; |
| | | |
| | | private Thread thread; |
| | | |
| | | private static PwrdevAcdcdataService acdcdataService; |
| | | |
| | | private static PowerheartParamService pwrHeartService; |
| | | |
| | | private static HttpSession httpSession; |
| | | |
| | | private volatile boolean runFlag = true; |
| | | |
| | | private volatile Map<String, Thread> threadMap = new HashMap<>(); |
| | | |
| | | private volatile Map<Long,Boolean> threadFlagMap = new HashMap<>(); |
| | | |
| | | @Autowired |
| | | public void setPwrdevAcdcdataService(PwrdevAcdcdataService service) { |
| | | RealDataStaticAcSocket.acdcdataService = service; |
| | | } |
| | | |
| | | @Autowired |
| | | public void setPowerheartParamService(PowerheartParamService service) { |
| | | RealDataStaticAcSocket.pwrHeartService = service; |
| | | } |
| | | |
| | | @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) { |
| | | SticRealDataAc realData= ActionUtil.getGson().fromJson(message,SticRealDataAc.class); |
| | | thread = new Thread("Thread_HeartacinSocket") { |
| | | @Override |
| | | public void run() { |
| | | while (runFlag && !isInterrupted()) { |
| | | Thread thread = currentThread(); |
| | | threadFlagMap.put(thread.getId(), true); |
| | | try { |
| | | ResRealDataAc dataAc = acdcdataService.getSticRealDataAc(realData); |
| | | if (session.isOpen()) { |
| | | //推送信息 |
| | | synchronized (session) { |
| | | session.getBasicRemote().sendObject(new Response<>().setII(1,dataAc!=null,dataAc,"实时数据统计-交流单元")); |
| | | } |
| | | threadFlagMap.put(thread.getId(), false); |
| | | } |
| | | PowerheartParam powerheartParam=pwrHeartService.getHeartParamByPowerId(realData.getPowerId()); |
| | | sleep(1000*60*powerheartParam.getAcinInterverCfg()); |
| | | } 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); |
| | | } |
| | | @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()); |
| | | } |
| | | } |