Browse Source

feat:增加工艺步骤执行

tags/freeze
白凤吉 3 months ago
parent
commit
91ea9817ea
  1. 8
      src/main/java/com/iflytop/gd/app/core/CraftsContext.java
  2. 3
      src/main/java/com/iflytop/gd/app/service/CraftsService.java
  3. 58
      src/main/java/com/iflytop/gd/app/service/CraftsStepService.java

8
src/main/java/com/iflytop/gd/app/core/CraftsContext.java

@ -5,6 +5,7 @@ import com.iflytop.gd.app.common.enums.CraftEvents;
import com.iflytop.gd.app.common.enums.CraftStates;
import com.iflytop.gd.app.model.bo.CraftsStep;
import com.iflytop.gd.app.model.entity.Crafts;
import com.iflytop.gd.app.service.CraftsStepService;
import com.iflytop.gd.app.service.WebSocketService;
import com.iflytop.gd.common.constant.WebSocketMessageType;
import lombok.Getter;
@ -26,12 +27,14 @@ public class CraftsContext implements Runnable {
private final List<CraftsStep> craftsStepList;
private final StateMachine<CraftStates, CraftEvents> sm;
private final WebSocketService ws;
private final CraftsStepService craftsStepService;
private int currentIndex = 0;
public CraftsContext(String heatId, Crafts craft, StateMachineFactory<CraftStates, CraftEvents> factory, WebSocketService ws) {
public CraftsContext(String heatId, Crafts craft, StateMachineFactory<CraftStates, CraftEvents> factory, WebSocketService ws, CraftsStepService craftsStepService) {
this.heatId = heatId;
this.craftsStepList = JSONUtil.parseArray(craft.getSteps()).toList(CraftsStep.class);
this.ws = ws;
this.craftsStepService = craftsStepService;
this.sm = factory.getStateMachine(heatId);
Mono.from(sm.startReactively()).block();
@ -83,14 +86,13 @@ public class CraftsContext implements Runnable {
}
private boolean executeStep(CraftsStep step) throws InterruptedException {
// TODO: 推送单步信息并实际调用设备服务
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("heatId", heatId);
dataMap.put("currentStep", step.getMethod());
ws.push(WebSocketMessageType.CRAFTS_STEP, dataMap);
Thread.sleep(3000);
return true;
return craftsStepService.executeStep(heatId, step);
}
// 暂停

3
src/main/java/com/iflytop/gd/app/service/CraftsService.java

@ -26,6 +26,7 @@ import java.util.stream.Collectors;
public class CraftsService extends ServiceImpl<CraftsMapper, Crafts> {
private final StateMachineFactory<CraftStates, CraftEvents> stateMachineFactory;
private final WebSocketService webSocketService;
private final CraftsStepService craftsStepService;
private ExecutorService executor;
private final ConcurrentHashMap<String, CraftsContext> contextMap = new ConcurrentHashMap<>();
@ -47,7 +48,7 @@ public class CraftsService extends ServiceImpl<CraftsMapper, Crafts> {
if (craft == null) {
return false;
}
CraftsContext ctx = new CraftsContext(heatId, craft, stateMachineFactory, webSocketService);
CraftsContext ctx = new CraftsContext(heatId, craft, stateMachineFactory, webSocketService, craftsStepService);
CompletableFuture<Void> cf = CompletableFuture.runAsync(() -> {
try {
ctx.run();

58
src/main/java/com/iflytop/gd/app/service/CraftsStepService.java

@ -0,0 +1,58 @@
package com.iflytop.gd.app.service;
import cn.hutool.json.JSONObject;
import com.iflytop.gd.app.model.bo.CraftsStep;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
/**
* 工艺步骤
*/
@Service
@RequiredArgsConstructor
public class CraftsStepService {
/**
* 执行单个工艺步骤
*/
public boolean executeStep(String heatId, CraftsStep step) throws InterruptedException {
String method = step.getMethod();
JSONObject params = step.getParams();
return switch (method) {
case "UP_TRAY" -> upTray(heatId);
case "DOWN_TRAY" -> downTray(heatId);
case "ADD_LIQUID" -> addLiquid(heatId, params);
case "DELAY" -> delay(params);
default -> false;
};
}
/**
* 将托盘升起
*/
private boolean upTray(String heatId) {
return true;
}
/**
* 将托盘降下
*/
private boolean downTray(String heatId) {
return true;
}
/**
* 加液操作
*/
private boolean addLiquid(String heatId, JSONObject params) {
return true;
}
/**
* 等待指定秒数
*/
private boolean delay(JSONObject params) throws InterruptedException {
Thread.sleep(params.getInt("second") * 1000L);
return true;
}
}
Loading…
Cancel
Save