当移除仿真用户时,发送ws消息给前端

This commit is contained in:
joylink_zhangsai 2023-05-22 17:17:01 +08:00
parent 4f599acffe
commit 477bda4557
7 changed files with 41 additions and 136 deletions

View File

@ -1,14 +1,10 @@
package club.joylink.rtss.simulation.cbtc;
import club.joylink.rtss.configuration.configProp.OtherConfig;
import club.joylink.rtss.configuration.configProp.WeChatConfig;
import club.joylink.rtss.constants.MapPrdTypeEnum;
import club.joylink.rtss.constants.ProjectDeviceType;
import club.joylink.rtss.entity.project.Project;
import club.joylink.rtss.exception.BusinessExceptionAssertEnum;
import club.joylink.rtss.services.*;
import club.joylink.rtss.services.org.IOrgService;
import club.joylink.rtss.services.org.IOrgUserService;
import club.joylink.rtss.services.project.ProjectService;
import club.joylink.rtss.services.runplan.IRunPlanParktimeService;
import club.joylink.rtss.services.runplan.IRunPlanRunlevelService;
@ -28,7 +24,6 @@ import club.joylink.rtss.simulation.cbtc.command.CommandExecuteService;
import club.joylink.rtss.simulation.cbtc.command.CommandInitiateVO;
import club.joylink.rtss.simulation.cbtc.communication.vo.fault.DeviceFaultInfo;
import club.joylink.rtss.simulation.cbtc.constant.SimulationConstants;
import club.joylink.rtss.simulation.cbtc.conversation.ConversationManagerService;
import club.joylink.rtss.simulation.cbtc.data.SimulationDataRepository;
import club.joylink.rtss.simulation.cbtc.data.SimulationIscsDataRepository;
import club.joylink.rtss.simulation.cbtc.data.map.DestinationCodeDefinition;
@ -121,28 +116,18 @@ public class GroupSimulationServiceImpl implements GroupSimulationService {
@Autowired
private IExamService iExamService;
@Autowired
private ILessonService iLessonService;
@Autowired
private StompMessageService stompMessageService;
@Autowired
private IScriptService iScriptService;
@Autowired
private OtherConfig otherConfig;
@Autowired
private WeChatConfig weChatConfig;
@Autowired
private IScriptSimulationService iScriptSimulationService;
@Autowired
private SimulationRealDeviceConnectManager simulationRealDeviceConnectManager;
@Autowired
private PassengerFlowSimulateService passengerFlowSimulateService;
@Autowired
private IOrgUserService iOrgUserService;
@Autowired
private IUserPermissionService iUserPermissionService;
@Autowired
private IOrgService iOrgService;
@Autowired
private SimulationManager simulationManager;
@Autowired
private LoginSessionManager loginSessionManager;
@ -155,13 +140,9 @@ public class GroupSimulationServiceImpl implements GroupSimulationService {
@Autowired
private QRCodeManager qrCodeManager;
@Autowired
private ConversationManagerService conversationManagerService;
@Autowired
private ProjectService projectService;
@Autowired
private SimulationService simulationService;
@Autowired
private RunPlanDraftService runPlanDraftService;
/**
@ -844,6 +825,9 @@ public class GroupSimulationServiceImpl implements GroupSimulationService {
this.memberManager.cancelPlay(simulation, simulationUser);
// 移除仿真用户
simulation.removeSimulationUser(simulationUser);
Set<String> userIds = simulation.getSimulationUserIds();
SocketMessageVO<SimulationUserMessageVO> message = SocketMessageFactory.buildSimulationUserExitMessage(simulation, simulationUser);
stompMessageService.sendToUser(userIds, message);
}
@Override

View File

@ -1,11 +1,8 @@
package club.joylink.rtss.simulation.cbtc;
import club.joylink.rtss.event.OrderPaySuccessEvent;
import club.joylink.rtss.simulation.cbtc.ATS.data.AtsAlarm;
import club.joylink.rtss.simulation.cbtc.conversation.Conversation;
import club.joylink.rtss.simulation.cbtc.conversation.ConversationMessage;
import club.joylink.rtss.simulation.cbtc.data.plan.RealRun;
import club.joylink.rtss.simulation.cbtc.data.status.DeviceStatus;
import club.joylink.rtss.simulation.cbtc.data.status.IbpStatus;
import club.joylink.rtss.simulation.cbtc.data.vo.ConversationSocketMessageVO;
import club.joylink.rtss.simulation.cbtc.data.vo.TrainIsAbout2ArriveVO;
@ -20,7 +17,6 @@ import club.joylink.rtss.vo.client.WebSocketMessageType;
import club.joylink.rtss.vo.client.factory.SocketMessageFactory;
import club.joylink.rtss.vo.client.psl.PslStatus;
import club.joylink.rtss.vo.client.runplan.RunPlanEChartsDataVO;
import club.joylink.rtss.vo.client.runplan.RunPlanTripVO;
import club.joylink.rtss.vo.client.script.ScriptActionVO;
import club.joylink.rtss.vo.client.simulationv1.SimulationMemberMessageVO;
import club.joylink.rtss.vo.client.simulationv1.SimulationUserMessageVO;
@ -28,14 +24,13 @@ import club.joylink.rtss.websocket.StompMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.Map;
import java.util.Set;
/**
* 仿真主线程
@ -59,73 +54,6 @@ public class SimulationMainThread {
}
}
@Async("nsExecutor")
@EventListener
public void userClientOnline(SimulationUserConnectEvent event) {
log.info(String.format("收到用户[%s]连接仿真[%s]事件", event.getUserId(), event.getGroup()));
String group = event.getGroup();
Simulation simulation = this.groupSimulationCache.findSimulationByGroup(group);
if (Objects.isNull(simulation)) {
return;
}
SimulationUser simulationUser = simulation.getSimulationUserById(event.getUserId());
if (Objects.nonNull(simulationUser)) {
log.info(String.format("仿真[%s]用户[%s]上线,并同步相关仿真状态", group, event.getUserId()));
// 用户上线状态更新并通知其他在线成员
simulationUser.online();
Set<String> userIds = simulation.getSimulationUserIds();
SocketMessageVO<SimulationUserMessageVO> message = SocketMessageFactory
.buildSimulationUserOnlineMessage(simulation, simulationUser);
this.stompMessageService.sendToUser(userIds, message);
// 将当前所有设备状态同步给用户
List<DeviceStatus> statusList = simulation.getRepository().getAllStatusList();
if (!CollectionUtils.isEmpty(statusList)) {
SocketMessageVO allStatusMessage =
SocketMessageFactory.build(WebSocketMessageType.Simulation_DeviceStatus,
group, statusList);
this.stompMessageService.sendToUser(String.valueOf(event.getUserId()), allStatusMessage);
}
// 将当前所有列车实际运行数据同步给用户
List<RealRun> realRunRecordList = simulation.getRepository().getRealRunRecordList();
if (!CollectionUtils.isEmpty(realRunRecordList)) {
SocketMessageVO allTrainRealRunRecord =
SocketMessageFactory.build(WebSocketMessageType.Simulation_RunFact,
group, realRunRecordList);
this.stompMessageService.sendToUser(String.valueOf(event.getUserId()), allTrainRealRunRecord);
}
// 将车次计划变化信息同步给用户
List<RunPlanTripVO> changeTrips = simulation.getRepository().getChangeTrips();
if (!CollectionUtils.isEmpty(changeTrips)) {
SocketMessageVO<RunPlanEChartsDataVO> tripPlanChangeMessage = SocketMessageFactory.buildTripPlanChangeMessage(event.getGroup(), changeTrips);
this.stompMessageService.sendToUser(String.valueOf(event.getUserId()), tripPlanChangeMessage);
}
// ats告警信息同步
List<AtsAlarm> alarmList = simulation.getRepository().getAlarmList();
if (!CollectionUtils.isEmpty(alarmList)) {
SocketMessageVO<Collection<AtsAlarm>> alarmMessageVO = SocketMessageFactory
.buildAtsAlarmMessage(simulation.getId(), alarmList);
this.stompMessageService.sendToUser(String.valueOf(event.getUserId()), alarmMessageVO);
}
}
}
@EventListener
public void userClientOffline(SimulationUserDisconnectEvent event) {
log.info(String.format("收到用户[%s]断开仿真[%s]连接事件", event.getUserId(), event.getGroup()));
String group = event.getGroup();
Simulation simulation = this.groupSimulationCache.findSimulationByGroup(group);
if (Objects.isNull(simulation)) {
return;
}
SimulationUser simulationUser = simulation.getSimulationUserById(event.getUserId());
simulationUser.offline();
Set<String> userIds = simulation.getSimulationUserIds();
SocketMessageVO<SimulationUserMessageVO> message = SocketMessageFactory
.buildSimulationUserOfflineMessage(simulation, simulationUser);
this.stompMessageService.sendToUser(userIds, message);
}
@EventListener
public void userPlayChangeNotify(SimulationUserPlayChangeEvent event) {
Simulation simulation = event.getSimulation();

View File

@ -1,21 +0,0 @@
package club.joylink.rtss.simulation.cbtc.event;
import lombok.Getter;
import org.springframework.context.ApplicationEvent;
/**
* 仿真进入事件
*/
@Getter
public class SimulationUserConnectEvent extends ApplicationEvent {
private String group;
private Long userId;
public SimulationUserConnectEvent(Long userId, String group, Object source) {
super(source);
this.group = group;
this.userId = userId;
}
}

View File

@ -1,21 +0,0 @@
package club.joylink.rtss.simulation.cbtc.event;
import lombok.Getter;
import org.springframework.context.ApplicationEvent;
/**
* 仿真进入事件
*/
@Getter
public class SimulationUserDisconnectEvent extends ApplicationEvent {
private String group;
private Long userId;
public SimulationUserDisconnectEvent(Long userId, String group, Object source) {
super(source);
this.group = group;
this.userId = userId;
}
}

View File

@ -61,6 +61,35 @@ public class SimulationUserWsListener {
}
}
@Async("nsExecutor")
@EventListener
public void handle(SimulationUserUnsubscribeEvent event) {
Simulation sim = event.getSimulation();
if (sim instanceof club.joylink.rtss.simulation.cbtc.Simulation) {
club.joylink.rtss.simulation.cbtc.Simulation simulation = (club.joylink.rtss.simulation.cbtc.Simulation) sim;
SimulationUser simulationUser = simulation.querySimulationUserById(event.getUserId());
if (simulationUser == null)
return;
updateOnlineStatus(simulation, simulationUser);
if (simulationUser.isNoSubscription()) {
applicationContext.publishEvent(new SimulationUserUnsubscribeAllEvent(this, simulation, Long.valueOf(simulationUser.getId())));
}
}
}
@Async("nsExecutor")
@EventListener
public void handle(SimulationUserDisconnectEvent event) {
Simulation sim = event.getSimulation();
if (sim instanceof club.joylink.rtss.simulation.cbtc.Simulation) {
club.joylink.rtss.simulation.cbtc.Simulation simulation = (club.joylink.rtss.simulation.cbtc.Simulation) sim;
SimulationUser simulationUser = simulation.querySimulationUserById(event.getUserId());
if (simulationUser == null)
return;
updateOnlineStatus(simulation, simulationUser);
}
}
/**
* 更新用户在线状态如果是刚上/下线发送消息通知其他人
*/

View File

@ -255,6 +255,11 @@ public class SocketMessageFactory {
new SimulationUserMessageVO(simulationUser, SimulationUserMessageVO.Type.PLAY_CHANGE));
}
public static SocketMessageVO<SimulationUserMessageVO> buildSimulationUserExitMessage(Simulation simulation, SimulationUser simulationUser) {
return build(WebSocketMessageType.Simulation_User, simulation.getId(),
new SimulationUserMessageVO(simulationUser, SimulationUserMessageVO.Type.EXIT));
}
public static SocketMessageVO<SimulationMemberMessageVO> buildSimulationMemberAddMessage(Simulation simulation, SimulationMember simulationMember) {
return build(WebSocketMessageType.Simulation_Member, simulation.getId(),
SimulationMemberMessageVO.buildAddMemberMessage(simulationMember));

View File

@ -23,6 +23,7 @@ public class SimulationUserMessageVO extends SimulationUserVO {
OFFLINE,
ENTER,
PLAY_CHANGE,
KICK_OUT
KICK_OUT,
EXIT
}
}