Browse Source

重构代码

tags/v0
zhaohe 1 year ago
parent
commit
4fd5d2d40c
  1. 14
      src/main/java/a8k/appbean/HardwareException.java
  2. 83
      src/main/java/a8k/appbean/ecode/AppRet.java
  3. 77
      src/main/java/a8k/appbean/ecode/AppRetEcodeInfo.java
  4. 31
      src/main/java/a8k/service/ctrl_service/DeviceInitializationCtrlService.java
  5. 9
      src/main/java/a8k/service/hardware/CommonHardwareOpeartion.java
  6. BIN
      zhaohe_app.db

14
src/main/java/a8k/appbean/HardwareException.java

@ -1,6 +1,7 @@
package a8k.appbean; package a8k.appbean;
import a8k.a8k_can_protocol.A8kEcode; import a8k.a8k_can_protocol.A8kEcode;
import a8k.a8k_can_protocol.CmdId;
import a8k.a8k_can_protocol.MId; import a8k.a8k_can_protocol.MId;
public class HardwareException extends Exception { public class HardwareException extends Exception {
@ -8,6 +9,7 @@ public class HardwareException extends Exception {
A8kEcode errorCode; A8kEcode errorCode;
MId moduleId; MId moduleId;
CmdId cmdId;
public HardwareException() { public HardwareException() {
super(); super();
@ -17,6 +19,14 @@ public class HardwareException extends Exception {
super(String.format("Module ID %s has error code %s", mid, ecode)); super(String.format("Module ID %s has error code %s", mid, ecode));
this.errorCode = ecode; this.errorCode = ecode;
this.moduleId = mid; 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() { public MId getModuleId() {
@ -27,4 +37,8 @@ public class HardwareException extends Exception {
return errorCode; return errorCode;
} }
public CmdId getCmdId() {
return cmdId;
}
} }

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

@ -5,47 +5,80 @@ import a8k.a8k_can_protocol.MId;
import a8k.a8k_can_protocol.A8kEcode; import a8k.a8k_can_protocol.A8kEcode;
import a8k.appbean.HardwareException; import a8k.appbean.HardwareException;
public class AppRet {
public A8kEcode errorCode;
public MId mid;
public CmdId cmd;
public class AppRet<T> {
public AppRet(A8kEcode errorCode, MId mid) {
this.errorCode = errorCode;
this.mid = mid;
this.cmd = CmdId.NotSet;
boolean suc;
AppRetEcodeInfo ecode;
T data;
long timestamp;//接口请求时间
public AppRet() {
this.timestamp = System.currentTimeMillis();
}
public boolean isSuc() {
return suc;
} }
public AppRet(A8kEcode errorCode) {
this.errorCode = errorCode;
this.mid = MId.NotSet;
this.cmd = CmdId.NotSet;
public long getTimestamp() {
return timestamp;
} }
public AppRet(HardwareException e) {
this.errorCode = e.getErrorCode();
this.mid = e.getModuleId();
public AppRetEcodeInfo getEcode() {
return ecode;
} }
public T getData() {
return data;
}
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 boolean isOk() {
return errorCode == A8kEcode.Success;
public static <T> AppRet<T> success() {
AppRet<T> r = new AppRet<>();
r.suc = true;
r.ecode = null;
r.data = null;
return r;
} }
public String getEcodeChName() {
return errorCode.getChname();
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 MId getMid() {
return mid;
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 String getMidChName() {
return mid.chname;
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 String getCmdChName() {
return cmd.chName;
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;
return r;
} }
} }

77
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();
}
}

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

@ -49,25 +49,29 @@ public class DeviceInitializationCtrlService {
AppRet ecode; AppRet ecode;
//硬件光电初始化 //硬件光电初始化
logger.info("hardwareStaticInit");
ecode = hardwareStaticInit(); ecode = hardwareStaticInit();
if (!ecode.isOk()) {
if (!ecode.isSuc()) {
return ecode; return ecode;
} }
//检查设备状态 //检查设备状态
logger.info("checkDeviceState");
ecode = checkDeviceState(); ecode = checkDeviceState();
if (!ecode.isOk()) {
if (!ecode.isSuc()) {
return ecode; return ecode;
} }
// 复位设备 // 复位设备
logger.info("moveMotorToZero");
ecode = moveMotorToZero(); ecode = moveMotorToZero();
if (!ecode.isOk()) {
if (!ecode.isSuc()) {
return ecode; return ecode;
} }
// 清空耗材 // 清空耗材
initSuc = true; initSuc = true;
// throw new RuntimeException("test");
} catch (HardwareException e) { } catch (HardwareException e) {
@ -88,7 +92,8 @@ public class DeviceInitializationCtrlService {
} }
} }
return new AppRet(A8kEcode.Success);
logger.info("Device initialization completed");
return AppRet.success();
} }
@ -109,46 +114,46 @@ public class DeviceInitializationCtrlService {
// 试管夹紧电机打开一点好方便有试管夹住的时突然断电时将试管取出 // 试管夹紧电机打开一点好方便有试管夹住的时突然断电时将试管取出
canBus.stepMotorEasyMoveBy(MId.ShakeModClampingM, 2); canBus.stepMotorEasyMoveBy(MId.ShakeModClampingM, 2);
canBus.waitForMod(MId.ShakeModClampingM, actionOvertime); canBus.waitForMod(MId.ShakeModClampingM, actionOvertime);
return new AppRet(A8kEcode.Success);
return AppRet.success();
} }
private AppRet checkDeviceState() throws HardwareException { private AppRet checkDeviceState() throws HardwareException {
//试管平移通道是否有障碍 //试管平移通道是否有障碍
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");
return new AppRet(A8kEcode.PlateStuckDetectorSensorTrigger);
return AppRet.fail(A8kEcode.PlateStuckDetectorSensorTrigger);
} }
//板夹仓盖子是否盖上 //板夹仓盖子是否盖上
if (!canBus.getIOState(IOId.PlateBoxCoverClosurePPS)) { if (!canBus.getIOState(IOId.PlateBoxCoverClosurePPS)) {
logger.warn("PlateBoxCoverClosure is open"); logger.warn("PlateBoxCoverClosure is open");
return new AppRet(A8kEcode.PlateBoxNotCover);
return AppRet.fail(A8kEcode.PlateBoxNotCover);
} }
//板夹仓卡板检测 //板夹仓卡板检测
if (canBus.getIOState(IOId.PlateBoxPlateStuckPPS)) { if (canBus.getIOState(IOId.PlateBoxPlateStuckPPS)) {
logger.warn("PlateBoxPlateStuckPPS is trigger"); logger.warn("PlateBoxPlateStuckPPS is trigger");
return new AppRet(A8kEcode.PlateStuckDetectorSensorTrigger);
return AppRet.fail(A8kEcode.PlateStuckDetectorSensorTrigger);
} }
//检查钩板电机是否处于终点位置 //检查钩板电机是否处于终点位置
if (!canBus.getIOState(IOId.PullerMZeroPPS)) { if (!canBus.getIOState(IOId.PullerMZeroPPS)) {
logger.warn("PullerM is not in zero pos"); logger.warn("PullerM is not in zero pos");
return new AppRet(A8kEcode.PullerMInitPosError);
return AppRet.fail(A8kEcode.PullerMInitPosError);
} }
//检查板夹仓光电是否处于起点位置 //检查板夹仓光电是否处于起点位置
if (!canBus.getIOState(IOId.PusherMZeroPPS)) { if (!canBus.getIOState(IOId.PusherMZeroPPS)) {
logger.warn("PusherM is not in zero pos"); logger.warn("PusherM is not in zero pos");
return new AppRet(A8kEcode.PusherMInitPosError);
return AppRet.fail(A8kEcode.PusherMInitPosError);
} }
//板夹仓光电 //板夹仓光电
if (canBus.getIOState(IOId.RecycleBinOverflowPPS)) { if (canBus.getIOState(IOId.RecycleBinOverflowPPS)) {
logger.warn("RecycleBinOverflow is trigger"); logger.warn("RecycleBinOverflow is trigger");
return new AppRet(A8kEcode.RecycleBinOverflow);
return AppRet.fail(A8kEcode.RecycleBinOverflow);
} }
return new AppRet(A8kEcode.Success);
return AppRet.success();
} }
private AppRet moveMotorToZero() throws HardwareException, InterruptedException { private AppRet moveMotorToZero() throws HardwareException, InterruptedException {
@ -204,7 +209,7 @@ public class DeviceInitializationCtrlService {
//canBus.waitForMod(MId.ShakeModGripperZM, actionOvertime); //canBus.waitForMod(MId.ShakeModGripperZM, actionOvertime);
return new AppRet(A8kEcode.Success);
return AppRet.success();
} }

9
src/main/java/a8k/service/hardware/CommonHardwareOpeartion.java

@ -23,9 +23,9 @@ public class CommonHardwareOpeartion {
try { try {
enableAllMotor(); enableAllMotor();
} catch (HardwareException e) { } catch (HardwareException e) {
return new AppRet(e);
return AppRet.fail(e);
} }
return new AppRet(A8kEcode.Success);
return AppRet.success();
} }
@PostMapping("/api/CommonHardwareOpeartion/disableAllMotorNoExcep") @PostMapping("/api/CommonHardwareOpeartion/disableAllMotorNoExcep")
@ -34,9 +34,9 @@ public class CommonHardwareOpeartion {
try { try {
enableAllMotor(); enableAllMotor();
} catch (HardwareException e) { } catch (HardwareException e) {
return new AppRet(e);
return AppRet.fail(e);
} }
return new AppRet(A8kEcode.Success);
return AppRet.success();
} }
public void enableAllMotor(Boolean enable) throws HardwareException { public void enableAllMotor(Boolean enable) throws HardwareException {
@ -68,6 +68,7 @@ public class CommonHardwareOpeartion {
//转盘归零 //转盘归零
canBus.stepMotorEnable(MId.IncubatorRotateCtrlM, enable ? 1 : 0); canBus.stepMotorEnable(MId.IncubatorRotateCtrlM, enable ? 1 : 0);
} }
@PostMapping("/api/CommonHardwareOpeartion/enableAllMotor") @PostMapping("/api/CommonHardwareOpeartion/enableAllMotor")

BIN
zhaohe_app.db

Loading…
Cancel
Save