From ac7d5c283c21e7a18014970f9541020630a02d67 Mon Sep 17 00:00:00 2001 From: stream Date: Tue, 16 Dec 2025 14:02:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=91=86=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/test/TeFlowiseController.java | 12 ++ .../impl/EdgeHumanoidRobotServiceImpl.java | 3 + .../edge/EdgeTouchOperateService.java | 1 - .../test/service/FlowiseActionService.java | 127 ++++++++++++++++++ 4 files changed, 142 insertions(+), 1 deletion(-) 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 d14a5aa..c2700a2 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 @@ -53,4 +53,16 @@ public class TeFlowiseController extends BaseController { public AjaxResult chat(@RequestBody FlowiseChatRequestVO request) { return AjaxResult.ok(flowiseActionService.chat(request)); } + + @ApiOperation("动作(眨眼+动嘴+头部微动)") + @GetMapping("/action/start") + public AjaxResult actionStart() { + return AjaxResult.ok(flowiseActionService.actionStart()); + } + + @ApiOperation("动作停止") + @GetMapping("/action/stop") + public AjaxResult actionStop() { + return AjaxResult.ok(flowiseActionService.actionStop()); + } } \ No newline at end of file diff --git a/cmvr-iot-api/cmvr-iot-edge/cmvr-iot-grpc-client/src/main/java/com/cmvr/edge/client/service/impl/EdgeHumanoidRobotServiceImpl.java b/cmvr-iot-api/cmvr-iot-edge/cmvr-iot-grpc-client/src/main/java/com/cmvr/edge/client/service/impl/EdgeHumanoidRobotServiceImpl.java index d614981..36d6d92 100644 --- a/cmvr-iot-api/cmvr-iot-edge/cmvr-iot-grpc-client/src/main/java/com/cmvr/edge/client/service/impl/EdgeHumanoidRobotServiceImpl.java +++ b/cmvr-iot-api/cmvr-iot-edge/cmvr-iot-grpc-client/src/main/java/com/cmvr/edge/client/service/impl/EdgeHumanoidRobotServiceImpl.java @@ -44,7 +44,10 @@ public class EdgeHumanoidRobotServiceImpl implements EdgeHumanoidRobotService { } } +// log.info("movej start time={}", System.currentTimeMillis()); + HumanoidRobotCommand.MoveJ.Response response = stub.moveJ(builder.build()); +// log.info("movej success time={}", System.currentTimeMillis()); return JSON.toJSONString(response.getHeader()); } diff --git a/cmvr-iot-test/src/main/java/com/cmvr/test/flow/runtime/operator/edge/EdgeTouchOperateService.java b/cmvr-iot-test/src/main/java/com/cmvr/test/flow/runtime/operator/edge/EdgeTouchOperateService.java index d3a108b..7373a2c 100644 --- a/cmvr-iot-test/src/main/java/com/cmvr/test/flow/runtime/operator/edge/EdgeTouchOperateService.java +++ b/cmvr-iot-test/src/main/java/com/cmvr/test/flow/runtime/operator/edge/EdgeTouchOperateService.java @@ -12,7 +12,6 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; -import java.util.List; @Slf4j @Service 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 79a227a..0edd3cb 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 @@ -7,7 +7,10 @@ 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.model.humanoid.EdgeMoveJVO; import com.cmvr.edge.client.service.EdgeBioHeadService; +import com.cmvr.edge.client.service.EdgeHumanoidRobotService; import com.cmvr.test.enums.FlowiseActionEnum; import com.cmvr.test.flow.context.TaskContext; import com.cmvr.test.flow.context.TaskContextManager; @@ -22,8 +25,10 @@ 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 @@ -31,6 +36,7 @@ import java.util.Map; public class FlowiseActionService { private final EdgeBioHeadService edgeBioHeadService; + private final EdgeHumanoidRobotService edgeHumanoidRobotService; private final TaskContextManager taskContextManager; private final FlowControlService flowControlService; @@ -52,6 +58,11 @@ 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; @@ -161,4 +172,120 @@ public class FlowiseActionService { return root; } + + public String actionStart() { + + // 防止重复启动 + if (!actionRunning.compareAndSet(false, true)) { + log.warn("action 已在运行中"); + return "already running"; + } + + String terminalId = "8c7c5d5d8c8b09df9d1e5fc9b76c46ab"; +// String terminalId = "25d13e8ee0cbc2b1046e190f840b8ab0"; + String bioId = "hc01"; + String spkId = "spk1"; + + executor.execute(() -> { + EdgeCommonVO speakVO = new EdgeCommonVO(); + speakVO.setTerminalId(terminalId); + speakVO.setDeviceId(spkId); + try { + // 张嘴 +// edgeBioHeadService.speakStart(speakVO); + log.info("张嘴动作触发完成"); + + // 摆头循环 + boolean toRight = true; + + while (actionRunning.get()) { + + double rad = toRight ? 0.1d : -0.1d; + + EdgeMoveJVO moveJVO = buildHeadMoveJVO( + terminalId, + bioId, + 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, + bioId, + 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"; + } + + public String actionStop() { + if (!actionRunning.get()) { + return "not running"; + } + + 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); + headCmd.setVel(0d); + + vo.setCmds(Collections.singletonList(headCmd)); + return vo; + } } \ No newline at end of file