|
|
@ -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<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); |
|
|
@ -82,6 +94,11 @@ public class ChannelCtrlService { |
|
|
|
} else if (SolutionAddMode.CLICK.equals(deviceState.getMode())) {//点动模式 |
|
|
|
List<CommandFuture> 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<Formulation>().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<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) { |
|
|
|
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); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |