From c88ae2b17bc8e621526cecffc8cad2d563ab0a5a Mon Sep 17 00:00:00 2001 From: weizhihong Date: Mon, 17 Apr 2023 16:48:29 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E3=80=90=E4=BB=BF=E7=9C=9F=E4=BC=9A?= =?UTF-8?q?=E8=AF=9D=E7=BE=A4=E7=BB=84=E9=80=9A=E7=9F=A5=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E5=A4=84=E7=90=86=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rtss/simulation/cbtc/Simulation.java | 31 +++++ .../cbtc/conversation/ConversationGroup.java | 107 ++++++++++++---- .../ConversationGroupHandlerService.java | 114 ++++++++++++++++-- .../ConversationManagerService.java | 1 - .../cbtc/conversation/ConversationMember.java | 43 +++++++ .../vo/ConversationGroupSocketMessageVO.java | 66 ++++++---- .../cbtc/data/vo/ConversationGroupVO.java | 21 +--- .../cbtc/data/vo/ConversationMemberVO.java | 3 + ...imulationConversationGroupInviteEvent.java | 7 +- ...imulationConversationGroupUpdateEvent.java | 7 +- .../rtss/vo/client/WebSocketMessageType.java | 5 + .../client/factory/SocketMessageFactory.java | 3 +- 12 files changed, 325 insertions(+), 83 deletions(-) diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/Simulation.java b/src/main/java/club/joylink/rtss/simulation/cbtc/Simulation.java index c3c6aa7e2..fe25bdd14 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/Simulation.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/Simulation.java @@ -322,29 +322,56 @@ public class Simulation extends club.joylink.rtss.simulation.Simulation(this.simulationConversationMap.values()); } + /** + * 添加群组 + * @param group 群组信息 + */ public void addConversationGroup(ConversationGroup group) { this.simulationConversationGroupMap.put(group.getId(), group); } + /** + * 移除群组 + * @param group 群组信息 + */ public void removeConversionGroup(ConversationGroup group) { this.simulationConversationGroupMap.remove(group.getId()); } + /** + * 初始化群组信息 + * @param map 群组信息Map + */ public void initDefaultConversationGroupMap(Map map) { + this.simulationConversationGroupMap.clear(); this.simulationConversationGroupMap.putAll(map); Long groupId = map.keySet().stream().max(Comparator.comparing(Long::longValue)).orElse(0L); this.conversationGroupId.set(groupId); } + /** + * 根据名称查找群组 + * @param name 名称 + * @return 群组信息 + */ public ConversationGroup getConversationGroupByName(String name) { return this.simulationConversationGroupMap.values().stream().filter(group -> Objects.equals(group.getName(), name)) .findAny().orElse(null); } + /** + * 获取下一个群组ID + * @return ID + */ public Long getMaxConversationGroupId() { return conversationGroupId.incrementAndGet(); } + /** + * 根据ID获取群组信息 + * @param id 群组ID + * @return 群组 + */ public ConversationGroup getConversationGroup(Long id) { ConversationGroup group = this.simulationConversationGroupMap.get(id); if (Objects.isNull(group)) { @@ -354,6 +381,10 @@ public class Simulation extends club.joylink.rtss.simulation.Simulation queryAllConversationGroup(){ return new ArrayList<>(this.simulationConversationGroupMap.values()); } diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroup.java b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroup.java index 58a55f787..4a8ba1509 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroup.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroup.java @@ -12,6 +12,7 @@ import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; @@ -31,11 +32,6 @@ public class ConversationGroup extends Chat { */ private String name; - /** - * 群主 - */ - private SimulationMember leader; - /** * 群消息列表 */ @@ -56,33 +52,39 @@ public class ConversationGroup extends Chat { this.id = info.getId(); this.name = info.getName(); setTime(simulation.getCorrectSystemTime()); - if (info.getLeaderId() != null) { // 设置群主 - this.leader = simulation.getSimulationMemberById(info.getLeaderId()); - setCreator(this.leader); - } - setTime(simulation.getCorrectSystemTime()); - List memberList = null; - if (!CollectionUtils.isEmpty(info.getMemberIds())) { - memberList = info.getMemberIds().stream().distinct().map(simulation::getSimulationMemberById) - .map(ConversationMember::new).collect(Collectors.toList()); - setMemberList(memberList); + // 成员中放入群主 + SimulationMember leader = simulation.getSimulationMemberById(info.getLeaderId()); + setCreator(leader); + int size = CollectionUtils.isEmpty(info.getMemberIds()) ? 1 : info.getMemberIds().size() + 1; + List memberList = new ArrayList<>(size); + ConversationMember conversationLeader = new ConversationMember(leader); + conversationLeader.setLeader(); + memberList.add(conversationLeader); + // 处理群成员 + if (size > 1) { + info.getMemberIds().stream().filter(id -> !Objects.equals(id, info.getLeaderId())).distinct() + .map(simulation::getSimulationMemberById) + .forEach(member -> memberList.add(new ConversationMember(member))); } + setMemberList(memberList); } public ConversationGroup(Long id, String name, LocalDateTime time, SimulationMember leader, List simulationMembers){ this.id = id; this.name = name; - this.leader = leader; setTime(time); setCreator(leader); - if (!CollectionUtils.isEmpty(simulationMembers)) { - List memberList = simulationMembers.stream().map(member -> { - ConversationMember conversationMember = new ConversationMember(member); - conversationMember.setTime(time); - return conversationMember; - }).collect(Collectors.toList()); - setMemberList(memberList); + int size = CollectionUtils.isEmpty(simulationMembers) ? 1 : simulationMembers.size() + 1; + List memberList = new ArrayList<>(size); + ConversationMember conversationLeader = new ConversationMember(leader); + conversationLeader.setLeader(); + memberList.add(conversationLeader); + // 处理群成员 + if (size > 1) { + simulationMembers.stream().filter(member -> !Objects.equals(member.getId(), leader.getId())).distinct() + .forEach(member -> memberList.add(new ConversationMember(member))); } + setMemberList(memberList); } /** @@ -142,12 +144,69 @@ public class ConversationGroup extends Chat { } } + /** + * 设置群主 + * @param member 角色 + */ + public void setLeader(SimulationMember member) { + ConversationMember leader = getMemberList().stream().filter(ConversationMember::isLeader).findFirst().orElse(null); + if (leader != null) { + leader.setMember(); + } + ConversationMember conversationMember = getConversionMember(member); + conversationMember.setLeader(); + } + /** * 是否群主 * @param member 成员 * @return 结果 */ public boolean isLeader(SimulationMember member) { - return Objects.equals(member, this.leader); + ConversationMember conversationMember = getConversionMember(member); + return conversationMember != null && conversationMember.isLeader(); + } + + /** + * 获取成员用户ID + * @return 用户ID + */ + public Set getMemberUserId() { + return getMemberList().stream().filter(member -> !member.isRobot()) + .map(member -> member.getMember().getUserId()).collect(Collectors.toSet()); + } + + /** + * 获取不含群组的用户ID + * @return 用户ID + */ + public Set getMemberUserIdWithOutLeader() { + return getMemberList().stream().filter(member -> !member.isRobot() && !member.isLeader()) + .map(member -> member.getMember().getUserId()).collect(Collectors.toSet()); + } + + /** + * 获取成员用户ID + * @param memberIds 指定角色ID + * @return 用户ID + */ + public Set getMemberUserId(List memberIds) { + if (CollectionUtils.isEmpty(memberIds)) { + return Set.of(); + } + return getMemberList().stream().filter(m -> !m.isRobot() && memberIds.contains(m.getMember().getId())) + .map(member -> member.getMember().getUserId()).collect(Collectors.toSet()); + } + + /** + * 获取群主信息 + * @return 群主角色 + */ + public SimulationMember getLeader() { + ConversationMember member = getMemberList().stream().filter(ConversationMember::isLeader).findFirst().orElse(null); + if (member == null) { + throw new IllegalArgumentException("不存在群主"); + } + return member.getMember(); } } diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupHandlerService.java b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupHandlerService.java index 54b93c28f..b90babcf1 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupHandlerService.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupHandlerService.java @@ -4,13 +4,18 @@ import club.joylink.rtss.services.voice.VoiceService; import club.joylink.rtss.simulation.cbtc.GroupSimulationCache; import club.joylink.rtss.simulation.cbtc.Simulation; import club.joylink.rtss.simulation.cbtc.data.vo.ConversationGroupMessageVO; +import club.joylink.rtss.simulation.cbtc.data.vo.ConversationGroupSocketMessageVO; import club.joylink.rtss.simulation.cbtc.data.vo.ConversationGroupVO; import club.joylink.rtss.simulation.cbtc.event.conversation.*; import club.joylink.rtss.simulation.cbtc.exception.SimulationException; import club.joylink.rtss.simulation.cbtc.exception.SimulationExceptionType; import club.joylink.rtss.simulation.cbtc.member.SimulationMember; +import club.joylink.rtss.vo.client.SocketMessageVO; +import club.joylink.rtss.vo.client.WebSocketMessageType; +import club.joylink.rtss.vo.client.factory.SocketMessageFactory; import club.joylink.rtss.vo.client.voice.VoiceRecognitionResult; import club.joylink.rtss.vo.client.voice.VoiceRecognitionVO; +import club.joylink.rtss.websocket.StompMessageService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; @@ -38,6 +43,9 @@ public class ConversationGroupHandlerService { @Autowired private ApplicationEventPublisher applicationEventPublisher; + @Autowired + private StompMessageService stompMessageService; + /** * 获取所有群组信息 * @param group 仿真ID @@ -90,7 +98,7 @@ public class ConversationGroupHandlerService { } conversationGroup.setName(name); // 通知其他用户群名变更 - applicationEventPublisher.publishEvent(new SimulationConversationGroupUpdateEvent(this, simulation, conversationGroup)); + applicationEventPublisher.publishEvent(new SimulationConversationGroupUpdateEvent(this, simulation, conversationGroup, ConversationGroupSocketMessageVO.MessageType.UPDATE_NAME)); } /** @@ -108,9 +116,9 @@ public class ConversationGroupHandlerService { conversationGroup.addSimulationMember(simulationMember, simulation.getCorrectSystemTime()); } // 设置群组 - conversationGroup.setCreator(simulationMember); + conversationGroup.setLeader(simulationMember); // 发送通知消息 - applicationEventPublisher.publishEvent(new SimulationConversationGroupUpdateEvent(this, simulation, conversationGroup)); + applicationEventPublisher.publishEvent(new SimulationConversationGroupUpdateEvent(this, simulation, conversationGroup, ConversationGroupSocketMessageVO.MessageType.UPDATE_MEMBER)); } /** @@ -127,12 +135,14 @@ public class ConversationGroupHandlerService { } List groupMemberIds = conversationGroup.getSimulationMemberIds(); List newMemberIds = memberIds.stream().filter(mid -> !groupMemberIds.contains(mid)).collect(Collectors.toList()); + List newSimulationMemberList = new ArrayList<>(newMemberIds.size()); newMemberIds.forEach(memberId -> { SimulationMember newMember = simulation.getSimulationMemberById(memberId); + newSimulationMemberList.add(newMember); conversationGroup.addSimulationMember(newMember, simulation.getCorrectSystemTime()); }); // 给新加入的用户发送消息 - applicationEventPublisher.publishEvent(new SimulationConversationGroupInviteEvent(this, simulation, conversationGroup, groupMemberIds, newMemberIds)); + applicationEventPublisher.publishEvent(new SimulationConversationGroupInviteEvent(this, simulation, conversationGroup, groupMemberIds, newSimulationMemberList)); } /** @@ -275,7 +285,18 @@ public class ConversationGroupHandlerService { @EventListener public void handleCreate(SimulationConversationGroupCreateEvent event) { ConversationGroup conversationGroup = event.getConversationGroup(); - + SimulationMember leader = conversationGroup.getLeader(); + ConversationGroupSocketMessageVO messageVO = ConversationGroupSocketMessageVO.MessageType.JOIN.generateMessageVO(conversationGroup, null); + if (!leader.isRobot()) { + doSendMessage(event.getSimulation(), Set.of(leader.getUserId()), messageVO); + } + Set userIds = conversationGroup.getMemberUserIdWithOutLeader(); + if (CollectionUtils.isEmpty(userIds)) { + return; + } + // 给非群主的群人员发送消息、添加tips信息 + messageVO.setMessageTips(String.format("你加入了【%s】", conversationGroup.getName())); + doSendMessage(event.getSimulation(), userIds, messageVO); } /** @@ -284,7 +305,13 @@ public class ConversationGroupHandlerService { */ @EventListener public void handleUpdate(SimulationConversationGroupUpdateEvent event) { - + ConversationGroup conversationGroup = event.getConversationGroup(); + Set userIds = conversationGroup.getMemberUserId(); + if (CollectionUtils.isEmpty(userIds)) { + return; + } + ConversationGroupSocketMessageVO messageVO = event.getMessageType().generateMessageVO(conversationGroup, null); + doSendMessage(event.getSimulation(), userIds, messageVO); } /** @@ -293,7 +320,29 @@ public class ConversationGroupHandlerService { */ @EventListener public void handleInvite(SimulationConversationGroupInviteEvent event) { - + ConversationGroup conversationGroup = event.getConversationGroup(); + Set newUserIdSet = new HashSet<>(); // 发送加入信息的用户列表 + StringBuilder stringBuilder = new StringBuilder(); + event.getNewMemberList().forEach(m -> { + if (!m.isRobot()) { + newUserIdSet.add(m.getUserId()); + } + stringBuilder.append(m.getName()).append("、"); + }); + // 通知新成员 + if (!CollectionUtils.isEmpty(newUserIdSet)) { + ConversationGroupSocketMessageVO joinMessageVO = ConversationGroupSocketMessageVO.MessageType.JOIN.generateMessageVO(conversationGroup, null); + // 添加tips信息 + joinMessageVO.setMessageTips(String.format("你加入了【%s】", conversationGroup.getName())); + doSendMessage(event.getSimulation(), newUserIdSet, joinMessageVO); + } + Set oldUserIdSet = conversationGroup.getMemberUserId(event.getGroupMemberIds()); + // 通知老成员 + if (!CollectionUtils.isEmpty(oldUserIdSet)) { + ConversationGroupSocketMessageVO updateMessageVO = ConversationGroupSocketMessageVO.MessageType.UPDATE_MEMBER.generateMessageVO(conversationGroup, null); + updateMessageVO.setMessageTips("欢迎【" + stringBuilder.substring(0, stringBuilder.length() - 1) + "】加入本群"); + doSendMessage(event.getSimulation(), conversationGroup.getMemberUserId(event.getGroupMemberIds()), updateMessageVO); + } } /** @@ -303,7 +352,22 @@ public class ConversationGroupHandlerService { */ @EventListener public void handleRemove(SimulationConversationGroupRemoveEvent event) { - + ConversationGroup conversationGroup = event.getConversationGroup(); + Set removeUserIdSet = event.getRemoveGroupMember().stream().filter(m -> !m.isRobot()).map(SimulationMember::getUserId).collect(Collectors.toSet()); + // 通知移除 + if (!CollectionUtils.isEmpty(removeUserIdSet)) { + ConversationGroupSocketMessageVO exitMessageVO = ConversationGroupSocketMessageVO.MessageType.EXIT.generateMessageVO(conversationGroup, null); + // 添加tips信息 + exitMessageVO.setMessageTips(String.format("你被移出了【%s】", conversationGroup.getName())); + doSendMessage(event.getSimulation(), removeUserIdSet, exitMessageVO); + } + // 更新其他用户的成员列表 + Set userIdSet = conversationGroup.getMemberUserId(); + if (CollectionUtils.isEmpty(userIdSet)) { + return; + } + ConversationGroupSocketMessageVO updateMessageVO = ConversationGroupSocketMessageVO.MessageType.UPDATE_MEMBER.generateMessageVO(conversationGroup, null); + doSendMessage(event.getSimulation(), userIdSet, updateMessageVO); } /** @@ -312,7 +376,12 @@ public class ConversationGroupHandlerService { */ @EventListener public void handleExit(SimulationConversationGroupExitEvent event) { - + if (event.getMember().isRobot()) { + return; + } + ConversationGroup conversationGroup = event.getConversationGroup(); + ConversationGroupSocketMessageVO exitMessageVO = ConversationGroupSocketMessageVO.MessageType.EXIT.generateMessageVO(conversationGroup, null); + doSendMessage(event.getSimulation(), Set.of(event.getMember().getUserId()), exitMessageVO); } /** @@ -321,7 +390,15 @@ public class ConversationGroupHandlerService { */ @EventListener public void handleDissolve(SimulationConversationGroupDissolveEvent event) { - + ConversationGroup conversationGroup = event.getConversationGroup(); + // 更新其他用户的成员列表 + Set userIdSet = conversationGroup.getMemberUserId(); + if (CollectionUtils.isEmpty(userIdSet)) { + return; + } + ConversationGroupSocketMessageVO exitMessageVO = ConversationGroupSocketMessageVO.MessageType.EXIT.generateMessageVO(conversationGroup, null); + exitMessageVO.setMessageTips(String.format("【%s】解散了", conversationGroup.getName())); + doSendMessage(event.getSimulation(), userIdSet, exitMessageVO); } /** @@ -367,7 +444,6 @@ public class ConversationGroupHandlerService { ConversationGroupMessage message = new ConversationGroupMessage(messageId, member, simulation.getCorrectSystemTime(), handledContent, recognitionResult.getFilePath()); conversationGroup.addMessage(message); - // 发送消息 @@ -375,5 +451,19 @@ public class ConversationGroupHandlerService { return message; } - + /** + * 发送消息 + * + * @param simulation 仿真 + * @param userSet 用户列表 + * @param messageVO 消息体 + */ + private void doSendMessage(Simulation simulation, Set userSet, ConversationGroupSocketMessageVO messageVO) { + if (CollectionUtils.isEmpty(userSet)) { + return; + } + SocketMessageVO chatMessage = + SocketMessageFactory.build(WebSocketMessageType.Simulation_Conversation_Group, simulation.getId(), messageVO); + stompMessageService.sendToUser(userSet, chatMessage); + } } diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationManagerService.java b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationManagerService.java index be429dfda..ad6bfee29 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationManagerService.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationManagerService.java @@ -4,7 +4,6 @@ import club.joylink.rtss.services.voice.VoiceService; import club.joylink.rtss.simulation.cbtc.GroupSimulationCache; import club.joylink.rtss.simulation.cbtc.Simulation; import club.joylink.rtss.simulation.cbtc.competition.ScriptExecuteService; -import club.joylink.rtss.simulation.cbtc.data.vo.ConversationGroupVO; import club.joylink.rtss.simulation.cbtc.data.vo.ConversationMessageVO; import club.joylink.rtss.simulation.cbtc.data.vo.ConversationVO; import club.joylink.rtss.simulation.cbtc.event.*; diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationMember.java b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationMember.java index f4205620a..86258131d 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationMember.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationMember.java @@ -5,6 +5,7 @@ import lombok.Getter; import lombok.Setter; import java.time.LocalDateTime; +import java.util.Objects; @Getter public class ConversationMember { @@ -19,16 +20,58 @@ public class ConversationMember { @Setter private LocalDateTime time; + /** + * 角色 + */ + @Setter + private Role role; + public ConversationMember(SimulationMember member) { this.member = member; this.connect = false; + this.role = Role.Member; } public boolean isRobot() { return member.isRobot(); } + /** + * 是否群主 + */ + public boolean isLeader() { + return Objects.equals(this.role, Role.Leader); + } + + /** + * 设置群主 + */ + public void setLeader() { + this.role = Role.Leader; + } + + /** + * 设置成员 + */ + public void setMember() { + this.role = Role.Member; + } + public void connect() { this.connect = true; } + + /** + * 成员角色 + */ + public enum Role { + /** + * 群主 + */ + Leader, + /** + * 成员 + */ + Member; + } } diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationGroupSocketMessageVO.java b/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationGroupSocketMessageVO.java index 3dfcfbf2d..7dfb92c0d 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationGroupSocketMessageVO.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationGroupSocketMessageVO.java @@ -1,16 +1,15 @@ package club.joylink.rtss.simulation.cbtc.data.vo; -import club.joylink.rtss.simulation.cbtc.Simulation; -import club.joylink.rtss.simulation.cbtc.conversation.Conversation; import club.joylink.rtss.simulation.cbtc.conversation.ConversationGroup; -import club.joylink.rtss.simulation.cbtc.conversation.ConversationMessage; -import club.joylink.rtss.simulation.cbtc.discriminate.ParamExtractResult; -import club.joylink.rtss.simulation.cbtc.member.SimulationMember; import lombok.Getter; +import lombok.Setter; import java.util.List; import java.util.stream.Collectors; +/** + * 群组socket消息内容 + */ @Getter public class ConversationGroupSocketMessageVO { @@ -26,11 +25,6 @@ public class ConversationGroupSocketMessageVO { */ private String creatorId; - /** - * 群主 - */ - private String leaderId; - /** * 成员列表 */ @@ -46,25 +40,27 @@ public class ConversationGroupSocketMessageVO { */ private MessageType messageType; + @Setter + private String messageTips; + public enum MessageType { /** * 会话组创建 */ - CREATE { + JOIN { @Override public ConversationGroupSocketMessageVO generateMessageVO(ConversationGroup group, ConversationGroupMessageVO message) { ConversationGroupSocketMessageVO messageVO = new ConversationGroupSocketMessageVO(); messageVO.id = group.getId(); messageVO.name = group.getName(); messageVO.creatorId = group.getCreator() != null ? group.getCreator().getId() : null; - messageVO.leaderId = group.getLeader() != null ? group.getLeader().getId() : null; messageVO.memberList = group.getMemberList().stream().map(ConversationMemberVO::new).collect(Collectors.toList()); - messageVO.messageType = MessageType.CREATE; + messageVO.messageType = MessageType.JOIN; return messageVO; } }, /** - * 会话组更新 + * 会话组全量更新 */ UPDATE { @Override @@ -72,37 +68,59 @@ public class ConversationGroupSocketMessageVO { ConversationGroupSocketMessageVO messageVO = new ConversationGroupSocketMessageVO(); messageVO.id = group.getId(); messageVO.name = group.getName(); - messageVO.leaderId = group.getLeader() != null ? group.getLeader().getId() : null; messageVO.memberList = group.getMemberList().stream().map(ConversationMemberVO::new).collect(Collectors.toList()); messageVO.messageType = MessageType.UPDATE; return messageVO; } }, - - - /** 会话组邀请 */ - INVITE { + /** + * 修改名称 + */ + UPDATE_NAME { @Override public ConversationGroupSocketMessageVO generateMessageVO(ConversationGroup group, ConversationGroupMessageVO message) { - return null; + ConversationGroupSocketMessageVO messageVO = new ConversationGroupSocketMessageVO(); + messageVO.id = group.getId(); + messageVO.name = group.getName(); + messageVO.messageType = MessageType.UPDATE_NAME; + return messageVO; + } + }, + /** + * 修改人员信息 + */ + UPDATE_MEMBER { + @Override + public ConversationGroupSocketMessageVO generateMessageVO(ConversationGroup group, ConversationGroupMessageVO message) { + ConversationGroupSocketMessageVO messageVO = new ConversationGroupSocketMessageVO(); + messageVO.id = group.getId(); + messageVO.memberList = group.getMemberList().stream().map(ConversationMemberVO::new).collect(Collectors.toList()); + messageVO.messageType = MessageType.UPDATE_MEMBER; + return messageVO; } }, /** 成员发送消息 */ MESSAGE { @Override public ConversationGroupSocketMessageVO generateMessageVO(ConversationGroup group, ConversationGroupMessageVO message) { - return null; + ConversationGroupSocketMessageVO messageVO = new ConversationGroupSocketMessageVO(); + messageVO.id = group.getId(); + messageVO.message = message; + messageVO.messageType = MessageType.MESSAGE; + return messageVO; } }, - /** 会话结束 */ + /** 会话组解散 */ EXIT { @Override public ConversationGroupSocketMessageVO generateMessageVO(ConversationGroup group, ConversationGroupMessageVO message) { - return null; + ConversationGroupSocketMessageVO messageVO = new ConversationGroupSocketMessageVO(); + messageVO.id = group.getId(); + messageVO.messageType = MessageType.EXIT; + return messageVO; } }; public abstract ConversationGroupSocketMessageVO generateMessageVO(ConversationGroup group, ConversationGroupMessageVO message); } - } diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationGroupVO.java b/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationGroupVO.java index 25a32d4a6..f8ba1c73f 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationGroupVO.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationGroupVO.java @@ -20,6 +20,8 @@ public class ConversationGroupVO { private String creatorId; + private String leaderId; + private List memberList; private List messageList; @@ -27,27 +29,12 @@ public class ConversationGroupVO { public ConversationGroupVO(ConversationGroup conversation) { this.id = conversation.getId(); this.name = conversation.getName(); - if (conversation.getCreator() != null) { - this.creatorId = conversation.getCreator().getId(); - } + this.creatorId = conversation.getCreator() != null ? conversation.getCreator().getId() : null; + this.leaderId = conversation.getLeader() != null ? conversation.getLeader().getId() : null; this.memberList = ConversationMemberVO.convert2VOList(conversation.getMemberList()); this.messageList = ConversationGroupMessageVO.convert2VO(conversation.getMessageList()); } - - /** - * 生成不带消息的VO对象 - * @param conversation - */ - public void generateConversationGroupVOWithOutMessage(ConversationGroup conversation) { - this.id = conversation.getId(); - this.name = conversation.getName(); - if (conversation.getCreator() != null) { - this.creatorId = conversation.getCreator().getId(); - } - this.memberList = ConversationMemberVO.convert2VOList(conversation.getMemberList()); - } - public static List convert2VOList(List list) { List voList = new ArrayList<>(); if (!CollectionUtils.isEmpty(list)) { diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationMemberVO.java b/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationMemberVO.java index 602384248..c3581af97 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationMemberVO.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/data/vo/ConversationMemberVO.java @@ -16,9 +16,12 @@ public class ConversationMemberVO { private boolean connect; + private ConversationMember.Role role; + public ConversationMemberVO(ConversationMember conversationMember) { this.memberId = conversationMember.getMember().getId(); this.connect = conversationMember.isConnect(); + this.role = conversationMember.getRole(); } public static List convert2VOList(List list) { diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/event/conversation/SimulationConversationGroupInviteEvent.java b/src/main/java/club/joylink/rtss/simulation/cbtc/event/conversation/SimulationConversationGroupInviteEvent.java index 2fa1c191e..aded8b99a 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/event/conversation/SimulationConversationGroupInviteEvent.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/event/conversation/SimulationConversationGroupInviteEvent.java @@ -3,6 +3,7 @@ package club.joylink.rtss.simulation.cbtc.event.conversation; import club.joylink.rtss.simulation.cbtc.Simulation; import club.joylink.rtss.simulation.cbtc.conversation.ConversationGroup; import club.joylink.rtss.simulation.cbtc.event.AbstractSimulationEvent; +import club.joylink.rtss.simulation.cbtc.member.SimulationMember; import lombok.Getter; import java.util.List; @@ -23,13 +24,13 @@ public class SimulationConversationGroupInviteEvent extends AbstractSimulationEv /** * 新加入的组员 */ - private List newGroupMemberIds; + private List newMemberList; public SimulationConversationGroupInviteEvent(Object source, Simulation simulation, ConversationGroup conversationGroup - , List groupMemberIds, List newGroupMemberIds) { + , List groupMemberIds, List newMemberList) { super(source, simulation); this.conversationGroup = conversationGroup; this.groupMemberIds = groupMemberIds; - this.newGroupMemberIds = newGroupMemberIds; + this.newMemberList = newMemberList; } } diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/event/conversation/SimulationConversationGroupUpdateEvent.java b/src/main/java/club/joylink/rtss/simulation/cbtc/event/conversation/SimulationConversationGroupUpdateEvent.java index 4b58b302d..b4e942996 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/event/conversation/SimulationConversationGroupUpdateEvent.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/event/conversation/SimulationConversationGroupUpdateEvent.java @@ -2,6 +2,7 @@ package club.joylink.rtss.simulation.cbtc.event.conversation; import club.joylink.rtss.simulation.cbtc.Simulation; import club.joylink.rtss.simulation.cbtc.conversation.ConversationGroup; +import club.joylink.rtss.simulation.cbtc.data.vo.ConversationGroupSocketMessageVO; import club.joylink.rtss.simulation.cbtc.event.AbstractSimulationEvent; import lombok.Getter; @@ -13,8 +14,12 @@ public class SimulationConversationGroupUpdateEvent extends AbstractSimulationEv private ConversationGroup conversationGroup; - public SimulationConversationGroupUpdateEvent(Object source, Simulation simulation, ConversationGroup conversationGroup) { + private ConversationGroupSocketMessageVO.MessageType messageType; + + public SimulationConversationGroupUpdateEvent(Object source, Simulation simulation, ConversationGroup conversationGroup + , ConversationGroupSocketMessageVO.MessageType messageType) { super(source, simulation); this.conversationGroup = conversationGroup; + this.messageType = messageType; } } diff --git a/src/main/java/club/joylink/rtss/vo/client/WebSocketMessageType.java b/src/main/java/club/joylink/rtss/vo/client/WebSocketMessageType.java index e415b831c..c807ae23f 100644 --- a/src/main/java/club/joylink/rtss/vo/client/WebSocketMessageType.java +++ b/src/main/java/club/joylink/rtss/vo/client/WebSocketMessageType.java @@ -168,6 +168,11 @@ public enum WebSocketMessageType { */ Simulation_Operation_Mode_Apply, + /** + * 仿真群组消息 + */ + Simulation_Conversation_Group, + //------------------ 仿真剧本 ------------------ /** * 仿真-剧本已加载 diff --git a/src/main/java/club/joylink/rtss/vo/client/factory/SocketMessageFactory.java b/src/main/java/club/joylink/rtss/vo/client/factory/SocketMessageFactory.java index 5fb3ba018..fef06d477 100644 --- a/src/main/java/club/joylink/rtss/vo/client/factory/SocketMessageFactory.java +++ b/src/main/java/club/joylink/rtss/vo/client/factory/SocketMessageFactory.java @@ -99,7 +99,8 @@ public class SocketMessageFactory { case Competition_Practical: case SIMULATION_RAIL_TICKET: case Simulation_Alarm: - case Simulation_Operation_Mode_Apply: { + case Simulation_Operation_Mode_Apply: + case Simulation_Conversation_Group: { topicList.add(SimulationSubscribeTopic.Main.buildDestination(group)); break; } From 1eb4e2c6cf041e594f8b1100d6017b34cf4387a1 Mon Sep 17 00:00:00 2001 From: weizhihong Date: Mon, 17 Apr 2023 16:58:12 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E3=80=90=E7=BE=A4=E7=BB=84=E5=8F=91?= =?UTF-8?q?=E9=80=81=E6=B6=88=E6=81=AF=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConversationGroupHandlerService.java | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupHandlerService.java b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupHandlerService.java index b90babcf1..a7623fe97 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupHandlerService.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupHandlerService.java @@ -210,13 +210,12 @@ public class ConversationGroupHandlerService { * @return 消息信息实体 */ public ConversationGroupMessageVO audioChat(Simulation simulation, SimulationMember member, Long groupId, String audioPath, String content) { - ConversationGroupMessage message = sendMessage(simulation, member, groupId, () -> { + return sendMessage(simulation, member, groupId, () -> { VoiceRecognitionResult recognitionResult = new VoiceRecognitionResult(); recognitionResult.setFilePath(audioPath); recognitionResult.setResult(content); return recognitionResult; }); - return new ConversationGroupMessageVO(message); } /** @@ -229,12 +228,11 @@ public class ConversationGroupHandlerService { * @return 消息信息实体 */ public ConversationGroupMessageVO textChat(Simulation simulation, SimulationMember member, Long groupId, String content) { - ConversationGroupMessage message = sendMessage(simulation, member, groupId, () -> { + return sendMessage(simulation, member, groupId, () -> { VoiceRecognitionResult recognitionResult = new VoiceRecognitionResult(); recognitionResult.setResult(content); return recognitionResult; }); - return new ConversationGroupMessageVO(message); } /** @@ -246,11 +244,10 @@ public class ConversationGroupHandlerService { * @return 消息信息实体 */ public ConversationGroupMessageVO audioBase64(Simulation simulation, SimulationMember member, Long groupId, String fileBase64Str) { - ConversationGroupMessage message = sendMessage(simulation, member, groupId, () -> { + return sendMessage(simulation, member, groupId, () -> { VoiceRecognitionVO vo = VoiceRecognitionVO.load(fileBase64Str); return iVoiceService.voiceRecognition(vo); }); - return new ConversationGroupMessageVO(message); } /** @@ -428,27 +425,27 @@ public class ConversationGroupHandlerService { * @param handleSupplier 处理消息方法 * @return 用户组消息 */ - private ConversationGroupMessage sendMessage(Simulation simulation, SimulationMember member, Long groupId + private ConversationGroupMessageVO sendMessage(Simulation simulation, SimulationMember member, Long groupId , Supplier handleSupplier) { ConversationGroup conversationGroup = checkGroupIdAndReturn(simulation, null, groupId); if (!conversationGroup.isConversationMember(member)) { - throw new SimulationException(SimulationExceptionType.Illegal_Argument, "非本组成员,不能邀请用户"); + throw new SimulationException(SimulationExceptionType.Illegal_Argument, "非本组成员"); } - VoiceRecognitionResult recognitionResult = handleSupplier.get(); String upperCaseResult = recognitionResult.getResult().toUpperCase(); String handledContent = simulationVoiceHandler.handle(upperCaseResult); - // 创建消息信息 String messageId = conversationGroup.generateMessageId(); ConversationGroupMessage message = new ConversationGroupMessage(messageId, member, simulation.getCorrectSystemTime(), handledContent, recognitionResult.getFilePath()); conversationGroup.addMessage(message); // 发送消息 - - - - return message; + ConversationGroupMessageVO messageVO = new ConversationGroupMessageVO(message); + Set userIdSet = conversationGroup.getMemberUserId(); + if (!CollectionUtils.isEmpty(userIdSet)) { + doSendMessage(simulation, userIdSet, ConversationGroupSocketMessageVO.MessageType.MESSAGE.generateMessageVO(conversationGroup, messageVO)); + } + return messageVO; } /** From 0dddb9b4e89d9aed4a704a0f7e778dc089848adf Mon Sep 17 00:00:00 2001 From: weizhihong Date: Mon, 17 Apr 2023 18:24:00 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E3=80=90=E8=AF=AD=E9=9F=B3=E8=AF=86?= =?UTF-8?q?=E5=88=AB=E4=B8=8E=E7=BE=A4=E7=BB=84=E4=BC=9A=E8=AF=9D=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E6=B7=BB=E5=8A=A0=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../VoiceDiscriminateService.java | 19 +++++++++- .../ConversationGroupHandlerService.java | 3 ++ ...mulationConversationGroupMessageEvent.java | 37 +++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/main/java/club/joylink/rtss/simulation/cbtc/event/conversation/SimulationConversationGroupMessageEvent.java diff --git a/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateService.java b/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateService.java index 98020a7ee..8899a227f 100644 --- a/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateService.java +++ b/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateService.java @@ -1,16 +1,21 @@ package club.joylink.rtss.services.voice.discriminate; +import club.joylink.rtss.simulation.cbtc.conversation.ConversationGroup; import club.joylink.rtss.simulation.cbtc.discriminate.VoiceDiscriminateResult; import club.joylink.rtss.simulation.cbtc.Simulation; +import club.joylink.rtss.simulation.cbtc.event.conversation.SimulationConversationGroupDissolveEvent; +import club.joylink.rtss.simulation.cbtc.event.conversation.SimulationConversationGroupMessageEvent; +import club.joylink.rtss.simulation.cbtc.member.SimulationMember; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; /** - * 语音识别服务类(多例) + * 语音识别服务类 */ @Slf4j @Service @@ -50,4 +55,16 @@ public class VoiceDiscriminateService { replyParseService.doExec(result, simulation); // 回复执行 correctSourceService.doExec(result, simulation);// 原信息纠错 } + + /** + * 处理聊天信息 + * @param event 聊天事件信息 + */ + @EventListener + public void handleMessage(SimulationConversationGroupMessageEvent event) { + ConversationGroup conversationGroup = event.getConversationGroup(); + + + + } } diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupHandlerService.java b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupHandlerService.java index a7623fe97..be80c5d2c 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupHandlerService.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/conversation/ConversationGroupHandlerService.java @@ -1,6 +1,7 @@ package club.joylink.rtss.simulation.cbtc.conversation; import club.joylink.rtss.services.voice.VoiceService; +import club.joylink.rtss.services.voice.discriminate.VoiceDiscriminateService; import club.joylink.rtss.simulation.cbtc.GroupSimulationCache; import club.joylink.rtss.simulation.cbtc.Simulation; import club.joylink.rtss.simulation.cbtc.data.vo.ConversationGroupMessageVO; @@ -445,6 +446,8 @@ public class ConversationGroupHandlerService { if (!CollectionUtils.isEmpty(userIdSet)) { doSendMessage(simulation, userIdSet, ConversationGroupSocketMessageVO.MessageType.MESSAGE.generateMessageVO(conversationGroup, messageVO)); } + // 语音识别开始 + applicationEventPublisher.publishEvent(new SimulationConversationGroupMessageEvent(this, simulation, conversationGroup, member, message.getContent())); return messageVO; } diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/event/conversation/SimulationConversationGroupMessageEvent.java b/src/main/java/club/joylink/rtss/simulation/cbtc/event/conversation/SimulationConversationGroupMessageEvent.java new file mode 100644 index 000000000..ef5816db2 --- /dev/null +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/event/conversation/SimulationConversationGroupMessageEvent.java @@ -0,0 +1,37 @@ +package club.joylink.rtss.simulation.cbtc.event.conversation; + +import club.joylink.rtss.simulation.cbtc.Simulation; +import club.joylink.rtss.simulation.cbtc.conversation.ConversationGroup; +import club.joylink.rtss.simulation.cbtc.event.AbstractSimulationEvent; +import club.joylink.rtss.simulation.cbtc.member.SimulationMember; +import lombok.Getter; + +/** + * 群组退群事件 + */ +@Getter +public class SimulationConversationGroupMessageEvent extends AbstractSimulationEvent { + + /** + * 会话对象 + */ + private ConversationGroup conversationGroup; + + /** + * 操作用户 + */ + private SimulationMember member; + + /** + * 消息内容 + */ + private String content; + + public SimulationConversationGroupMessageEvent(Object source, Simulation simulation, ConversationGroup conversationGroup + , SimulationMember member, String content) { + super(source, simulation); + this.conversationGroup = conversationGroup; + this.member = member; + this.content = content; + } +} From c02fa14d318cffb82a7aa948cd004a3af5cfb61d Mon Sep 17 00:00:00 2001 From: weizhihong Date: Tue, 18 Apr 2023 11:31:47 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=AD?= =?UTF-8?q?=E9=9F=B3=E8=AF=86=E5=88=AB=E6=88=90=E5=91=98=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../discriminate/OperateParseServiceImpl.java | 65 ++++++++++++------- .../discriminate/ReplyParseServiceImpl.java | 42 +++++++----- .../VoiceDiscriminateService.java | 29 ++++++++- .../cbtc/discriminate/OperateResult.java | 10 --- 4 files changed, 91 insertions(+), 55 deletions(-) diff --git a/src/main/java/club/joylink/rtss/services/voice/discriminate/OperateParseServiceImpl.java b/src/main/java/club/joylink/rtss/services/voice/discriminate/OperateParseServiceImpl.java index 37848c5a3..45f73f99d 100644 --- a/src/main/java/club/joylink/rtss/services/voice/discriminate/OperateParseServiceImpl.java +++ b/src/main/java/club/joylink/rtss/services/voice/discriminate/OperateParseServiceImpl.java @@ -44,11 +44,16 @@ public class OperateParseServiceImpl implements VoiceTransactionalService { if (operateRule == null) { return; } - // 参数解析 List paramExtractResults = result.getParamExtractResultList(); Map paramExtractResultMap = IntStream.range(0,paramExtractResults.size()).boxed().collect(Collectors.toMap(i->++i,paramExtractResults::get)); OperateResult operateResult = new OperateResult(operateRule.getType()); int paramSize = paramExtractResults.size(); + // 成员解析 + List simulationMemberList = filterSimulationMember(simulation, result, paramExtractResultMap, operateRule.getMemberRule()); + if (CollectionUtils.isEmpty(simulationMemberList)) { + return; + } + // 参数解析 for (CommandParamRule paramRule : operateRule.getParamRules()) { ParamExtractResult[] paramArr = new ParamExtractResult[paramRule.getIndexArr().length]; for (int index = 0, len = paramArr.length; index < len; index ++) { @@ -67,33 +72,18 @@ public class OperateParseServiceImpl implements VoiceTransactionalService { operateResult.putParam(getMethodParamName(paramRule.getParamIndex(), operateRule.getType().name()), val); } else { // 参数位置未指定 - return; } } - // 成员解析 - MemberRule memberRule = operateRule.getMemberRule(); - if (memberRule != null) { - List simulationMemberList = null; - if (memberRule.getIndex() == null) { - simulationMemberList = memberRule.getParseRule().matchMember(simulation, null); - } else if (memberRule.getIndex() < paramExtractResults.size()) { - simulationMemberList = memberRule.getParseRule().matchMember(simulation, paramExtractResultMap.get(memberRule.getIndex())); - } else { - return; - } - if (CollectionUtils.isEmpty(simulationMemberList)) { - return; - } - operateResult.setMember(simulationMemberList.get(0)); - } // 执行操作 - try { - atsOperationDispatcher.execute(simulation, operateResult.getMember(), operateResult.getType().name(), operateResult.getParams()); - log.info("执行语音指令操作成功,操作[{}] 源指令[{}]",operateResult.getType().name(),result.getOriginContent()); - } catch (Exception e) { - log.error("执行操作失败[{}] 输入源指令[{}] msg[{}]",operateResult.getType().name(),result.getOriginContent(),e.getMessage(),e); - } + simulationMemberList.forEach(member -> { + try { + atsOperationDispatcher.execute(simulation, member, operateResult.getType().name(), operateResult.getParams()); + log.info("[{}]执行语音指令操作成功,操作[{}] 源指令[{}]", member.getName(), operateResult.getType().name(), result.getOriginContent()); + } catch (Exception e) { + log.error("[{}]执行操作失败[{}] 输入源指令[{}] msg[{}]", member.getName(), operateResult.getType().name(), result.getOriginContent(),e.getMessage(),e); + } + }); } /** @@ -115,7 +105,7 @@ public class OperateParseServiceImpl implements VoiceTransactionalService { * @param operateName 操作名称 * @return 参数名称 */ - public String getMethodParamName(int index, String operateName) { + private String getMethodParamName(int index, String operateName) { OperateMethod handlerMethod = this.atsManager.getHandlerMethod(operateName); Parameter[] parameters = handlerMethod.getMethod().getParameters(); if (parameters.length < index) { @@ -123,4 +113,29 @@ public class OperateParseServiceImpl implements VoiceTransactionalService { } return parameters[index].getName(); } + + /** + * 获取执行操作的人员 + * @return 人员列表 + */ + private List filterSimulationMember(Simulation simulation, VoiceDiscriminateResult result + , Map paramExtractResultMap, MemberRule memberRule) { + int paramSize = result.getParamExtractResultList().size(); + List simulationMemberList = null; + if (memberRule != null) { // 如果有指定成员解析规则,使用此规则解析 + if (memberRule.getIndex() == null) { + simulationMemberList = memberRule.getParseRule().matchMember(simulation, null); + } else if (memberRule.getIndex() < paramSize) { + simulationMemberList = memberRule.getParseRule().matchMember(simulation, paramExtractResultMap.get(memberRule.getIndex())); + } + return simulationMemberList; // 返回解析结果 + } else { + simulationMemberList = result.getSimulationMemberList(); // 获取群组成员对象 + if (CollectionUtils.isEmpty(simulationMemberList) || simulationMemberList.size() > 1) { + return List.of(); + } else { + return simulationMemberList; + } + } + } } diff --git a/src/main/java/club/joylink/rtss/services/voice/discriminate/ReplyParseServiceImpl.java b/src/main/java/club/joylink/rtss/services/voice/discriminate/ReplyParseServiceImpl.java index fe9f124b7..6d5d1765c 100644 --- a/src/main/java/club/joylink/rtss/services/voice/discriminate/ReplyParseServiceImpl.java +++ b/src/main/java/club/joylink/rtss/services/voice/discriminate/ReplyParseServiceImpl.java @@ -30,10 +30,16 @@ public class ReplyParseServiceImpl implements VoiceTransactionalService { if (replyRule == null) { return; } + // 回复成员 + List simulationMemberList = filterSimulationMember(simulation, result, replyRule.getMemberRule()); + if (CollectionUtils.isEmpty(simulationMemberList)) { + return; + } + // 格式化回复语句 List paramExtractResults = result.getParamExtractResultList(); ReplyResult replyResult = new ReplyResult(replyRule.getParamRules().size()); - ParamExtractResult[] paramArr = null; int paramSize = paramExtractResults.size(); + ParamExtractResult[] paramArr = null; for (CommandParamRule paramRule : replyRule.getParamRules()) { paramArr = new ParamExtractResult[paramRule.getIndexArr().length]; for (int index = 0, len = paramArr.length; index < len; index ++) { @@ -45,31 +51,33 @@ public class ReplyParseServiceImpl implements VoiceTransactionalService { } replyResult.addParams(paramRule.getParseRule().extractParam(simulation, paramArr)); } - - // 格式化回复语句 Object[] objArr = new Object[replyResult.getParamList().size()]; replyResult.getParamList().toArray(objArr); replyResult.setContent(String.format(replyRule.getMessageFormat(), objArr)); - // 匹配回复人员 - MemberRule memberRule = replyRule.getMemberRule(); + // 执行回复 + simulationMemberList.forEach(member -> conversationManagerService.conversationChat(simulation, member, replyResult.getContent(), null)); + } + + /** + * 获取回复成员列表 + * + * @param simulation 仿真 + * @param result 封装的结果信息 + * @param memberRule 成员解析规则 + * @return 回复成员列表 + */ + private List filterSimulationMember(Simulation simulation, VoiceDiscriminateResult result, MemberRule memberRule) { + List paramExtractResults = result.getParamExtractResultList(); + List simulationMemberList = null; if (memberRule != null) { - List simulationMemberList = null; if (memberRule.getIndex() == null) { simulationMemberList = memberRule.getParseRule().matchMember(simulation, null); } else if (memberRule.getIndex() < paramExtractResults.size()) { simulationMemberList = memberRule.getParseRule().matchMember(simulation, paramExtractResults.get(memberRule.getIndex() - 1)); - } else { - return; } - if (CollectionUtils.isEmpty(simulationMemberList)) { - return; - } - replyResult.setSimulationMemberList(simulationMemberList); + return simulationMemberList; + } else { + return result.getSimulationMemberList(); } - - // 执行回复 - replyResult.getSimulationMemberList().forEach(member -> { - conversationManagerService.conversationChat(simulation, member, replyResult.getContent(), null); - }); } } diff --git a/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateService.java b/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateService.java index 8899a227f..587d4c173 100644 --- a/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateService.java +++ b/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateService.java @@ -1,6 +1,7 @@ package club.joylink.rtss.services.voice.discriminate; import club.joylink.rtss.simulation.cbtc.conversation.ConversationGroup; +import club.joylink.rtss.simulation.cbtc.conversation.ConversationMember; import club.joylink.rtss.simulation.cbtc.discriminate.VoiceDiscriminateResult; import club.joylink.rtss.simulation.cbtc.Simulation; import club.joylink.rtss.simulation.cbtc.event.conversation.SimulationConversationGroupDissolveEvent; @@ -11,8 +12,13 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; import org.springframework.web.multipart.MultipartFile; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + /** * 语音识别服务类 @@ -56,6 +62,17 @@ public class VoiceDiscriminateService { correctSourceService.doExec(result, simulation);// 原信息纠错 } + /** + * 接受语音文件、资源等必要条件,解析开始 + */ + public void doAnalysis(Simulation simulation, String content, List memberList) { + VoiceDiscriminateResult result = voiceParseService.doParse(simulation, content); + result.setSimulationMemberList(memberList); // 匹配到人员集合 + operateParseService.doExec(result, simulation); // 指令执行 + replyParseService.doExec(result, simulation); // 回复执行 + correctSourceService.doExec(result, simulation);// 原信息纠错 + } + /** * 处理聊天信息 * @param event 聊天事件信息 @@ -63,8 +80,14 @@ public class VoiceDiscriminateService { @EventListener public void handleMessage(SimulationConversationGroupMessageEvent event) { ConversationGroup conversationGroup = event.getConversationGroup(); - - - + SimulationMember member = event.getMember(); + List memberList = conversationGroup.getMemberList().stream() + .filter(cm -> cm.isRobot() && !Objects.equals(cm.getMember(), member)) + .map(ConversationMember::getMember).collect(Collectors.toList()); + if (CollectionUtils.isEmpty(memberList)) { // 人员全部为真实人员时直接返回 + return; + } + // 执行语音识别 + doAnalysis(event.getSimulation(), event.getContent(), memberList); } } diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/OperateResult.java b/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/OperateResult.java index 279566222..5cf59b699 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/OperateResult.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/OperateResult.java @@ -1,7 +1,6 @@ package club.joylink.rtss.simulation.cbtc.discriminate; import club.joylink.rtss.simulation.cbtc.ATS.operation.Operation; -import club.joylink.rtss.simulation.cbtc.member.SimulationMember; import lombok.Data; import java.util.HashMap; @@ -18,24 +17,15 @@ public class OperateResult { */ private Operation.Type type; -// private Boolean operationResult; -// private String errResultMsg; /** * 指令参数 */ private Map params = new HashMap<>(); - /** - * 角色ID - */ - private SimulationMember member; - public OperateResult(Operation.Type type) { this.type = type; } - - /** * 参数放入集合 * @param key 集合key