Skip to content

Commit 8179f69

Browse files
committed
!refactor:重构设备管理与角色页面,所有配置项放到角色页面进行配置
1 parent 20203a0 commit 8179f69

File tree

19 files changed

+1490
-1557
lines changed

19 files changed

+1490
-1557
lines changed

db/2025_06_14.sql

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
-- 1. 首先在sys_role表中添加新字段
2+
ALTER TABLE `xiaozhi`.`sys_role`
3+
ADD COLUMN `modelId` int unsigned DEFAULT NULL COMMENT '模型ID' AFTER `voiceName`,
4+
ADD COLUMN `sttId` int unsigned DEFAULT NULL COMMENT 'STT服务ID' AFTER `modelId`,
5+
ADD COLUMN `temperature` FLOAT DEFAULT 0.7 COMMENT '温度' AFTER `modelId`,
6+
ADD COLUMN `topP` FLOAT DEFAULT 0.9 COMMENT '核心采样' AFTER `temperature`,
7+
ADD COLUMN `vadSpeechTh` FLOAT DEFAULT 0.5 COMMENT '语音检测阈值' AFTER `temperature`,
8+
ADD COLUMN `vadSilenceTh` FLOAT DEFAULT 0.3 COMMENT '静音检测阈值' AFTER `vadSpeechTh`,
9+
ADD COLUMN `vadEnergyTh` FLOAT DEFAULT 0.01 COMMENT '能量检测阈值' AFTER `vadSilenceTh`,
10+
ADD COLUMN `vadSilenceMs` INT DEFAULT 1200 COMMENT '静音检测时间' AFTER `vadEnergyTh`;
11+
12+
-- 2. 将数据从sys_device表迁移到sys_role表
13+
-- 使用JOIN语法更新sys_role表中的字段,从sys_device表获取对应的值
14+
UPDATE `xiaozhi`.`sys_role` r
15+
JOIN `xiaozhi`.`sys_device` d ON r.roleId = d.roleId
16+
SET
17+
r.modelId = d.modelId,
18+
r.sttId = d.sttId,
19+
r.vadSpeechTh = d.vadSpeechTh,
20+
r.vadSilenceTh = d.vadSilenceTh,
21+
r.vadEnergyTh = d.vadEnergyTh,
22+
r.vadSilenceMs = d.vadSilenceMs
23+
WHERE d.roleId IS NOT NULL;
24+
25+
-- 3. 从sys_device表中移除这些字段(可选,取决于你是否想保留这些字段)
26+
ALTER TABLE `xiaozhi`.`sys_device`
27+
DROP COLUMN `modelId`,
28+
DROP COLUMN `sttId`,
29+
DROP COLUMN `vadSpeechTh`,
30+
DROP COLUMN `vadSilenceTh`,
31+
DROP COLUMN `vadEnergyTh`,
32+
DROP COLUMN `vadSilenceMs`;

