10 changed files with 244 additions and 98 deletions
-
25src/main/java/a8k/A8kApplication.java
-
1src/main/java/a8k/a8k_can_protocol/A8kEcode.java
-
14src/main/java/a8k/appbean/HardwareException.java
-
51src/main/java/a8k/appbean/ecode/AppEcode.java
-
104src/main/java/a8k/appbean/ecode/AppRet.java
-
77src/main/java/a8k/appbean/ecode/AppRetEcodeInfo.java
-
5src/main/java/a8k/controller/TmpTestController.java
-
50src/main/java/a8k/service/ctrl_service/DeviceInitializationCtrlService.java
-
15src/main/java/a8k/service/hardware/CommonHardwareOpeartion.java
-
BINzhaohe_app.db
@ -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,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; |
|
||||
} |
|
||||
|
|
||||
} |
|
@ -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<T> { |
||||
|
|
||||
|
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 <T> AppRet<T> success(T data) { |
||||
|
AppRet<T> r = new AppRet<>(); |
||||
|
r.suc = true; |
||||
|
r.ecode = null; |
||||
|
r.data = data; |
||||
|
return r; |
||||
|
} |
||||
|
|
||||
|
public static <T> AppRet<T> success() { |
||||
|
AppRet<T> r = new AppRet<>(); |
||||
|
r.suc = true; |
||||
|
r.ecode = null; |
||||
|
r.data = null; |
||||
|
return r; |
||||
|
} |
||||
|
|
||||
|
public static <T> AppRet<T> fail(A8kEcode errorCode, MId mid, CmdId cmd) { |
||||
|
AppRet<T> r = new AppRet<>(); |
||||
|
r.suc = false; |
||||
|
r.ecode = new AppRetEcodeInfo(errorCode, mid, cmd); |
||||
|
r.data = null; |
||||
|
return r; |
||||
|
} |
||||
|
|
||||
|
public static <T> AppRet<T> fail(A8kEcode errorCode, MId mid) { |
||||
|
AppRet<T> r = new AppRet<>(); |
||||
|
r.suc = false; |
||||
|
r.ecode = new AppRetEcodeInfo(errorCode, mid, null); |
||||
|
r.data = null; |
||||
|
return r; |
||||
|
} |
||||
|
|
||||
|
public static <T> AppRet<T> fail(A8kEcode errorCode) { |
||||
|
AppRet<T> r = new AppRet<>(); |
||||
|
r.suc = false; |
||||
|
r.ecode = new AppRetEcodeInfo(errorCode, null, null); |
||||
|
r.data = null; |
||||
|
return r; |
||||
|
} |
||||
|
|
||||
|
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; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -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(); |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue