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.
289 lines
12 KiB
289 lines
12 KiB
package com.qyft.ms.app.service;
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.qyft.ms.app.common.constant.WebSocketMessageType;
|
|
import com.qyft.ms.app.common.generator.PathGenerator;
|
|
import com.qyft.ms.app.common.result.CMDResultCode;
|
|
import com.qyft.ms.app.model.entity.OperationLog;
|
|
import com.qyft.ms.app.model.form.CMDForm;
|
|
import com.qyft.ms.app.model.vo.ExecutionResult;
|
|
import com.qyft.ms.device.service.DeviceTcpCMDService;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.awt.*;
|
|
import java.util.*;
|
|
import java.util.List;
|
|
import java.util.function.Function;
|
|
import java.util.function.Supplier;
|
|
|
|
import static com.qyft.ms.app.common.generator.PathGenerator.generatePathPoints;
|
|
|
|
@Slf4j
|
|
@RequiredArgsConstructor
|
|
@Service
|
|
public class CMDService {
|
|
|
|
private final WebSocketService webSocketService;
|
|
private final DeviceTcpCMDService deviceTcpCMDService;
|
|
private final OperationLogService operationLogService;
|
|
private final MatrixCraftService matrixCraftService;
|
|
|
|
|
|
private void initExecutorThread(List<Supplier<Boolean>> cmdList, CMDForm form) {
|
|
new Thread(() -> run(cmdList, form)).start();
|
|
}
|
|
|
|
// 电机移动
|
|
public boolean moveMotorToPosition(CMDForm form) {
|
|
Map<String, Object> params = form.getParams();
|
|
List<Supplier<Boolean>> cmdList = new ArrayList<>();
|
|
String axis = (String) params.get("axis");
|
|
double position = Optional.ofNullable(params.get("position"))
|
|
.filter(Number.class::isInstance)
|
|
.map(Number.class::cast)
|
|
.map(Number::doubleValue)
|
|
.orElse(0.0);
|
|
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition(axis, position));
|
|
initExecutorThread(cmdList, form);
|
|
return true;
|
|
};
|
|
|
|
// 切换清洗管路/喷涂管路
|
|
public boolean switchThreeWayValve(CMDForm form) {
|
|
Map<String, Object> params = form.getParams();
|
|
List<Supplier<Boolean>> cmdList = new ArrayList<>();
|
|
String type = (String) params.get("type");
|
|
if (type.equals("clean")) {
|
|
cmdList.add(deviceTcpCMDService::switchThreeWayValveToSubstrate);
|
|
} else if (type.equals("spray")) {
|
|
cmdList.add(deviceTcpCMDService::switchThreeWayValveToSpray);
|
|
}
|
|
initExecutorThread(cmdList, form);
|
|
return true;
|
|
}
|
|
|
|
// 除湿阀、清洗阀、喷嘴阀控制方法
|
|
public boolean controlValve(CMDForm form) {
|
|
Map<String, Object> params = form.getParams();
|
|
List<Supplier<Boolean>> cmdList = new ArrayList<>();
|
|
if(params.get("valveType") !=null && params.get("isOpen") !=null) {
|
|
String valveType = (String) params.get("valveType");
|
|
boolean isOpen = (boolean) params.get("isOpen");
|
|
cmdList.add(() -> deviceTcpCMDService.controlValve(valveType,isOpen));
|
|
}
|
|
initExecutorThread(cmdList, form);
|
|
return true;
|
|
}
|
|
|
|
// 以指定电压值开启高压电
|
|
public boolean turnOnHighVoltage(CMDForm form) {
|
|
Map<String, Object> params = form.getParams();
|
|
List<Supplier<Boolean>> cmdList = new ArrayList<>();
|
|
double voltage = Optional.ofNullable(params.get("voltage"))
|
|
.filter(Number.class::isInstance)
|
|
.map(Number.class::cast)
|
|
.map(Number::doubleValue)
|
|
.orElse(0.0);
|
|
cmdList.add(() -> deviceTcpCMDService.turnOnHighVoltage(voltage));
|
|
initExecutorThread(cmdList, form);
|
|
return true;
|
|
}
|
|
|
|
// 关闭高压电
|
|
public boolean turnOffHighVoltage(CMDForm form) {
|
|
Map<String, Object> params = form.getParams();
|
|
List<Supplier<Boolean>> cmdList = new ArrayList<>();
|
|
cmdList.add(deviceTcpCMDService::turnOffHighVoltage);
|
|
initExecutorThread(cmdList, form);
|
|
return true;
|
|
}
|
|
|
|
// 以指定转速、方向和时间开启注射泵
|
|
public boolean turnOnSyringePump(CMDForm form) {
|
|
Map<String, Object> params = form.getParams();
|
|
List<Supplier<Boolean>> cmdList = new ArrayList<>();
|
|
double rotationSpeed = Optional.ofNullable(params.get("rotationSpeed"))
|
|
.filter(Number.class::isInstance)
|
|
.map(Number.class::cast)
|
|
.map(Number::doubleValue)
|
|
.orElse(0.0);
|
|
double time = Optional.ofNullable(params.get("time"))
|
|
.filter(Number.class::isInstance)
|
|
.map(Number.class::cast)
|
|
.map(Number::doubleValue)
|
|
.orElse(0.0);
|
|
String direction = (String) params.get("direction");
|
|
cmdList.add(() -> deviceTcpCMDService.turnOnSyringePump(rotationSpeed, direction, time));
|
|
initExecutorThread(cmdList, form);
|
|
return true;
|
|
}
|
|
|
|
// 停止注射泵
|
|
public boolean turnOffSyringePump(CMDForm form) {
|
|
Map<String, Object> params = form.getParams();
|
|
List<Supplier<Boolean>> cmdList = new ArrayList<>();
|
|
cmdList.add(deviceTcpCMDService::turnOffSyringePump);
|
|
initExecutorThread(cmdList, form);
|
|
return true;
|
|
}
|
|
|
|
// 设置指定轴的电机运行时电流
|
|
public boolean setMotorRunningCurrent(CMDForm form) {
|
|
Map<String, Object> params = form.getParams();
|
|
List<Supplier<Boolean>> cmdList = new ArrayList<>();
|
|
String axis = (String) params.get("axis");
|
|
double current = Optional.ofNullable(params.get("current"))
|
|
.filter(Number.class::isInstance)
|
|
.map(Number.class::cast)
|
|
.map(Number::doubleValue)
|
|
.orElse(0.0);
|
|
cmdList.add(() -> deviceTcpCMDService.setMotorRunningCurrent(axis, current));
|
|
initExecutorThread(cmdList, form);
|
|
return true;
|
|
}
|
|
|
|
// 开始喷涂
|
|
public boolean startWork(CMDForm form) {
|
|
Map<String, Object> params = form.getParams();
|
|
List<Supplier<Boolean>> cmdList = new ArrayList<>();
|
|
// 托盘位置
|
|
Map<String, Integer> trayPosition = new HashMap<>();
|
|
trayPosition.put("x", 0);
|
|
trayPosition.put("y", 0);
|
|
trayPosition.put("z", 0);
|
|
int space = (Integer) params.get("space");
|
|
List<Map<String, Integer>> position = (List<Map<String, Integer>>) params.get("position");
|
|
Map<String, Integer> p1 = position.get(0);
|
|
Map<String, Integer> p2 = position.get(1);
|
|
// 托盘点位 x y z
|
|
int left = trayPosition.get("x")+ p1.get("x");
|
|
int right = trayPosition.get("x") + p2.get("x");
|
|
int top = trayPosition.get("y") + p2.get("y");
|
|
int bottom = trayPosition.get("y") + p1.get("y");
|
|
// 获取轨迹list
|
|
int direction = (Integer) params.get("direction");
|
|
List<Point> horizontalPath = generatePathPoints(
|
|
left, right, bottom, top, space, direction == 1 ? PathGenerator.MoveMode.HORIZONTAL_ZIGZAG_TOP_DOWN : PathGenerator.MoveMode.VERTICAL_ZIGZAG_LEFT_RIGHT
|
|
);
|
|
|
|
// 将三通阀转至喷涂管路
|
|
cmdList.add(deviceTcpCMDService::switchThreeWayValveToSpray);
|
|
// 设置指定轴的电机的运行速度
|
|
int setMotorSpeed = (Integer) params.get("setMotorSpeed");
|
|
cmdList.add(() -> deviceTcpCMDService.setMotorSpeed("x", setMotorSpeed));
|
|
cmdList.add(() -> deviceTcpCMDService.setMotorSpeed("y", setMotorSpeed));
|
|
cmdList.add(() -> deviceTcpCMDService.setMotorSpeed("z", setMotorSpeed));
|
|
// 移动到指定高度(位置)
|
|
int height = (Integer) params.get("height");
|
|
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("z",trayPosition.get("z") + height ));
|
|
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("x", left));
|
|
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("y", top));
|
|
|
|
// 是否加电 电压
|
|
Object voltage = params.get("voltage");
|
|
if (voltage instanceof Integer) {
|
|
cmdList.add(() -> deviceTcpCMDService.turnOnHighVoltage( (Integer) voltage));
|
|
}
|
|
// 开启喷嘴阀
|
|
cmdList.add(() -> deviceTcpCMDService.controlValve("Nozzle", true));
|
|
// 推注射泵
|
|
cmdList.add(() -> deviceTcpCMDService.turnOnSyringePump(100, "right", 5000));
|
|
|
|
// 插入日志
|
|
cmdList.add( () -> {
|
|
OperationLog operationLog = new OperationLog();
|
|
operationLog.setStatus(0);
|
|
operationLog.setMatrixId((Long) params.get("matrixCraftId"));
|
|
operationLog.setMatrixInfo(JSON.toJSONString(params));
|
|
operationLogService.add(operationLog);
|
|
return true;
|
|
});
|
|
// 执行轨迹
|
|
double curX = left;
|
|
double curY = top;
|
|
for (Point point : horizontalPath) {
|
|
double nextX = (int) point.getX();
|
|
double nextY = (int) point.getY();
|
|
double distanceX = nextX - curX;
|
|
double distanceY = nextY - curY;
|
|
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("x", distanceX));
|
|
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("y", distanceY));
|
|
curX = nextX;
|
|
curY = nextY;
|
|
}
|
|
// 停止喷涂
|
|
cmdList.add(deviceTcpCMDService::turnOffSyringePump);
|
|
// 关闭喷嘴阀
|
|
cmdList.add(() -> deviceTcpCMDService.controlValve("Nozzle", true));
|
|
// 回到原点
|
|
cmdList.add(() -> deviceTcpCMDService.motorMoveToHome("X"));
|
|
cmdList.add(() -> deviceTcpCMDService.motorMoveToHome("Y"));
|
|
cmdList.add(() -> deviceTcpCMDService.motorMoveToHome("Z"));
|
|
// 结束日志
|
|
cmdList.add( () -> {
|
|
OperationLog operationLog = operationLogService.getIng();
|
|
operationLog.setStatus(1);
|
|
operationLogService.updateById(operationLog);
|
|
return true;
|
|
});
|
|
initExecutorThread(cmdList, form);
|
|
return true;
|
|
}
|
|
|
|
// 结束喷涂
|
|
public boolean stopWork(CMDForm form) {
|
|
Map<String, Object> params = form.getParams();
|
|
List<Supplier<Boolean>> cmdList = new ArrayList<>();
|
|
// 停止喷涂
|
|
cmdList.add(deviceTcpCMDService::turnOffSyringePump);
|
|
// 关闭喷嘴阀
|
|
cmdList.add(() -> deviceTcpCMDService.controlValve("Nozzle", true));
|
|
// 回到原点
|
|
cmdList.add(() -> deviceTcpCMDService.motorMoveToHome("X"));
|
|
cmdList.add(() -> deviceTcpCMDService.motorMoveToHome("Z"));
|
|
initExecutorThread(cmdList, form);
|
|
return true;
|
|
}
|
|
|
|
// 测试轴转动
|
|
public boolean rotate(CMDForm form) {
|
|
Map<String, Object> params = form.getParams();
|
|
List<Supplier<Boolean>> cmdList = new ArrayList<>();
|
|
String axis = (String) params.get("axis");
|
|
double rotationSpeed = Optional.ofNullable(params.get("rotationSpeed"))
|
|
.filter(Number.class::isInstance)
|
|
.map(Number.class::cast)
|
|
.map(Number::doubleValue)
|
|
.orElse(0.0);
|
|
int time = (Integer) params.get("time");
|
|
cmdList.add(() -> deviceTcpCMDService.rotate(axis, rotationSpeed, time));
|
|
initExecutorThread(cmdList, form);
|
|
return true;
|
|
}
|
|
|
|
|
|
private void run(List<Supplier<Boolean>> cmdList, CMDForm form) {
|
|
ExecutionResult executionResult = new ExecutionResult();
|
|
executionResult.setCommandId(form.getCommandId());
|
|
executionResult.setCommandName(form.getCommandName());
|
|
|
|
// 执行所有命令
|
|
for (Supplier<Boolean> command : cmdList) {
|
|
boolean result = command.get();
|
|
if (!result) {
|
|
log.error("指令执行异常: {}", JSONUtil.toJsonStr(form));
|
|
executionResult.setStatus(CMDResultCode.FAILURE.getCode());
|
|
executionResult.setMessage(CMDResultCode.FAILURE.getMsg());
|
|
webSocketService.pushMsg(WebSocketMessageType.CMD, executionResult);
|
|
return;
|
|
}
|
|
}
|
|
log.info("指令执行成功: {}", JSONUtil.toJsonStr(form));
|
|
executionResult.setStatus(CMDResultCode.SUCCESS.getCode());
|
|
executionResult.setMessage(CMDResultCode.SUCCESS.getMsg());
|
|
webSocketService.pushMsg(WebSocketMessageType.CMD, executionResult);
|
|
}
|
|
}
|