CMVR-IOT/cmvr-iot-test/src/main/java/com/cmvr/test/service/FlowiseActionService.java

152 lines
4.8 KiB
Java
Raw Normal View History

2025-12-04 15:04:46 +08:00
package com.cmvr.test.service;
2025-12-08 14:51:58 +08:00
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSONObject;
import com.cmvr.common.exception.GlobalException;
2025-12-05 17:04:51 +08:00
import com.cmvr.common.utils.http.CallAPIUtil;
2025-12-08 14:51:58 +08:00
import com.cmvr.common.utils.uuid.IdUtils;
2025-12-04 15:04:46 +08:00
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;
2025-12-08 14:51:58 +08:00
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
2025-12-04 15:04:46 +08:00
import org.springframework.stereotype.Service;
2025-12-08 14:51:58 +08:00
import javax.annotation.Resource;
2025-12-05 17:04:51 +08:00
import java.util.HashMap;
import java.util.Map;
2025-12-04 15:04:46 +08:00
@Slf4j
@Service
@RequiredArgsConstructor
public class FlowiseActionService {
2025-12-08 14:51:58 +08:00
2025-12-04 15:04:46 +08:00
private final EdgeBioHeadService edgeBioHeadService;
2025-12-08 14:51:58 +08:00
@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;
}
}
2025-12-05 17:04:51 +08:00
public String command(FlowiseActionRequestVO request) {
2025-12-04 15:04:46 +08:00
String action = request.getAction();
FlowiseActionEnum actionEnum = FlowiseActionEnum.fromAction(action);
switch (actionEnum) {
// ==== 表情 ====
case SMILE:
2025-12-05 17:04:51 +08:00
return edgeBioHeadService.specialExpression(terminalId, bioId, 1);
2025-12-04 15:04:46 +08:00
case SURPRISED:
2025-12-05 17:04:51 +08:00
return edgeBioHeadService.specialExpression(terminalId, bioId, 2);
2025-12-04 15:04:46 +08:00
case ANGRY:
2025-12-05 17:04:51 +08:00
return edgeBioHeadService.specialExpression(terminalId, bioId, 3);
2025-12-04 15:04:46 +08:00
case SAD:
2025-12-05 17:04:51 +08:00
return edgeBioHeadService.specialExpression(terminalId, bioId, 4);
2025-12-04 15:04:46 +08:00
case SLEEP:
2025-12-05 17:04:51 +08:00
return edgeBioHeadService.specialExpression(terminalId, bioId, 5);
2025-12-04 15:04:46 +08:00
case YAWN:
2025-12-05 17:04:51 +08:00
return edgeBioHeadService.specialExpression(terminalId, bioId, 6);
2025-12-04 15:04:46 +08:00
2025-12-05 17:04:51 +08:00
// ==== 动作 ====
2025-12-04 15:04:46 +08:00
default:
throw new UnsupportedOperationException("未实现的 Flowise Action: " + action);
}
}
2025-12-05 17:04:51 +08:00
2025-12-08 14:51:58 +08:00
public String chat(String text) {
2025-12-05 17:04:51 +08:00
// 播放前调用张嘴
EdgeCommonVO edgeCommonVO = new EdgeCommonVO();
edgeCommonVO.setDeviceId(bioId);
edgeCommonVO.setTerminalId(terminalId);
try {
Map<String, String> body = new HashMap<>();
body.put("text", text);
body.put("voice", "x4_yezi");
// edgeBioHeadService.speakStart(edgeCommonVO);
2025-12-08 14:51:58 +08:00
CallAPIUtil.doPostJson(flowiseTts + "play", null, body);
2025-12-05 17:04:51 +08:00
} finally {
// 播放完成调用闭嘴
// edgeBioHeadService.speakStop(edgeCommonVO);
}
return "ok";
}
2025-12-04 15:04:46 +08:00
}