|
|
@ -1,8 +1,20 @@ |
|
|
|
package com.iflytop.sgs.app.service.device; |
|
|
|
|
|
|
|
import com.iflytop.sgs.app.model.bo.DeviceInitializationData; |
|
|
|
import com.iflytop.sgs.app.model.bo.status.device.DeviceState; |
|
|
|
import com.iflytop.sgs.app.model.bo.status.device.HeatModuleState; |
|
|
|
import com.iflytop.sgs.app.model.bo.status.device.SolutionContainerState; |
|
|
|
import com.iflytop.sgs.app.model.entity.Container; |
|
|
|
import com.iflytop.sgs.app.model.entity.DeviceParamConfig; |
|
|
|
import com.iflytop.sgs.app.service.api.ContainerService; |
|
|
|
import com.iflytop.sgs.app.service.api.DeviceParamConfigService; |
|
|
|
import com.iflytop.sgs.common.cmd.DeviceCommandBundle; |
|
|
|
import com.iflytop.sgs.common.cmd.DeviceCommandGenerator; |
|
|
|
import com.iflytop.sgs.common.enums.ContainerCode; |
|
|
|
import com.iflytop.sgs.common.enums.ContainerType; |
|
|
|
import com.iflytop.sgs.common.enums.HeatModuleCode; |
|
|
|
import com.iflytop.sgs.common.enums.cmd.CmdColor; |
|
|
|
import com.iflytop.sgs.common.service.CanBusService; |
|
|
|
import jakarta.annotation.PostConstruct; |
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
@ -10,6 +22,7 @@ import org.springframework.beans.factory.ObjectProvider; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.concurrent.CompletableFuture; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@Service |
|
|
@ -17,14 +30,24 @@ import java.util.List; |
|
|
|
public class DeviceInitService { |
|
|
|
private final DeviceStateService deviceStateService; |
|
|
|
private final ObjectProvider<HeatModuleState> heatModuleStateProvider; |
|
|
|
private final ContainerService containerService; |
|
|
|
private final DeviceParamConfigService deviceParamConfigService; |
|
|
|
private final CanBusService canBusService; |
|
|
|
private final DeviceCommandService deviceCommandService; |
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
public void init() { |
|
|
|
new Thread(() -> { |
|
|
|
try { |
|
|
|
Thread.sleep(2000); |
|
|
|
CompletableFuture.runAsync(() -> { |
|
|
|
DeviceCommandBundle deviceCommandBundle = DeviceCommandGenerator.tricolorLightOpen(CmdColor.blue); |
|
|
|
deviceCommandService.sendCommand(deviceCommandBundle); |
|
|
|
}); |
|
|
|
initDeviceState(); |
|
|
|
initDeviceSetData(); |
|
|
|
canBusService.initOvertime(); |
|
|
|
initEnable(); |
|
|
|
deviceStateService.getDeviceState().setInitComplete(true); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("设备初始化失败", e); |
|
|
@ -34,10 +57,50 @@ public class DeviceInitService { |
|
|
|
|
|
|
|
|
|
|
|
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 { |
|
|
|
canBusService.moduleSetRegByApp(data.getMid(), data.getRegIndex(), data.getRegInitVal()); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("设备初始化写入参数失败,2秒后重试", e); |
|
|
|
Thread.sleep(2000); |
|
|
|
initDeviceSetData(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 初始化所有设备使能 |
|
|
|
*/ |
|
|
|
public void initEnable() throws Exception { |
|
|
|
//门电机 |
|
|
|
DeviceCommandBundle doorEnableDeviceCommandBundle = DeviceCommandGenerator.doorEnable(); |
|
|
|
deviceCommandService.sendCommand(doorEnableDeviceCommandBundle); |
|
|
|
//x轴 |
|
|
|
DeviceCommandBundle xEnableDeviceCommandBundle = DeviceCommandGenerator.transferXEnable(); |
|
|
|
deviceCommandService.sendCommand(xEnableDeviceCommandBundle); |
|
|
|
//z轴 |
|
|
|
DeviceCommandBundle yEnableDeviceCommandBundle = DeviceCommandGenerator.transferZEnable(); |
|
|
|
deviceCommandService.sendCommand(yEnableDeviceCommandBundle); |
|
|
|
//机械臂 |
|
|
|
DeviceCommandBundle liquidMotorEnableDeviceCommandBundle = DeviceCommandGenerator.liquidMotorEnable(); |
|
|
|
deviceCommandService.sendCommand(liquidMotorEnableDeviceCommandBundle); |
|
|
|
//蠕动泵 |
|
|
|
DeviceCommandBundle liquidPumpEnableDeviceCommandBundle = DeviceCommandGenerator.liquidPumpEnable(); |
|
|
|
deviceCommandService.sendCommand(liquidPumpEnableDeviceCommandBundle); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void initDeviceState() { |
|
|
|
//初始化加热模块属性 |
|
|
|
DeviceState deviceState = deviceStateService.getDeviceState(); |
|
|
@ -45,5 +108,15 @@ public class DeviceInitService { |
|
|
|
for (HeatModuleCode code : HeatModuleCode.values()) { |
|
|
|
heatArea.add(heatModuleStateProvider.getObject(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.waste)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |