diff --git a/src/main/java/com/iflytop/handacid/app/service/ChannelCtrlService.java b/src/main/java/com/iflytop/handacid/app/service/ChannelCtrlService.java index c1f6e3b..6e0a5b3 100644 --- a/src/main/java/com/iflytop/handacid/app/service/ChannelCtrlService.java +++ b/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.ChannelStateCode; 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.core.command.CommandFuture; 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.service.AuditRecordService; import com.iflytop.handacid.common.service.FormulationService; +import com.iflytop.handacid.common.service.SystemConfigService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -33,6 +35,7 @@ public class ChannelCtrlService { private final DeviceCommandService deviceCommandService; private final DeviceState deviceState; 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")); DeviceCommand deviceCommand = getPumpMoveByCommandByChannel(channelState.getChannelCode(), formulation.getRevolutions()); 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); @@ -74,7 +75,18 @@ public class ChannelCtrlService { CommandFuture currentPositionCommandFuture = deviceCommandService.sendCommand(currentPositionDeviceCommand); CommandUtil.wait(currentPositionCommandFuture); Double currentPosition = currentPositionCommandFuture.getResponseResult().getJSONObject("data").getDouble("position"); - + Formulation formulation = formulationService.getOne(new LambdaQueryWrapper().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); @@ -82,6 +94,11 @@ public class ChannelCtrlService { } else if (SolutionAddMode.CLICK.equals(deviceState.getMode())) {//点动模式 List commandFutureList = new ArrayList<>(); for (ChannelState channelState : channelStateList) { + //打开阀门 + DeviceCommand valveOpenDeviceCommand = getValveOpenCommandByChannel(channelState.getChannelCode()); + CommandFuture valveOpenCommandFuture = deviceCommandService.sendCommand(valveOpenDeviceCommand); + CommandUtil.wait(valveOpenCommandFuture); + Formulation formulation = formulationService.getOne(new LambdaQueryWrapper().eq(Formulation::getSolutionId, channelState.getSolutionId()) .eq(Formulation::getConcentration, channelState.getConcentration()).eq(Formulation::getVolume, channelState.getTargetVolume()).last("limit 1")); DeviceCommand deviceCommand = getPumpMoveByCommandByChannel(channelState.getChannelCode(), formulation.getRevolutions()); @@ -91,6 +108,25 @@ public class ChannelCtrlService { auditRecordService.saveOrUpdate(auditRecord); } 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().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) { 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); + } + } diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index c6fc5f3..74b3e29 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -27,7 +27,3 @@ iflytophald: modbus: port: ttyS1 baudrate: 9600 - -photo: - url: http://192.168.8.168/static/photo - path: /home/firefly/package/photo \ No newline at end of file diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index c04b527..3e5c70f 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -19,7 +19,3 @@ iflytophald: modbus: port: ttyS1 baudrate: 9600 - -photo: - url: http://192.168.8.168/static/photo - path: /home/firefly/package/photo \ No newline at end of file diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml index 75f28ab..be6bc21 100644 --- a/src/main/resources/application-test.yml +++ b/src/main/resources/application-test.yml @@ -25,7 +25,3 @@ iflytophald: modbus: port: ttyS1 baudrate: 9600 - -photo: - url: http://192.168.8.168/static/photo - path: /home/firefly/package/photo \ No newline at end of file