Browse Source

重构异常和代码通用回执

tags/v0
zhaohe 1 year ago
parent
commit
c336680972
  1. 25
      src/main/java/a8k/A8kApplication.java
  2. 1
      src/main/java/a8k/a8k_can_protocol/A8kEcode.java
  3. 30
      src/main/java/a8k/appbean/ecode/AppRet.java
  4. 2
      src/main/java/a8k/controller/TmpTestController.java
  5. 11
      src/main/java/a8k/service/ctrl_service/DeviceInitializationCtrlService.java
  6. BIN
      zhaohe_app.db

25
src/main/java/a8k/A8kApplication.java

@ -1,23 +1,24 @@
package a8k; 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.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ControllerAdvice @ControllerAdvice
public class A8kApplication { public class A8kApplication {
Logger logger = LoggerFactory.getLogger(A8kApplication.class);
@ResponseBody @ResponseBody
@ExceptionHandler(value=Exception.class)
public Map<String,Object> controllerExceptionHandler(Exception e){
StringBuilder traceSb = new StringBuilder();
for ( var trace : e.getStackTrace() ) {
traceSb.append(trace.toString()).append("\n");
}
String trace = traceSb.toString();
Map<String,Object> 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);
} }
} }

1
src/main/java/a8k/a8k_can_protocol/A8kEcode.java

@ -21,6 +21,7 @@ public enum A8kEcode {
RecycleBinOverflow(15, "回收仓满"),// RecycleBinOverflow(15, "回收仓满"),//
MotorLostStep(16, "电机丢步"),// MotorLostStep(16, "电机丢步"),//
ActionOvertime(17, "动作执行超时"),// ActionOvertime(17, "动作执行超时"),//
CodeException(18, "代码异常"),//
// //

30
src/main/java/a8k/appbean/ecode/AppRet.java

@ -11,6 +11,7 @@ public class AppRet<T> {
AppRetEcodeInfo ecode; AppRetEcodeInfo ecode;
T data; T data;
long timestamp;//接口请求时间 long timestamp;//接口请求时间
String traceInfo;
public AppRet() { public AppRet() {
this.timestamp = System.currentTimeMillis(); this.timestamp = System.currentTimeMillis();
@ -32,6 +33,10 @@ public class AppRet<T> {
return data; return data;
} }
public String getTraceInfo() {
return traceInfo;
}
public static <T> AppRet<T> success(T data) { public static <T> AppRet<T> success(T data) {
AppRet<T> r = new AppRet<>(); AppRet<T> r = new AppRet<>();
@ -73,12 +78,27 @@ public class AppRet<T> {
return r; return r;
} }
public static <T> AppRet<T> fail(HardwareException e) {
AppRet<T> r = new AppRet<>();
r.suc = false;
r.ecode = new AppRetEcodeInfo(e.getErrorCode(), e.getModuleId(), e.getCmdId());
r.data = null;
public static <T> AppRet<T> fail(Exception e) {
StringBuilder traceSb = new StringBuilder();
for (var trace : e.getStackTrace()) {
traceSb.append(trace.toString()).append("\n");
}
String trace = traceSb.toString();
AppRet<T> 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; return r;
} }
} }

2
src/main/java/a8k/controller/TmpTestController.java

@ -63,7 +63,7 @@ public class TmpTestController {
@PostMapping("/api/zhaohe_test/initialize_device") @PostMapping("/api/zhaohe_test/initialize_device")
@ResponseBody @ResponseBody
public AppRet initialize_device() {
public AppRet initialize_device() throws HardwareException, InterruptedException {
return deviceInitializationCtrlService.initializeDevice(); return deviceInitializationCtrlService.initializeDevice();
// return "OK"; // return "OK";
} }

11
src/main/java/a8k/service/ctrl_service/DeviceInitializationCtrlService.java

@ -35,7 +35,7 @@ public class DeviceInitializationCtrlService {
@PostMapping("/api/DeviceInitializationCtrlService/initializeDevice") @PostMapping("/api/DeviceInitializationCtrlService/initializeDevice")
@ResponseBody @ResponseBody
public AppRet initializeDevice() {
public AppRet initializeDevice() throws HardwareException, InterruptedException {
logger.info("Initializing device ..."); logger.info("Initializing device ...");
boolean initSuc = false; boolean initSuc = false;
try { try {
@ -71,15 +71,9 @@ public class DeviceInitializationCtrlService {
// 清空耗材 // 清空耗材
initSuc = true; initSuc = true;
// throw new RuntimeException("test");
// 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 { } finally {
if (!initSuc) { if (!initSuc) {
try { try {
@ -121,6 +115,7 @@ public class DeviceInitializationCtrlService {
//试管平移通道是否有障碍 //试管平移通道是否有障碍
if (canBus.getIOState(IOId.THChInterPPS) || canBus.getIOState(IOId.THChOuterPPS)) { if (canBus.getIOState(IOId.THChInterPPS) || canBus.getIOState(IOId.THChOuterPPS)) {
logger.warn("THChInterPPS or THChOuterPPS is trigger"); logger.warn("THChInterPPS or THChOuterPPS is trigger");
// throw new HardwareException(MId.A8kIdCardReaderBoard, A8kEcode.PlateStuckDetectorSensorTrigger);
return AppRet.fail(A8kEcode.PlateStuckDetectorSensorTrigger); return AppRet.fail(A8kEcode.PlateStuckDetectorSensorTrigger);
} }

BIN
zhaohe_app.db

Loading…
Cancel
Save