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 88f956f..e50af75 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 @@ -20,19 +20,29 @@ import org.springframework.web.bind.annotation.RestController; @RequiredArgsConstructor public class TeFlowiseController extends BaseController { - private final FlowiseActionService flowiseActionService; + @ApiOperation("调用flowise") + @PostMapping("/start") + public AjaxResult start(String question) { + return AjaxResult.ok(flowiseActionService.start(question)); + } + + @ApiOperation("终止flowise") + @GetMapping("/abort") + public AjaxResult abort() { + return AjaxResult.ok(flowiseActionService.abort()); + } + @ApiOperation("执行指令") @PostMapping("/command") public AjaxResult command(@RequestBody FlowiseActionRequestVO request) { return AjaxResult.ok(flowiseActionService.command(request)); } + @ApiOperation("闲聊") @GetMapping("/chat") - public AjaxResult chat(@RequestParam("terminalId") String terminalId, - @RequestParam("text") String text, - @RequestParam("bioId") String bioId) { - return AjaxResult.success(flowiseActionService.chat(terminalId, text,bioId)); + public AjaxResult chat(@RequestParam("text") String text) { + return AjaxResult.ok(flowiseActionService.chat(text)); } } \ No newline at end of file diff --git a/cmvr-iot-admin/src/main/resources/application-dev.yml b/cmvr-iot-admin/src/main/resources/application-dev.yml index c523d21..65d18a4 100644 --- a/cmvr-iot-admin/src/main/resources/application-dev.yml +++ b/cmvr-iot-admin/src/main/resources/application-dev.yml @@ -16,9 +16,9 @@ spring: slave: # 从数据源开关/默认关闭 enabled: false - url: - username: - password: + url: + username: + password: # 初始连接数 initialSize: 5 # 最小连接池数量 @@ -42,7 +42,7 @@ spring: testWhileIdle: true testOnBorrow: false testOnReturn: false - webStatFilter: + webStatFilter: enabled: true statViewServlet: enabled: true @@ -119,4 +119,10 @@ api: agents: INTENT_RECOGNITION: # 意图识别 app-id: d3c98sp0gdon0fcf6l00 - app-key: d3c98vp0gdon0fcf6n20 \ No newline at end of file + app-key: d3c98vp0gdon0fcf6n20 + +flowise: + tts: 192.168.0.222:8080/tts/ + start: http://192.168.0.134:3000/api/v1/prediction/f99329e9-b33d-437d-90ed-69eaa8a05418 + abort: http://192.168.0.134:3000/api/v1/chatmessage/abort/f99329e9-b33d-437d-90ed-69eaa8a05418/ + api-key: pg61JW6W_GXyqmqSoURJ4mlSRrCMRzGLBJli_w3BFkg \ No newline at end of file diff --git a/cmvr-iot-admin/src/main/resources/application-prod.yml b/cmvr-iot-admin/src/main/resources/application-prod.yml index 004f75e..641ce3b 100644 --- a/cmvr-iot-admin/src/main/resources/application-prod.yml +++ b/cmvr-iot-admin/src/main/resources/application-prod.yml @@ -119,4 +119,10 @@ api: agents: INTENT_RECOGNITION: # 意图识别 app-id: d3c98sp0gdon0fcf6l00 - app-key: d3c98vp0gdon0fcf6n20 \ No newline at end of file + app-key: d3c98vp0gdon0fcf6n20 + +flowise: + tts: 192.168.0.222:8080/tts/ + start: http://192.168.0.134:3000/api/v1/prediction/f99329e9-b33d-437d-90ed-69eaa8a05418 + abort: http://192.168.0.134:3000/api/v1/chatmessage/abort/f99329e9-b33d-437d-90ed-69eaa8a05418/ + api-key: pg61JW6W_GXyqmqSoURJ4mlSRrCMRzGLBJli_w3BFkg \ No newline at end of file diff --git a/cmvr-iot-framework/src/main/java/com/cmvr/framework/config/SecurityConfig.java b/cmvr-iot-framework/src/main/java/com/cmvr/framework/config/SecurityConfig.java index 864f08a..0061ff1 100644 --- a/cmvr-iot-framework/src/main/java/com/cmvr/framework/config/SecurityConfig.java +++ b/cmvr-iot-framework/src/main/java/com/cmvr/framework/config/SecurityConfig.java @@ -114,7 +114,7 @@ public class SecurityConfig requests.antMatchers("/login", "/register", "/captchaImage").permitAll() // 静态资源,可匿名访问 .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll() - .antMatchers("/flowise/**","/kws/**","/ws/**","/api/grpc/**","/show/**","/node-red/**","/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll() + .antMatchers("/flow/**","/flowise/**","/kws/**","/ws/**","/api/grpc/**","/show/**","/node-red/**","/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll() // 除上面外的所有请求全部需要鉴权认证 .anyRequest().authenticated(); }) 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 d639c5c..4518a88 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 @@ -1,14 +1,21 @@ package com.cmvr.test.service; +import cn.hutool.http.HttpRequest; +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 lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Service; +import javax.annotation.Resource; import java.util.HashMap; import java.util.Map; @@ -16,11 +23,87 @@ import java.util.Map; @Service @RequiredArgsConstructor public class FlowiseActionService { + private final EdgeBioHeadService edgeBioHeadService; + @Value("${flowise.tts}") + private String flowiseTts; + + @Value("${flowise.start}") + private String flowiseStart; + + @Value("${flowise.abort}") + private String flowiseAbort; + + @Value("${flowise.api-key}") + private String flowiseApiKey; + + @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) { + synchronized (lock) { + if (chatId != null) { + return "Flow is already running: " + chatId; + } + + chatId = IdUtils.fastUUID(); + + JSONObject body = new JSONObject(); + body.put("question", question); + body.put("chatId", chatId); + body.put("streaming", true); + + executor.submit(() -> { + try { + HttpRequest.post(flowiseStart) + .header("Content-Type", "application/json") + .body(body.toJSONString()) + .timeout(0) + .execute() + .body(); + } catch (Exception e) { + throw new GlobalException("flowise 流程执行失败: ", e.getMessage()); + } + }); + + return chatId; + } + } + + 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."; + } + + String id = chatId; + + String result = HttpRequest.put(flowiseAbort + id) + .header("Authorization", "Bearer " + flowiseApiKey) + .execute() + .body(); + + chatId = null; + + return result; + } + } + public String command(FlowiseActionRequestVO request) { - String terminalId = "4ed1246c465b97975f96c9ef8371a3bd"; - String bioId = "bio_head"; String action = request.getAction(); FlowiseActionEnum actionEnum = FlowiseActionEnum.fromAction(action); switch (actionEnum) { @@ -45,7 +128,7 @@ public class FlowiseActionService { } } - public String chat(String terminalId, String text, String bioId) { + public String chat(String text) { // 播放前调用张嘴 EdgeCommonVO edgeCommonVO = new EdgeCommonVO(); edgeCommonVO.setDeviceId(bioId); @@ -56,7 +139,7 @@ public class FlowiseActionService { body.put("voice", "x4_yezi"); // edgeBioHeadService.speakStart(edgeCommonVO); - CallAPIUtil.doPostJson("192.168.0.222:8080/tts/play", null, body); + CallAPIUtil.doPostJson(flowiseTts + "play", null, body); } finally { // 播放完成调用闭嘴 // edgeBioHeadService.speakStop(edgeCommonVO);