mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-29 18:23:59 +08:00
update:智控台版本ota增加token返回字段
This commit is contained in:
@@ -19,7 +19,7 @@ public class DeviceReportRespDTO {
|
|||||||
|
|
||||||
@Schema(description = "固件版本信息")
|
@Schema(description = "固件版本信息")
|
||||||
private Firmware firmware;
|
private Firmware firmware;
|
||||||
|
|
||||||
@Schema(description = "WebSocket配置")
|
@Schema(description = "WebSocket配置")
|
||||||
private Websocket websocket;
|
private Websocket websocket;
|
||||||
|
|
||||||
@@ -66,12 +66,14 @@ public class DeviceReportRespDTO {
|
|||||||
@Schema(description = "时区偏移量,单位为分钟")
|
@Schema(description = "时区偏移量,单位为分钟")
|
||||||
private Integer timezone_offset;
|
private Integer timezone_offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public static class Websocket {
|
public static class Websocket {
|
||||||
@Schema(description = "WebSocket服务器地址")
|
@Schema(description = "WebSocket服务器地址")
|
||||||
private String url;
|
private String url;
|
||||||
|
@Schema(description = "WebSocket 认证 token")
|
||||||
|
private String token;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
|
|||||||
+1
@@ -169,6 +169,7 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
|
|||||||
DeviceReportRespDTO.Websocket websocket = new DeviceReportRespDTO.Websocket();
|
DeviceReportRespDTO.Websocket websocket = new DeviceReportRespDTO.Websocket();
|
||||||
// 从系统参数获取WebSocket URL,如果未配置则使用默认值
|
// 从系统参数获取WebSocket URL,如果未配置则使用默认值
|
||||||
String wsUrl = sysParamsService.getValue(Constant.SERVER_WEBSOCKET, true);
|
String wsUrl = sysParamsService.getValue(Constant.SERVER_WEBSOCKET, true);
|
||||||
|
websocket.setToken("");
|
||||||
if (StringUtils.isBlank(wsUrl) || wsUrl.equals("null")) {
|
if (StringUtils.isBlank(wsUrl) || wsUrl.equals("null")) {
|
||||||
log.error("WebSocket地址未配置,请登录智控台,在参数管理找到【server.websocket】配置");
|
log.error("WebSocket地址未配置,请登录智控台,在参数管理找到【server.websocket】配置");
|
||||||
wsUrl = "ws://xiaozhi.server.com:8000/xiaozhi/v1/";
|
wsUrl = "ws://xiaozhi.server.com:8000/xiaozhi/v1/";
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
|
import { otaStatusStyle } from './document.js';
|
||||||
import { log } from './utils/logger.js';
|
import { log } from './utils/logger.js';
|
||||||
import { otaStatusStyle } from './document.js'
|
|
||||||
|
|
||||||
// WebSocket 连接
|
// WebSocket 连接
|
||||||
export async function webSocketConnect(otaUrl, config){
|
export async function webSocketConnect(otaUrl, config) {
|
||||||
|
|
||||||
if (!validateConfig(config)) {
|
if (!validateConfig(config)) {
|
||||||
return;
|
return;
|
||||||
@@ -17,7 +17,7 @@ export async function webSocketConnect(otaUrl, config){
|
|||||||
|
|
||||||
// 从OTA响应中提取websocket信息
|
// 从OTA响应中提取websocket信息
|
||||||
const { websocket } = otaResult;
|
const { websocket } = otaResult;
|
||||||
if (!websocket || !websocket.url || !websocket.token) {
|
if (!websocket || !websocket.url) {
|
||||||
log('OTA响应中缺少websocket信息', 'error');
|
log('OTA响应中缺少websocket信息', 'error');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -27,10 +27,10 @@ export async function webSocketConnect(otaUrl, config){
|
|||||||
|
|
||||||
// 添加token参数(从OTA响应中获取)
|
// 添加token参数(从OTA响应中获取)
|
||||||
if (websocket.token) {
|
if (websocket.token) {
|
||||||
if(websocket.token.startsWith("Bearer ")){
|
if (websocket.token.startsWith("Bearer ")) {
|
||||||
connUrl.searchParams.append('authorization', websocket.token);
|
connUrl.searchParams.append('authorization', websocket.token);
|
||||||
} else {
|
} else {
|
||||||
connUrl.searchParams.append('authorization','Bearer ' + websocket.token);
|
connUrl.searchParams.append('authorization', 'Bearer ' + websocket.token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +63,7 @@ function validateConfig(config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 判断wsUrl路径是否存在错误
|
// 判断wsUrl路径是否存在错误
|
||||||
function validateWsUrl(wsUrl){
|
function validateWsUrl(wsUrl) {
|
||||||
if (wsUrl === '') return false;
|
if (wsUrl === '') return false;
|
||||||
// 检查URL格式
|
// 检查URL格式
|
||||||
if (!wsUrl.startsWith('ws://') && !wsUrl.startsWith('wss://')) {
|
if (!wsUrl.startsWith('ws://') && !wsUrl.startsWith('wss://')) {
|
||||||
|
|||||||
@@ -133,10 +133,8 @@
|
|||||||
<div class="connection-controls">
|
<div class="connection-controls">
|
||||||
<input type="text" id="otaUrl" value="http://127.0.0.1:8002/xiaozhi/ota/"
|
<input type="text" id="otaUrl" value="http://127.0.0.1:8002/xiaozhi/ota/"
|
||||||
placeholder="OTA服务器地址,如:http://127.0.0.1:8002/xiaozhi/ota/" />
|
placeholder="OTA服务器地址,如:http://127.0.0.1:8002/xiaozhi/ota/" />
|
||||||
<input type="text" id="serverUrl" value="" readonly disabled
|
<input type="text" id="serverUrl" value="" readonly disabled placeholder="点击连接按钮后,自动从OTA接口获取" />
|
||||||
placeholder="自动从OTA接口获取" />
|
|
||||||
<button id="connectButton">连接</button>
|
<button id="connectButton">连接</button>
|
||||||
<button id="authTestButton">测试认证</button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -1009,7 +1007,6 @@
|
|||||||
// 初始化事件监听器
|
// 初始化事件监听器
|
||||||
function initEventListeners() {
|
function initEventListeners() {
|
||||||
connectButton.addEventListener('click', connectToServer);
|
connectButton.addEventListener('click', connectToServer);
|
||||||
document.getElementById('authTestButton').addEventListener('click', testAuthentication);
|
|
||||||
|
|
||||||
// 设备配置面板折叠/展开
|
// 设备配置面板折叠/展开
|
||||||
const toggleButton = document.getElementById('toggleConfig');
|
const toggleButton = document.getElementById('toggleConfig');
|
||||||
@@ -1086,52 +1083,6 @@
|
|||||||
window.addEventListener('resize', initVisualizer);
|
window.addEventListener('resize', initVisualizer);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试认证
|
|
||||||
async function testAuthentication() {
|
|
||||||
log('开始测试认证...', 'info');
|
|
||||||
|
|
||||||
const config = getConfig();
|
|
||||||
|
|
||||||
const otaUrl = document.getElementById('otaUrl').value.trim();
|
|
||||||
|
|
||||||
|
|
||||||
// 显示服务器配置
|
|
||||||
log('-------- 服务器认证配置检查 --------', 'info');
|
|
||||||
log('请确认config.yaml中的auth配置:', 'info');
|
|
||||||
log('1. server.auth.enabled 为 false 或服务器已正确配置认证', 'info');
|
|
||||||
log('2. 如果启用了认证,请确认使用了正确的token', 'info');
|
|
||||||
log(`3. 或者在allowed_devices中添加了测试设备MAC:${config.deviceMac}`, 'info');
|
|
||||||
|
|
||||||
try {
|
|
||||||
log('尝试连接...', 'info');
|
|
||||||
const ws = await webSocketConnect(otaUrl, config)
|
|
||||||
|
|
||||||
if (ws) {
|
|
||||||
ws.onopen = () => {
|
|
||||||
log('测试成功: 可连接,服务器可能没有启用认证', 'success');
|
|
||||||
ws.close();
|
|
||||||
};
|
|
||||||
|
|
||||||
ws.onerror = (error) => {
|
|
||||||
log('测试失败: 连接被拒绝,服务器可能启用了认证', 'error');
|
|
||||||
};
|
|
||||||
|
|
||||||
// 5秒后关闭测试连接
|
|
||||||
setTimeout(() => {
|
|
||||||
if (ws.readyState === WebSocket.CONNECTING || ws.readyState === WebSocket.OPEN) {
|
|
||||||
ws.close();
|
|
||||||
}
|
|
||||||
}, 5000);
|
|
||||||
} else {
|
|
||||||
log(`测试失败: 连接被拒绝,服务器可能启用了认证`, 'error');
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
log(`测试出错: ${error.message}`, 'error');
|
|
||||||
}
|
|
||||||
|
|
||||||
log('认证测试已启动,请查看测试结果...', 'info');
|
|
||||||
}
|
|
||||||
|
|
||||||
// 帮助函数:ArrayBuffer转Base64
|
// 帮助函数:ArrayBuffer转Base64
|
||||||
function arrayBufferToBase64(buffer) {
|
function arrayBufferToBase64(buffer) {
|
||||||
let binary = '';
|
let binary = '';
|
||||||
|
|||||||
Reference in New Issue
Block a user