Browse Source

fix:修复指令bug

master
guoapeng 5 months ago
parent
commit
e54756c06a
  1. BIN
      matrix-spray.db
  2. 14
      src/main/java/com/qyft/ms/app/config/WebSocketServer.java
  3. 23
      src/main/java/com/qyft/ms/app/controller/CMDController.java
  4. 14
      src/main/java/com/qyft/ms/app/controller/MatrixController.java
  5. 15
      src/main/java/com/qyft/ms/app/model/vo/DeleteMatrixResult.java
  6. 77
      src/main/java/com/qyft/ms/app/service/CMDService.java
  7. 4
      src/main/java/com/qyft/ms/app/service/MatrixService.java
  8. 26
      src/main/java/com/qyft/ms/app/service/impl/MatrixServiceImpl.java
  9. 15
      src/main/java/com/qyft/ms/device/service/DeviceTcpCMDService.java
  10. 2
      src/test/java/com/qyft/ms/SprayTest.java

BIN
matrix-spray.db

14
src/main/java/com/qyft/ms/app/config/WebSocketServer.java

@ -38,13 +38,13 @@ public class WebSocketServer {
@OnMessage
public void onMessage(String message, Session session) {
ObjectMapper objectMapper = new ObjectMapper();
try {
WebsocketDTO c = objectMapper.readValue(message, WebsocketDTO.class);
log.info("解析后的信息: commandName={}, commandId={}", c.getCommandName(), c.getCommandId());
} catch (Exception e) {
e.printStackTrace();
}
// ObjectMapper objectMapper = new ObjectMapper();
// try {
// WebsocketDTO c = objectMapper.readValue(message, WebsocketDTO.class);
// log.info("解析后的信息: commandName={}, commandId={}", c.getCommandName(), c.getCommandId());
// } catch (Exception e) {
// e.printStackTrace();
// }
}
@OnClose

23
src/main/java/com/qyft/ms/app/controller/CMDController.java

@ -511,6 +511,29 @@ public class CMDController {
}
}
@Operation(summary = "结束除湿")
@PostMapping("/stopDehumidify")
public Result<String> stopDehumidify(@RequestBody CMDForm cmdForm) {
try {
if (cmdForm.getCommandId() == null || cmdForm.getCommandId().isEmpty()) {
String commandId = UUID.randomUUID().toString();
cmdForm.setCommandId(commandId);
}
if (cmdForm.getCommandName() == null || cmdForm.getCommandName().isEmpty()) {
cmdForm.setCommandName("stopDehumidify");
}
log.info("接收到指令: {}", JSONUtil.toJsonStr(cmdForm));
if (cmdService.stopDehumidify(cmdForm)) {
return Result.success(cmdForm.getCommandId());
} else {
return Result.failed("参数错误");
}
} catch (Exception e) {
log.error("指令执行异常: {}", JSONUtil.toJsonStr(cmdForm), e);
return Result.failed("执行失败");
}
}
@Operation(summary = "推出托盘")
@PostMapping("/trayOut")
public Result<String> trayOut(@RequestBody CMDForm cmdForm) {

14
src/main/java/com/qyft/ms/app/controller/MatrixController.java

@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qyft.ms.app.model.dto.MatrixDTO;
import com.qyft.ms.app.model.entity.Matrix;
import com.qyft.ms.app.model.vo.DeleteMatrixResult;
import com.qyft.ms.app.service.MatrixService;
import com.qyft.ms.device.model.entity.CtrlFuncStep;
import com.qyft.ms.system.common.base.BasePageQuery;
@ -17,6 +18,8 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Tag(name = "基质")
@RestController
@ -56,7 +59,14 @@ public class MatrixController {
@Operation(summary = "基质删除")
@DeleteMapping("/{ids}")
public Result<Boolean> deleteMatrix(@PathVariable String ids) {
return Result.success(matrixService.deleteMatrix(ids));
public Result<DeleteMatrixResult> deleteMatrix(@PathVariable String ids) {
List<String> nameList = matrixService.deleteMatrix(ids);
if(nameList != null){
DeleteMatrixResult matrixResult = new DeleteMatrixResult();
matrixResult.setMatrixName(nameList);
return Result.success(matrixResult);
}else{
return Result.failed();
}
}
}

15
src/main/java/com/qyft/ms/app/model/vo/DeleteMatrixResult.java

@ -0,0 +1,15 @@
package com.qyft.ms.app.model.vo;
import lombok.Data;
import java.util.List;
@Data
public class DeleteMatrixResult {
/**
* 任务id
*/
private List<String> matrixName;
}

77
src/main/java/com/qyft/ms/app/service/CMDService.java

@ -96,11 +96,10 @@ public class CMDService {
return false;
}
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);
int voltage = (int) params.get("voltage");
if(voltage> 5000) {
return false;
}
cmdList.add(() -> deviceTcpCMDService.turnOnHighVoltage(voltage));
initExecutorThread(cmdList, form);
return true;
@ -108,20 +107,16 @@ public class CMDService {
// 关闭高压电
public boolean turnOffHighVoltage(CMDForm form) {
Map<String, Object> params = form.getParams();
if (params == null) {
return false;
}
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();
if (params == null || !params.containsKey("rotationSpeed") || !params.containsKey("time")) {
if (params == null || !params.containsKey("rotationSpeed")) {
return false;
}
List<Supplier<Boolean>> cmdList = new ArrayList<>();
@ -130,12 +125,7 @@ public class CMDService {
.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);
cmdList.add(() -> deviceTcpCMDService.turnOnSyringePump(rotationSpeed, time));
cmdList.add(() -> deviceTcpCMDService.turnOnSyringePump(rotationSpeed));
initExecutorThread(cmdList, form);
return true;
}
@ -231,11 +221,11 @@ public class CMDService {
int movementSpeed = (Integer) params.get("movementSpeed");
cmdList.add(() -> deviceTcpCMDService.setMotorSpeed("x", movementSpeed));
cmdList.add(() -> deviceTcpCMDService.setMotorSpeed("y", movementSpeed));
cmdList.add(() -> deviceTcpCMDService.setMotorSpeed("z", movementSpeed));
cmdList.add(() -> deviceTcpCMDService.setMotorSpeed("z", 10));
// 移动到指定高度(位置)
int height = (Integer) params.get("height");
if(z - height < 6.5) {
if(z - height < 15) {
return "高度设置太低,有撞针的风险";
}
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("Z",z - height ));
@ -251,9 +241,9 @@ public class CMDService {
// 开启喷嘴阀
cmdList.add(() -> deviceTcpCMDService.controlValve("Nozzle", true));
// 推注射泵
// TODO 这里不管
// int matrixFlowVelocity = (int) params.get("matrixFlowVelocity");
cmdList.add(deviceTcpCMDService::syringePumpStart);
int matrixFlowVelocity = (int) params.get("matrixFlowVelocity");
cmdList.add(() -> deviceTcpCMDService.turnOnSyringePump(matrixFlowVelocity));
// 插入日志
if(i == 0) {
@ -285,6 +275,8 @@ public class CMDService {
// 关闭喷嘴阀
cmdList.add(() -> deviceTcpCMDService.controlValve("Nozzle", true));
}
// 关闭高压
cmdList.add(deviceTcpCMDService::turnOffHighVoltage);
// 回到原点
cmdList.add(() -> deviceTcpCMDService.motorMoveToHome("X"));
cmdList.add(() -> deviceTcpCMDService.motorMoveToHome("Y"));
@ -310,6 +302,8 @@ public class CMDService {
List<Supplier<Boolean>> cmdList = new ArrayList<>();
// 停止喷涂
cmdList.add(deviceTcpCMDService::turnOffSyringePump);
// 关闭高压
cmdList.add(deviceTcpCMDService::turnOffHighVoltage);
// 关闭喷嘴阀
cmdList.add(() -> deviceTcpCMDService.controlValve("Nozzle", true));
// 回到原点
@ -359,8 +353,20 @@ public class CMDService {
// 开始清洗
public boolean startWash(CMDForm form) {
List<Supplier<Boolean>> cmdList = new ArrayList<>();
cmdList.add(deviceTcpCMDService::switchThreeWayValveToSubstrate);
cmdList.add(() -> deviceTcpCMDService.controlValve("Cleaning", true));
Map<String, Object> params = form.getParams();
String type = (String) params.get("type");
// type: "injector" | "nozzle"
if(Objects.equals(type, "injector")) {
cmdList.add(() -> deviceTcpCMDService.switchThreeWayValve("clear_spray"));
cmdList.add(() -> deviceTcpCMDService.controlValve("Cleaning", true));
} else if (Objects.equals(type, "nozzle")) {
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("X", 173.08));
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("Y", 75.2));
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("Z", 70));
cmdList.add(() -> deviceTcpCMDService.switchThreeWayValve("clear_nozzle"));
cmdList.add(() -> deviceTcpCMDService.controlValve("Nozzle", true));
}
cmdList.add(() -> {
sysSettingsService.updateWorkStatus("washing");
return true;
@ -372,7 +378,9 @@ public class CMDService {
// 结束清洗
public boolean stopWash(CMDForm form) {
List<Supplier<Boolean>> cmdList = new ArrayList<>();
cmdList.add(() -> deviceTcpCMDService.switchThreeWayValve("close_all"));
cmdList.add(() -> deviceTcpCMDService.controlValve("Cleaning", false));
cmdList.add(() -> deviceTcpCMDService.controlValve("Nozzle", false));
cmdList.add(() -> {
sysSettingsService.updateWorkStatus("idle");
return true;
@ -409,8 +417,12 @@ public class CMDService {
// 开始预充
public boolean startPrefill(CMDForm form) {
List<Supplier<Boolean>> cmdList = new ArrayList<>();
cmdList.add(deviceTcpCMDService::switchThreeWayValveToSpray);
Map<String, Object> params = form.getParams();
cmdList.add(() -> deviceTcpCMDService.switchThreeWayValve("clear_spray"));
cmdList.add(() -> deviceTcpCMDService.controlValve("Nozzle", true));
cmdList.add(() -> deviceTcpCMDService.turnOnSyringePump((Double) params.get("rotationSpeed")));
cmdList.add(() -> {
sysSettingsService.updateWorkStatus("prefilling");
return true;
@ -421,7 +433,9 @@ public class CMDService {
// 停止预充
public boolean stopPrefill(CMDForm form) {
List<Supplier<Boolean>> cmdList = new ArrayList<>();
cmdList.add(deviceTcpCMDService::turnOffSyringePump);
cmdList.add(() -> deviceTcpCMDService.controlValve("Nozzle", false));
cmdList.add(() -> {
sysSettingsService.updateWorkStatus("idle");
return true;
@ -462,6 +476,13 @@ public class CMDService {
return true;
}
public boolean stopDehumidify(CMDForm form) {
List<Supplier<Boolean>> cmdList = new ArrayList<>();
cmdList.add(() -> deviceTcpCMDService.controlValve("Dehumidification", false));
initExecutorThread(cmdList, form);
return true;
}
// 设置电机速度
public boolean setMotorSpeed(CMDForm form) {
List<Supplier<Boolean>> cmdList = new ArrayList<>();
@ -499,7 +520,13 @@ public class CMDService {
// 执行所有命令
for (Supplier<Boolean> command : cmdList) {
boolean result = command.get();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (!result) {
log.error("指令执行异常: {}", JSONUtil.toJsonStr(form));
executionResult.setStatus(CMDResultCode.FAILURE.getCode());

4
src/main/java/com/qyft/ms/app/service/MatrixService.java

@ -3,6 +3,8 @@ package com.qyft.ms.app.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.qyft.ms.app.model.entity.Matrix;
import java.util.List;
/**
* 日志业务接口
@ -10,5 +12,5 @@ import com.qyft.ms.app.model.entity.Matrix;
public interface MatrixService extends IService<Matrix> {
int addMatrix(Matrix matrix);
boolean updateMatrix(Matrix matrix);
boolean deleteMatrix(String ids);
List<String> deleteMatrix(String ids);
}

26
src/main/java/com/qyft/ms/app/service/impl/MatrixServiceImpl.java

@ -1,12 +1,16 @@
package com.qyft.ms.app.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qyft.ms.app.mapper.MatrixMapper;
import com.qyft.ms.app.model.entity.Matrix;
import com.qyft.ms.app.model.entity.MatrixCraft;
import com.qyft.ms.app.service.MatrixCraftService;
import com.qyft.ms.app.service.MatrixService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@ -19,6 +23,7 @@ import java.util.stream.Collectors;
public class MatrixServiceImpl extends ServiceImpl<MatrixMapper, Matrix> implements MatrixService {
private final MatrixMapper matrixMapper;
private final MatrixCraftService matrixCraftService;
@Override
public int addMatrix(Matrix matrix) {
return matrixMapper.insert(matrix);
@ -31,10 +36,25 @@ public class MatrixServiceImpl extends ServiceImpl<MatrixMapper, Matrix> impleme
}
@Override
public boolean deleteMatrix(String ids) {
public List<String> deleteMatrix(String ids) {
List<String> nameList = new ArrayList<>();
List<Long> idsArr = Arrays.stream(ids.split(","))
.map(Long::parseLong)
.collect(Collectors.toList());
return this.removeByIds(idsArr);
.toList();
List<Long> removeIdList = new ArrayList<>();
for (Long id : idsArr) {
List<MatrixCraft> listByMatrix = matrixCraftService.list(new LambdaQueryWrapper<MatrixCraft>() .eq( MatrixCraft::getMatrixId, id));
if(listByMatrix.isEmpty()){
removeIdList.add(id);
}else{
Matrix matrix = this.getById(id);
nameList.add(matrix.getName());
}
}
if(!this.removeByIds(removeIdList)){
return null;
}
return nameList;
}
}

15
src/main/java/com/qyft/ms/device/service/DeviceTcpCMDService.java

@ -198,7 +198,7 @@ public class DeviceTcpCMDService {
* @param voltage 电压值
* @return 操作是否成功
*/
public boolean turnOnHighVoltage(double voltage) {
public boolean turnOnHighVoltage(int voltage) {
Map<String, Object> params = new HashMap<>();
params.put("voltage", voltage);
return this.putTask(DeviceCommands.TURN_ON_HIGH_VOLTAGE, params);
@ -214,15 +214,13 @@ public class DeviceTcpCMDService {
// 注射泵控制方法
/**
* 以指定转速方向和时间开启注射泵
* 以指定转速开启注射泵
* @param rotationSpeed 转速
* @param time 时间
* @return 操作是否成功
*/
public boolean turnOnSyringePump(double rotationSpeed, double time) {
public boolean turnOnSyringePump(double rotationSpeed) {
Map<String, Object> params = new HashMap<>();
params.put("rotationSpeed", rotationSpeed);
params.put("time", time);
return this.putTask(DeviceCommands.TURN_ON_SYRINGE_PUMP, params);
}
@ -310,4 +308,11 @@ public class DeviceTcpCMDService {
return this.putTask(DeviceCommands.START_DEHUMIDIFY, params);
}
// 切换三通阀
public boolean switchThreeWayValve(String type) {
Map<String, Object> params = new HashMap<>();
params.put("type", type);
return this.putTask(DeviceCommands.START_DEHUMIDIFY, params);
}
}

2
src/test/java/com/qyft/ms/SprayTest.java

@ -71,7 +71,7 @@ public class SprayTest {
when(deviceTcpCMDService.switchThreeWayValveToSpray()).thenReturn(true);
when(deviceTcpCMDService.setMotorSpeed(anyString(), anyInt())).thenReturn(true);
when(deviceTcpCMDService.moveMotorToPosition(anyString(), anyDouble())).thenReturn(true);
when(deviceTcpCMDService.turnOnHighVoltage(anyDouble())).thenReturn(true);
when(deviceTcpCMDService.turnOnHighVoltage((int) anyDouble())).thenReturn(true);
when(deviceTcpCMDService.controlValve(anyString(), anyBoolean())).thenReturn(true);
when(deviceTcpCMDService.syringePumpMoveAtSpeed(anyInt())).thenReturn(true);
when(deviceTcpCMDService.turnOffSyringePump()).thenReturn(true);

Loading…
Cancel
Save