集成京东数字人对话
parent
3e6344f93b
commit
1b532ce628
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 文 件 名: CustomException
|
||||
* 版 权:
|
||||
* 描 述: <描述>
|
||||
* 修 改 人: RedName
|
||||
* 修改时间: 2022/8/5
|
||||
* 跟踪单号: <跟踪单号>
|
||||
* 修改单号: <修改单号>
|
||||
* 修改内容: <修改内容>
|
||||
*/
|
||||
package com.supervision.exception;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
/**
|
||||
* <功能详细描述>
|
||||
* 自定义异常
|
||||
*
|
||||
* @author ljt
|
||||
* @version [版本号, 2022/8/5]
|
||||
* @see [相关类/方法]
|
||||
* @since [产品/模块版本]
|
||||
*/
|
||||
@Slf4j
|
||||
public class HumanException extends RuntimeException {
|
||||
|
||||
/**
|
||||
* 房间状态获取异常时,前端刷新房间,重新进入
|
||||
*/
|
||||
private static final Integer HUMAN_ERROR = 6001;
|
||||
/**
|
||||
* 异常编码
|
||||
*/
|
||||
private final Integer code;
|
||||
|
||||
/**
|
||||
* 异常信息
|
||||
*/
|
||||
private final String message;
|
||||
|
||||
private HumanException(Integer code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public static HumanException humanError(String message) {
|
||||
return new HumanException(HUMAN_ERROR, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.supervision.mapper;
|
||||
|
||||
import com.supervision.model.Lock;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* @author flevance
|
||||
* @description 针对表【lock(并发锁,目前没有redis,使用这个)】的数据库操作Mapper
|
||||
* @createDate 2023-11-01 13:11:34
|
||||
* @Entity com.supervision.model.Lock
|
||||
*/
|
||||
public interface LockMapper extends BaseMapper<Lock> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,45 @@
|
||||
package com.supervision.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import java.io.Serializable;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 并发锁,目前没有redis,使用这个
|
||||
* @TableName lock
|
||||
*/
|
||||
@TableName(value ="lock")
|
||||
@Data
|
||||
public class Lock implements Serializable {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableId
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 锁
|
||||
*/
|
||||
private String lockCode;
|
||||
|
||||
/**
|
||||
* 时间戳
|
||||
*/
|
||||
private Long timestamp;
|
||||
|
||||
/**
|
||||
* 超时毫秒数
|
||||
*/
|
||||
private Long expireMs;
|
||||
|
||||
/**
|
||||
* 线程ID,以支持重入
|
||||
*/
|
||||
private String threadId;
|
||||
|
||||
@TableField(exist = false)
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.supervision.service;
|
||||
|
||||
import com.supervision.model.Lock;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
/**
|
||||
* @author flevance
|
||||
* @description 针对表【lock(并发锁,目前没有redis,使用这个)】的数据库操作Service
|
||||
* @createDate 2023-11-01 13:11:34
|
||||
*/
|
||||
public interface LockService extends IService<Lock> {
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.supervision.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.supervision.model.Lock;
|
||||
import com.supervision.service.LockService;
|
||||
import com.supervision.mapper.LockMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author flevance
|
||||
* @description 针对表【lock(并发锁,目前没有redis,使用这个)】的数据库操作Service实现
|
||||
* @createDate 2023-11-01 13:11:34
|
||||
*/
|
||||
@Service
|
||||
public class LockServiceImpl extends ServiceImpl<LockMapper, Lock>
|
||||
implements LockService{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.supervision.rasa.pojo.vo;
|
||||
package com.supervision.vo.rasa;
|
||||
|
||||
import lombok.Data;
|
||||
|
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.supervision.mapper.LockMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="com.supervision.model.Lock">
|
||||
<id property="id" column="id" jdbcType="VARCHAR"/>
|
||||
<result property="lockCode" column="lock_code" jdbcType="VARCHAR"/>
|
||||
<result property="timestamp" column="timestamp" jdbcType="INTEGER"/>
|
||||
<result property="expireMs" column="expire_ms" jdbcType="INTEGER"/>
|
||||
<result property="threadId" column="thread_id" jdbcType="VARCHAR"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id,lock_code,timestamp,
|
||||
expire_ms,thread_id
|
||||
</sql>
|
||||
</mapper>
|
@ -1,4 +1,4 @@
|
||||
package com.supervision.websocket.dto;
|
||||
package com.supervision.pojo.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
@ -0,0 +1,18 @@
|
||||
package com.supervision.pojo.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class TalkResultResVO {
|
||||
|
||||
@ApiModelProperty("若有动作,这是需要执行的动作内容")
|
||||
private ActionDTO action;
|
||||
|
||||
/**
|
||||
* 后端返回给前端时使用,表示该是消息还是action动作,1消息,2动作
|
||||
*/
|
||||
@ApiModelProperty("后端返回给前端时使用,表示该是消息还是action动作,1消息,2动作")
|
||||
private Integer type;
|
||||
|
||||
}
|
@ -1,20 +1,15 @@
|
||||
package com.supervision.service;
|
||||
|
||||
import com.supervision.websocket.dto.SocketMessageDTO;
|
||||
import com.supervision.pojo.vo.TalkResultResVO;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public interface AskService {
|
||||
|
||||
void handlerMessageBySocket(SocketMessageDTO socketMessageDTO) throws IOException;
|
||||
|
||||
String receiveVoiceFile(MultipartFile file) throws IOException;
|
||||
TalkResultResVO receiveVoiceFile(MultipartFile file, String processId, String roomKey, String roomToken) throws IOException;
|
||||
|
||||
String replyVoice();
|
||||
|
||||
List<String> conversation(String question, String sessionId);
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,39 +0,0 @@
|
||||
package com.supervision.websocket.cache;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.supervision.exception.BusinessException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
@Slf4j
|
||||
public class WebSocketUserCache {
|
||||
|
||||
private static final Map<String, WebSocketSession> map = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
public static void login(String id, WebSocketSession socketSession) {
|
||||
map.put(id, socketSession);
|
||||
log.info("sessionId:{}注册成功", id);
|
||||
|
||||
}
|
||||
|
||||
public static void logout(String id) {
|
||||
map.remove(id);
|
||||
log.info("sessionId:{}注销成功", id);
|
||||
}
|
||||
|
||||
public static WebSocketSession getSession(String id){
|
||||
WebSocketSession webSocketSession = map.get(id);
|
||||
if (ObjectUtil.isEmpty(webSocketSession)){
|
||||
throw new BusinessException("未找到socket链接");
|
||||
}
|
||||
return webSocketSession;
|
||||
}
|
||||
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package com.supervision.websocket.config;
|
||||
|
||||
import com.supervision.websocket.handler.AskWebSocketHandler;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.WebSocketHandler;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
|
||||
|
||||
@Configuration
|
||||
public class WebSocketConfig implements WebSocketConfigurer {
|
||||
|
||||
@Override
|
||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||
registry.addHandler(askWebSocketHandler(), "/askSocket")
|
||||
.setAllowedOrigins("*");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WebSocketHandler askWebSocketHandler() {
|
||||
return new AskWebSocketHandler();
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
package com.supervision.websocket.dto;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@ApiModel
|
||||
public class SocketMessageDTO {
|
||||
|
||||
@ApiModelProperty("socket链接ID")
|
||||
private String socketId;
|
||||
|
||||
@ApiModelProperty("当前用户ID")
|
||||
private String userId;
|
||||
|
||||
@ApiModelProperty("文字消息内容")
|
||||
private String textMessage;
|
||||
|
||||
@ApiModelProperty("语音消息内容")
|
||||
private String voiceMessage;
|
||||
|
||||
@ApiModelProperty("若有动作,这是需要执行的动作内容")
|
||||
private ActionDTO action;
|
||||
|
||||
/**
|
||||
* 后端返回给前端时使用,表示该是消息还是action动作,1消息,2动作
|
||||
*/
|
||||
@ApiModelProperty("后端返回给前端时使用,表示该是消息还是action动作,1消息,2动作")
|
||||
private Integer type;
|
||||
|
||||
/**
|
||||
* 表示是消息还是action动作,0语音,1文字
|
||||
*/
|
||||
@ApiModelProperty("前端到后端使用,表示是语音还是文字,1语音,2文字")
|
||||
private Integer messageType;
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
package com.supervision.websocket.handler;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.supervision.service.AskService;
|
||||
import com.supervision.util.SpringBeanUtil;
|
||||
import com.supervision.util.UserUtil;
|
||||
import com.supervision.websocket.cache.WebSocketUserCache;
|
||||
import com.supervision.websocket.dto.SocketMessageDTO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.lang.NonNullApi;
|
||||
import org.springframework.web.socket.CloseStatus;
|
||||
import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
import org.springframework.web.socket.handler.TextWebSocketHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Slf4j
|
||||
public class AskWebSocketHandler extends TextWebSocketHandler {
|
||||
|
||||
@Override
|
||||
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
||||
// 获取本次
|
||||
String id = session.getId();
|
||||
// 缓存sessionId
|
||||
WebSocketUserCache.login(id, session);
|
||||
// 返回sessionId到前端
|
||||
SocketMessageDTO socketMessageDTO = new SocketMessageDTO();
|
||||
socketMessageDTO.setSocketId(id);
|
||||
session.sendMessage(new TextMessage(JSONUtil.toJsonStr(socketMessageDTO)));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {
|
||||
// 处理接收到的消息
|
||||
log.info("收到消息:{}", message.toString());
|
||||
try {
|
||||
// 这里反序列化消息,将消息形成固定的格式
|
||||
SocketMessageDTO socketMessageDTO = JSONUtil.toBean(message.getPayload(), SocketMessageDTO.class);
|
||||
AskService askService = SpringBeanUtil.getBean(AskService.class);
|
||||
askService.handlerMessageBySocket(socketMessageDTO);
|
||||
} catch (Exception e) {
|
||||
log.error("对话出现异常", e);
|
||||
SocketMessageDTO res = new SocketMessageDTO();
|
||||
res.setSocketId(session.getId());
|
||||
res.setUserId(UserUtil.getUser().getId());
|
||||
res.setTextMessage("出错了");
|
||||
res.setType(1);
|
||||
session.sendMessage(new TextMessage(JSONUtil.toJsonStr(res)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
|
||||
// 连接关闭时的处理逻辑
|
||||
String id = session.getId();
|
||||
WebSocketUserCache.logout(id);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue