From 8eaf0206d089025f0d2b967625341b8a0733c065 Mon Sep 17 00:00:00 2001 From: tiger_zhou <123456> Date: Tue, 14 Feb 2023 11:29:28 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E8=80=83=E8=AF=95=E8=B0=83=E6=95=B4?= =?UTF-8?q?=EF=BC=8C=E5=BA=94=E6=80=A5ws=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../paper/PaperUserSubmitAnswerService.java | 72 ++++++------------- .../rtss/websocket/WebsocketConfig.java | 30 ++++---- .../PresenceChannelInterceptor.java | 16 +++-- .../interceptor/VSimpleInterceptor.java | 42 ++++++++--- 4 files changed, 75 insertions(+), 85 deletions(-) diff --git a/src/main/java/club/joylink/rtss/services/paper/PaperUserSubmitAnswerService.java b/src/main/java/club/joylink/rtss/services/paper/PaperUserSubmitAnswerService.java index 070ae285d..9f6e6d735 100644 --- a/src/main/java/club/joylink/rtss/services/paper/PaperUserSubmitAnswerService.java +++ b/src/main/java/club/joylink/rtss/services/paper/PaperUserSubmitAnswerService.java @@ -22,6 +22,7 @@ import club.joylink.rtss.vo.client.training2.Step2VO; import club.joylink.rtss.vo.paper.*; import club.joylink.rtss.vo.training2.publish.PublishedTraining2DetailRspVo; import com.google.common.base.Joiner; +import com.google.common.base.Splitter; import com.google.common.base.Strings; import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; @@ -79,18 +80,14 @@ public class PaperUserSubmitAnswerService { rsp.setSubType(req.getSubType()); String tmpAnswer = null; //处理用户提交的答案 - switch (req.getType()) { - case Common: - rsp.setResult(submitCommonAnswer(puq, req)); - tmpAnswer = this.getAnswerFromSubmit(req); - break; - case Training:{ - BusinessExceptionAssertEnum.ARGUMENT_ILLEGAL.assertNotTrue((req.getSubType() != PaperQType.SubType.Single && req.getSubType() != PaperQType.SubType.Scene),String.format("未知答题类型[%s]",req.getSubType())); - TrainAnswerVO answerVO = this.submitTrainingAnswer(req,puq); - rsp.setResult(answerVO.getState()); - tmpAnswer = JsonUtils.writeValueAsString(answerVO.getDetail()); - break; - } + if(req.getType() == PaperQType.GroupType.Common){ + rsp.setResult(submitCommonAnswer(puq, req)); + tmpAnswer = this.getAnswerFromSubmit(req); + }else if(req.getType() == PaperQType.GroupType.Training){ + BusinessExceptionAssertEnum.ARGUMENT_ILLEGAL.assertNotTrue((req.getSubType() != PaperQType.SubType.Single && req.getSubType() != PaperQType.SubType.Scene),String.format("未知答题类型[%s]",req.getSubType())); + TrainAnswerVO answerVO = this.submitTrainingAnswer(req,puq); + rsp.setResult(answerVO.getState()); + tmpAnswer = JsonUtils.writeValueAsString(answerVO.getDetail()); } //记录用户答题情况 PaperUserQuestion record = new PaperUserQuestion(); @@ -122,15 +119,12 @@ public class PaperUserSubmitAnswerService { PublishedTraining2DetailRspVo detailVO = this.training2PublishService.findTrainingAllInfoById(reqVo.getPqId()); log.debug("查找对应的实训数据 type:[{}],id[{}]",reqVo.getSubType(),reqVo.getPqId()); - BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(Objects.nonNull(detailVO),String.format("未找到对应的实训数据[%s]",reqVo.getPqId())); - List stepList = JsonUtils.readCollection(detailVO.getStepJson(),ArrayList.class, Step2VO.class); - if(reqVo.getSubType() == PaperQType.SubType.Single){ - return this.submitTrainingAnswerForSingle(reqVo,stepList,puq); + return this.submitTrainingAnswerForSingle(reqVo,detailVO/*,stepList,puq*/); }else if(reqVo.getSubType() == PaperQType.SubType.Scene){ - return this.submitTrainingAnswerForScene(detailVO,reqVo,puq); + return this.submitTrainingAnswerForScene(detailVO,reqVo); } return new TrainAnswerVO(PaperQuestionState.Wrong, null); } @@ -141,18 +135,21 @@ public class PaperUserSubmitAnswerService { * * @return */ - private TrainAnswerVO submitTrainingAnswerForSingle(PaperSubmitAnswerReqVo req,List allStepList,PaperUserQuestion puq) { + private TrainAnswerVO submitTrainingAnswerForSingle(PaperSubmitAnswerReqVo req,PublishedTraining2DetailRspVo trainDetailVO/*List allStepList,PaperUserQuestion puq*/) { + BusinessExceptionAssertEnum.DATA_ERROR.assertTrue(StringUtils.hasText(trainDetailVO.getStepJson()),"场景实训没有对应的操作步骤"); + List allStepList = JsonUtils.readCollection(trainDetailVO.getStepJson(),ArrayList.class, Step2VO.class); + List cosplayStepList = allStepList.stream().filter(d->Objects.equals(d.getMemberId(),req.getTrainAnswerDetail().getCosplayMemberId())).collect(Collectors.toList()); if(req.getTrainAnswerDetail().getTrainDetail().size() != cosplayStepList.size()){ return new TrainAnswerVO(PaperQuestionState.Wrong, req.getTrainAnswerDetail()); } - if(req.getTrainAnswerDetail().getTrainDetail().stream().allMatch(d->d.isSuccess())){ + if(req.getTrainAnswerDetail().getTrainDetail().stream().allMatch(PaperTrainAnswerDetail::isSuccess)){ return new TrainAnswerVO(PaperQuestionState.Right, req.getTrainAnswerDetail()); } return new TrainAnswerVO(PaperQuestionState.Wrong, req.getTrainAnswerDetail()); } - private TrainAnswerVO submitTrainingAnswerForScene(PublishedTraining2DetailRspVo trainVO,PaperSubmitAnswerReqVo req,PaperUserQuestion puq){ + private TrainAnswerVO submitTrainingAnswerForScene(PublishedTraining2DetailRspVo trainVO,PaperSubmitAnswerReqVo req/*,PaperUserQuestion puq*/){ BusinessExceptionAssertEnum.DATA_ERROR.assertTrue(StringUtils.hasText(trainVO.getScoringRuleJson()),"场景实训没有对应的评分规则"); List scoreList = JsonUtils.readCollection(trainVO.getScoringRuleJson(),ArrayList.class, ScoringRuleVO.class); BusinessExceptionAssertEnum.DATA_ERROR.assertTrue(!CollectionUtils.isEmpty(scoreList),String.format("场景实训[%s]没有评分规则",req.getPqId())); @@ -166,7 +163,6 @@ public class PaperUserSubmitAnswerService { Map answerDetailMap = answerDetailList.stream().collect(Collectors.toMap(d->d.getStepId().toString(),Function.identity())); List resultAnswerDetailList = new ArrayList<>(allDetailVOList.size()); - for (ScoringRuleVO.DetailVO detailVO : allDetailVOList) { PaperTrainAnswerDetail answerDetail = answerDetailMap.get(detailVO.getElementId()); if(Objects.nonNull(answerDetail)){ @@ -201,38 +197,12 @@ public class PaperUserSubmitAnswerService { private String getAnswerFromQuestion(PaperQuestion pq) { String dan = pq.getQuestionAnswer().trim(); - String[] ans = dan.split(","); - TreeSet set = new TreeSet<>(); - for (String an : ans) { - set.add(an.trim()); - } - return Joiner.on(",").join(set); - /*StringBuilder sb = new StringBuilder(); - set.forEach(an -> { - if (sb.length() <= 0) { - sb.append(an); - } else { - sb.append(",").append(an); - } - });*/ -// return sb.toString(); + return Splitter.on(",").trimResults().omitEmptyStrings().splitToList(dan).stream().sorted().collect(Collectors.joining(",")); + } private String getAnswerFromSubmit(PaperSubmitAnswerReqVo req) { - TreeSet set = new TreeSet<>(); - for (String an : req.getAnswer()) { - set.add(an.trim()); - } - return Joiner.on(",").join(set); - /*StringBuilder sb = new StringBuilder(); - set.forEach(an -> { - if (sb.length() <= 0) { - sb.append(an); - } else { - sb.append(",").append(an); - } - }); - return sb.toString();*/ - } + return req.getAnswer().stream().filter(StringUtils::hasText).peek(String::trim).sorted().collect(Collectors.joining(",")); + } } diff --git a/src/main/java/club/joylink/rtss/websocket/WebsocketConfig.java b/src/main/java/club/joylink/rtss/websocket/WebsocketConfig.java index 89494009d..685b3a495 100644 --- a/src/main/java/club/joylink/rtss/websocket/WebsocketConfig.java +++ b/src/main/java/club/joylink/rtss/websocket/WebsocketConfig.java @@ -2,6 +2,7 @@ package club.joylink.rtss.websocket; import club.joylink.rtss.services.ISysUserService; import club.joylink.rtss.services.LoginSessionManager; +import club.joylink.rtss.services.project.ProjectService; import club.joylink.rtss.vo.AccountVO; import club.joylink.rtss.vo.LoginUserInfoVO; import club.joylink.rtss.websocket.interceptor.PresenceChannelInterceptor; @@ -38,6 +39,9 @@ public class WebsocketConfig implements WebSocketMessageBrokerConfigurer { private ISysUserService sysUserService; @Resource private PresenceChannelInterceptor presenceChannelInterceptor; + @Autowired + @Lazy + private ProjectService projectService; @Override public void configureMessageBroker(MessageBrokerRegistry config) { ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); @@ -75,13 +79,13 @@ public class WebsocketConfig implements WebSocketMessageBrokerConfigurer { }) .setAllowedOrigins("*"); registry.addEndpoint("/joylink-tbi-websocket") - .addInterceptors(new VSimpleInterceptor(this.sysUserService)) + .addInterceptors(new VSimpleInterceptor(this.sysUserService,this.projectService)) .setHandshakeHandler(new DefaultHandshakeHandler() { @Override protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map attributes) { - return new TrainPositionPrincipal((AccountVO) attributes.get(VSimpleInterceptor.ATT_USER_KEY)); + return new MyPrincipal((LoginUserInfoVO) attributes.get(VSimpleInterceptor.ATT_USER_KEY),true); } }) @@ -116,11 +120,16 @@ public class WebsocketConfig implements WebSocketMessageBrokerConfigurer { public class MyPrincipal implements Principal { private LoginUserInfoVO user; - + private boolean yjddzh; public MyPrincipal(LoginUserInfoVO user) { Objects.requireNonNull(user, "用户不能为null"); this.user = user; } + public MyPrincipal(LoginUserInfoVO user,boolean yjddzh) { + this(user); + this.yjddzh = yjddzh; + } + @Override public String getName() { @@ -136,20 +145,5 @@ public class WebsocketConfig implements WebSocketMessageBrokerConfigurer { } } - @Getter - public class TrainPositionPrincipal implements Principal { - private AccountVO user; - - public TrainPositionPrincipal(AccountVO user) { - Objects.requireNonNull(user, "用户不能为null"); - this.user = user; - } - - - @Override - public String getName() { - return this.user.getMobile(); - } - } } diff --git a/src/main/java/club/joylink/rtss/websocket/interceptor/PresenceChannelInterceptor.java b/src/main/java/club/joylink/rtss/websocket/interceptor/PresenceChannelInterceptor.java index 35eb12f8f..931aa6db1 100644 --- a/src/main/java/club/joylink/rtss/websocket/interceptor/PresenceChannelInterceptor.java +++ b/src/main/java/club/joylink/rtss/websocket/interceptor/PresenceChannelInterceptor.java @@ -41,17 +41,21 @@ public class PresenceChannelInterceptor implements ChannelInterceptor { } private void register(String wsSessionId, String destination, Principal principal) { - if(Objects.equals(false,principal instanceof WebsocketConfig.TrainPositionPrincipal)){ - return; + if(principal instanceof WebsocketConfig.MyPrincipal){ + WebsocketConfig.MyPrincipal myPrincipal = (WebsocketConfig.MyPrincipal) principal; + if(myPrincipal.isYjddzh()){ + log.info("武汉8号线 创建stmop链接name:{}",principal.getName()); + } } - log.info("武汉8号线 创建stmop链接name:{}",principal.getName()); } private void unregister(String wsSessionId, String destination, Principal principal) { - if(Objects.equals(false,principal instanceof WebsocketConfig.TrainPositionPrincipal)){ - return; + if(principal instanceof WebsocketConfig.MyPrincipal){ + WebsocketConfig.MyPrincipal myPrincipal = (WebsocketConfig.MyPrincipal) principal; + if(myPrincipal.isYjddzh()){ + log.info("武汉8号线 关闭stmop链接name:{}",principal.getName()); + } } - log.info("武汉8号线 关闭stmop链接name:{}",principal.getName()); } } diff --git a/src/main/java/club/joylink/rtss/websocket/interceptor/VSimpleInterceptor.java b/src/main/java/club/joylink/rtss/websocket/interceptor/VSimpleInterceptor.java index d47226201..86d3ffb7b 100644 --- a/src/main/java/club/joylink/rtss/websocket/interceptor/VSimpleInterceptor.java +++ b/src/main/java/club/joylink/rtss/websocket/interceptor/VSimpleInterceptor.java @@ -5,7 +5,11 @@ package club.joylink.rtss.websocket.interceptor; import club.joylink.rtss.entity.project.Project; import club.joylink.rtss.services.ISysUserService; +import club.joylink.rtss.services.project.ProjectService; import club.joylink.rtss.vo.AccountVO; +import club.joylink.rtss.vo.LoginUserInfoVO; +import club.joylink.rtss.vo.project.ProjectVO; +import com.google.common.base.Strings; import lombok.extern.slf4j.Slf4j; import org.springframework.http.server.ServerHttpRequest; import org.springframework.http.server.ServerHttpResponse; @@ -17,33 +21,51 @@ import java.util.Objects; @Slf4j public class VSimpleInterceptor extends BaseInterceptor { private ISysUserService sysUserService; - - public VSimpleInterceptor(ISysUserService sysUserService) { + private ProjectService projectService; + public VSimpleInterceptor(ISysUserService sysUserService,ProjectService projectService) { this.sysUserService = sysUserService; + this.projectService = projectService; } public final static String ATT_USER_KEY = "userVo"; + private final static String USER_ACCOUNT = "user_account"; + private final static String ACCOUNT_PWD = "account_pwd"; + private final static String PROJECT_CODE = "project_code"; + + + @Override public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Map attributes) throws Exception { Map map = this.findQueryParams(request); - log.debug(String.format("webSocket handshake query: [%s] uri:[%s]", map,request.getURI().getPath())); + log.error(String.format("webSocket handshake query: [%s] uri:[%s]", map,request.getRemoteAddress())); + String account = map.get(USER_ACCOUNT); + String accountPwd = map.get(ACCOUNT_PWD); + /*String projectCode = map.get(PROJECT_CODE); + if(Strings.isNullOrEmpty(projectCode)){ + return false; + }*/ try{ - AccountVO vo = this.checkUserInfo(map); +// ProjectVO projectVO = projectService.queryLoginProjectByCode(projectCode); + AccountVO vo = this.checkUserInfo(account,accountPwd); if(Objects.isNull(vo)){ + log.error("未找到对应的用户 account[{}] pwd[{}]",account,accountPwd); return false; } - attributes.put(ATT_USER_KEY,vo); + LoginUserInfoVO infoVO = new LoginUserInfoVO(); + infoVO.setAccountVO(vo); +// infoVO.setProjectInfo(projectVO); + attributes.put(ATT_USER_KEY,infoVO); return true; }catch (Exception e){ + log.error("ws 登录失败 msg:" + e.getMessage(),e); return false; } } - - private final static String USER_ACCOUNT = "user_account"; - private final static String ACCOUNT_PWD = "account_pwd"; - private AccountVO checkUserInfo(Map dataMap){ - AccountVO vo = this.sysUserService.findUserByAccountAndPassword(dataMap.get(USER_ACCOUNT),dataMap.get(ACCOUNT_PWD), Project.DEFAULT_PROJECT_CODE); + private AccountVO checkUserInfo(String account,String accountPwd){ + AccountVO vo = this.sysUserService.findUserByAccountAndPassword(account,accountPwd, Project.DEFAULT_PROJECT_CODE); return vo; } + + } From a0419ab2183298b04878b5952a709abda239f8ee Mon Sep 17 00:00:00 2001 From: tiger_zhou <123456> Date: Tue, 14 Feb 2023 13:25:38 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=BA=94=E6=80=A5ws=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rtss/websocket/WebsocketConfig.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/club/joylink/rtss/websocket/WebsocketConfig.java b/src/main/java/club/joylink/rtss/websocket/WebsocketConfig.java index 685b3a495..5b44b6fd7 100644 --- a/src/main/java/club/joylink/rtss/websocket/WebsocketConfig.java +++ b/src/main/java/club/joylink/rtss/websocket/WebsocketConfig.java @@ -85,7 +85,8 @@ public class WebsocketConfig implements WebSocketMessageBrokerConfigurer { @Override protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map attributes) { - return new MyPrincipal((LoginUserInfoVO) attributes.get(VSimpleInterceptor.ATT_USER_KEY),true); +// return new MyPrincipal((LoginUserInfoVO) attributes.get(VSimpleInterceptor.ATT_USER_KEY),true); + return new TrainPositionPrincipal((LoginUserInfoVO) attributes.get(VSimpleInterceptor.ATT_USER_KEY)); } }) @@ -145,5 +146,20 @@ public class WebsocketConfig implements WebSocketMessageBrokerConfigurer { } } + @Getter + public class TrainPositionPrincipal implements Principal { + private LoginUserInfoVO user; + + public TrainPositionPrincipal(LoginUserInfoVO user) { + Objects.requireNonNull(user, "用户不能为null"); + this.user = user; + } + + + @Override + public String getName() { + return this.user.getAccountVO().getMobile(); + } + } } From 007e4797554e14f466d31c6b73949784515cd51d Mon Sep 17 00:00:00 2001 From: tiger_zhou <123456> Date: Tue, 14 Feb 2023 13:34:06 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=BA=94=E6=80=A5ws=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/club/joylink/rtss/websocket/WebsocketConfig.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/club/joylink/rtss/websocket/WebsocketConfig.java b/src/main/java/club/joylink/rtss/websocket/WebsocketConfig.java index 5b44b6fd7..afad11652 100644 --- a/src/main/java/club/joylink/rtss/websocket/WebsocketConfig.java +++ b/src/main/java/club/joylink/rtss/websocket/WebsocketConfig.java @@ -85,8 +85,8 @@ public class WebsocketConfig implements WebSocketMessageBrokerConfigurer { @Override protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map attributes) { -// return new MyPrincipal((LoginUserInfoVO) attributes.get(VSimpleInterceptor.ATT_USER_KEY),true); - return new TrainPositionPrincipal((LoginUserInfoVO) attributes.get(VSimpleInterceptor.ATT_USER_KEY)); + return new MyPrincipal((LoginUserInfoVO) attributes.get(VSimpleInterceptor.ATT_USER_KEY),true); +// return new TrainPositionPrincipal((LoginUserInfoVO) attributes.get(VSimpleInterceptor.ATT_USER_KEY)); } }) From 5cb582f23c30c20cd194e257d419a9ae22bed487 Mon Sep 17 00:00:00 2001 From: tiger_zhou <123456> Date: Tue, 14 Feb 2023 14:19:14 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=BA=94=E6=80=A5ws=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rtss/simulation/cbtc/ATS/service/TrainPositionService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/ATS/service/TrainPositionService.java b/src/main/java/club/joylink/rtss/simulation/cbtc/ATS/service/TrainPositionService.java index 2cdd98b1c..43fd6986c 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/ATS/service/TrainPositionService.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/ATS/service/TrainPositionService.java @@ -30,7 +30,7 @@ public class TrainPositionService { public void initTrainOrUpdate(String mapName,String json){ TrainPosition tp = JsonUtils.read(json, TrainPosition.class); tp.setReceiveTime(System.currentTimeMillis()); - log.debug(json); + log.info(json); // SocketMessageVO vo = SocketMessageFactory.buildBasic(WebSocketMessageType.YJDDZH_TRAIN_POSITION,tp); // this.messageService.send(vo); this.timeOverService.addTrainAccept(mapName,tp);