From c702f091b5d37cffb83708039a3facc26b4fa9fe Mon Sep 17 00:00:00 2001 From: stream Date: Mon, 8 Dec 2025 15:51:26 +0800 Subject: [PATCH] fix: flowise --- .../controller/test/TeFlowiseController.java | 11 ++--- .../test/model/vo/FlowiseChatRequestVO.java | 9 ++++ .../test/model/vo/FlowiseStartRequestVO.java | 9 ++++ .../test/service/FlowiseActionService.java | 45 +++++++------------ 4 files changed, 39 insertions(+), 35 deletions(-) create mode 100644 cmvr-iot-test/src/main/java/com/cmvr/test/model/vo/FlowiseChatRequestVO.java create mode 100644 cmvr-iot-test/src/main/java/com/cmvr/test/model/vo/FlowiseStartRequestVO.java diff --git a/cmvr-iot-admin/src/main/java/com/cmvr/web/controller/test/TeFlowiseController.java b/cmvr-iot-admin/src/main/java/com/cmvr/web/controller/test/TeFlowiseController.java index e50af75..f93b6eb 100644 --- a/cmvr-iot-admin/src/main/java/com/cmvr/web/controller/test/TeFlowiseController.java +++ b/cmvr-iot-admin/src/main/java/com/cmvr/web/controller/test/TeFlowiseController.java @@ -3,6 +3,8 @@ package com.cmvr.web.controller.test; import com.cmvr.common.core.controller.BaseController; import com.cmvr.common.core.domain.AjaxResult; import com.cmvr.test.model.vo.FlowiseActionRequestVO; +import com.cmvr.test.model.vo.FlowiseChatRequestVO; +import com.cmvr.test.model.vo.FlowiseStartRequestVO; import com.cmvr.test.service.FlowiseActionService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -11,7 +13,6 @@ 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.RequestParam; import org.springframework.web.bind.annotation.RestController; @Api(tags = "测试--flowise服务") @@ -24,8 +25,8 @@ public class TeFlowiseController extends BaseController { @ApiOperation("调用flowise") @PostMapping("/start") - public AjaxResult start(String question) { - return AjaxResult.ok(flowiseActionService.start(question)); + public AjaxResult start(@RequestBody FlowiseStartRequestVO request) { + return AjaxResult.ok(flowiseActionService.start(request)); } @ApiOperation("终止flowise") @@ -42,7 +43,7 @@ public class TeFlowiseController extends BaseController { @ApiOperation("闲聊") @GetMapping("/chat") - public AjaxResult chat(@RequestParam("text") String text) { - return AjaxResult.ok(flowiseActionService.chat(text)); + public AjaxResult chat(@RequestBody FlowiseChatRequestVO request) { + return AjaxResult.ok(flowiseActionService.chat(request)); } } \ No newline at end of file diff --git a/cmvr-iot-test/src/main/java/com/cmvr/test/model/vo/FlowiseChatRequestVO.java b/cmvr-iot-test/src/main/java/com/cmvr/test/model/vo/FlowiseChatRequestVO.java new file mode 100644 index 0000000..7a143aa --- /dev/null +++ b/cmvr-iot-test/src/main/java/com/cmvr/test/model/vo/FlowiseChatRequestVO.java @@ -0,0 +1,9 @@ +package com.cmvr.test.model.vo; + +import lombok.Data; + +@Data +public class FlowiseChatRequestVO { + + private String text; +} diff --git a/cmvr-iot-test/src/main/java/com/cmvr/test/model/vo/FlowiseStartRequestVO.java b/cmvr-iot-test/src/main/java/com/cmvr/test/model/vo/FlowiseStartRequestVO.java new file mode 100644 index 0000000..3b2fec7 --- /dev/null +++ b/cmvr-iot-test/src/main/java/com/cmvr/test/model/vo/FlowiseStartRequestVO.java @@ -0,0 +1,9 @@ +package com.cmvr.test.model.vo; + +import lombok.Data; + +@Data +public class FlowiseStartRequestVO { + + private String question; +} diff --git a/cmvr-iot-test/src/main/java/com/cmvr/test/service/FlowiseActionService.java b/cmvr-iot-test/src/main/java/com/cmvr/test/service/FlowiseActionService.java index 4518a88..6bede16 100644 --- a/cmvr-iot-test/src/main/java/com/cmvr/test/service/FlowiseActionService.java +++ b/cmvr-iot-test/src/main/java/com/cmvr/test/service/FlowiseActionService.java @@ -5,10 +5,11 @@ import com.alibaba.fastjson2.JSONObject; import com.cmvr.common.exception.GlobalException; import com.cmvr.common.utils.http.CallAPIUtil; import com.cmvr.common.utils.uuid.IdUtils; -import com.cmvr.edge.client.model.EdgeCommonVO; import com.cmvr.edge.client.service.EdgeBioHeadService; import com.cmvr.test.enums.FlowiseActionEnum; import com.cmvr.test.model.vo.FlowiseActionRequestVO; +import com.cmvr.test.model.vo.FlowiseChatRequestVO; +import com.cmvr.test.model.vo.FlowiseStartRequestVO; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -41,23 +42,20 @@ public class FlowiseActionService { @Resource(name = "threadPoolTaskExecutor") private final ThreadPoolTaskExecutor executor; - private final String terminalId = "4ed1246c465b97975f96c9ef8371a3bd"; - private final String bioId = "bio_head"; - private final Object lock = new Object(); private String chatId = null; - public String start(String question) { + public String start(FlowiseStartRequestVO request) { synchronized (lock) { if (chatId != null) { - return "Flow is already running: " + chatId; + throw new GlobalException("Flow is already running: " + chatId); } chatId = IdUtils.fastUUID(); JSONObject body = new JSONObject(); - body.put("question", question); + body.put("question", request.getQuestion()); body.put("chatId", chatId); body.put("streaming", true); @@ -70,7 +68,7 @@ public class FlowiseActionService { .execute() .body(); } catch (Exception e) { - throw new GlobalException("flowise 流程执行失败: ", e.getMessage()); + throw new GlobalException("flowise 流程执行失败: ", e.getMessage()); } }); @@ -79,15 +77,11 @@ public class FlowiseActionService { } public String abort() { - EdgeCommonVO edgeCommonVO = new EdgeCommonVO(); - edgeCommonVO.setDeviceId(bioId); - edgeCommonVO.setTerminalId(terminalId); - edgeBioHeadService.speakStop(edgeCommonVO); CallAPIUtil.doPostJson(flowiseTts + "stop", null, null); synchronized (lock) { if (chatId == null) { - return "No flow running."; + throw new GlobalException( "No flow running."); } String id = chatId; @@ -106,6 +100,8 @@ public class FlowiseActionService { public String command(FlowiseActionRequestVO request) { String action = request.getAction(); FlowiseActionEnum actionEnum = FlowiseActionEnum.fromAction(action); + String terminalId = "4ed1246c465b97975f96c9ef8371a3bd"; + String bioId = "bio_head"; switch (actionEnum) { // ==== 表情 ==== case SMILE: @@ -128,24 +124,13 @@ public class FlowiseActionService { } } - public String chat(String text) { - // 播放前调用张嘴 - EdgeCommonVO edgeCommonVO = new EdgeCommonVO(); - edgeCommonVO.setDeviceId(bioId); - edgeCommonVO.setTerminalId(terminalId); - try { - Map body = new HashMap<>(); - body.put("text", text); - body.put("voice", "x4_yezi"); + public String chat(FlowiseChatRequestVO requestVO) { + Map body = new HashMap<>(); + body.put("text", requestVO.getText()); + body.put("voice", "x4_yezi"); -// edgeBioHeadService.speakStart(edgeCommonVO); - CallAPIUtil.doPostJson(flowiseTts + "play", null, body); - } finally { - // 播放完成调用闭嘴 -// edgeBioHeadService.speakStop(edgeCommonVO); - } + CallAPIUtil.doPostJson(flowiseTts + "play", null, body); return "ok"; } -} - +} \ No newline at end of file