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 e0bdce4..6de15dc 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 @@ -9,7 +9,6 @@ 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.model.humanoid.EdgeMoveJVO; import com.cmvr.edge.client.service.EdgeBioHeadService; import com.cmvr.edge.client.service.EdgeHumanoidRobotService; import com.cmvr.test.enums.FlowiseActionEnum; @@ -22,15 +21,14 @@ import com.cmvr.test.model.vo.FlowiseChatRequestVO; import com.cmvr.test.model.vo.FlowiseStartRequestVO; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.jetbrains.annotations.NotNull; 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.Collections; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.atomic.AtomicBoolean; @Slf4j @Service @@ -61,12 +59,6 @@ public class FlowiseActionService { @Resource(name = "threadPoolTaskExecutor") private final ThreadPoolTaskExecutor executor; - /** - * 整个动作是否在运行(张嘴 + 摆头) - */ - private final AtomicBoolean actionRunning = new AtomicBoolean(true); - - private final Object lock = new Object(); private String chatId = null; @@ -235,118 +227,41 @@ public class FlowiseActionService { public String actionStart() { - // 防止重复启动 - if (!actionRunning.compareAndSet(false, true)) { - log.warn("action 已在运行中"); - return "already running"; + EdgeCommonVO speakVO = getEdgeCommonVO(); + try { + // 张嘴 + String speakStart = edgeBioHeadService.speakStart(speakVO); + log.info("张嘴动作触发完成: {}", speakStart); + } catch (Exception e) { + throw new GlobalException("张嘴执行异常", e); } + return "ok"; + } + + @NotNull + private static EdgeCommonVO getEdgeCommonVO() { String terminalId = "4ed1246c465b97975f96c9ef8371a3bd"; - String robotId = "hc01"; String bioId = "bio_head"; - executor.execute(() -> { - EdgeCommonVO speakVO = new EdgeCommonVO(); - speakVO.setTerminalId(terminalId); - speakVO.setDeviceId(bioId); - try { - // 张嘴 - String speakStart = edgeBioHeadService.speakStart(speakVO); - log.info("张嘴动作触发完成: {}", speakStart); - -// // 摆头循环 -// boolean toRight = true; -// -// while (actionRunning.get()) { -// -// double rad = toRight ? 0.1d : -0.1d; -// -// EdgeMoveJVO moveJVO = buildHeadMoveJVO( -// terminalId, -// robotId, -// rad -// ); -// -// long t0 = System.currentTimeMillis(); -// log.info(">>> moveJ start, rad={},time={}", rad, t0); -// String res = edgeHumanoidRobotService.moveJ(moveJVO); -// -// long t1 = System.currentTimeMillis(); -// log.info(">>> moveJ end, rad={}, cost={}", rad, t1 - t0); -// -// // 判断返回 -// try { -// JSONObject resJson = JSON.parseObject(res); -// if (!resJson.getBooleanValue("success")) { -// log.warn("moveJ 返回失败,res={}", res); -// } -// } catch (Exception ignore) { -// } -// -// toRight = !toRight; -// } - - } catch (Exception e) { - log.error("action 执行异常", e); - - } finally { - // 回正 -// try { -// EdgeMoveJVO resetVO = buildHeadMoveJVO( -// terminalId, -// robotId, -// 0d -// ); -// log.info("结束动作,头部回正 rad=0"); -// edgeHumanoidRobotService.moveJ(resetVO); -// } catch (Exception e) { -// log.error("头部回正异常", e); -// } - - // 停止张嘴 - try { - edgeBioHeadService.speakStop(speakVO); - log.info("speakStop 已调用"); - } catch (Exception e) { - log.error("speakStop 调用失败", e); - } - - actionRunning.set(false); - log.info("action 线程结束"); - } - }); - - return "ok"; + EdgeCommonVO speakVO = new EdgeCommonVO(); + speakVO.setTerminalId(terminalId); + speakVO.setDeviceId(bioId); + return speakVO; } public String actionStop() { - if (!actionRunning.get()) { - return "not running"; + EdgeCommonVO speakVO = getEdgeCommonVO(); + // 停止张嘴 + try { + edgeBioHeadService.speakStop(speakVO); + log.info("停止张嘴成功"); + } catch (Exception e) { + throw new GlobalException("停止张嘴失败", e); } - - log.info("actionStop 调用,准备停止所有动作"); - actionRunning.set(false); return "ok"; } - private EdgeMoveJVO buildHeadMoveJVO(String terminalId, - String deviceId, - double rad) { - - EdgeMoveJVO vo = new EdgeMoveJVO(); - vo.setTerminalId(terminalId); - vo.setDeviceId(deviceId); - vo.setAcc(1d); - vo.setVel(1d); - - EdgeMoveJVO.JointCmd headCmd = new EdgeMoveJVO.JointCmd(); - headCmd.setJointName("HEAD_R"); - headCmd.setRad(rad); - - vo.setCmds(Collections.singletonList(headCmd)); - return vo; - } - private void speakStatus(String text) { Map body = new HashMap<>(); body.put("text", text);