mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 23:23:55 +08:00
update:优化无用组件
This commit is contained in:
+27
-23
@@ -1,16 +1,28 @@
|
||||
package xiaozhi.modules.sys.controller;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.socket.WebSocketHttpHeaders;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.socket.WebSocketHttpHeaders;
|
||||
import xiaozhi.common.annotation.LogOperation;
|
||||
import xiaozhi.common.constant.Constant;
|
||||
import xiaozhi.common.exception.RenException;
|
||||
@@ -22,11 +34,6 @@ import xiaozhi.modules.sys.enums.ServerActionEnum;
|
||||
import xiaozhi.modules.sys.service.SysParamsService;
|
||||
import xiaozhi.modules.sys.utils.WebSocketClientManager;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 服务端管理控制器
|
||||
*/
|
||||
@@ -34,11 +41,10 @@ import java.util.concurrent.TimeUnit;
|
||||
@RequestMapping("admin/server")
|
||||
@Tag(name = "服务端管理")
|
||||
@AllArgsConstructor
|
||||
public class ServerSideManageController
|
||||
{
|
||||
public class ServerSideManageController {
|
||||
private final SysParamsService sysParamsService;
|
||||
private static final ObjectMapper objectMapper;
|
||||
static {
|
||||
static {
|
||||
objectMapper = new ObjectMapper();
|
||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
}
|
||||
@@ -54,7 +60,7 @@ public class ServerSideManageController
|
||||
@Operation(summary = "通知python服务端更新配置")
|
||||
@PostMapping("/emit-action")
|
||||
@LogOperation("通知python服务端更新配置")
|
||||
// @RequiresPermissions("sys:role:superAdmin")
|
||||
@RequiresPermissions("sys:role:superAdmin")
|
||||
public Result<Boolean> emitServerAction(@RequestBody @Valid EmitSeverActionDTO emitSeverActionDTO) {
|
||||
if (emitSeverActionDTO.getAction() == null) {
|
||||
throw new RenException("无效服务端操作");
|
||||
@@ -78,8 +84,8 @@ public class ServerSideManageController
|
||||
}
|
||||
String serverSK = sysParamsService.getValue(Constant.SERVER_SECRET, true);
|
||||
WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
|
||||
headers.add("Authorization", "Bearer " + serverSK);
|
||||
headers.add("device-id", serverSK);
|
||||
headers.add("device-id", UUID.randomUUID().toString());
|
||||
headers.add("client-id", UUID.randomUUID().toString());
|
||||
|
||||
try (WebSocketClientManager client = new WebSocketClientManager.Builder()
|
||||
.connectTimeout(3, TimeUnit.SECONDS)
|
||||
@@ -91,22 +97,20 @@ public class ServerSideManageController
|
||||
client.sendJson(
|
||||
ServerActionPayloadDTO.build(
|
||||
actionEnum,
|
||||
Map.of("secret", serverSK)
|
||||
));
|
||||
Map.of("secret", serverSK)));
|
||||
// 等待服务端响应并持续监听信息
|
||||
client.listener((jsonText)-> {
|
||||
client.listener((jsonText) -> {
|
||||
if (StringUtils.isBlank(jsonText)) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
return ServerActionResponseDTO.isSuccess(objectMapper.readValue(jsonText, ServerActionResponseDTO.class));
|
||||
}
|
||||
catch (JsonProcessingException e) {
|
||||
return ServerActionResponseDTO
|
||||
.isSuccess(objectMapper.readValue(jsonText, ServerActionResponseDTO.class));
|
||||
} catch (JsonProcessingException e) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
// 捕获全部错误,由全局异常处理器返回
|
||||
throw new RenException("WebSocket连接失败或连接超时");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user