Browse Source

feat:计算实际出液量

master
白凤吉 2 days ago
parent
commit
c99f904af4
  1. 74
      src/main/java/com/iflytop/handacid/app/service/ChannelCtrlService.java
  2. 4
      src/main/resources/application-dev.yml
  3. 4
      src/main/resources/application-prod.yml
  4. 4
      src/main/resources/application-test.yml

74
src/main/java/com/iflytop/handacid/app/service/ChannelCtrlService.java

@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.iflytop.handacid.app.common.enums.ChannelCode; import com.iflytop.handacid.app.common.enums.ChannelCode;
import com.iflytop.handacid.app.common.enums.ChannelStateCode; import com.iflytop.handacid.app.common.enums.ChannelStateCode;
import com.iflytop.handacid.app.common.enums.SolutionAddMode; import com.iflytop.handacid.app.common.enums.SolutionAddMode;
import com.iflytop.handacid.app.common.enums.SystemConfigKey;
import com.iflytop.handacid.app.common.utils.CommandUtil; import com.iflytop.handacid.app.common.utils.CommandUtil;
import com.iflytop.handacid.app.core.command.CommandFuture; import com.iflytop.handacid.app.core.command.CommandFuture;
import com.iflytop.handacid.app.core.command.DeviceCommand; import com.iflytop.handacid.app.core.command.DeviceCommand;
@ -14,6 +15,7 @@ import com.iflytop.handacid.common.model.entity.AuditRecord;
import com.iflytop.handacid.common.model.entity.Formulation; import com.iflytop.handacid.common.model.entity.Formulation;
import com.iflytop.handacid.common.service.AuditRecordService; import com.iflytop.handacid.common.service.AuditRecordService;
import com.iflytop.handacid.common.service.FormulationService; import com.iflytop.handacid.common.service.FormulationService;
import com.iflytop.handacid.common.service.SystemConfigService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -33,6 +35,7 @@ public class ChannelCtrlService {
private final DeviceCommandService deviceCommandService; private final DeviceCommandService deviceCommandService;
private final DeviceState deviceState; private final DeviceState deviceState;
private final AuditRecordService auditRecordService; private final AuditRecordService auditRecordService;
private final SystemConfigService systemConfigService;
/** /**
* 开始加液 * 开始加液
@ -63,9 +66,7 @@ public class ChannelCtrlService {
.eq(Formulation::getConcentration, channelState.getConcentration()).eq(Formulation::getVolume, channelState.getTargetVolume()).last("limit 1")); .eq(Formulation::getConcentration, channelState.getConcentration()).eq(Formulation::getVolume, channelState.getTargetVolume()).last("limit 1"));
DeviceCommand deviceCommand = getPumpMoveByCommandByChannel(channelState.getChannelCode(), formulation.getRevolutions()); DeviceCommand deviceCommand = getPumpMoveByCommandByChannel(channelState.getChannelCode(), formulation.getRevolutions());
commandFutureList.add(deviceCommandService.sendCommand(deviceCommand)); commandFutureList.add(deviceCommandService.sendCommand(deviceCommand));
AuditRecord auditRecord = new AuditRecord(deviceState.getCurrentUser().getId(), deviceState.getCurrentUser().getNickname(), channelState.getSolutionId(),
channelState.getSolutionName(), formulation.getConcentration(), channelState.getChannelCode().name(), channelState.getTargetVolume());
auditRecordService.saveOrUpdate(auditRecord);
} }
CommandUtil.wait(commandFutureList); CommandUtil.wait(commandFutureList);
@ -74,7 +75,18 @@ public class ChannelCtrlService {
CommandFuture currentPositionCommandFuture = deviceCommandService.sendCommand(currentPositionDeviceCommand); CommandFuture currentPositionCommandFuture = deviceCommandService.sendCommand(currentPositionDeviceCommand);
CommandUtil.wait(currentPositionCommandFuture); CommandUtil.wait(currentPositionCommandFuture);
Double currentPosition = currentPositionCommandFuture.getResponseResult().getJSONObject("data").getDouble("position"); Double currentPosition = currentPositionCommandFuture.getResponseResult().getJSONObject("data").getDouble("position");
Formulation formulation = formulationService.getOne(new LambdaQueryWrapper<Formulation>().eq(Formulation::getSolutionId, channelState.getSolutionId())
.eq(Formulation::getConcentration, channelState.getConcentration()).eq(Formulation::getVolume, channelState.getTargetVolume()).last("limit 1"));
Double dispensedVolume = calculateActualSolutionDispensedVolume(channelState.getPumpPositionCache(), currentPosition, formulation);
log.info("实际加液量:{}", dispensedVolume);
double currentVolume = channelState.getCurrentVolume() - dispensedVolume;
if (currentVolume < 0) {
currentVolume = 0;
}
channelState.setCurrentVolume(currentVolume);
AuditRecord auditRecord = new AuditRecord(deviceState.getCurrentUser().getId(), deviceState.getCurrentUser().getNickname(), channelState.getSolutionId(),
channelState.getSolutionName(), formulation.getConcentration(), channelState.getChannelCode().name(), dispensedVolume);
auditRecordService.saveOrUpdate(auditRecord);
} }
Thread.sleep(deviceState.getDelay() * 1000L); Thread.sleep(deviceState.getDelay() * 1000L);
@ -82,6 +94,11 @@ public class ChannelCtrlService {
} else if (SolutionAddMode.CLICK.equals(deviceState.getMode())) {//点动模式 } else if (SolutionAddMode.CLICK.equals(deviceState.getMode())) {//点动模式
List<CommandFuture> commandFutureList = new ArrayList<>(); List<CommandFuture> commandFutureList = new ArrayList<>();
for (ChannelState channelState : channelStateList) { for (ChannelState channelState : channelStateList) {
//打开阀门
DeviceCommand valveOpenDeviceCommand = getValveOpenCommandByChannel(channelState.getChannelCode());
CommandFuture valveOpenCommandFuture = deviceCommandService.sendCommand(valveOpenDeviceCommand);
CommandUtil.wait(valveOpenCommandFuture);
Formulation formulation = formulationService.getOne(new LambdaQueryWrapper<Formulation>().eq(Formulation::getSolutionId, channelState.getSolutionId()) Formulation formulation = formulationService.getOne(new LambdaQueryWrapper<Formulation>().eq(Formulation::getSolutionId, channelState.getSolutionId())
.eq(Formulation::getConcentration, channelState.getConcentration()).eq(Formulation::getVolume, channelState.getTargetVolume()).last("limit 1")); .eq(Formulation::getConcentration, channelState.getConcentration()).eq(Formulation::getVolume, channelState.getTargetVolume()).last("limit 1"));
DeviceCommand deviceCommand = getPumpMoveByCommandByChannel(channelState.getChannelCode(), formulation.getRevolutions()); DeviceCommand deviceCommand = getPumpMoveByCommandByChannel(channelState.getChannelCode(), formulation.getRevolutions());
@ -91,6 +108,25 @@ public class ChannelCtrlService {
auditRecordService.saveOrUpdate(auditRecord); auditRecordService.saveOrUpdate(auditRecord);
} }
CommandUtil.wait(commandFutureList); CommandUtil.wait(commandFutureList);
for (ChannelState channelState : channelStateList) {//与缓存的位置比较计算加液量
DeviceCommand currentPositionDeviceCommand = getPumpPositionCommandByChannel(channelState.getChannelCode());
CommandFuture currentPositionCommandFuture = deviceCommandService.sendCommand(currentPositionDeviceCommand);
CommandUtil.wait(currentPositionCommandFuture);
Double currentPosition = currentPositionCommandFuture.getResponseResult().getJSONObject("data").getDouble("position");
Formulation formulation = formulationService.getOne(new LambdaQueryWrapper<Formulation>().eq(Formulation::getSolutionId, channelState.getSolutionId())
.eq(Formulation::getConcentration, channelState.getConcentration()).eq(Formulation::getVolume, channelState.getTargetVolume()).last("limit 1"));
Double dispensedVolume = calculateActualSolutionDispensedVolume(channelState.getPumpPositionCache(), currentPosition, formulation);
log.info("实际加液量:{}", dispensedVolume);
double currentVolume = channelState.getCurrentVolume() - dispensedVolume;
if (currentVolume < 0) {
currentVolume = 0;
}
channelState.setCurrentVolume(currentVolume);
AuditRecord auditRecord = new AuditRecord(deviceState.getCurrentUser().getId(), deviceState.getCurrentUser().getNickname(), channelState.getSolutionId(),
channelState.getSolutionName(), formulation.getConcentration(), channelState.getChannelCode().name(), dispensedVolume);
auditRecordService.saveOrUpdate(auditRecord);
}
} }
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -283,5 +319,35 @@ public class ChannelCtrlService {
}; };
} }
/**
* 计算实际出液量
*
* @param beforePosition 加液前旋转圈数
* @param afterPosition 加液后旋转圈数
* @param formulation 配方对象包含指定加液量及其对应的旋转圈数
* - volume 目标加液量
* - revolutions 目标加液对应的旋转圈数
* @return double 实际出液量
*/
public double calculateActualSolutionDispensedVolume(Double beforePosition, Double afterPosition, Formulation formulation) {
if (beforePosition == null || afterPosition == null) {
throw new IllegalArgumentException("加液前/后位置参数不能为空");
}
if (formulation == null
|| formulation.getVolume() == null
|| formulation.getRevolutions() == null) {
throw new IllegalArgumentException("Formulation 或其字段 volume/revolutions 不能为空");
}
Integer pumpConversionFactor = systemConfigService.getValueByKeyToInteger(SystemConfigKey.PUMP_CONVERSION_FACTOR);
double deltaRevolutions = afterPosition - beforePosition;
double targetVolume = formulation.getVolume() / pumpConversionFactor;
double targetRevs = formulation.getRevolutions() / pumpConversionFactor;
if (targetRevs == 0) {
throw new IllegalArgumentException("Formulation.revolutions 不能为 0");
}
// 实际出液量 = 差值圈数 * (目标加液量 ÷ 目标圈数)
return deltaRevolutions * (targetVolume / targetRevs);
}
} }

4
src/main/resources/application-dev.yml

@ -27,7 +27,3 @@ iflytophald:
modbus: modbus:
port: ttyS1 port: ttyS1
baudrate: 9600 baudrate: 9600
photo:
url: http://192.168.8.168/static/photo
path: /home/firefly/package/photo

4
src/main/resources/application-prod.yml

@ -19,7 +19,3 @@ iflytophald:
modbus: modbus:
port: ttyS1 port: ttyS1
baudrate: 9600 baudrate: 9600
photo:
url: http://192.168.8.168/static/photo
path: /home/firefly/package/photo

4
src/main/resources/application-test.yml

@ -25,7 +25,3 @@ iflytophald:
modbus: modbus:
port: ttyS1 port: ttyS1
baudrate: 9600 baudrate: 9600
photo:
url: http://192.168.8.168/static/photo
path: /home/firefly/package/photo
Loading…
Cancel
Save