db/init.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,7 @@ DROP TABLE IF EXISTS `xiaozhi`.`sys_device`;
5555
CREATE TABLE `xiaozhi`.`sys_device` (
5656
`deviceId` varchar(255) NOT NULL COMMENT '设备ID,主键',
5757
`deviceName` varchar(100) NOT NULL COMMENT '设备名称',
58-
`modelId` int unsigned DEFAULT NULL COMMENT '模型ID',
59-
`sttId` int unsigned DEFAULT NULL COMMENT 'STT服务ID',
6058
`roleId` int unsigned DEFAULT NULL COMMENT '角色ID,主键',
61-
`vadSpeechTh` FLOAT DEFAULT 0.5 COMMENT '语音检测阈值',
62-
`vadSilenceTh` FLOAT DEFAULT 0.3 COMMENT '静音检测阈值',
63-
`vadEnergyTh` FLOAT DEFAULT 0.01 COMMENT '能量检测阈值',
64-
`vadSilenceMs` INT DEFAULT 1200 COMMENT '静音检测时间',
6559
`function_names` varchar(250) NULL COMMENT '可用全局function的名称列表(逗号分割),为空则使用所有全局function',
6660
`ip` varchar(45) DEFAULT NULL COMMENT 'IP地址',
6761
`wifiName` varchar(100) DEFAULT NULL COMMENT 'WiFi名称',
@@ -103,6 +97,12 @@ CREATE TABLE `xiaozhi`.`sys_role` (
10397
`roleName` varchar(100) NOT NULL COMMENT '角色名称',
10498
`roleDesc` TEXT DEFAULT NULL COMMENT '角色描述',
10599
`ttsId` int DEFAULT NULL COMMENT 'TTS服务ID',
100+
`modelId` int unsigned DEFAULT NULL COMMENT '模型ID',
101+
`sttId` int unsigned DEFAULT NULL COMMENT 'STT服务ID',
102+
`vadSpeechTh` FLOAT DEFAULT 0.5 COMMENT '语音检测阈值',
103+
`vadSilenceTh` FLOAT DEFAULT 0.3 COMMENT '静音检测阈值',
104+
`vadEnergyTh` FLOAT DEFAULT 0.01 COMMENT '能量检测阈值',
105+
`vadSilenceMs` INT DEFAULT 1200 COMMENT '静音检测时间',
106106
`voiceName` varchar(100) NOT NULL COMMENT '角色语音名称',
107107
`state` enum('1','0') DEFAULT '1' COMMENT '状态:1-启用,0-禁用',
108108
`isDefault` enum('1','0') DEFAULT '0' COMMENT '是否默认角色:1-是,0-否',

src/main/java/com/xiaozhi/communication/common/MessageHandler.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ public void afterConnection(ChatSession chatSession, String deviceIdAuth) {
9393
logger.info("开始查询设备信息 - DeviceId: {}", deviceId);
9494
final SysDevice device = Optional.ofNullable(deviceService.selectDeviceById(deviceId)).orElse(new SysDevice());
9595

96+
device.setDeviceId(deviceId);
97+
device.setSessionId(sessionId);
98+
sessionManager.registerDevice(sessionId, device);
9699
// 如果已绑定,则初始化其他内容
97100
if (!ObjectUtils.isEmpty(device)) {
98-
device.setDeviceId(deviceId);
99-
device.setSessionId(sessionId);
100-
sessionManager.registerDevice(sessionId, device);
101101
//这里需要放在虚拟线程外
102102
ToolsSessionHolder toolsSessionHolder = new ToolsSessionHolder(chatSession.getSessionId(),
103103
device, toolsGlobalRegistry);
@@ -118,7 +118,7 @@ public void afterConnection(ChatSession chatSession, String deviceIdAuth) {
118118
}
119119
}
120120
if (device.getModelId() != null) {
121-
chatModelFactory.takeChatModel(device.getModelId());// 提前初始化,加速后续使用
121+
chatModelFactory.takeChatModel(device);// 提前初始化,加速后续使用
122122
chatService.initializeHistory(chatSession);
123123
// 注册全局函数
124124
toolsSessionHolder.registerGlobalFunctionTools(chatSession);
@@ -195,6 +195,10 @@ public void handleBinaryMessage(String sessionId, byte[] opusData) {
195195

196196
public void handleUnboundDevice(String sessionId, SysDevice device) {
197197
String deviceId = device.getDeviceId();
198+
if (device == null || deviceId == null) {
199+
logger.error("设备或设备ID为空,无法处理未绑定设备");
200+
return;
201+
}
198202
ChatSession chatSession = sessionManager.getSession(sessionId);
199203
if (chatSession == null || !chatSession.isOpen()) {
200204
return;
@@ -208,7 +212,7 @@ public void handleUnboundDevice(String sessionId, SysDevice device) {
208212
Thread.startVirtualThread(() -> {
209213
try {
210214
// 设备已注册但未配置模型
211-
if (device.getDeviceName() != null && device.getModelId() == null) {
215+
if (device.getDeviceName() != null && device.getRoleId() == null) {
212216
String message = "设备未配置对话模型,请到配置页面完成配置后开始对话";
213217

214218
String audioFilePath = ttsService.getDefaultTtsService().textToSpeech(message);

src/main/java/com/xiaozhi/communication/server/websocket/WebSocketHandler.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,8 @@ protected void handleTextMessage(org.springframework.web.socket.WebSocketSession
9494
if (Objects.requireNonNull(msg) instanceof HelloMessage m) {
9595
handleHelloMessage(session, m);
9696
} else {
97-
if (device.getModelId() == null) {
97+
if (device.getRoleId() == null) {
9898
// 设备未绑定,处理未绑定设备的消息
99-
device = new SysDevice();
100-
device.setDeviceId(deviceId);
10199
messageHandler.handleUnboundDevice(sessionId, device);
102100
} else {
103101
sessionManager.registerDevice(sessionId, device);

src/main/java/com/xiaozhi/controller/DeviceController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import io.swagger.v3.oas.annotations.enums.ParameterIn;
1515
import jakarta.annotation.Resource;
1616
import jakarta.servlet.http.HttpServletRequest;
17+
18+
import org.apache.ibatis.javassist.NotFoundException;
1719
import org.springframework.core.env.Environment;
1820
import org.springframework.http.HttpHeaders;
1921
import org.springframework.http.HttpStatus;
@@ -124,6 +126,9 @@ public AjaxResult add(String code) {
124126
}
125127
} catch (Exception e) {
126128
logger.error(e.getMessage(), e);
129+
if (e.getMessage() != null && e.getMessage().contains("没有配置角色")) {
130+
return AjaxResult.error(e.getMessage());
131+
}
127132
return AjaxResult.error();
128133
}
129134
}

src/main/java/com/xiaozhi/dao/DeviceMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public interface DeviceMapper {
1414
List<SysDevice> query(SysDevice device);
1515

16-
SysDevice selectDeviceById(String deviceId, boolean checkConfig, boolean countMessage);
16+
SysDevice selectDeviceById(String deviceId);
1717

1818
int generateCode(SysDevice device);
1919

0 commit comments

Comments
 (0)