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.
92 lines
3.6 KiB
92 lines
3.6 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.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.exception.HardwareException;
|
|
import com.iflytop.gd.hardware.type.MId;
|
|
import com.iflytop.gd.hardware.type.RegIndex;
|
|
import com.opencsv.CSVReader;
|
|
import jakarta.annotation.PostConstruct;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.io.FileReader;
|
|
import java.nio.file.Paths;
|
|
import java.util.List;
|
|
|
|
@Slf4j
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class DeviceInitService {
|
|
private final ContainerService containerService;
|
|
private final DeviceStateService deviceStateService;
|
|
private final A8kCanBusService canBusService;
|
|
|
|
@PostConstruct
|
|
public void init() {
|
|
new Thread(() -> {
|
|
try {
|
|
initDeviceState();
|
|
initDeviceSetData();
|
|
|
|
deviceStateService.setInitComplete(true);
|
|
} catch (Exception ignored) {
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
public void initDeviceSetData() {
|
|
if (deviceStateService.getDeviceState().isVirtual() || deviceStateService.getDeviceState().isInitComplete()) {
|
|
return;
|
|
}
|
|
try {
|
|
Thread.sleep(2000);
|
|
String filePath = Paths.get("src", "main", "resources", "init", "zapp_sub_module_reg_initial_value.csv").toString();
|
|
CSVReader reader = new CSVReader(new FileReader(filePath));
|
|
reader.readNext();
|
|
String[] line;
|
|
while ((line = reader.readNext()) != null) {
|
|
DeviceInitializationData data = new DeviceInitializationData();
|
|
data.setId(Integer.parseInt(line[0]));
|
|
data.setMid(line[1]);
|
|
data.setRegIndex(line[2]);
|
|
data.setRegInitVal(Integer.parseInt(line[3]));
|
|
sendToDevice(data);
|
|
}
|
|
reader.close();
|
|
} catch (Exception e) {
|
|
log.error("设备初始化失败", e);
|
|
initDeviceSetData();
|
|
}
|
|
|
|
}
|
|
|
|
public void sendToDevice(DeviceInitializationData data) throws HardwareException {
|
|
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));
|
|
}
|
|
}
|
|
}
|
|
}
|