diff --git a/src/main/java/a8k/A8kApplication.java b/src/main/java/a8k/A8kApplication.java index 96faa68..2eb159a 100644 --- a/src/main/java/a8k/A8kApplication.java +++ b/src/main/java/a8k/A8kApplication.java @@ -1,23 +1,24 @@ package a8k; + +import a8k.appbean.ecode.AppRet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; + import java.util.HashMap; import java.util.Map; + @ControllerAdvice public class A8kApplication { + + Logger logger = LoggerFactory.getLogger(A8kApplication.class); + @ResponseBody - @ExceptionHandler(value=Exception.class) - public Map controllerExceptionHandler(Exception e){ - StringBuilder traceSb = new StringBuilder(); - for ( var trace : e.getStackTrace() ) { - traceSb.append(trace.toString()).append("\n"); - } - String trace = traceSb.toString(); - Map map = new HashMap<>(); - map.put("success",false); - map.put("message",String.format("%s\n\nStack Trace : \n%s",e.getMessage(), trace)); - map.put("data", null); - return map; + @ExceptionHandler(value = Exception.class) + public AppRet controllerExceptionHandler(Exception e) { + logger.info("捕获到异常 : ", e); + return AppRet.fail(e); } } diff --git a/src/main/java/a8k/a8k_can_protocol/A8kEcode.java b/src/main/java/a8k/a8k_can_protocol/A8kEcode.java index 4f64aa6..2d81daa 100644 --- a/src/main/java/a8k/a8k_can_protocol/A8kEcode.java +++ b/src/main/java/a8k/a8k_can_protocol/A8kEcode.java @@ -21,6 +21,7 @@ public enum A8kEcode { RecycleBinOverflow(15, "回收仓满"),// MotorLostStep(16, "电机丢步"),// ActionOvertime(17, "动作执行超时"),// + CodeException(18, "代码异常"),// // diff --git a/src/main/java/a8k/appbean/HardwareException.java b/src/main/java/a8k/appbean/HardwareException.java index 6013f16..c7d8698 100644 --- a/src/main/java/a8k/appbean/HardwareException.java +++ b/src/main/java/a8k/appbean/HardwareException.java @@ -1,6 +1,7 @@ package a8k.appbean; import a8k.a8k_can_protocol.A8kEcode; +import a8k.a8k_can_protocol.CmdId; import a8k.a8k_can_protocol.MId; public class HardwareException extends Exception { @@ -8,6 +9,7 @@ public class HardwareException extends Exception { A8kEcode errorCode; MId moduleId; + CmdId cmdId; public HardwareException() { super(); @@ -17,6 +19,14 @@ public class HardwareException extends Exception { super(String.format("Module ID %s has error code %s", mid, ecode)); this.errorCode = ecode; this.moduleId = mid; + this.cmdId = null; + } + + public HardwareException(MId mid, A8kEcode ecode, CmdId cmd) { + super(String.format("Module ID %s has error code %s", mid, ecode)); + this.errorCode = ecode; + this.moduleId = mid; + this.cmdId = cmd; } public MId getModuleId() { @@ -27,4 +37,8 @@ public class HardwareException extends Exception { return errorCode; } + public CmdId getCmdId() { + return cmdId; + } + } diff --git a/src/main/java/a8k/appbean/ecode/AppEcode.java b/src/main/java/a8k/appbean/ecode/AppEcode.java deleted file mode 100644 index 27900f2..0000000 --- a/src/main/java/a8k/appbean/ecode/AppEcode.java +++ /dev/null @@ -1,51 +0,0 @@ -package a8k.appbean.ecode; - -import a8k.a8k_can_protocol.CmdId; -import a8k.a8k_can_protocol.MId; -import a8k.a8k_can_protocol.A8kEcode; -import a8k.appbean.HardwareException; - -public class AppEcode { - public A8kEcode errorCode; - public MId mid; - public CmdId cmd; - - public AppEcode(A8kEcode errorCode, MId mid) { - this.errorCode = errorCode; - this.mid = mid; - this.cmd = CmdId.NotSet; - } - - public AppEcode(A8kEcode errorCode) { - this.errorCode = errorCode; - this.mid = MId.NotSet; - this.cmd = CmdId.NotSet; - } - - public AppEcode(HardwareException e) { - this.errorCode = e.getErrorCode(); - this.mid = e.getModuleId(); - } - - - public boolean isOk() { - return errorCode == A8kEcode.Success; - } - - public String getEcodeChName() { - return errorCode.getChname(); - } - - public MId getMid() { - return mid; - } - - public String getMidChName() { - return mid.chname; - } - - public String getCmdChName() { - return cmd.chName; - } - -} diff --git a/src/main/java/a8k/appbean/ecode/AppRet.java b/src/main/java/a8k/appbean/ecode/AppRet.java new file mode 100644 index 0000000..f0b0242 --- /dev/null +++ b/src/main/java/a8k/appbean/ecode/AppRet.java @@ -0,0 +1,104 @@ +package a8k.appbean.ecode; + +import a8k.a8k_can_protocol.CmdId; +import a8k.a8k_can_protocol.MId; +import a8k.a8k_can_protocol.A8kEcode; +import a8k.appbean.HardwareException; + +public class AppRet { + + boolean suc; + AppRetEcodeInfo ecode; + T data; + long timestamp;//接口请求时间 + String traceInfo; + + public AppRet() { + this.timestamp = System.currentTimeMillis(); + } + + public boolean isSuc() { + return suc; + } + + public long getTimestamp() { + return timestamp; + } + + public AppRetEcodeInfo getEcode() { + return ecode; + } + + public T getData() { + return data; + } + + public String getTraceInfo() { + return traceInfo; + } + + + public static AppRet success(T data) { + AppRet r = new AppRet<>(); + r.suc = true; + r.ecode = null; + r.data = data; + return r; + } + + public static AppRet success() { + AppRet r = new AppRet<>(); + r.suc = true; + r.ecode = null; + r.data = null; + return r; + } + + public static AppRet fail(A8kEcode errorCode, MId mid, CmdId cmd) { + AppRet r = new AppRet<>(); + r.suc = false; + r.ecode = new AppRetEcodeInfo(errorCode, mid, cmd); + r.data = null; + return r; + } + + public static AppRet fail(A8kEcode errorCode, MId mid) { + AppRet r = new AppRet<>(); + r.suc = false; + r.ecode = new AppRetEcodeInfo(errorCode, mid, null); + r.data = null; + return r; + } + + public static AppRet fail(A8kEcode errorCode) { + AppRet r = new AppRet<>(); + r.suc = false; + r.ecode = new AppRetEcodeInfo(errorCode, null, null); + r.data = null; + return r; + } + + public static AppRet fail(Exception e) { + StringBuilder traceSb = new StringBuilder(); + for (var trace : e.getStackTrace()) { + traceSb.append(trace.toString()).append("\n"); + } + String trace = traceSb.toString(); + AppRet r = new AppRet<>(); + + if (e instanceof HardwareException hexcep) { + r.suc = false; + r.ecode = new AppRetEcodeInfo(hexcep.getErrorCode(), hexcep.getModuleId(), hexcep.getCmdId()); + r.data = null; + r.traceInfo = trace; + } else { + r.suc = false; + r.ecode = new AppRetEcodeInfo(A8kEcode.CodeException, null, null); + r.data = null; + r.traceInfo = trace; + } + return r; + } + + +} diff --git a/src/main/java/a8k/appbean/ecode/AppRetEcodeInfo.java b/src/main/java/a8k/appbean/ecode/AppRetEcodeInfo.java new file mode 100644 index 0000000..7d09018 --- /dev/null +++ b/src/main/java/a8k/appbean/ecode/AppRetEcodeInfo.java @@ -0,0 +1,77 @@ +package a8k.appbean.ecode; + +import a8k.a8k_can_protocol.A8kEcode; +import a8k.a8k_can_protocol.CmdId; +import a8k.a8k_can_protocol.MId; + +public class AppRetEcodeInfo { + A8kEcode errorCode; + MId mid; + CmdId cmd; + + AppRetEcodeInfo(A8kEcode errorCode, MId mid, CmdId cmd) { + this.errorCode = errorCode; + this.mid = mid; + this.cmd = cmd; + } + + //code + + public Integer getCode() { + return errorCode.index; + } + + public String getCodeChName() { + return errorCode.getChname(); + } + + public String getCodeName() { + return errorCode.toString(); + } + + //mid code + + public Integer getMidCode() { + if (mid == null) { + return null; + } + return mid.toInt(); + } + + public String getMidChName() { + if (mid == null) { + return null; + } + return mid.chname; + } + + public String getMidCodeName() { + if (mid == null) { + return null; + } + return mid.toString(); + } + + //cmd code + + public Integer getCmdCode() { + if (cmd == null) { + return null; + } + return cmd.toInt(); + } + + public String getCmdChName() { + if (cmd == null) { + return null; + } + return cmd.chName; + } + + public String getCmdCodeName() { + if (cmd == null) { + return null; + } + return cmd.toString(); + } +} diff --git a/src/main/java/a8k/controller/TmpTestController.java b/src/main/java/a8k/controller/TmpTestController.java index 1351028..d01a122 100644 --- a/src/main/java/a8k/controller/TmpTestController.java +++ b/src/main/java/a8k/controller/TmpTestController.java @@ -3,7 +3,7 @@ package a8k.controller; import a8k.a8k_can_protocol.CmdId; import a8k.a8k_can_protocol.MId; import a8k.appbean.HardwareException; -import a8k.appbean.ecode.AppEcode; +import a8k.appbean.ecode.AppRet; import a8k.base_hardware.A8kCanBusService; import a8k.service.A8kDebugTaskExecutorService; import a8k.service.ctrl_service.DeviceInitializationCtrlService; @@ -16,7 +16,6 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.ResponseBody; -import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.Map; @@ -64,7 +63,7 @@ public class TmpTestController { @PostMapping("/api/zhaohe_test/initialize_device") @ResponseBody - public AppEcode initialize_device() { + public AppRet initialize_device() throws HardwareException, InterruptedException { return deviceInitializationCtrlService.initializeDevice(); // return "OK"; } diff --git a/src/main/java/a8k/service/ctrl_service/DeviceInitializationCtrlService.java b/src/main/java/a8k/service/ctrl_service/DeviceInitializationCtrlService.java index 006f762..cd3fe59 100644 --- a/src/main/java/a8k/service/ctrl_service/DeviceInitializationCtrlService.java +++ b/src/main/java/a8k/service/ctrl_service/DeviceInitializationCtrlService.java @@ -4,7 +4,7 @@ import a8k.a8k_can_protocol.A8kEcode; import a8k.a8k_can_protocol.IOId; import a8k.a8k_can_protocol.MId; import a8k.appbean.HardwareException; -import a8k.appbean.ecode.AppEcode; +import a8k.appbean.ecode.AppRet; import a8k.base_hardware.A8kCanBusService; import a8k.service.db.dao.SamplesPreProcessModuleCtrlParamsService; import a8k.service.hardware.CommonHardwareOpeartion; @@ -35,7 +35,7 @@ public class DeviceInitializationCtrlService { @PostMapping("/api/DeviceInitializationCtrlService/initializeDevice") @ResponseBody - public AppEcode initializeDevice() { + public AppRet initializeDevice() throws HardwareException, InterruptedException { logger.info("Initializing device ..."); boolean initSuc = false; try { @@ -47,35 +47,33 @@ public class DeviceInitializationCtrlService { * 清空耗材 */ - AppEcode ecode; + AppRet ecode; //硬件光电初始化 + logger.info("hardwareStaticInit"); ecode = hardwareStaticInit(); - if (!ecode.isOk()) { + if (!ecode.isSuc()) { return ecode; } //检查设备状态 + logger.info("checkDeviceState"); ecode = checkDeviceState(); - if (!ecode.isOk()) { + if (!ecode.isSuc()) { return ecode; } // 复位设备 + logger.info("moveMotorToZero"); ecode = moveMotorToZero(); - if (!ecode.isOk()) { + if (!ecode.isSuc()) { return ecode; } // 清空耗材 initSuc = true; + // throw new RuntimeException("test"); - } catch (HardwareException e) { - logger.error("HardwareException ", e); - throw new RuntimeException(e); - } catch (InterruptedException e) { - logger.error("InterruptedException", e); - throw new RuntimeException(e); } finally { if (!initSuc) { try { @@ -88,11 +86,12 @@ public class DeviceInitializationCtrlService { } } - return new AppEcode(A8kEcode.Success); + logger.info("Device initialization completed"); + return AppRet.success(); } - private AppEcode hardwareStaticInit() throws HardwareException, InterruptedException { + private AppRet hardwareStaticInit() throws HardwareException, InterruptedException { /* * 硬件初始化: * 1.使能所有舵机 @@ -109,49 +108,50 @@ public class DeviceInitializationCtrlService { // 试管夹紧电机打开一点,好方便有试管夹住的时,突然断电时,将试管取出 canBus.stepMotorEasyMoveBy(MId.ShakeModClampingM, 2); canBus.waitForMod(MId.ShakeModClampingM, actionOvertime); - return new AppEcode(A8kEcode.Success); + return AppRet.success(); } - private AppEcode checkDeviceState() throws HardwareException { + private AppRet checkDeviceState() throws HardwareException { //试管平移通道是否有障碍 if (canBus.getIOState(IOId.THChInterPPS) || canBus.getIOState(IOId.THChOuterPPS)) { logger.warn("THChInterPPS or THChOuterPPS is trigger"); - return new AppEcode(A8kEcode.PlateStuckDetectorSensorTrigger); + // throw new HardwareException(MId.A8kIdCardReaderBoard, A8kEcode.PlateStuckDetectorSensorTrigger); + return AppRet.fail(A8kEcode.PlateStuckDetectorSensorTrigger); } //板夹仓盖子是否盖上 if (!canBus.getIOState(IOId.PlateBoxCoverClosurePPS)) { logger.warn("PlateBoxCoverClosure is open"); - return new AppEcode(A8kEcode.PlateBoxNotCover); + return AppRet.fail(A8kEcode.PlateBoxNotCover); } //板夹仓卡板检测 if (canBus.getIOState(IOId.PlateBoxPlateStuckPPS)) { logger.warn("PlateBoxPlateStuckPPS is trigger"); - return new AppEcode(A8kEcode.PlateStuckDetectorSensorTrigger); + return AppRet.fail(A8kEcode.PlateStuckDetectorSensorTrigger); } //检查钩板电机是否处于终点位置 if (!canBus.getIOState(IOId.PullerMZeroPPS)) { logger.warn("PullerM is not in zero pos"); - return new AppEcode(A8kEcode.PullerMInitPosError); + return AppRet.fail(A8kEcode.PullerMInitPosError); } //检查板夹仓光电是否处于起点位置 if (!canBus.getIOState(IOId.PusherMZeroPPS)) { logger.warn("PusherM is not in zero pos"); - return new AppEcode(A8kEcode.PusherMInitPosError); + return AppRet.fail(A8kEcode.PusherMInitPosError); } //板夹仓光电 if (canBus.getIOState(IOId.RecycleBinOverflowPPS)) { logger.warn("RecycleBinOverflow is trigger"); - return new AppEcode(A8kEcode.RecycleBinOverflow); + return AppRet.fail(A8kEcode.RecycleBinOverflow); } - return new AppEcode(A8kEcode.Success); + return AppRet.success(); } - private AppEcode moveMotorToZero() throws HardwareException, InterruptedException { + private AppRet moveMotorToZero() throws HardwareException, InterruptedException { //进出料初始化 // canBus.stepMotorEasyMoveToZero(MId.FeedingModInfeedM); @@ -204,7 +204,7 @@ public class DeviceInitializationCtrlService { //canBus.waitForMod(MId.ShakeModGripperZM, actionOvertime); - return new AppEcode(A8kEcode.Success); + return AppRet.success(); } diff --git a/src/main/java/a8k/service/hardware/CommonHardwareOpeartion.java b/src/main/java/a8k/service/hardware/CommonHardwareOpeartion.java index 02b8055..c026a55 100644 --- a/src/main/java/a8k/service/hardware/CommonHardwareOpeartion.java +++ b/src/main/java/a8k/service/hardware/CommonHardwareOpeartion.java @@ -3,7 +3,7 @@ package a8k.service.hardware; import a8k.a8k_can_protocol.A8kEcode; import a8k.a8k_can_protocol.MId; import a8k.appbean.HardwareException; -import a8k.appbean.ecode.AppEcode; +import a8k.appbean.ecode.AppRet; import a8k.base_hardware.A8kCanBusService; import jakarta.annotation.Resource; import org.springframework.stereotype.Controller; @@ -19,24 +19,24 @@ public class CommonHardwareOpeartion { @PostMapping("/api/CommonHardwareOpeartion/enableAllMotorNoExcep") @ResponseBody - public AppEcode enableAllMotorNoExcep() { + public AppRet enableAllMotorNoExcep() { try { enableAllMotor(); } catch (HardwareException e) { - return new AppEcode(e); + return AppRet.fail(e); } - return new AppEcode(A8kEcode.Success); + return AppRet.success(); } @PostMapping("/api/CommonHardwareOpeartion/disableAllMotorNoExcep") @ResponseBody - public AppEcode disableAllMotorNoExcep() { + public AppRet disableAllMotorNoExcep() { try { enableAllMotor(); } catch (HardwareException e) { - return new AppEcode(e); + return AppRet.fail(e); } - return new AppEcode(A8kEcode.Success); + return AppRet.success(); } public void enableAllMotor(Boolean enable) throws HardwareException { @@ -68,6 +68,7 @@ public class CommonHardwareOpeartion { //转盘归零 canBus.stepMotorEnable(MId.IncubatorRotateCtrlM, enable ? 1 : 0); + } @PostMapping("/api/CommonHardwareOpeartion/enableAllMotor") diff --git a/zhaohe_app.db b/zhaohe_app.db index 56c73d1..7e8293b 100644 Binary files a/zhaohe_app.db and b/zhaohe_app.db differ