|
|
@ -1,8 +1,16 @@ |
|
|
|
package com.qyft.gd.service; |
|
|
|
|
|
|
|
import cn.hutool.json.JSONArray; |
|
|
|
import cn.hutool.json.JSONObject; |
|
|
|
import cn.hutool.json.JSONUtil; |
|
|
|
import com.qyft.gd.device.service.DeviceStepService; |
|
|
|
import com.qyft.gd.model.bo.TubeSol; |
|
|
|
import com.qyft.gd.model.entity.Crafts; |
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
import java.util.concurrent.ExecutorService; |
|
|
|
import java.util.concurrent.Executors; |
|
|
@ -11,14 +19,17 @@ import java.util.concurrent.Future; |
|
|
|
/** |
|
|
|
* 工艺操作 |
|
|
|
*/ |
|
|
|
@Slf4j |
|
|
|
@Service |
|
|
|
@RequiredArgsConstructor |
|
|
|
public class CraftsStepService { |
|
|
|
|
|
|
|
private final CraftsService craftsService; |
|
|
|
private final DeviceStepService deviceStepService; |
|
|
|
|
|
|
|
private final ExecutorService executorService = Executors.newCachedThreadPool(); |
|
|
|
private final ConcurrentHashMap<Long, Future<?>> taskMap = new ConcurrentHashMap<>(); |
|
|
|
|
|
|
|
|
|
|
|
// 启动任务的方法 |
|
|
|
public synchronized boolean startCrafts(Long craftId, Long heatId) { |
|
|
|
Future<?> taskFuture = taskMap.get(heatId); |
|
|
@ -45,7 +56,7 @@ public class CraftsStepService { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static class CraftsTask implements Runnable { |
|
|
|
class CraftsTask implements Runnable { |
|
|
|
private final Long heatId; |
|
|
|
private final Long craftId; |
|
|
|
|
|
|
@ -56,16 +67,120 @@ public class CraftsStepService { |
|
|
|
|
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
try { |
|
|
|
while (!Thread.currentThread().isInterrupted()) { |
|
|
|
System.out.println("加热区 " + heatId + " 工艺运行中..."); |
|
|
|
Thread.sleep(1000); |
|
|
|
while (!Thread.currentThread().isInterrupted()) { |
|
|
|
Crafts crafts = craftsService.findCraftsById(craftId); |
|
|
|
log.info("开始执行工艺,加热区 {},工艺 {}", heatId, crafts.getName()); |
|
|
|
String steps = crafts.getSteps(); |
|
|
|
JSONArray stepsJsonArray = JSONUtil.parseArray(steps); |
|
|
|
for (Object stepsJsonObject : stepsJsonArray) { |
|
|
|
JSONObject stepsJson = (JSONObject) stepsJsonObject; |
|
|
|
String method = stepsJson.get("method").toString(); |
|
|
|
JSONObject params = (JSONObject) stepsJson.get("params"); |
|
|
|
boolean result = switch (method) { |
|
|
|
case "upTray" -> upTray(params); |
|
|
|
case "downTray" -> downTray(params); |
|
|
|
case "addLiquid" -> addLiquid(params); |
|
|
|
case "moveToSol" -> moveToSol(params); |
|
|
|
case "moveToHeater" -> moveToHeater(params); |
|
|
|
case "shaking" -> shaking(params); |
|
|
|
case "startHeating" -> startHeating(params); |
|
|
|
case "stopHeating" -> stopHeating(params); |
|
|
|
case "takePhoto" -> takePhoto(); |
|
|
|
case "moveToExc" -> delay(params); |
|
|
|
default -> false; |
|
|
|
}; |
|
|
|
if (!result) { |
|
|
|
//TODO 向前端反馈执行失败 |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (InterruptedException e) { |
|
|
|
Thread.currentThread().interrupt(); |
|
|
|
System.out.println("加热区 " + heatId + " 工艺被中断"); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 抬起托盘 |
|
|
|
*/ |
|
|
|
private boolean upTray(JSONObject params) { |
|
|
|
String heaterId = params.get("heaterId").toString(); |
|
|
|
return deviceStepService.upTray(heaterId); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 降下托盘 |
|
|
|
*/ |
|
|
|
private boolean downTray(JSONObject params) { |
|
|
|
String heaterId = params.get("heaterId").toString(); |
|
|
|
return deviceStepService.downTray(heaterId); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 添加溶液 |
|
|
|
*/ |
|
|
|
private boolean addLiquid(JSONObject params) { |
|
|
|
JSONArray tubeSolJSONArray = params.getJSONArray("tubeSolList"); |
|
|
|
List<TubeSol> tubeSolList = JSONUtil.toList(tubeSolJSONArray.toString(), TubeSol.class); |
|
|
|
return deviceStepService.addLiquid(tubeSolList); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 将指定加热区的托盘移至加液区 |
|
|
|
*/ |
|
|
|
private boolean moveToSol(JSONObject params) { |
|
|
|
String heaterId = params.get("heaterId").toString(); |
|
|
|
return deviceStepService.moveToSol(heaterId); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 移至加热 |
|
|
|
*/ |
|
|
|
private boolean moveToHeater(JSONObject params) { |
|
|
|
String heaterId = params.get("heaterId").toString(); |
|
|
|
return deviceStepService.moveToHeater(heaterId); |
|
|
|
} |
|
|
|
|
|
|
|
//摇匀 |
|
|
|
private boolean shaking(JSONObject params) { |
|
|
|
Integer second = params.getInt("second"); |
|
|
|
return deviceStepService.shaking(second); |
|
|
|
} |
|
|
|
|
|
|
|
//开始加热 |
|
|
|
private boolean startHeating(JSONObject params) { |
|
|
|
String heaterId = params.get("heaterId").toString(); |
|
|
|
Double temperature = params.getDouble("temperature"); |
|
|
|
return deviceStepService.startHeating(heaterId, temperature); |
|
|
|
} |
|
|
|
|
|
|
|
//停止加热 |
|
|
|
private boolean stopHeating(JSONObject params) { |
|
|
|
String heaterId = params.get("heaterId").toString(); |
|
|
|
return deviceStepService.stopHeating(heaterId); |
|
|
|
} |
|
|
|
|
|
|
|
//拍照 |
|
|
|
private boolean takePhoto() { |
|
|
|
return deviceStepService.takePhoto(); |
|
|
|
} |
|
|
|
|
|
|
|
//移至异常 |
|
|
|
private boolean moveToExc() { |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
//移除异常 |
|
|
|
private boolean moveOutToExc() { |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 等待 |
|
|
|
*/ |
|
|
|
private boolean delay(JSONObject params) { |
|
|
|
Integer second = params.getInt("second"); |
|
|
|
return deviceStepService.delay(second); |
|
|
|
} |
|
|
|
|
|
|
|
} |