加酸仪(java版本)
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.

206 lines
8.8 KiB

package com.iflytop.handacid.app.service;
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.utils.CommandUtil;
import com.iflytop.handacid.app.core.command.CommandFuture;
import com.iflytop.handacid.app.core.command.DeviceCommand;
import com.iflytop.handacid.app.core.command.DeviceCommandGenerator;
import com.iflytop.handacid.app.core.state.ChannelState;
import com.iflytop.handacid.app.core.state.DeviceState;
import com.iflytop.handacid.common.model.entity.Formulation;
import com.iflytop.handacid.common.service.FormulationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* 通道控制
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class ChannelCtrlService {
private final FormulationService formulationService;
private final DeviceCommandService deviceCommandService;
private final DeviceState deviceState;
/**
* 开始加液
*/
public void solutionAddStart() {
CompletableFuture.runAsync(() -> {
List<ChannelState> channelStateList = deviceState.filterChannelStatesIsSelected();
try {
for (ChannelState channelState : channelStateList) {
channelState.setStateCode(ChannelStateCode.ADD);
}
if (SolutionAddMode.AUTO.equals(deviceState.getMode())) {//自动模式
while (!deviceState.isSolutionAddStop()) {
List<CommandFuture> commandFutureList = new ArrayList<>();
for (ChannelState channelState : channelStateList) {
Formulation formulation = formulationService.getOne(new LambdaQueryWrapper<Formulation>().eq(Formulation::getSolutionId, channelState.getSolutionId()));
DeviceCommand deviceCommand = getPumpMoveByCommandByChannel(channelState.getChannelCode(), formulation.getRevolutions());
commandFutureList.add(deviceCommandService.sendCommand(deviceCommand));
}
CommandUtil.wait(commandFutureList);
Thread.sleep(deviceState.getDelay() * 1000L);
}
} else if (SolutionAddMode.CLICK.equals(deviceState.getMode())) {//点动模式
List<CommandFuture> commandFutureList = new ArrayList<>();
for (ChannelState channelState : channelStateList) {
Formulation formulation = formulationService.getOne(new LambdaQueryWrapper<Formulation>().eq(Formulation::getSolutionId, channelState.getSolutionId()));
DeviceCommand deviceCommand = getPumpMoveByCommandByChannel(channelState.getChannelCode(), formulation.getRevolutions());
commandFutureList.add(deviceCommandService.sendCommand(deviceCommand));
}
CommandUtil.wait(commandFutureList);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
for (ChannelState channelState : channelStateList) {
channelState.setStateCode(ChannelStateCode.IDLE);
}
deviceState.setSolutionAddStop(false);
}
});
}
/**
* 停止加液
*/
public void solutionAddStop() {
List<ChannelState> channelCodeList = deviceState.filterChannelStatesByState(ChannelStateCode.ADD);
deviceState.setSolutionAddStop(true);
try {
List<CommandFuture> commandFutureList = new ArrayList<>();
for (ChannelState channelState : channelCodeList) {
DeviceCommand deviceCommand = getPumpStopCommandByChannel(channelState.getChannelCode());
commandFutureList.add(deviceCommandService.sendCommand(deviceCommand));
}
CommandUtil.wait(commandFutureList);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
for (ChannelState channelCode : channelCodeList) {
channelCode.setStateCode(ChannelStateCode.IDLE);
}
}
}
/**
* 开始预充
*/
public void solutionPreFillStart() {
CompletableFuture.runAsync(() -> {
List<ChannelState> channelStateList = deviceState.filterChannelStatesIsSelected();
try {
for (ChannelState channelState : channelStateList) {
channelState.setStateCode(ChannelStateCode.PRE);
}
List<CommandFuture> commandFutureList = new ArrayList<>();
for (ChannelState channelState : channelStateList) {
DeviceCommand deviceCommand = getPumpForwardRotateCommandByChannel(channelState.getChannelCode());
commandFutureList.add(deviceCommandService.sendCommand(deviceCommand));
}
CommandUtil.wait(commandFutureList);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
/**
* 结束预充
*/
public void solutionPreFillStop() {
CompletableFuture.runAsync(() -> {
List<ChannelState> channelCodeList = deviceState.filterChannelStatesByState(ChannelStateCode.PRE);
deviceState.setSolutionAddStop(true);
try {
List<CommandFuture> commandFutureList = new ArrayList<>();
for (ChannelState channelState : channelCodeList) {
DeviceCommand deviceCommand = getPumpStopCommandByChannel(channelState.getChannelCode());
commandFutureList.add(deviceCommandService.sendCommand(deviceCommand));
}
CommandUtil.wait(commandFutureList);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
for (ChannelState channelCode : channelCodeList) {
channelCode.setPre(true);
channelCode.setStateCode(ChannelStateCode.IDLE);
}
}
});
}
/**
* 根据通道code获取泵相对移动指令
*/
public DeviceCommand getPumpMoveByCommandByChannel(ChannelCode channelCode, Double position) {
return switch (channelCode) {
case CHANNEL_1 -> DeviceCommandGenerator.pump1MoveBy(position);
case CHANNEL_2 -> DeviceCommandGenerator.pump2MoveBy(position);
case CHANNEL_3 -> DeviceCommandGenerator.pump3MoveBy(position);
case CHANNEL_4 -> DeviceCommandGenerator.pump4MoveBy(position);
};
}
/**
* 根据通道code获取泵正转指令
*/
public DeviceCommand getPumpForwardRotateCommandByChannel(ChannelCode channelCode) {
return switch (channelCode) {
case CHANNEL_1 -> DeviceCommandGenerator.pump1ForwardRotate();
case CHANNEL_2 -> DeviceCommandGenerator.pump2ForwardRotate();
case CHANNEL_3 -> DeviceCommandGenerator.pump3ForwardRotate();
case CHANNEL_4 -> DeviceCommandGenerator.pump4ForwardRotate();
};
}
/**
* 根据通道code获取泵转数指令
*/
public DeviceCommand getPumpPositionCommandByChannel(ChannelCode channelCode) {
return switch (channelCode) {
case CHANNEL_1 -> DeviceCommandGenerator.pump1GetPosition();
case CHANNEL_2 -> DeviceCommandGenerator.pump2GetPosition();
case CHANNEL_3 -> DeviceCommandGenerator.pump3GetPosition();
case CHANNEL_4 -> DeviceCommandGenerator.pump4GetPosition();
};
}
/**
* 根据通道code获取泵反转指令
*/
public DeviceCommand getPumpBackwardRotateCommandByChannel(ChannelCode channelCode) {
return switch (channelCode) {
case CHANNEL_1 -> DeviceCommandGenerator.pump1BackwardRotate();
case CHANNEL_2 -> DeviceCommandGenerator.pump2BackwardRotate();
case CHANNEL_3 -> DeviceCommandGenerator.pump3BackwardRotate();
case CHANNEL_4 -> DeviceCommandGenerator.pump4BackwardRotate();
};
}
/**
* 根据通道code获取停止泵指令
*/
public DeviceCommand getPumpStopCommandByChannel(ChannelCode channelCode) {
return switch (channelCode) {
case CHANNEL_1 -> DeviceCommandGenerator.pump1Stop();
case CHANNEL_2 -> DeviceCommandGenerator.pump2Stop();
case CHANNEL_3 -> DeviceCommandGenerator.pump3Stop();
case CHANNEL_4 -> DeviceCommandGenerator.pump4Stop();
};
}
}