| @@ -2,7 +2,12 @@ package com.xueyi.nlt.netty.server.handler; | |||||
| import com.alibaba.fastjson2.JSONObject; | import com.alibaba.fastjson2.JSONObject; | ||||
| import com.baomidou.mybatisplus.core.toolkit.StringUtils; | import com.baomidou.mybatisplus.core.toolkit.StringUtils; | ||||
| import com.xueyi.common.core.utils.core.ObjectUtil; | |||||
| import com.xueyi.nlt.api.nlt.domain.vo.DmIntentVo; | |||||
| import com.xueyi.nlt.api.nlt.domain.vo.response.DmIntentResponse; | |||||
| import com.xueyi.nlt.netty.server.config.ServerConfig; | import com.xueyi.nlt.netty.server.config.ServerConfig; | ||||
| import com.xueyi.nlt.nlt.service.IDmIntentService; | |||||
| import com.xueyi.nlt.nlt.service.IDmRegularService; | |||||
| import com.xueyi.nlt.nlt.template.FreeChatTemplate; | import com.xueyi.nlt.nlt.template.FreeChatTemplate; | ||||
| import com.xueyi.nlt.nlt.template.MovieChatTemplate; | import com.xueyi.nlt.nlt.template.MovieChatTemplate; | ||||
| import com.yomahub.tlog.core.annotation.TLogAspect; | import com.yomahub.tlog.core.annotation.TLogAspect; | ||||
| @@ -32,6 +37,10 @@ public class ChatServerHandler extends SimpleChannelInboundHandler<TextWebSocket | |||||
| @Autowired | @Autowired | ||||
| private FreeChatTemplate freeChatTemplate; | private FreeChatTemplate freeChatTemplate; | ||||
| @Autowired | |||||
| private IDmIntentService intentService; | |||||
| @Autowired | @Autowired | ||||
| private MovieChatTemplate movieChatTemplate; | private MovieChatTemplate movieChatTemplate; | ||||
| @@ -42,6 +51,7 @@ public class ChatServerHandler extends SimpleChannelInboundHandler<TextWebSocket | |||||
| INSTANCE = this; | INSTANCE = this; | ||||
| INSTANCE.freeChatTemplate = this.freeChatTemplate; | INSTANCE.freeChatTemplate = this.freeChatTemplate; | ||||
| INSTANCE.movieChatTemplate = this.movieChatTemplate; | INSTANCE.movieChatTemplate = this.movieChatTemplate; | ||||
| INSTANCE.intentService = this.intentService; | |||||
| } | } | ||||
| @Override | @Override | ||||
| protected void channelRead0(ChannelHandlerContext channelHandlerContext, TextWebSocketFrame textWebSocketFrame) throws Exception { | protected void channelRead0(ChannelHandlerContext channelHandlerContext, TextWebSocketFrame textWebSocketFrame) throws Exception { | ||||
| @@ -65,6 +75,7 @@ public class ChatServerHandler extends SimpleChannelInboundHandler<TextWebSocket | |||||
| } | } | ||||
| // 获取到发送人的设备号 | // 获取到发送人的设备号 | ||||
| String devId = jsonObject.getString("devId"); | String devId = jsonObject.getString("devId"); | ||||
| Long operatorId = jsonObject.getLong("operatorId"); | |||||
| // 获取到发送人的用户id | // 获取到发送人的用户id | ||||
| String msg = jsonObject.getString("msg"); | String msg = jsonObject.getString("msg"); | ||||
| synchronized (ServerConfig.class) { | synchronized (ServerConfig.class) { | ||||
| @@ -78,7 +89,34 @@ public class ChatServerHandler extends SimpleChannelInboundHandler<TextWebSocket | |||||
| update(devId, channel); | update(devId, channel); | ||||
| } | } | ||||
| try { | try { | ||||
| sendMsg(devId, msg); | |||||
| DmIntentVo dmIntentVo = new DmIntentVo(); | |||||
| dmIntentVo.setContent(msg); | |||||
| dmIntentVo.setDevId(devId); | |||||
| // 调用正则判断,如果正则匹配到了,则返回正则匹配到的结果 | |||||
| DmIntentResponse dmIntentResponse = INSTANCE.intentService.regularAnswer(dmIntentVo); | |||||
| if (ObjectUtil.isNotNull(dmIntentResponse) && StringUtils.isNotEmpty(dmIntentResponse.getSkillCode())) { | |||||
| JSONObject jo = formatResult(dmIntentResponse,2); | |||||
| channel.writeAndFlush(new TextWebSocketFrame(jo.toJSONString())); | |||||
| return; | |||||
| } | |||||
| // 调用知识库,如果知识库匹配到了,则返回知识库匹配到的结果 | |||||
| dmIntentResponse = INSTANCE.intentService.knowledgeAnswer(dmIntentVo); | |||||
| if (ObjectUtil.isNotNull(dmIntentResponse) && StringUtils.isNotEmpty(dmIntentResponse.getSkillCode()) && dmIntentResponse.getH5().getInteger("accurate") == 1) { | |||||
| JSONObject jo = formatResult(dmIntentResponse,2); | |||||
| channel.writeAndFlush(new TextWebSocketFrame(jo.toJSONString())); | |||||
| return; | |||||
| } | |||||
| else { | |||||
| JSONObject jo = new JSONObject(); | |||||
| jo.put("action","chat"); | |||||
| jo.put("motion","idle"); | |||||
| jo.put("traceId",""); | |||||
| jo.put("status",1); | |||||
| jo.put("tts","请稍等一下,我要查询一下功能。"); | |||||
| channel.writeAndFlush(new TextWebSocketFrame(jo.toJSONString())); | |||||
| sendMsg(devId, msg); | |||||
| } | |||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| JSONObject jo = new JSONObject(); | JSONObject jo = new JSONObject(); | ||||
| jo.put("action","chat"); | jo.put("action","chat"); | ||||
| @@ -211,4 +249,16 @@ public class ChatServerHandler extends SimpleChannelInboundHandler<TextWebSocket | |||||
| ctx.close(); | ctx.close(); | ||||
| } | } | ||||
| private JSONObject formatResult(DmIntentResponse dmIntentResponse,Integer status) { | |||||
| JSONObject resultJson = new JSONObject(); | |||||
| resultJson.put("action",dmIntentResponse.getAction()); | |||||
| resultJson.put("motion",dmIntentResponse.getMotion()); | |||||
| resultJson.put("traceId",""); | |||||
| resultJson.put("skillCode",dmIntentResponse.getSkillCode()); | |||||
| resultJson.put("status",status); | |||||
| resultJson.put("tts",""); | |||||
| resultJson.put("h5",dmIntentResponse.getH5()); | |||||
| return resultJson; | |||||
| } | |||||
| } | } | ||||