6 changed files with 110 additions and 123 deletions
-
14src/main/java/com/iflytop/gd/app/controller/CraftsController.java
-
17src/main/java/com/iflytop/gd/app/core/CraftsContext.java
-
6src/main/java/com/iflytop/gd/app/model/bo/CraftsStep.java
-
86src/main/java/com/iflytop/gd/app/service/CraftsService.java
-
100src/main/java/com/iflytop/gd/app/service/CraftsStepService.java
-
10src/main/java/com/iflytop/gd/common/constant/WebSocketMessageType.java
@ -1,8 +1,10 @@ |
|||
package com.iflytop.gd.app.model.bo; |
|||
|
|||
import cn.hutool.json.JSONObject; |
|||
import lombok.Data; |
|||
|
|||
@Data |
|||
public class CraftsStep { |
|||
String name; |
|||
JSONObject params; |
|||
private String method; |
|||
private JSONObject params; |
|||
} |
@ -1,100 +0,0 @@ |
|||
package com.iflytop.gd.app.service; |
|||
|
|||
import com.iflytop.gd.app.common.enums.CraftEvents; |
|||
import com.iflytop.gd.app.common.enums.CraftStates; |
|||
import com.iflytop.gd.app.core.CraftsContext; |
|||
import com.iflytop.gd.app.model.entity.Crafts; |
|||
import jakarta.annotation.PostConstruct; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.statemachine.config.StateMachineFactory; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.util.concurrent.ConcurrentHashMap; |
|||
import java.util.concurrent.ExecutorService; |
|||
import java.util.concurrent.Executors; |
|||
import java.util.concurrent.Future; |
|||
|
|||
/** |
|||
* 设备步骤操作 |
|||
*/ |
|||
@Slf4j |
|||
@Service |
|||
@RequiredArgsConstructor |
|||
public class CraftsStepService { |
|||
private final CraftsService craftsService; |
|||
private final StateMachineFactory<CraftStates, CraftEvents> stateMachineFactory; |
|||
private final WebSocketService webSocketService; |
|||
|
|||
private ExecutorService executor; |
|||
|
|||
private final ConcurrentHashMap<String, CraftsContext> contextMap = new ConcurrentHashMap<>(); |
|||
private final ConcurrentHashMap<String, Future<?>> futureMap = new ConcurrentHashMap<>(); |
|||
|
|||
@PostConstruct |
|||
public void init() { |
|||
this.executor = Executors.newCachedThreadPool(); |
|||
} |
|||
|
|||
/** |
|||
* 开始执行工艺 |
|||
*/ |
|||
public synchronized boolean startCrafts(Long craftId, String heatId) { |
|||
if (futureMap.containsKey(heatId)) {// 已有任务在执行,不重复启动 |
|||
return false; |
|||
} |
|||
Crafts craft = craftsService.getById(craftId); |
|||
if (craft == null) { |
|||
return false; |
|||
} |
|||
CraftsContext ctx = new CraftsContext( |
|||
heatId, |
|||
craft, |
|||
stateMachineFactory, |
|||
this, |
|||
webSocketService |
|||
); |
|||
Future<?> future = executor.submit(ctx); |
|||
contextMap.put(heatId, ctx); |
|||
futureMap.put(heatId, future); |
|||
return true; |
|||
} |
|||
|
|||
/** |
|||
* 暂停执行工艺 |
|||
*/ |
|||
public synchronized void pauseCrafts(String heatId) { |
|||
CraftsContext ctx = contextMap.get(heatId); |
|||
if (ctx != null) { |
|||
ctx.pause(); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 恢复执行工艺 |
|||
*/ |
|||
public synchronized void resumeCrafts(String heatId) { |
|||
CraftsContext ctx = contextMap.get(heatId); |
|||
if (ctx != null) { |
|||
ctx.resume(); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 停止执行工艺 |
|||
*/ |
|||
public synchronized boolean stopCrafts(String heatId) { |
|||
CraftsContext ctx = contextMap.get(heatId); |
|||
Future<?> future = futureMap.get(heatId); |
|||
if (ctx != null && future != null) { |
|||
ctx.stop(); |
|||
future.cancel(true); |
|||
contextMap.remove(heatId); |
|||
futureMap.remove(heatId); |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
// TODO: 如需提供查询当前执行状态,可在此类添加 getStatus(heatId) 方法,返回 ctx.getCurrentIndex(), ctx.getSm().getState().getId(), ctx.getRemainingSteps() 等 |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue