Commit e7d0d0ea authored by duanruiming's avatar duanruiming

[update] 优化海信发送http灯态,通过方案倒计时计算灯态,通过时段切换方案

parent b13b8f7b
......@@ -78,11 +78,12 @@ public class CrossPhaseDirTurnCache implements CommandLineRunner {
List<CrossLightsPO> crossLightsPOS = crossLightsMapper.selectByCrossId(crossId);
Map<Integer, List<CrossLightsPO>> phaseDirMap = crossLightsPOS.stream().filter(po -> phaseLightsList.contains(po.getId())).collect(Collectors.groupingBy(CrossLightsPO::getDir));
Map<Integer, List<Integer>> dirTurnMap = new HashMap<>();
Map<Integer, List<CrossLightsPO>> dirTurnMap = new HashMap<>();
for (Map.Entry<Integer, List<CrossLightsPO>> entry : phaseDirMap.entrySet()) {
Integer dir = entry.getKey();
List<Integer> turnList = entry.getValue().stream().map(CrossLightsPO::getTurn).sorted().collect(Collectors.toList());
dirTurnMap.put(dir, turnList);
// List<Integer> turnList = entry.getValue().stream().map(CrossLightsPO::getTurn).sorted().collect(Collectors.toList());
// dirTurnMap.put(dir, turnList);
dirTurnMap.put(dir, entry.getValue());
crossPhaseDirTurnPojo.setDirTurnMap(dirTurnMap);
}
results.add(crossPhaseDirTurnPojo);
......
package net.wanji.utc.hisense.cache;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.wanji.common.framework.Constants;
import net.wanji.common.utils.tool.DateUtil;
import net.wanji.common.utils.tool.StringUtils;
import net.wanji.databus.dao.entity.CrossSchedulesPO;
import net.wanji.databus.dao.entity.CrossSchemePO;
import net.wanji.databus.dao.entity.CrossSectionPO;
import net.wanji.databus.dao.mapper.CrossSchedulesMapper;
import net.wanji.databus.dao.mapper.CrossSchemeMapper;
import net.wanji.databus.dao.mapper.CrossSectionMapper;
import net.wanji.databus.po.CrossInfoPO;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author duanruiming
* @date 2023/09/23 17:26
*/
@Slf4j
@Component
@SuppressWarnings("unchecked")
@RequiredArgsConstructor
public class CrossRunSchemeCache implements CommandLineRunner {
public static final Map<String, String> currentRunSchemeNoCache = new HashMap<>();
@Resource
private CrossSchedulesMapper crossSchedulesMapper;
@Resource
private CrossSectionMapper crossSectionMapper;
@Resource
private CrossSchemeMapper crossSchemeMapper;
@Override
public void run(String... args) throws Exception {
init();
}
private void init() throws Exception {
Map<String, CrossInfoPO> crossInfoCache = CrossInfoCache.getCrossInfoCache();
if (!crossInfoCache.isEmpty()) {
for (Map.Entry<String, CrossInfoPO> entry : crossInfoCache.entrySet()) {
String crossId = entry.getKey();
List<CrossSectionPO> crossSectionPOS = crossSectionMapper.selectByCrossId(crossId);
List<CrossSchedulesPO> crossSchedulesPOS = crossSchedulesMapper.selectByCrossIds(Arrays.asList(crossId));
if (!CollectionUtils.isEmpty(crossSchedulesPOS)) {
Map<Integer, List<CrossSchedulesPO>> schedulesMap = crossSchedulesPOS.stream().collect(Collectors.groupingBy(CrossSchedulesPO::getPlanId));
// 判断当前执行的调度,获取日计划号
Date date = new Date();
LocalTime currentTime = LocalTime.parse(DateUtil.getTime());
Integer currentPlanId = getCurrentPlanId(schedulesMap, date);
Integer schemeId = getCurrentSchemeId(crossSectionPOS, currentTime, currentPlanId);
CrossSchemePO crossSchemePO = crossSchemeMapper.selectSchemePOById(schemeId);
currentRunSchemeNoCache.put(crossId, crossSchemePO.getSchemeNo());
}
}
}
}
private static Integer getCurrentSchemeId(List<CrossSectionPO> crossSectionPOS, LocalTime currentTime, Integer currentPlanId) {
if (!CollectionUtils.isEmpty(crossSectionPOS)) {
List<CrossSectionPO> currentPlanIdSectionList = crossSectionPOS.stream().filter(po -> Objects.equals(currentPlanId, po.getPlanId())).collect(Collectors.toList());
for (CrossSectionPO crossSectionPO : currentPlanIdSectionList) {
Integer schemeId = crossSectionPO.getSchemeId();
LocalTime startTime = LocalTime.parse(crossSectionPO.getStartTime());
LocalTime endTime = LocalTime.parse(crossSectionPO.getEndTime());
// 过滤非当前时段数据
if (currentTime.isAfter(startTime) && currentTime.isBefore(endTime)) {
return schemeId;
}
}
}
return 0;
}
private static Integer getCurrentPlanId(Map<Integer, List<CrossSchedulesPO>> schedulesMap, Date currentDate) {
for (Map.Entry<Integer, List<CrossSchedulesPO>> entry : schedulesMap.entrySet()) {
Integer planId = entry.getKey();
List<CrossSchedulesPO> schedulesPOS = entry.getValue();
if (!CollectionUtils.isEmpty(schedulesPOS)) {
for (CrossSchedulesPO schedulesPO : schedulesPOS) {
Integer isSpectialDay = schedulesPO.getWeek(); // 0 为特殊日期
Date specialDate = schedulesPO.getSpecialDate();
String specialDate4DB = DateUtil.format(specialDate, Constants.DATE_FORMAT.E_DATE_FORMAT_SECOND);
String currentDateStr = DateUtil.format(currentDate, Constants.DATE_FORMAT.E_DATE_FORMAT_SECOND);
if (Objects.equals(0, isSpectialDay)) {
if (StringUtils.equalsIgnoreCase(currentDateStr.substring(0, 10), specialDate4DB.substring(0, 10))) {
return planId;
}
}
Integer week4DB = schedulesPO.getWeek();
int weekCurrent = DateUtil.getWeek(currentDate);
if (week4DB == weekCurrent) {
return planId;
}
}
}
}
return 0;
}
}
package net.wanji.utc.hisense.pojo;
import lombok.Data;
import net.wanji.databus.dao.entity.CrossLightsPO;
import java.util.List;
import java.util.Map;
......@@ -14,5 +15,5 @@ public class CrossPhaseDirTurnPojo {
private String crossId;
private Integer phaseNo;
private Integer schemeNo;
private Map<Integer, List<Integer>> dirTurnMap;
private Map<Integer, List<CrossLightsPO>> dirTurnMap;
}
......@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import net.wanji.common.utils.tool.JacksonUtils;
import net.wanji.common.utils.tool.StringUtils;
import net.wanji.databus.dao.entity.CrossLightsPO;
import net.wanji.databus.po.CrossInfoPO;
import net.wanji.databus.po.SignalStatusLogPO;
import net.wanji.databus.vo.LightsStatusVO;
......@@ -138,14 +139,16 @@ public class SignalStatusServiceImpl implements SignalStatusService {
if (!CollectionUtils.isEmpty(crossPhaseDirTurnList)) {
List<CrossPhaseDirTurnPojo> schemeNoList = crossPhaseDirTurnList.stream().filter(po -> Objects.equals(po.getSchemeNo(), planId)).collect(Collectors.toList());
for (CrossPhaseDirTurnPojo crossPhaseDirTurnPojo : schemeNoList) {
Map<Integer, List<Integer>> dirTurnCache = crossPhaseDirTurnPojo.getDirTurnMap();
for (Map.Entry<Integer, List<Integer>> dirTurnCacheEntry : dirTurnCache.entrySet()) {
// Map<Integer, List<Integer>> dirTurnCache = crossPhaseDirTurnPojo.getDirTurnMap();
Map<Integer, List<CrossLightsPO>> dirTurnMap = crossPhaseDirTurnPojo.getDirTurnMap();
for (Map.Entry<Integer, List<CrossLightsPO>> dirTurnCacheEntry : dirTurnMap.entrySet()) {
Integer dirCache = dirTurnCacheEntry.getKey();
for (Map.Entry<Integer, List<Integer>> hisEntry : hisDirTurnMap.entrySet()) {
Integer hisDir = hisEntry.getKey();
if (Objects.equals(dirCache, CrossLisghtsLaneDirEnum.getLightsDirByLaneDir(hisDir))) {
List<Integer> turnListCache = dirTurnCacheEntry.getValue();
if (hisEntry.getValue().stream().allMatch(turnListCache::contains)) {
List<CrossLightsPO> turnListCache = dirTurnCacheEntry.getValue();
List<Integer> turnList = turnListCache.stream().map(CrossLightsPO::getTurn).sorted().collect(Collectors.toList());
if (hisEntry.getValue().stream().allMatch(turnList::contains)) {
phaseNo = crossPhaseDirTurnPojo.getPhaseNo();
}
}
......
......@@ -2,23 +2,25 @@ package net.wanji.utc.hisense.task;
import lombok.extern.slf4j.Slf4j;
import net.wanji.common.utils.tool.StringUtils;
import net.wanji.databus.dao.entity.CrossLightsPO;
import net.wanji.databus.dao.entity.CrossPhasePO;
import net.wanji.databus.dao.entity.CrossSchemePO;
import net.wanji.databus.dao.mapper.CrossPhaseMapper;
import net.wanji.databus.dao.mapper.CrossSchemeMapper;
import net.wanji.databus.vo.LightsStatusVO;
import net.wanji.utc.hisense.cache.CrossPhaseDirTurnCache;
import net.wanji.utc.hisense.cache.CrossRunSchemeCache;
import net.wanji.utc.hisense.cache.CrossSchemePhaseTimeCountCache;
import net.wanji.utc.hisense.cache.SignalDataCache;
import net.wanji.utc.hisense.pojo.CrossPhaseDirTurnPojo;
import net.wanji.utc.hisense.pojo.dto.CrossSchemePhaseCountDownDTO;
import net.wanji.utc.hisense.pojo.dto.PhaseCountDownDTO;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
/**
* @author duanruiming
......@@ -35,20 +37,38 @@ public class HisensePhaseCountDownTask {
@Scheduled(fixedRate = 1000, initialDelay = 1000)
public void phaseTimeCountDown() {
Map<String, LightsStatusVO> runningStateInfoCache4DataBrain = SignalDataCache.runningStateInfoCache;
Map<String, LightsStatusVO> runningStateInfoCache = SignalDataCache.runningStateInfoCache;
Map<String, Long> runningStatusStampMap = SignalDataCache.runningStatusStampMap;
if (!runningStateInfoCache4DataBrain.isEmpty()) {
for (Map.Entry<String, LightsStatusVO> entry : runningStateInfoCache4DataBrain.entrySet()) {
if (!runningStateInfoCache.isEmpty()) {
for (Map.Entry<String, LightsStatusVO> entry : runningStateInfoCache.entrySet()) {
String crossId = entry.getKey();
LightsStatusVO lightsStatusVO = entry.getValue();
String schemeStartTime = lightsStatusVO.getSchemeStartTime();
Long lastPhaseTimeStamp = runningStatusStampMap.get(crossId);
String phaseId = lightsStatusVO.getPhaseId();
String schemeId = lightsStatusVO.getSchemeId();
// log.error("当前相位号:{},上一时间戳:{},当前时间时间戳:{}", crossId, lastPhaseTimeStamp, schemeStartTime);
// log.error("当前路口参数:{}", entry);
if (Objects.nonNull(lastPhaseTimeStamp) && StringUtils.equalsIgnoreCase(schemeStartTime, String.valueOf(lastPhaseTimeStamp))) {
CrossSchemePO crossSchemePO = crossSchemeMapper.selectByCrossIdAndSchemeNo(crossId, Integer.valueOf(schemeId));
// 通过时段表,切换方案
String nextSectionSchemeNo = CrossRunSchemeCache.currentRunSchemeNoCache.get(crossId);
if (!StringUtils.equalsIgnoreCase(nextSectionSchemeNo, schemeId)) {
executeNextSectionScheme();
} else {
}
// 当前方案下一周期
executeNextPeriod(crossId, lightsStatusVO, phaseId, crossSchemePO);
setDirLampGroupMap(lightsStatusVO);
runningStateInfoCache.put(crossId, lightsStatusVO);
}
}
}
}
private void executeNextSectionScheme() {
}
private void executeNextPeriod(String crossId, LightsStatusVO lightsStatusVO, String phaseId, CrossSchemePO crossSchemePO) {
if (lightsStatusVO.getCyclePhaseCountDown() == 0 && Objects.nonNull(crossSchemePO)) {
List<CrossPhasePO> crossPhasePOS = crossPhaseMapper.selectByCrossIdAndPlanId(crossId, String.valueOf(crossSchemePO.getId()));
Integer nextPhaseTime = 0;
......@@ -71,11 +91,6 @@ public class HisensePhaseCountDownTask {
} else {
lightsStatusVO.setCyclePhaseCountDown(lightsStatusVO.getCyclePhaseCountDown() - 1);
}
setDirLampGroupMap(lightsStatusVO);
runningStateInfoCache4DataBrain.put(crossId, lightsStatusVO);
}
}
}
}
public void setDirLampGroupMap(LightsStatusVO lightsStatusVO) {
......@@ -85,47 +100,92 @@ public class HisensePhaseCountDownTask {
Integer cyclePhaseCountDown = lightsStatusVO.getCyclePhaseCountDown();
List<CrossPhaseDirTurnPojo> crossPhaseDirTurnCache = CrossPhaseDirTurnCache.getCrossPhaseDirTurnCache(crossId);
if (!CollectionUtils.isEmpty(crossPhaseDirTurnCache)) {
Map<String,Object> dirLampGroupMap = new HashMap<>();
Map<String, Object> dirLampGroupMap = new HashMap<>();
for (CrossPhaseDirTurnPojo phaseDirTurnPojo : crossPhaseDirTurnCache) {
String schemeNo = String.valueOf(phaseDirTurnPojo.getSchemeNo());
String phaseNo = String.valueOf(phaseDirTurnPojo.getPhaseNo());
if (StringUtils.equalsIgnoreCase(schemeNo, currentSchemeNo) && StringUtils.equalsIgnoreCase(currentPhaseNo, String.valueOf(phaseNo))) {
Map<Integer, List<Integer>> dirTurnMap = phaseDirTurnPojo.getDirTurnMap();
PhaseCountDownDTO phaseCountDownDTO = getPhaseCountDownDTO(crossId, schemeNo, phaseNo);
Integer yellowTime = phaseCountDownDTO.getYellowTime();
Integer redTime = phaseCountDownDTO.getRedTime();
Map<Integer, List<CrossLightsPO>> dirTurnMap = phaseDirTurnPojo.getDirTurnMap();
if (!dirTurnMap.isEmpty()) {
for (Map.Entry<Integer, List<Integer>> dirEntry : dirTurnMap.entrySet()) {
for (Map.Entry<Integer, List<CrossLightsPO>> dirEntry : dirTurnMap.entrySet()) {
Integer dir = dirEntry.getKey();
List<Integer> turnList = dirEntry.getValue();
if (CollectionUtils.isEmpty(turnList)) {
List<CrossLightsPO> lightsList = dirEntry.getValue();
if (CollectionUtils.isEmpty(lightsList)) {
continue;
}
HashMap<String, String> turnColor = new HashMap<>();
for (Integer turn : turnList) {
Map<String, String> turnColor = new HashMap<>();
Integer type = 2; // 1 圆饼灯
String color = null;
for (CrossLightsPO crossLightsPO : lightsList) {
type = crossLightsPO.getType();
Integer turn = crossLightsPO.getTurn();
String turnStr = String.valueOf(turn);
if (cyclePhaseCountDown > 5) {
turnColor.put(turnStr, "green");
if (cyclePhaseCountDown > yellowTime + redTime) {
color = "green";
if (turn == 20) {
turnColor.put(turnStr, "red");
}
}
if (cyclePhaseCountDown <= 5 && cyclePhaseCountDown > 2) {
turnColor.put(turnStr, "yellow");
if (cyclePhaseCountDown <= yellowTime + redTime && cyclePhaseCountDown > redTime) {
color = "yellow";
if (turn == 20) {
turnColor.put(turnStr, "red");
}
}
if (cyclePhaseCountDown < 2) {
turnColor.put(turnStr, "red");
if (cyclePhaseCountDown < redTime) {
color = "red";
if (turn == 20) {
turnColor.put(turnStr, "green");
}
}
turnColor.put(turnStr, color);
}
setAllTurnColor(turnColor, type, color);
dirLampGroupMap.put(String.valueOf(dir), turnColor);
}
}
}
lightsStatusVO.setDirLampGroupMap(dirLampGroupMap);
}
}
}
}
private static void setAllTurnColor(Map<String, String> turnColor, Integer type, String color) {
List<String> allTurn = Arrays.asList("1", "2", "3");
for (String turn : allTurn) {
if (!turnColor.containsKey(turn)) {
if (Objects.equals(type, 1)) { // 圆饼灯 相同颜色
turnColor.put(turn, color);
} else {
if ("red".equals(color)) { // 箭头灯 相反
turnColor.put(turn, "green");
} else { // green yellow 都是黄色
turnColor.put(turn, "red");
}
}
}
}
}
private PhaseCountDownDTO getPhaseCountDownDTO(String crossId, String schemeNo, String phaseNo) {
List<CrossSchemePhaseCountDownDTO> crossSchemePhaseCountDownList = CrossSchemePhaseTimeCountCache.crossSchemePhaseCountDownList;
if (!CollectionUtils.isEmpty(crossSchemePhaseCountDownList)) {
for (CrossSchemePhaseCountDownDTO dto : crossSchemePhaseCountDownList) {
if (Objects.equals(crossId, dto.getCrossId()) && Objects.equals(schemeNo, dto.getSchemeNo())) {
List<PhaseCountDownDTO> phaseCountDownDTOList = dto.getPhaseCountDownDTOList();
if (!CollectionUtils.isEmpty(phaseCountDownDTOList)) {
for (PhaseCountDownDTO phaseCountDownDTO : phaseCountDownDTOList) {
if (Objects.equals(phaseNo, phaseCountDownDTO.getPhaseNo())) {
return phaseCountDownDTO;
}
}
}
}
}
}
return null;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment