From 4f8f0169d234298cf1389c31169c65717f4b6f60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E5=87=A4=E5=90=89?= Date: Fri, 1 Aug 2025 16:20:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=B0=83=E6=95=B4=E5=8A=A0=E6=B6=B2?= =?UTF-8?q?=E9=87=8F=E8=AE=A1=E7=AE=97=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handacid/app/service/ChannelCtrlService.java | 156 ++++++++++----------- 1 file changed, 72 insertions(+), 84 deletions(-) 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 3b709a9..a321614 100644 --- a/src/main/java/com/iflytop/handacid/app/service/ChannelCtrlService.java +++ b/src/main/java/com/iflytop/handacid/app/service/ChannelCtrlService.java @@ -22,6 +22,8 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; @@ -44,101 +46,31 @@ public class ChannelCtrlService { * 开始加液 */ public void solutionAddStart() { - if(deviceState.isSolutionAdding()){ + if (deviceState.isSolutionAdding()) { return; } CompletableFuture.runAsync(() -> { List channelStateList = deviceState.filterChannelStatesIsSelected(); try { + List valveOpenDeviceCommandFutureList = new ArrayList<>(); for (ChannelState channelState : channelStateList) { channelState.setStateCode(ChannelStateCode.ADD); + + DeviceCommand valveOpenDeviceCommand = getValveOpenCommandByChannel(channelState.getChannelCode()); + valveOpenDeviceCommandFutureList.add(deviceCommandService.sendCommand(valveOpenDeviceCommand)); } + CommandUtil.wait(valveOpenDeviceCommandFutureList); if (SolutionAddMode.AUTO.equals(deviceState.getMode())) {//自动模式 while (!deviceState.isSolutionAddStop()) { - List commandFutureList = new ArrayList<>(); - for (ChannelState channelState : channelStateList) { - //打开阀门 - DeviceCommand valveOpenDeviceCommand = getValveOpenCommandByChannel(channelState.getChannelCode()); - CommandFuture valveOpenCommandFuture = deviceCommandService.sendCommand(valveOpenDeviceCommand); - CommandUtil.wait(valveOpenCommandFuture); - - DeviceCommand currentPositionDeviceCommand = getPumpPositionCommandByChannel(channelState.getChannelCode()); - CommandFuture currentPositionCommandFuture = deviceCommandService.sendCommand(currentPositionDeviceCommand); - CommandUtil.wait(currentPositionCommandFuture); - Double currentPosition = currentPositionCommandFuture.getResponseResult().getJSONObject("data").getDouble("position"); - channelState.setPumpPositionCache(currentPosition); - - 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()); - commandFutureList.add(deviceCommandService.sendCommand(deviceCommand)); - - } - 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); - Channel channel = channelService.getOne(new LambdaQueryWrapper<>(new Channel()).eq(Channel::getCode, channelState.getChannelCode())); - channel.setCurrentVolume(currentVolume); - channelService.updateById(channel); - AuditRecord auditRecord = new AuditRecord(deviceState.getCurrentUser().getId(), deviceState.getCurrentUser().getNickname(), channelState.getSolutionId(), - channelState.getSolutionName(), formulation.getConcentration(), channelState.getChannelCode().name(), dispensedVolume); - auditRecordService.saveOrUpdate(auditRecord); + handleSolutionAdd(channelStateList); + handleCalculateActualDispensedVolume(channelStateList); + if (!deviceState.isSolutionAddStop()) { + Thread.sleep(deviceState.getDelay() * 1000L); } - - Thread.sleep(deviceState.getDelay() * 1000L); } } 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()); - 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); - - 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); - Channel channel = channelService.getOne(new LambdaQueryWrapper<>(new Channel()).eq(Channel::getCode, channelState.getChannelCode())); - channel.setCurrentVolume(currentVolume); - channelService.updateById(channel); - AuditRecord auditRecord = new AuditRecord(deviceState.getCurrentUser().getId(), deviceState.getCurrentUser().getNickname(), channelState.getSolutionId(), - channelState.getSolutionName(), formulation.getConcentration(), channelState.getChannelCode().name(), dispensedVolume); - auditRecordService.saveOrUpdate(auditRecord); - } + handleSolutionAdd(channelStateList); + handleCalculateActualDispensedVolume(channelStateList); } } catch (Exception e) { throw new RuntimeException(e); @@ -331,6 +263,62 @@ public class ChannelCtrlService { }; } + + //=================================================私有================================================================================== + + /** + * 进行加液 + */ + private void handleSolutionAdd(List channelStateList) throws Exception { + List pumpMoveByCommandFutureList = new ArrayList<>(); + for (ChannelState channelState : channelStateList) { + pumpPositionCache(channelState); + 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()); + pumpMoveByCommandFutureList.add(deviceCommandService.sendCommand(deviceCommand)); + } + CommandUtil.wait(pumpMoveByCommandFutureList); + } + + /** + * 缓存当前位置 + */ + private void pumpPositionCache(ChannelState channelState) throws Exception { + DeviceCommand currentPositionDeviceCommand = getPumpPositionCommandByChannel(channelState.getChannelCode()); + CommandFuture currentPositionCommandFuture = deviceCommandService.sendCommand(currentPositionDeviceCommand); + CommandUtil.wait(currentPositionCommandFuture); + Double currentPosition = currentPositionCommandFuture.getResponseResult().getJSONObject("data").getDouble("position"); + channelState.setPumpPositionCache(currentPosition); + } + + /** + * 处理实际加液计算 + */ + private void handleCalculateActualDispensedVolume(List channelStateList) throws Exception { + 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); + double currentVolume = channelState.getCurrentVolume() - dispensedVolume; + BigDecimal bd = BigDecimal.valueOf(currentVolume); + double roundedCurrentVolume = bd.setScale(2, RoundingMode.HALF_UP).doubleValue(); + log.info("旧位置与新位置:{},{}", channelState.getPumpPositionCache(), currentPosition); + log.info("实际加液量:{}", dispensedVolume); + channelState.setCurrentVolume(roundedCurrentVolume); + Channel channel = channelService.getOne(new LambdaQueryWrapper<>(new Channel()).eq(Channel::getCode, channelState.getChannelCode())); + channel.setCurrentVolume(roundedCurrentVolume); + channelService.updateById(channel); + AuditRecord auditRecord = new AuditRecord(deviceState.getCurrentUser().getId(), deviceState.getCurrentUser().getNickname(), channelState.getSolutionId(), + channelState.getSolutionName(), formulation.getConcentration(), channelState.getChannelCode().name(), roundedCurrentVolume); + auditRecordService.saveOrUpdate(auditRecord); + } + } + /** * 计算实际出液量 * @@ -341,7 +329,7 @@ public class ChannelCtrlService { * - revolutions 目标加液对应的旋转圈数 * @return double 实际出液量 */ - public double calculateActualSolutionDispensedVolume(Double beforePosition, Double afterPosition, Formulation formulation) { + private double calculateActualSolutionDispensedVolume(Double beforePosition, Double afterPosition, Formulation formulation) { if (beforePosition == null || afterPosition == null) { throw new IllegalArgumentException("加液前/后位置参数不能为空"); } @@ -351,7 +339,7 @@ public class ChannelCtrlService { throw new IllegalArgumentException("Formulation 或其字段 volume/revolutions 不能为空"); } Integer pumpConversionFactor = systemConfigService.getValueByKeyToInteger(SystemConfigKey.PUMP_CONVERSION_FACTOR); - int deltaRevolutions = (int) (((afterPosition - beforePosition) / pumpConversionFactor)); + double deltaRevolutions = (((afterPosition - beforePosition) / pumpConversionFactor)); double targetVolume = formulation.getVolume(); double targetRevs = formulation.getRevolutions(); if (targetRevs == 0) {