package com.gyee.runeconomy.controller.agc; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.gyee.common.model.PointData; import com.gyee.runeconomy.config.GyeeConfig; import com.gyee.runeconomy.init.CacheContext; import com.gyee.runeconomy.model.StatusTime; import com.gyee.runeconomy.model.auto.PointInfo; import com.gyee.runeconomy.model.auto.ProBasicEquipment; import com.gyee.runeconomy.model.auto.ProBasicEquipmentPoint; import com.gyee.runeconomy.service.agc.AgcDeviateService; import com.gyee.runeconomy.service.auto.IPointInfoService; import com.gyee.runeconomy.util.realtimesource.IEdosUtil; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; /** * 获取各个场站AGC偏差分析数据 */ @RestController @RequestMapping("/agc") public class AgcDeviateController { public static Map> fitcoef = new ConcurrentHashMap<>(); @Resource private AgcDeviateService agcDeviateService; @Resource private GyeeConfig gyeeConfig; @Resource private StringRedisTemplate stringRedisTemplate; @Resource private IEdosUtil edosUtil; @Resource private IPointInfoService pointInfoService; /** * 获取偏差信息 * * @param startTs 开始时间 * @param endTs 结束时间 * @param id 场站id * @param interval 时间间隔 * @return */ @GetMapping("/deviate") public Map getData(@RequestParam(value = "startTs") long startTs, @RequestParam(value = "endTs") long endTs, @RequestParam(value = "id") String id, @RequestParam(value = "interval", defaultValue = "60", required = false) int interval) { List ls = new ArrayList<>(); if (gyeeConfig.isOffLine()) { ls = agcDeviateService.getAgcDeviateTags(id, startTs, endTs, interval); } else { ls = agcDeviateService.getAgcDeviateTags(id, startTs, endTs, interval); } return ls.stream().collect(Collectors.toMap(AgcDeviateTag::getName, f -> f)); } /** * 获取配置 * * @return */ @GetMapping("/config") public Object getAgcConifg() { return agcDeviateService.getConfig(); } @GetMapping("/windturbine/curve") public Map> getWindturbineData(@RequestParam(value = "startTs") long startTs, @RequestParam(value = "endTs") long endTs, @RequestParam(value = "windturbineId") String windturbineId, @RequestParam(value = "interval", defaultValue = "60", required = false) int interval) throws Exception { //切换数据库 LettuceConnectionFactory factory = (LettuceConnectionFactory) stringRedisTemplate.getConnectionFactory(); int database = factory.getDatabase(); factory.setDatabase(5); stringRedisTemplate.setConnectionFactory(factory); factory.afterPropertiesSet(); factory.resetConnection(); DateTime date = DateUtil.date(endTs * 1000); Map> map = new HashMap<>(); List ls = new ArrayList<>(); String[] wtIds = windturbineId.split(","); List wtIdsLs = Arrays.asList(wtIds); for (String wtId : wtIdsLs) { SpeedPowerAnalysis spa = new SpeedPowerAnalysis(); List proBasicEquipmentPoints = CacheContext.pointNewMap.get(wtId); List speedPoint = proBasicEquipmentPoints.stream().filter(p -> p.getUniformCode().equals("AI066")).collect(Collectors.toList()); List historyspeedDatas = edosUtil.getHistoryDatasSnap(speedPoint.get(0).getNemCode(), startTs, endTs, interval); List speedDatas = historyspeedDatas.stream().map(PointData::getPointValueInDouble).collect(Collectors.toList()); spa.setWindSpeed(speedDatas); List powerPoint = proBasicEquipmentPoints.stream().filter(p -> p.getUniformCode().equals("AI114")).collect(Collectors.toList()); List historyPowerDatas = edosUtil.getHistoryDatasSnap(powerPoint.get(0).getNemCode(), startTs, endTs, interval); List powerData = historyPowerDatas.stream().map(PointData::getPointValueInDouble).collect(Collectors.toList()); spa.setActuatedPower(powerData); String powerCurveMonth = "glqxnh:" + date.month() + ":"; Set keys = stringRedisTemplate.keys(powerCurveMonth + wtId); Iterator iterator = keys.iterator(); // 获取迭代器 String firstKey = iterator.hasNext() ? iterator.next() : null; String yc = stringRedisTemplate.opsForValue().get(firstKey); ConcurrentHashMap wtyc = JSON.parseObject(yc, new TypeReference>() { }.getType()); fitcoef.put(firstKey.replaceFirst(powerCurveMonth, ""), wtyc); List llgl = speedDatas.stream().map(a -> fitcoef.get(wtId).get(a)).collect(Collectors.toList()); spa.setTheoreticalPower(llgl); ls.add(spa); map.put(wtId, ls); } //关闭数据库切换 factory.setDatabase(database); factory.afterPropertiesSet(); factory.resetConnection(); return map; } @GetMapping("/windtur") public Map> getTestData(@RequestParam(value = "startTs") long startTs, @RequestParam(value = "endTs") long endTs, @RequestParam(value = "uniformcode") String uniformcode) throws Exception { Map> map = new HashMap<>(); QueryWrapper qw = new QueryWrapper<>(); qw.lambda().eq(PointInfo::getUniformCode, uniformcode); List list = pointInfoService.list(qw); for (PointInfo ls : list) { List ll = new ArrayList<>(); List historyDatasRaw = edosUtil.getHistoryDatasRaw(ls.getPointKey(), startTs, endTs); for (PointData pointData : historyDatasRaw) { StatusTime st = new StatusTime(); st.setName(ls.getName()); Date da=new Date(pointData.getPointTime()); st.setTime(DateUtil.formatDateTime(da)); String getstatus = getstatus((int) pointData.getPointValueInDouble()); st.setStatus(getstatus); ll.add(st); } List collect = CacheContext.wtls.stream().filter(w -> w.getId().equals(ls.getTurbineId())).map(ProBasicEquipment::getNemCode).collect(Collectors.toList()); map.put(collect.get(0), ll); } List sortedKeys = new ArrayList<>(map.keySet()); Collections.sort(sortedKeys); Map> sortedMap = new LinkedHashMap<>(); for (String key : sortedKeys) { sortedMap.put(key, map.get(key)); } return sortedMap; } private String getstatus(int number) { String statusText = ""; switch (number) { case 0: statusText = "待机"; break; case 1: statusText = "停机"; break; case 2: statusText = "并网"; break; case 4: statusText = "故障"; break; case 6: statusText = "检修"; break; case 8: statusText = "限电"; break; case 12: statusText = "离线"; break; default: statusText = "未知"; break; } return statusText; } }