修改报警的数据结构

This commit is contained in:
joylink_zhangsai 2023-06-30 17:54:29 +08:00
parent 460a449f38
commit 1dfbacb370
12 changed files with 444 additions and 338 deletions

View File

@ -0,0 +1,12 @@
package club.joylink.xiannccda.alert;
import java.time.LocalDateTime;
public interface AlertDetail {
LocalDateTime getAlertTime();
Integer getAlertTipId();
String getInfo();
}

View File

@ -2,8 +2,10 @@ package club.joylink.xiannccda.alert;
import club.joylink.xiannccda.alert.core.AlertManager;
import club.joylink.xiannccda.event.Listener;
import club.joylink.xiannccda.ws.NccAlertMessageServer;
import club.joylink.xiannccda.ws.WsMessageServerManager;
import club.joylink.xiannccda.ws.XianNccAlertMessageServer;
import java.time.LocalDateTime;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
@ -20,22 +22,40 @@ public class AlertJob implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
//添加报警细节事件监听器
addAlertDetailListeners();
//添加报警事件监听器
addAlertListeners();
//注册西安NCC的报警ws消息发送服务
wsMessageServerManager.registerMessageServer(XianNccAlertMessageServer.getInstance());
wsMessageServerManager.registerMessageServer(NccAlertMessageServer.getInstance());
//启动报警源事件监测任务
AlertManager.getInstance().taskStart();
}
public void addAlertListeners() {
XianNccAlertMessageServer xianNccAlertMessageServer = XianNccAlertMessageServer.getInstance();
public void addAlertDetailListeners() {
AlertManager alertManager = AlertManager.getInstance();
//报警监听
alertManager.on(new Listener<XianNccAlertInfo>() {
alertManager.on(new Listener<AlertDetail>() {
private final AtomicInteger idGenerator = new AtomicInteger(1);
@Override
public void accept(XianNccAlertInfo event) {
xianNccAlertMessageServer.addMsg(event);
public void accept(AlertDetail event) {
String id = String.valueOf(idGenerator.getAndIncrement());
String level = "YELLOW";
LocalDateTime alertTime = event.getAlertTime();
NccAlertInfo<AlertDetail> nccAlertInfo = new NccAlertInfo<>(id, level, alertTime, event);
alertManager.emit(nccAlertInfo);
}
});
}
private void addAlertListeners() {
NccAlertMessageServer nccAlertMessageServer = NccAlertMessageServer.getInstance();
AlertManager alertManager = AlertManager.getInstance();
//报警监听
alertManager.on(new Listener<NccAlertInfo>() {
@Override
public void accept(NccAlertInfo event) {
nccAlertMessageServer.addMsg(event);
}
});
}

View File

@ -0,0 +1,37 @@
package club.joylink.xiannccda.alert;
import club.joylink.xiannccda.alert.core.AlertInfo;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class NccAlertInfo<D extends AlertDetail> implements AlertInfo {
private String id;
private String level;
private LocalDateTime alertTime;
private D detail;
@Override
public String getId() {
return id;
}
@Override
public String getLevel() {
return level;
}
@Override
public LocalDateTime getAlertTime() {
return alertTime;
}
@Override
public String getInfo() {
return detail == null ? "" : detail.getInfo();
}
public Integer getAlertTipId() {
return detail == null ? null : detail.getAlertTipId();
}
}

View File

@ -1,8 +0,0 @@
package club.joylink.xiannccda.alert;
import club.joylink.xiannccda.alert.core.AlertInfo;
public interface XianNccAlertInfo extends AlertInfo {
Integer getAlertTipId();
}

View File

@ -1,34 +1,20 @@
package club.joylink.xiannccda.alert;
package club.joylink.xiannccda.alert.xian3;
import club.joylink.xiannccda.alert.AlertDetail;
import club.joylink.xiannccda.alert.AlertType;
import java.time.LocalDateTime;
import lombok.Builder;
import lombok.Data;
import lombok.NonNull;
/**
* 西安三号线报警
*/
@Builder
@Data
public class TrainDelayAlert implements XianNccAlertInfo {
public class Xian3TrainDelayAlert implements AlertDetail {
@NonNull
private String id;
private String level;
private LocalDateTime alertTime;
private String info;
private Integer alertTipId;
@Override
public String getId() {
return id;
}
@Override
public String getLevel() {
return level;
}
@Override
public LocalDateTime getAlertTime() {
return alertTime;

View File

@ -1,6 +1,5 @@
package club.joylink.xiannccda.alert.xian3;
import club.joylink.xiannccda.alert.TrainDelayAlert;
import club.joylink.xiannccda.alert.core.AlertManager;
import club.joylink.xiannccda.alert.core.AlertMonitoringTask;
import java.time.LocalDateTime;
@ -30,9 +29,7 @@ public class Xian3TrainDelayAlertMonitoringTask implements AlertMonitoringTask {
} else {
if (now.getSecond() % 10 == 0) {
AlertManager alertManager = AlertManager.getInstance();
TrainDelayAlert alert = TrainDelayAlert.builder()
.id(String.valueOf(id.getAndIncrement()))
.level("YELLOW")
Xian3TrainDelayAlert alert = Xian3TrainDelayAlert.builder()
.alertTime(now)
.info(String.format("[3号线]列车[01-1001]按计划应于%s抵达[%s],现因[%s]晚点%s分钟",
now.minusMinutes(2), "鱼化寨", "道岔P0110失表", "2"))

View File

@ -62,13 +62,13 @@ public class EventEmitter {
private <E> void callInterfaceListener(Class<?> aClass, E event) {
for (Class<?> anInterface : aClass.getInterfaces()) {
callInterfaceListener(anInterface, event);
List<Listener<?>> superClassListeners = listenerMap.get(anInterface);
if (!CollectionUtils.isEmpty(superClassListeners)) {
for (Listener<?> listener : superClassListeners) {
((Listener) listener).accept(anInterface.cast(event));
}
}
callInterfaceListener(anInterface, event);
}
}

View File

@ -1,10 +1,12 @@
package club.joylink.xiannccda.ws;
import club.joylink.xiannccda.alert.XianNccAlertInfo;
import club.joylink.xiannccda.dto.protos.AlertInfoProto.AlertInfoMessage;
import club.joylink.xiannccda.dto.protos.AlertInfoProto.AlertInfoMessage.Builder;
import club.joylink.xiannccda.dto.protos.AlertInfoProto.AlertInfoMessage.Message;
import club.joylink.xiannccda.alert.NccAlertInfo;
import club.joylink.xiannccda.dto.protos.NccAlertInfoMessageProto.NccAlertInfoMessage;
import club.joylink.xiannccda.dto.protos.NccAlertInfoMessageProto.NccAlertInfoMessage.Builder;
import club.joylink.xiannccda.dto.protos.NccAlertInfoMessageProto.NccAlertInfoMessage.Message;
import com.google.protobuf.Int32Value;
import com.google.protobuf.Timestamp;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
@ -16,13 +18,13 @@ import java.util.concurrent.ConcurrentLinkedQueue;
/**
* 西安NCC系统消息管理服务
*/
public class XianNccAlertMessageServer implements IMessageServer {
private static final Map<String, XianNccAlertMessageServer> SERVER_MAP = new ConcurrentHashMap<>();
public class NccAlertMessageServer implements IMessageServer {
private static final Map<String, NccAlertMessageServer> SERVER_MAP = new ConcurrentHashMap<>();
private String id;
private final String Destination = "/queue/xian/ncc/alert";
private final LinkedHashMap<String, AlertInfoMessage.Message> allMsg = new LinkedHashMap<>();
Queue<XianNccAlertInfo> pendingMsgQueue = new ConcurrentLinkedQueue<>();
private final LinkedHashMap<String, NccAlertInfoMessage.Message> allMsg = new LinkedHashMap<>();
Queue<NccAlertInfo<?>> pendingMsgQueue = new ConcurrentLinkedQueue<>();
@Override
public String getDestinationPattern() {
@ -31,7 +33,7 @@ public class XianNccAlertMessageServer implements IMessageServer {
@Override
public Object onSubscription(String destination, Map<String, String> paramMap) {
byte[] bytes = AlertInfoMessage.newBuilder()
byte[] bytes = NccAlertInfoMessage.newBuilder()
.addAllMessages(allMsg.values())
.build()
.toByteArray();
@ -45,8 +47,8 @@ public class XianNccAlertMessageServer implements IMessageServer {
@Override
public List<TopicMessage> onTick() {
Builder builder = AlertInfoMessage.newBuilder();
XianNccAlertInfo alertInfo;
Builder builder = NccAlertInfoMessage.newBuilder();
NccAlertInfo<?> alertInfo;
for (int i = 0; i < 10; i++) { //加循环次数主要是为了防止while死循环
if ((alertInfo = pendingMsgQueue.poll()) != null) {
Message message = convertToMessage(alertInfo);
@ -63,31 +65,35 @@ public class XianNccAlertMessageServer implements IMessageServer {
return new ArrayList<>();
}
private XianNccAlertMessageServer(String id) {
private NccAlertMessageServer(String id) {
this.id = id;
}
public static XianNccAlertMessageServer getInstance() {
public static NccAlertMessageServer getInstance() {
return getInstance("default");
}
public static XianNccAlertMessageServer getInstance(String id) {
return SERVER_MAP.computeIfAbsent(id, k -> new XianNccAlertMessageServer(id));
public static NccAlertMessageServer getInstance(String id) {
return SERVER_MAP.computeIfAbsent(id, k -> new NccAlertMessageServer(id));
}
public static XianNccAlertMessageServer removeInstance(String id) {
public static NccAlertMessageServer removeInstance(String id) {
return SERVER_MAP.remove(id);
}
public void addMsg(XianNccAlertInfo alertInfo) {
public void addMsg(NccAlertInfo<?> alertInfo) {
pendingMsgQueue.add(alertInfo);
}
private AlertInfoMessage.Message convertToMessage(XianNccAlertInfo alertInfo) {
private NccAlertInfoMessage.Message convertToMessage(NccAlertInfo<?> alertInfo) {
Timestamp timestamp = Timestamp.newBuilder()
.setSeconds(alertInfo.getAlertTime().toEpochSecond(ZoneOffset.UTC))
.setNanos(alertInfo.getAlertTime().getNano())
.build();
Message.Builder builder = Message.newBuilder()
.setId(alertInfo.getId())
.setLevel(alertInfo.getLevel())
.setAlertTime(alertInfo.getAlertTime().toString())
.setAlertTime(timestamp)
.setInfo(alertInfo.getInfo());
Integer alertTipId = alertInfo.getAlertTipId();
if (alertTipId != null) {

View File

@ -14,6 +14,7 @@ import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.CollectionUtils;
import org.springframework.web.socket.messaging.SessionSubscribeEvent;
@Component
@ -40,6 +41,9 @@ public class WsMessageServerManager {
() -> {
try {
List<TopicMessage> topicMessages = messageServer.onTick();
if (CollectionUtils.isEmpty(topicMessages)) {
return;
}
for (TopicMessage topicMessage : topicMessages) {
if (topicMessage.getMsg() != null) {
smt.convertAndSend(topicMessage.destination, topicMessage.msg);

View File

@ -4,6 +4,7 @@ import club.joylink.xiannccda.alert.core.AlertManager;
import club.joylink.xiannccda.alert.core.AlertMonitoringTask;
import club.joylink.xiannccda.alert.core.AlertSourceEvent;
import club.joylink.xiannccda.alert.xian3.Xian3SupplyShortageAlert;
import club.joylink.xiannccda.alert.xian3.Xian3TrainDelayAlert;
import club.joylink.xiannccda.event.Listener;
import java.util.ArrayList;
import java.util.List;
@ -22,7 +23,7 @@ public class AlertManagerTest {
//可能报警事件监听器
manager.on(new SuppliesRemainInsufficientListener(sid));
//报警事件监听器
manager.on((Listener<TrainDelayAlert>) event -> System.out.println("列车延误报警"));
manager.on((Listener<Xian3TrainDelayAlert>) event -> System.out.println("列车延误报警"));
manager.on((Listener<Xian3SupplyShortageAlert>) event -> System.out.println("物资紧缺报警"));
manager.taskStart();
@ -47,7 +48,6 @@ public class AlertManagerTest {
@Override
public void run() {
AlertManager alertManager = AlertManager.getInstance(sId);
alertManager.emit(TrainDelayAlert.builder().id("1").build());
}
}

@ -1 +1 @@
Subproject commit 9a7b4b7a3a912970197155a149a9da463d2a68fc
Subproject commit a5229b090fd21a5f72cd437ccb9623257724cbed