Commit de6cffc6 authored by duwei's avatar duwei

信号灯倒计时,加入下发控制,1秒1帧

parent c30538f6
......@@ -128,7 +128,7 @@ public class SignalStatusTask {
/**
* 消费孪生灯态kafka数据
*/
@KafkaListener(topics = {"WHSpatData"}, groupId = "utc-whspat-consumer-05")
@KafkaListener(topics = {"WHSpatData"}, groupId = "utc-whspat-consumer-08")
public void consumeTwinSpat(ConsumerRecord<Object, Object> record, Acknowledgment ack) {
try {
String lightStatusJson = String.valueOf(record.value());
......@@ -209,6 +209,16 @@ public class SignalStatusTask {
// 记录每个路口最后发送灯态的时间戳,单位毫秒
private static final Map<String, Long> lastSentTimestampMap = new ConcurrentHashMap<>();
//map<路口id_方向_转向_灯色_倒计时, 1>
public static final ExpiringMap<String, Integer> controlSendMap = ExpiringMap.builder()
.expiration(10, TimeUnit.SECONDS)
.maxSize(1000)
.variableExpiration()
.expirationPolicy(ExpirationPolicy.CREATED)
//过期监听
.asyncExpirationListener((rcuIdStr, value) -> {
}).build();
/**
* 保存灯态数据并发送
......@@ -227,7 +237,7 @@ public class SignalStatusTask {
//2. 循环遍历灯态数据,并保存灯态数据到数据库,并发送灯态数据到websocket
for (LightsStatusVO lightsStatusVO : lightsStatusVOS) {
if (Objects.nonNull(lightsStatusVO) && Objects.equals(lightsStatusVO.getCrossId(), crossId) ) {
//
calculateControlCountDown(crossId, lightsStatusVO);
try {
// 相同路口不同websocket统一发送灯态
......@@ -235,14 +245,33 @@ public class SignalStatusTask {
if (CollectionUtil.isEmpty(listResult2)) {
continue;
}
//控制下发频率,条件:方向_转向_灯色_时间戳
String key = crossId
+ "_" +listResult2.get(0).getDirLampGroupMapList().get(0).getDir()
+ "_" + listResult2.get(0).getDirLampGroupMapList().get(0).getTurnList().get(0).getTurn()
+ "_" + listResult2.get(0).getDirLampGroupMapList().get(0).getTurnList().get(0).getColor()
+ "_" + listResult2.get(0).getDirLampGroupMapList().get(0).getTurnList().get(0).getCountDown();
Integer f = controlSendMap.get(key);
if (f != null && f == 1) {
continue;
} else {
controlSendMap.put(key, 1);
}
String json = mapper.writeValueAsString(listResult2);
// 新增:判断是否达到1秒间隔
long now = System.currentTimeMillis();
long lastSent = lastSentTimestampMap.getOrDefault(crossId, 0L);
if (now - lastSent < 1000) {
// 不足1秒,跳过本次推送
return;
while (true) {
long lastSent = lastSentTimestampMap.getOrDefault(crossId, 0L);
if (now - lastSent > 1000) {
// 更新最后发送时间
lastSentTimestampMap.put(crossId, now);
// 不足1秒,跳过本次推送
break;
}
Thread.sleep(100);
now = System.currentTimeMillis();
}
for (Map.Entry<String, RealTimeDataWebSocket> socketEntry : value) {
......@@ -252,9 +281,6 @@ public class SignalStatusTask {
webSocket.sendInfo(json, crossIdStr);
}
// 更新最后发送时间
lastSentTimestampMap.put(crossId, now);
insertIntoHist(crossId, json);
produceListMap.put(crossId, listResult2);
} catch (Exception e) {
......@@ -355,19 +381,30 @@ public class SignalStatusTask {
}
//后两个参数是map<方向,倒计时>, 倒计时
Map<String, Object> dirLampGroupMap1 = infoVo.getDirLampGroupMap();
Map<String, Object> existDirLampGroupMap = infoVo.getDirLampGroupMap();
Map<String, Object> dirLampGroupMap = getDirLampGroupMap(p, m, phaseMap, p.getLimitEndTime());
if (dirLampGroupMap1 != null) {
if (dirLampGroupMap != null) {
dirLampGroupMap1.putAll(dirLampGroupMap);
}
}else {
if (dirLampGroupMap != null){
infoVo.setDirLampGroupMap(dirLampGroupMap);
}else{
infoVo.setDirLampGroupMap(Maps.newHashMap());
if (dirLampGroupMap != null) {
for (Map.Entry<String, Object> entry : dirLampGroupMap.entrySet()) {
String direction = entry.getKey();
Map<Integer, String> turnColorMap = (Map<Integer, String>) entry.getValue();
if (existDirLampGroupMap != null) {
if (existDirLampGroupMap.containsKey(direction)) {
// 方向已存在,合并内部 Map
Map<Integer, String> existingTurnMap = (Map<Integer, String>) existDirLampGroupMap.get(direction);
existingTurnMap.putAll(turnColorMap); // 覆盖同 key 的值
} else {
// 方向不存在,直接放入
existDirLampGroupMap.put(direction, new HashMap<>(turnColorMap));
}
}else{
existDirLampGroupMap = new HashMap<>();
existDirLampGroupMap.put(direction, new HashMap<>(turnColorMap));
infoVo.setDirLampGroupMap(existDirLampGroupMap);
}
}
}
}
infoVo.setTimeStamp(light.getTimestamp() + "");
infoVo.setSchemeStartTime(light.getTimestamp() + "");
......@@ -396,6 +433,9 @@ public class SignalStatusTask {
String color = transferLampGroup(light);//车道灯态适配
int turn = phase.getTurn();
int turnDir = TurnDirectionMapper.mapToTargetValue(turn);//车道转向适配
// if (0 == turn || 2 == turn){
// System.out.println("无效的车道转向:" + turn);
// }
if (m.containsKey(dir + "-" + turnDir + "-" + color)){
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