You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
174 lines
8.7 KiB
174 lines
8.7 KiB
package com.iflytop.gd.app.service;
|
|
|
|
import com.iflytop.gd.app.core.device.DeviceState;
|
|
import com.iflytop.gd.app.core.device.HeatModuleState;
|
|
import com.iflytop.gd.app.core.device.SolutionContainerState;
|
|
import com.iflytop.gd.app.model.bo.DeviceInitializationData;
|
|
import com.iflytop.gd.app.model.entity.Container;
|
|
import com.iflytop.gd.app.model.entity.DeviceParamConfig;
|
|
import com.iflytop.gd.common.enums.ContainerCode;
|
|
import com.iflytop.gd.common.enums.ContainerType;
|
|
import com.iflytop.gd.common.enums.HeatModuleCode;
|
|
import com.iflytop.gd.hardware.comm.can.A8kCanBusService;
|
|
import com.iflytop.gd.hardware.constants.ActionOvertimeConstant;
|
|
import com.iflytop.gd.hardware.drivers.DODriver.OutputIOCtrlDriver;
|
|
import com.iflytop.gd.hardware.drivers.LeisaiServoDriver;
|
|
import com.iflytop.gd.hardware.drivers.MiniServoDriver.MiniServoDriver;
|
|
import com.iflytop.gd.hardware.drivers.StepMotorDriver.StepMotorCtrlDriver;
|
|
import com.iflytop.gd.hardware.drivers.TricolorLightDriver;
|
|
import com.iflytop.gd.hardware.exception.HardwareException;
|
|
import com.iflytop.gd.hardware.type.CmdId;
|
|
import com.iflytop.gd.hardware.drivers.LiquidDistributionArmDriver;
|
|
import com.iflytop.gd.hardware.type.IO.OutputIOMId;
|
|
import com.iflytop.gd.hardware.type.MId;
|
|
import com.iflytop.gd.hardware.type.RegIndex;
|
|
import com.iflytop.gd.hardware.type.Servo.LeisaiServoMId;
|
|
import com.iflytop.gd.hardware.type.Servo.LiquidArmMId;
|
|
import com.iflytop.gd.hardware.type.Servo.MiniServoMId;
|
|
import com.iflytop.gd.hardware.type.StepMotor.StepMotorMId;
|
|
import com.opencsv.CSVReader;
|
|
import jakarta.annotation.PostConstruct;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.core.io.ClassPathResource;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.io.InputStreamReader;
|
|
import java.util.List;
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
@Slf4j
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class DeviceInitService {
|
|
private final ContainerService containerService;
|
|
private final DeviceStateService deviceStateService;
|
|
private final A8kCanBusService canBusService;
|
|
private final StepMotorCtrlDriver stepMotorCtrlDriver;
|
|
private final MiniServoDriver miniServoDriver;
|
|
private final LiquidDistributionArmDriver liquidDistributionArmDriver;
|
|
private final TricolorLightDriver tricolorLightDriver;
|
|
private final LeisaiServoDriver leisaiServoDriver;
|
|
private final ActionOvertimeConstant actionOvertimeConstant;
|
|
private final OutputIOCtrlDriver outputIOCtrlDriver;
|
|
private final DeviceParamConfigService deviceParamConfigService;
|
|
|
|
@PostConstruct
|
|
public void init() {
|
|
new Thread(() -> {
|
|
try {
|
|
Thread.sleep(2000);
|
|
CompletableFuture.runAsync(() -> {
|
|
try {
|
|
tricolorLightDriver.open(MId.TriColorLight, TricolorLightDriver.Color.BLUE);
|
|
} catch (HardwareException e) {
|
|
log.error("设备初始化灯光失败,Color.BLUE");
|
|
}
|
|
});
|
|
initDeviceState();
|
|
initDeviceSetData();
|
|
initOvertime();
|
|
initEnable();
|
|
try {
|
|
tricolorLightDriver.open(MId.TriColorLight, TricolorLightDriver.Color.GREEN);
|
|
} catch (HardwareException e) {
|
|
log.error("设备初始化灯光失败,Color.GREEN");
|
|
}
|
|
deviceStateService.setInitComplete(true);
|
|
} catch (Exception e) {
|
|
try {
|
|
tricolorLightDriver.open(MId.TriColorLight, TricolorLightDriver.Color.RED);
|
|
} catch (Exception ignored) {
|
|
}
|
|
log.error("设备初始化失败", e);
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
/**
|
|
* 设置各动作超时时间
|
|
*/
|
|
public void initOvertime() {
|
|
actionOvertimeConstant.pushNewConfig(StepMotorMId.DOOR_MOTOR_MID, CmdId.step_motor_easy_move_to_zero, 90 * 1000);
|
|
actionOvertimeConstant.pushNewConfig(LeisaiServoMId.MainXSV, CmdId.step_motor_easy_move_to_zero, 300 * 1000);
|
|
actionOvertimeConstant.pushNewConfig(LeisaiServoMId.MainYSV, CmdId.step_motor_easy_move_to_zero, 120 * 1000);
|
|
actionOvertimeConstant.pushNewConfig(StepMotorMId.HBOT_Z_MOTOR_MID, CmdId.step_motor_easy_move_to_zero, 30 * 1000);
|
|
actionOvertimeConstant.pushNewConfig(StepMotorMId.HEATER_1_MOTOR_MID, CmdId.step_motor_easy_move_to_zero, 60 * 1000);
|
|
actionOvertimeConstant.pushNewConfig(StepMotorMId.HEATER_2_MOTOR_MID, CmdId.step_motor_easy_move_to_zero, 60 * 1000);
|
|
actionOvertimeConstant.pushNewConfig(StepMotorMId.HEATER_3_MOTOR_MID, CmdId.step_motor_easy_move_to_zero, 60 * 1000);
|
|
actionOvertimeConstant.pushNewConfig(StepMotorMId.HEATER_4_MOTOR_MID, CmdId.step_motor_easy_move_to_zero, 60 * 1000);
|
|
actionOvertimeConstant.pushNewConfig(StepMotorMId.HEATER_5_MOTOR_MID, CmdId.step_motor_easy_move_to_zero, 60 * 1000);
|
|
actionOvertimeConstant.pushNewConfig(StepMotorMId.HEATER_6_MOTOR_MID, CmdId.step_motor_easy_move_to_zero, 60 * 1000);
|
|
actionOvertimeConstant.pushNewConfig(StepMotorMId.TRAY_MOTOR_MID, CmdId.step_motor_easy_move_to_zero, 300 * 1000);
|
|
actionOvertimeConstant.pushNewConfig(LiquidArmMId.LiquidDistributionArm, CmdId.liquid_distribution_arm_move_to, 6 * 1000);
|
|
}
|
|
|
|
/**
|
|
* 初始化所有设备使能
|
|
*/
|
|
public void initEnable() throws Exception {
|
|
stepMotorCtrlDriver.stepMotorEnable(StepMotorMId.DOOR_MOTOR_MID, 1);
|
|
stepMotorCtrlDriver.stepMotorEnable(StepMotorMId.SHAKE_MOTOR_MID, 1);
|
|
stepMotorCtrlDriver.stepMotorEnable(StepMotorMId.TRAY_MOTOR_MID, 1);
|
|
stepMotorCtrlDriver.stepMotorEnable(StepMotorMId.HBOT_Z_MOTOR_MID, 1);
|
|
stepMotorCtrlDriver.stepMotorEnable(StepMotorMId.HEATER_1_MOTOR_MID, 1);
|
|
stepMotorCtrlDriver.stepMotorEnable(StepMotorMId.HEATER_2_MOTOR_MID, 1);
|
|
stepMotorCtrlDriver.stepMotorEnable(StepMotorMId.HEATER_3_MOTOR_MID, 1);
|
|
stepMotorCtrlDriver.stepMotorEnable(StepMotorMId.HEATER_4_MOTOR_MID, 1);
|
|
stepMotorCtrlDriver.stepMotorEnable(StepMotorMId.HEATER_5_MOTOR_MID, 1);
|
|
stepMotorCtrlDriver.stepMotorEnable(StepMotorMId.HEATER_6_MOTOR_MID, 1);
|
|
|
|
liquidDistributionArmDriver.liquidDistributionArmEnable(LiquidArmMId.LiquidDistributionArm, 1);
|
|
miniServoDriver.miniServoForceEnable(MiniServoMId.CLAW_MID, 1);
|
|
leisaiServoDriver.enable(LeisaiServoMId.MainXSV, 1);
|
|
leisaiServoDriver.enable(LeisaiServoMId.MainYSV, 1);
|
|
|
|
outputIOCtrlDriver.open(OutputIOMId.DO_HBOTZ_MOTOR_CLAMP);
|
|
outputIOCtrlDriver.open(OutputIOMId.DO_TRAY_MOTOR_CLAMP);
|
|
}
|
|
|
|
public void initDeviceSetData() throws Exception {
|
|
if (deviceStateService.getDeviceState().isVirtual() || deviceStateService.getDeviceState().isInitComplete()) {
|
|
return;
|
|
}
|
|
//从数据库中读取数据通过串口进行数据初始化
|
|
List<DeviceParamConfig> deviceParamConfigs = deviceParamConfigService.list();
|
|
for (DeviceParamConfig deviceParamConfig : deviceParamConfigs) {
|
|
DeviceInitializationData data = new DeviceInitializationData();
|
|
data.setId(Math.toIntExact(deviceParamConfig.getId()));
|
|
data.setMid(deviceParamConfig.getMid());
|
|
data.setRegIndex(deviceParamConfig.getRegIndex());
|
|
data.setRegInitVal(deviceParamConfig.getRegVal());
|
|
try {
|
|
sendToDevice(data);
|
|
} catch (Exception e) {
|
|
log.error("设备初始化写入参数失败,2秒后重试", e);
|
|
Thread.sleep(2000);
|
|
initDeviceSetData();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public void sendToDevice(DeviceInitializationData data) throws Exception {
|
|
canBusService.moduleSetReg(MId.valueOf(data.getMid()), RegIndex.valueOf(data.getRegIndex()), data.getRegInitVal());
|
|
}
|
|
|
|
public void initDeviceState() {
|
|
DeviceState deviceState = deviceStateService.getDeviceState();
|
|
List<HeatModuleState> heatArea = deviceState.getHeatModule();
|
|
for (HeatModuleCode code : HeatModuleCode.values()) {
|
|
heatArea.add(new HeatModuleState(code));
|
|
}
|
|
|
|
List<Container> containerList = containerService.getList();
|
|
List<SolutionContainerState> solutionBucket = deviceState.getSolutionModule().getSolutionContainer();
|
|
for (Container container : containerList) {
|
|
if (container.getType() == 0) {
|
|
solutionBucket.add(new SolutionContainerState(container.getId(), ContainerCode.valueOf(container.getCode()), ContainerType.solution));
|
|
} else {
|
|
solutionBucket.add(new SolutionContainerState(container.getId(), ContainerCode.valueOf(container.getCode()), ContainerType.neutralization));
|
|
}
|
|
}
|
|
}
|
|
}
|