From 1b4ac20d3e78a6ef75e990f8e969cac662df9bc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=A2=A6=E8=BF=9C?= <1063331231@qq.com> Date: Wed, 28 May 2025 17:37:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=88=9D=E5=A7=8B=E5=8C=96=E4=BD=BF?= =?UTF-8?q?=E8=83=BD=E5=A4=B1=E8=B4=A5=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sgs/app/cmd/debug/DebugBeeCloseCommand.java | 36 ----------- .../sgs/app/cmd/debug/DebugBeeOpenCommand.java | 42 ------------- .../sgs/app/cmd/debug/DebugBeepCloseCommand.java | 36 +++++++++++ .../sgs/app/cmd/debug/DebugBeepOpenCommand.java | 42 +++++++++++++ .../sgs/app/service/device/DeviceInitService.java | 73 ++++++++++++++++++++++ .../iflytop/sgs/common/service/CanBusService.java | 11 ++++ .../sgs/hardware/comm/can/A8kCanBusService.java | 22 ++++++- .../command/handlers/LeiSaiServoHandler.java | 2 +- .../hardware/command/handlers/MotorHandler.java | 3 +- src/main/resources/application-dev.yml | 2 +- 10 files changed, 187 insertions(+), 82 deletions(-) delete mode 100644 src/main/java/com/iflytop/sgs/app/cmd/debug/DebugBeeCloseCommand.java delete mode 100644 src/main/java/com/iflytop/sgs/app/cmd/debug/DebugBeeOpenCommand.java create mode 100644 src/main/java/com/iflytop/sgs/app/cmd/debug/DebugBeepCloseCommand.java create mode 100644 src/main/java/com/iflytop/sgs/app/cmd/debug/DebugBeepOpenCommand.java create mode 100644 src/main/java/com/iflytop/sgs/common/service/CanBusService.java diff --git a/src/main/java/com/iflytop/sgs/app/cmd/debug/DebugBeeCloseCommand.java b/src/main/java/com/iflytop/sgs/app/cmd/debug/DebugBeeCloseCommand.java deleted file mode 100644 index 33d28bb..0000000 --- a/src/main/java/com/iflytop/sgs/app/cmd/debug/DebugBeeCloseCommand.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.iflytop.sgs.app.cmd.debug; - -import com.iflytop.sgs.app.core.BaseCommandHandler; -import com.iflytop.sgs.app.model.dto.CmdDTO; -import com.iflytop.sgs.app.service.device.DeviceCommandService; -import com.iflytop.sgs.common.annotation.CommandDebugMapping; -import com.iflytop.sgs.common.cmd.CommandFuture; -import com.iflytop.sgs.common.cmd.DeviceCommandBundle; -import com.iflytop.sgs.common.cmd.DeviceCommandGenerator; -import com.iflytop.sgs.common.utils.CommandUtil; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; - -import java.util.concurrent.CompletableFuture; - -/** - * 蜂鸣器关闭 - */ -@Slf4j -@Component -@RequiredArgsConstructor -@CommandDebugMapping("bee_close") -public class DebugBeeCloseCommand extends BaseCommandHandler { - private final DeviceCommandService deviceCommandService; - - @Override - public CompletableFuture handle(CmdDTO cmdDTO) { - return runAsync(() -> { - DeviceCommandBundle beepCloseDeviceCommandBundle = DeviceCommandGenerator.beepClose(); - CommandFuture doorSetDeviceCommandSetFuture = deviceCommandService.sendCommand(cmdDTO.getCommandId(), cmdDTO.getCommand(), beepCloseDeviceCommandBundle); - CommandUtil.wait(doorSetDeviceCommandSetFuture); - }); - } -} - diff --git a/src/main/java/com/iflytop/sgs/app/cmd/debug/DebugBeeOpenCommand.java b/src/main/java/com/iflytop/sgs/app/cmd/debug/DebugBeeOpenCommand.java deleted file mode 100644 index 71050cf..0000000 --- a/src/main/java/com/iflytop/sgs/app/cmd/debug/DebugBeeOpenCommand.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.iflytop.sgs.app.cmd.debug; - -import cn.hutool.core.lang.Assert; -import com.iflytop.sgs.app.core.BaseCommandHandler; -import com.iflytop.sgs.app.model.dto.CmdDTO; -import com.iflytop.sgs.app.service.device.DeviceCommandService; -import com.iflytop.sgs.common.annotation.CommandDebugMapping; -import com.iflytop.sgs.common.cmd.CommandFuture; -import com.iflytop.sgs.common.cmd.DeviceCommandBundle; -import com.iflytop.sgs.common.cmd.DeviceCommandGenerator; -import com.iflytop.sgs.common.exception.AppException; -import com.iflytop.sgs.common.result.ResultCode; -import com.iflytop.sgs.common.utils.CommandUtil; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; - -import java.util.concurrent.CompletableFuture; - -/** - * 蜂鸣器打开 - */ -@Slf4j -@Component -@RequiredArgsConstructor -@CommandDebugMapping("bee_open") -public class DebugBeeOpenCommand extends BaseCommandHandler { - private final DeviceCommandService deviceCommandService; - - @Override - public CompletableFuture handle(CmdDTO cmdDTO) { - String mode = cmdDTO.getStringParam("mode"); - Assert.notNull(mode, ()->new AppException(ResultCode.INVALID_PARAMETER)); - return runAsync(() -> { - DeviceCommandBundle beeOpenDeviceCommand = DeviceCommandGenerator.beepOpen(mode); - CommandFuture beeOpenDeviceCommandFuture = deviceCommandService.sendCommand(cmdDTO.getCommandId(), cmdDTO.getCommand(), beeOpenDeviceCommand); - CommandUtil.wait(beeOpenDeviceCommandFuture); - - }); - } -} - diff --git a/src/main/java/com/iflytop/sgs/app/cmd/debug/DebugBeepCloseCommand.java b/src/main/java/com/iflytop/sgs/app/cmd/debug/DebugBeepCloseCommand.java new file mode 100644 index 0000000..f2607b1 --- /dev/null +++ b/src/main/java/com/iflytop/sgs/app/cmd/debug/DebugBeepCloseCommand.java @@ -0,0 +1,36 @@ +package com.iflytop.sgs.app.cmd.debug; + +import com.iflytop.sgs.app.core.BaseCommandHandler; +import com.iflytop.sgs.app.model.dto.CmdDTO; +import com.iflytop.sgs.app.service.device.DeviceCommandService; +import com.iflytop.sgs.common.annotation.CommandDebugMapping; +import com.iflytop.sgs.common.cmd.CommandFuture; +import com.iflytop.sgs.common.cmd.DeviceCommandBundle; +import com.iflytop.sgs.common.cmd.DeviceCommandGenerator; +import com.iflytop.sgs.common.utils.CommandUtil; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.concurrent.CompletableFuture; + +/** + * 蜂鸣器关闭 + */ +@Slf4j +@Component +@RequiredArgsConstructor +@CommandDebugMapping("beep_close") +public class DebugBeepCloseCommand extends BaseCommandHandler { + private final DeviceCommandService deviceCommandService; + + @Override + public CompletableFuture handle(CmdDTO cmdDTO) { + return runAsync(() -> { + DeviceCommandBundle beepCloseDeviceCommandBundle = DeviceCommandGenerator.beepClose(); + CommandFuture doorSetDeviceCommandSetFuture = deviceCommandService.sendCommand(cmdDTO.getCommandId(), cmdDTO.getCommand(), beepCloseDeviceCommandBundle); + CommandUtil.wait(doorSetDeviceCommandSetFuture); + }); + } +} + diff --git a/src/main/java/com/iflytop/sgs/app/cmd/debug/DebugBeepOpenCommand.java b/src/main/java/com/iflytop/sgs/app/cmd/debug/DebugBeepOpenCommand.java new file mode 100644 index 0000000..2ee1734 --- /dev/null +++ b/src/main/java/com/iflytop/sgs/app/cmd/debug/DebugBeepOpenCommand.java @@ -0,0 +1,42 @@ +package com.iflytop.sgs.app.cmd.debug; + +import cn.hutool.core.lang.Assert; +import com.iflytop.sgs.app.core.BaseCommandHandler; +import com.iflytop.sgs.app.model.dto.CmdDTO; +import com.iflytop.sgs.app.service.device.DeviceCommandService; +import com.iflytop.sgs.common.annotation.CommandDebugMapping; +import com.iflytop.sgs.common.cmd.CommandFuture; +import com.iflytop.sgs.common.cmd.DeviceCommandBundle; +import com.iflytop.sgs.common.cmd.DeviceCommandGenerator; +import com.iflytop.sgs.common.exception.AppException; +import com.iflytop.sgs.common.result.ResultCode; +import com.iflytop.sgs.common.utils.CommandUtil; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.concurrent.CompletableFuture; + +/** + * 蜂鸣器打开 + */ +@Slf4j +@Component +@RequiredArgsConstructor +@CommandDebugMapping("beep_open") +public class DebugBeepOpenCommand extends BaseCommandHandler { + private final DeviceCommandService deviceCommandService; + + @Override + public CompletableFuture handle(CmdDTO cmdDTO) { + String mode = cmdDTO.getStringParam("mode"); + Assert.notNull(mode, ()->new AppException(ResultCode.INVALID_PARAMETER)); + return runAsync(() -> { + DeviceCommandBundle beeOpenDeviceCommand = DeviceCommandGenerator.beepOpen(mode); + CommandFuture beeOpenDeviceCommandFuture = deviceCommandService.sendCommand(cmdDTO.getCommandId(), cmdDTO.getCommand(), beeOpenDeviceCommand); + CommandUtil.wait(beeOpenDeviceCommandFuture); + + }); + } +} + diff --git a/src/main/java/com/iflytop/sgs/app/service/device/DeviceInitService.java b/src/main/java/com/iflytop/sgs/app/service/device/DeviceInitService.java index 4138907..cb6452e 100644 --- a/src/main/java/com/iflytop/sgs/app/service/device/DeviceInitService.java +++ b/src/main/java/com/iflytop/sgs/app/service/device/DeviceInitService.java @@ -1,8 +1,20 @@ package com.iflytop.sgs.app.service.device; +import com.iflytop.sgs.app.model.bo.DeviceInitializationData; import com.iflytop.sgs.app.model.bo.status.device.DeviceState; import com.iflytop.sgs.app.model.bo.status.device.HeatModuleState; +import com.iflytop.sgs.app.model.bo.status.device.SolutionContainerState; +import com.iflytop.sgs.app.model.entity.Container; +import com.iflytop.sgs.app.model.entity.DeviceParamConfig; +import com.iflytop.sgs.app.service.api.ContainerService; +import com.iflytop.sgs.app.service.api.DeviceParamConfigService; +import com.iflytop.sgs.common.cmd.DeviceCommandBundle; +import com.iflytop.sgs.common.cmd.DeviceCommandGenerator; +import com.iflytop.sgs.common.enums.ContainerCode; +import com.iflytop.sgs.common.enums.ContainerType; import com.iflytop.sgs.common.enums.HeatModuleCode; +import com.iflytop.sgs.common.enums.cmd.CmdColor; +import com.iflytop.sgs.common.service.CanBusService; import jakarta.annotation.PostConstruct; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -10,6 +22,7 @@ import org.springframework.beans.factory.ObjectProvider; import org.springframework.stereotype.Service; import java.util.List; +import java.util.concurrent.CompletableFuture; @Slf4j @Service @@ -17,14 +30,24 @@ import java.util.List; public class DeviceInitService { private final DeviceStateService deviceStateService; private final ObjectProvider heatModuleStateProvider; + private final ContainerService containerService; + private final DeviceParamConfigService deviceParamConfigService; + private final CanBusService canBusService; + private final DeviceCommandService deviceCommandService; @PostConstruct public void init() { new Thread(() -> { try { Thread.sleep(2000); + CompletableFuture.runAsync(() -> { + DeviceCommandBundle deviceCommandBundle = DeviceCommandGenerator.tricolorLightOpen(CmdColor.blue); + deviceCommandService.sendCommand(deviceCommandBundle); + }); initDeviceState(); initDeviceSetData(); + canBusService.initOvertime(); + initEnable(); deviceStateService.getDeviceState().setInitComplete(true); } catch (Exception e) { log.error("设备初始化失败", e); @@ -34,10 +57,50 @@ public class DeviceInitService { public void initDeviceSetData() throws Exception { + if (deviceStateService.getDeviceState().isVirtual() || deviceStateService.getDeviceState().isInitComplete()) { + return; + } + //从数据库中读取数据通过串口进行数据初始化 + List deviceParamConfigs = deviceParamConfigService.list(); + for (DeviceParamConfig deviceParamConfig : deviceParamConfigs) { + DeviceInitializationData data = new DeviceInitializationData(); + data.setId(Math.toIntExact(deviceParamConfig.getId())); + data.setMid(deviceParamConfig.getMid()); + data.setRegIndex(deviceParamConfig.getRegIndex()); + data.setRegInitVal(deviceParamConfig.getRegVal()); + try { + canBusService.moduleSetRegByApp(data.getMid(), data.getRegIndex(), data.getRegInitVal()); + } catch (Exception e) { + log.error("设备初始化写入参数失败,2秒后重试", e); + Thread.sleep(2000); + initDeviceSetData(); + } + } + } + /** + * 初始化所有设备使能 + */ + public void initEnable() throws Exception { + //门电机 + DeviceCommandBundle doorEnableDeviceCommandBundle = DeviceCommandGenerator.doorEnable(); + deviceCommandService.sendCommand(doorEnableDeviceCommandBundle); + //x轴 + DeviceCommandBundle xEnableDeviceCommandBundle = DeviceCommandGenerator.transferXEnable(); + deviceCommandService.sendCommand(xEnableDeviceCommandBundle); + //z轴 + DeviceCommandBundle yEnableDeviceCommandBundle = DeviceCommandGenerator.transferZEnable(); + deviceCommandService.sendCommand(yEnableDeviceCommandBundle); + //机械臂 + DeviceCommandBundle liquidMotorEnableDeviceCommandBundle = DeviceCommandGenerator.liquidMotorEnable(); + deviceCommandService.sendCommand(liquidMotorEnableDeviceCommandBundle); + //蠕动泵 + DeviceCommandBundle liquidPumpEnableDeviceCommandBundle = DeviceCommandGenerator.liquidPumpEnable(); + deviceCommandService.sendCommand(liquidPumpEnableDeviceCommandBundle); } + public void initDeviceState() { //初始化加热模块属性 DeviceState deviceState = deviceStateService.getDeviceState(); @@ -45,5 +108,15 @@ public class DeviceInitService { for (HeatModuleCode code : HeatModuleCode.values()) { heatArea.add(heatModuleStateProvider.getObject(code)); } + List containerList = containerService.getList(); + List solutionBucket = deviceState.getSolutionModule().getSolutionContainer(); + for (Container container : containerList) { + if (container.getType() == 0) { + solutionBucket.add(new SolutionContainerState(container.getId(), ContainerCode.valueOf(container.getCode()), ContainerType.solution)); + } else { + solutionBucket.add(new SolutionContainerState(container.getId(), ContainerCode.valueOf(container.getCode()), ContainerType.waste)); + } + } + } } diff --git a/src/main/java/com/iflytop/sgs/common/service/CanBusService.java b/src/main/java/com/iflytop/sgs/common/service/CanBusService.java new file mode 100644 index 0000000..5e3a744 --- /dev/null +++ b/src/main/java/com/iflytop/sgs/common/service/CanBusService.java @@ -0,0 +1,11 @@ +package com.iflytop.sgs.common.service; + +import com.iflytop.sgs.hardware.exception.HardwareException; + +public interface CanBusService { + + void moduleSetRegByApp(String mid, String regindex, Integer reg) throws HardwareException; + + + void initOvertime(); +} diff --git a/src/main/java/com/iflytop/sgs/hardware/comm/can/A8kCanBusService.java b/src/main/java/com/iflytop/sgs/hardware/comm/can/A8kCanBusService.java index 7fa165b..1e62722 100644 --- a/src/main/java/com/iflytop/sgs/hardware/comm/can/A8kCanBusService.java +++ b/src/main/java/com/iflytop/sgs/hardware/comm/can/A8kCanBusService.java @@ -1,5 +1,7 @@ package com.iflytop.sgs.hardware.comm.can; +import com.iflytop.sgs.common.service.CanBusService; +import com.iflytop.sgs.hardware.constants.ActionOvertimeConstant; import com.iflytop.sgs.hardware.exception.HardwareException; import com.iflytop.sgs.hardware.type.*; import com.iflytop.sgs.hardware.type.error.A8kEcode; @@ -10,14 +12,17 @@ import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +import com.iflytop.sgs.hardware.type.StepMotor.StepMotorMId; import java.net.URISyntaxException; @Component @Slf4j -public class A8kCanBusService { +public class A8kCanBusService implements CanBusService { @Resource A8kCanBusConnection connection; + @Resource + ActionOvertimeConstant actionOvertimeConstant; @Value("${device.enableCanBus}") Boolean enableCanBus; @@ -76,6 +81,21 @@ public class A8kCanBusService { public ModuleStatus moduleGetStatus(MId id) throws HardwareException { return connection.moduleGetStatus(id); } + public void moduleSetRegByApp(String id, String regindex, Integer reg) throws HardwareException { + MId mId=MId.valueOf(id); + RegIndex regIndex=RegIndex.valueOf(reg); + this.moduleSetReg(mId,regIndex,reg); + } + /** + * 设置各动作超时时间 + */ + @Override + public void initOvertime() { + actionOvertimeConstant.pushNewConfig(StepMotorMId.DoorM, CmdId.step_motor_easy_move_to_zero, 30 * 1000); + actionOvertimeConstant.pushNewConfig(StepMotorMId.LiquidM, CmdId.step_motor_easy_move_to_zero, 30 * 1000); + actionOvertimeConstant.pushNewConfig(StepMotorMId.LiquidPumpM, CmdId.step_motor_easy_move_to_zero, 30 * 1000); + actionOvertimeConstant.pushNewConfig(StepMotorMId.ZM, CmdId.step_motor_easy_move_to_zero, 30 * 1000); + } public void moduleSetReg(MId id, RegIndex regindex, Integer reg) throws HardwareException { connection.callcmd2(id, CmdId.module_set_reg, 100, regindex.index, reg); diff --git a/src/main/java/com/iflytop/sgs/hardware/command/handlers/LeiSaiServoHandler.java b/src/main/java/com/iflytop/sgs/hardware/command/handlers/LeiSaiServoHandler.java index 2c08fba..e3da26f 100644 --- a/src/main/java/com/iflytop/sgs/hardware/command/handlers/LeiSaiServoHandler.java +++ b/src/main/java/com/iflytop/sgs/hardware/command/handlers/LeiSaiServoHandler.java @@ -25,7 +25,7 @@ public class LeiSaiServoHandler extends CommandHandler { private final LeiSaiServoWrapperDriver driver_; private final Map motorIdMap_ = Map.ofEntries( - Map.entry(CmdDevice.door_motor, LeisaiServoMId.XSV) + Map.entry(CmdDevice.x_motor, LeisaiServoMId.XSV) ); private final Map supportCmdDeviceMIdMap = motorIdMap_.entrySet().stream(). collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().mid)); diff --git a/src/main/java/com/iflytop/sgs/hardware/command/handlers/MotorHandler.java b/src/main/java/com/iflytop/sgs/hardware/command/handlers/MotorHandler.java index d26d663..f34a835 100644 --- a/src/main/java/com/iflytop/sgs/hardware/command/handlers/MotorHandler.java +++ b/src/main/java/com/iflytop/sgs/hardware/command/handlers/MotorHandler.java @@ -26,7 +26,8 @@ public class MotorHandler extends CommandHandler { private final Map stepMotorMIdMap_ = Map.ofEntries( Map.entry(CmdDevice.door_motor, StepMotorMId.DoorM), Map.entry(CmdDevice.z_motor, StepMotorMId.ZM), - Map.entry(CmdDevice.liquid_motor, StepMotorMId.LiquidM) // TODO: 蠕动泵后期可能会调整方案 + Map.entry(CmdDevice.liquid_motor, StepMotorMId.LiquidM), + Map.entry(CmdDevice.liquid_pump, StepMotorMId.LiquidPumpM)// TODO: 蠕动泵后期可能会调整方案 ); private final Map supportCmdDeviceMIdMap = stepMotorMIdMap_.entrySet().stream(). collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().mid)); diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index fd90bfd..7bc6f58 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -36,7 +36,7 @@ command_bus: device.enableCanBus: true iflytophald: - ip: 192.168.8.168 + ip: 192.168.10.168 cmdch.port: 19004 datach.port: 19005