|
|
@ -19,134 +19,136 @@ import java.net.URISyntaxException; |
|
|
|
@Component |
|
|
|
@Slf4j |
|
|
|
public class A8kCanBusService implements CanBusService { |
|
|
|
@Resource |
|
|
|
A8kCanBusConnection connection; |
|
|
|
@Resource |
|
|
|
ActionOvertimeConstant actionOvertimeConstant; |
|
|
|
|
|
|
|
@Value("${device.enableCanBus}") |
|
|
|
Boolean enableCanBus; |
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
public void init() throws URISyntaxException { |
|
|
|
if (enableCanBus) { |
|
|
|
connection.connect(); |
|
|
|
}else{ |
|
|
|
log.warn("canBus is disabled"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public Boolean ping(MId id) { |
|
|
|
return connection.ping(id); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 强制关闭websocket连接,让其与canbus服务重连 |
|
|
|
*/ |
|
|
|
public void forceShutdown() { |
|
|
|
connection.close(); |
|
|
|
} |
|
|
|
|
|
|
|
// |
|
|
|
// BASE_OPERATION |
|
|
|
// |
|
|
|
|
|
|
|
public A8kPacket callcmd(MId moduleId, CmdId cmdId, Integer... param) throws HardwareException { |
|
|
|
return connection.callcmd(moduleId, cmdId, param); |
|
|
|
} |
|
|
|
|
|
|
|
public A8kPacket callcmd2(MId moduleId, CmdId cmdId, Integer overtime, Integer... params) throws HardwareException { |
|
|
|
return connection.callcmd2(moduleId, cmdId, overtime, params); |
|
|
|
} |
|
|
|
|
|
|
|
synchronized public A8kPacket send(A8kPacket pack, int overtime) throws HardwareException { |
|
|
|
return connection.send(pack, overtime); |
|
|
|
} |
|
|
|
|
|
|
|
// |
|
|
|
// MODULE FUNCTION |
|
|
|
// |
|
|
|
|
|
|
|
public void moduleStop(MId id) throws HardwareException { |
|
|
|
connection.callcmd(id, CmdId.module_stop); |
|
|
|
} |
|
|
|
|
|
|
|
public void moduleStopNoException(MId id) { |
|
|
|
try { |
|
|
|
moduleStop(id); |
|
|
|
} catch (HardwareException ignored) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
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() { |
|
|
|
@Resource |
|
|
|
A8kCanBusConnection connection; |
|
|
|
@Resource |
|
|
|
ActionOvertimeConstant actionOvertimeConstant; |
|
|
|
|
|
|
|
@Value("${device.enableCanBus}") |
|
|
|
Boolean enableCanBus; |
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
public void init() throws URISyntaxException { |
|
|
|
if (enableCanBus) { |
|
|
|
connection.connect(); |
|
|
|
} else { |
|
|
|
log.warn("canBus is disabled"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public Boolean ping(MId id) { |
|
|
|
return connection.ping(id); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 强制关闭websocket连接,让其与canbus服务重连 |
|
|
|
*/ |
|
|
|
public void forceShutdown() { |
|
|
|
connection.close(); |
|
|
|
} |
|
|
|
|
|
|
|
// |
|
|
|
// BASE_OPERATION |
|
|
|
// |
|
|
|
|
|
|
|
public A8kPacket callcmd(MId moduleId, CmdId cmdId, Integer... param) throws HardwareException { |
|
|
|
return connection.callcmd(moduleId, cmdId, param); |
|
|
|
} |
|
|
|
|
|
|
|
public A8kPacket callcmd2(MId moduleId, CmdId cmdId, Integer overtime, Integer... params) throws HardwareException { |
|
|
|
return connection.callcmd2(moduleId, cmdId, overtime, params); |
|
|
|
} |
|
|
|
|
|
|
|
synchronized public A8kPacket send(A8kPacket pack, int overtime) throws HardwareException { |
|
|
|
return connection.send(pack, overtime); |
|
|
|
} |
|
|
|
|
|
|
|
// |
|
|
|
// MODULE FUNCTION |
|
|
|
// |
|
|
|
|
|
|
|
public void moduleStop(MId id) throws HardwareException { |
|
|
|
connection.callcmd(id, CmdId.module_stop); |
|
|
|
} |
|
|
|
|
|
|
|
public void moduleStopNoException(MId id) { |
|
|
|
try { |
|
|
|
moduleStop(id); |
|
|
|
} catch (HardwareException ignored) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
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(regindex); |
|
|
|
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); |
|
|
|
} |
|
|
|
|
|
|
|
public Integer moduleGetReg(MId id, RegIndex regindex) throws HardwareException { |
|
|
|
var packet = connection.callcmd2(id, CmdId.module_get_reg, 100, regindex.index); |
|
|
|
return packet.getContentI32(0); |
|
|
|
} |
|
|
|
|
|
|
|
public A8kEcode moduleGetError(MId id) throws HardwareException { |
|
|
|
var packet = connection.callcmd(id, CmdId.module_get_error); |
|
|
|
return A8kEcode.fromInt(packet.getContentI32(0)); |
|
|
|
} |
|
|
|
|
|
|
|
public Integer moduleGetDetailError(MId id) throws HardwareException { |
|
|
|
return connection.callcmd(id, CmdId.module_get_detail_error).getContentI32(0); |
|
|
|
} |
|
|
|
|
|
|
|
public Integer moduleReadVersion(MId id) throws HardwareException { |
|
|
|
return connection.callcmd(id, CmdId.module_get_version).getContentI32(0); |
|
|
|
} |
|
|
|
|
|
|
|
public ModuleType moduleReadType(MId id) throws HardwareException { |
|
|
|
var packet = connection.callcmd(id, CmdId.module_get_type); |
|
|
|
return ModuleType.of(packet.getContentI32(0)); |
|
|
|
} |
|
|
|
|
|
|
|
public void waitForMod(MId mid, Integer acitionOvertime) throws HardwareException { |
|
|
|
long startedAt = System.currentTimeMillis(); |
|
|
|
CmdId action = connection.getLastTxCmd(mid); |
|
|
|
do { |
|
|
|
ModuleStatus statu = moduleGetStatus(mid); |
|
|
|
if (statu == ModuleStatus.IDLE) { |
|
|
|
break; |
|
|
|
} else if (statu == ModuleStatus.ERROR) { |
|
|
|
log.error("{} waitting for action {} , catch error {}, defail ecode {}", mid, action, moduleGetError(mid), moduleGetDetailError(mid)); |
|
|
|
throw HardwareException.of(new AEHardwareError(moduleGetError(mid), mid, action)); |
|
|
|
} |
|
|
|
long now = System.currentTimeMillis(); |
|
|
|
if (now - startedAt > acitionOvertime) { |
|
|
|
log.error("{} waitting for action {} overtime({})", mid, action, acitionOvertime); |
|
|
|
moduleStopNoException(mid); |
|
|
|
throw HardwareException.of(new AEHardwareError(A8kEcode.LOW_ERROR_ACTION_OVERTIME, mid, action)); |
|
|
|
} |
|
|
|
OS.hsleep(30); |
|
|
|
} while (true); |
|
|
|
} |
|
|
|
|
|
|
|
public boolean isConnect() { |
|
|
|
return connection.isOpen(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void moduleSetReg(MId id, RegIndex regindex, Integer reg) throws HardwareException { |
|
|
|
connection.callcmd2(id, CmdId.module_set_reg, 100, regindex.index, reg); |
|
|
|
} |
|
|
|
|
|
|
|
public Integer moduleGetReg(MId id, RegIndex regindex) throws HardwareException { |
|
|
|
var packet = connection.callcmd2(id, CmdId.module_get_reg, 100, regindex.index); |
|
|
|
return packet.getContentI32(0); |
|
|
|
} |
|
|
|
|
|
|
|
public A8kEcode moduleGetError(MId id) throws HardwareException { |
|
|
|
var packet = connection.callcmd(id, CmdId.module_get_error); |
|
|
|
return A8kEcode.fromInt(packet.getContentI32(0)); |
|
|
|
} |
|
|
|
|
|
|
|
public Integer moduleGetDetailError(MId id) throws HardwareException { |
|
|
|
return connection.callcmd(id, CmdId.module_get_detail_error).getContentI32(0); |
|
|
|
} |
|
|
|
|
|
|
|
public Integer moduleReadVersion(MId id) throws HardwareException { |
|
|
|
return connection.callcmd(id, CmdId.module_get_version).getContentI32(0); |
|
|
|
} |
|
|
|
|
|
|
|
public ModuleType moduleReadType(MId id) throws HardwareException { |
|
|
|
var packet = connection.callcmd(id, CmdId.module_get_type); |
|
|
|
return ModuleType.of(packet.getContentI32(0)); |
|
|
|
} |
|
|
|
|
|
|
|
public void waitForMod(MId mid, Integer acitionOvertime) throws HardwareException { |
|
|
|
long startedAt = System.currentTimeMillis(); |
|
|
|
CmdId action = connection.getLastTxCmd(mid); |
|
|
|
do { |
|
|
|
ModuleStatus statu = moduleGetStatus(mid); |
|
|
|
if (statu == ModuleStatus.IDLE) { |
|
|
|
break; |
|
|
|
} else if (statu == ModuleStatus.ERROR) { |
|
|
|
log.error("{} waitting for action {} , catch error {}, defail ecode {}", mid, action, moduleGetError(mid), moduleGetDetailError(mid)); |
|
|
|
throw HardwareException.of(new AEHardwareError(moduleGetError(mid), mid, action)); |
|
|
|
} |
|
|
|
long now = System.currentTimeMillis(); |
|
|
|
if (now - startedAt > acitionOvertime) { |
|
|
|
log.error("{} waitting for action {} overtime({})", mid, action, acitionOvertime); |
|
|
|
moduleStopNoException(mid); |
|
|
|
throw HardwareException.of(new AEHardwareError(A8kEcode.LOW_ERROR_ACTION_OVERTIME, mid, action)); |
|
|
|
} |
|
|
|
OS.hsleep(30); |
|
|
|
} while (true); |
|
|
|
} |
|
|
|
|
|
|
|
public boolean isConnect() { |
|
|
|
return connection.isOpen(); |
|
|
|
} |
|
|
|
|
|
|
|
} |