Browse Source

feat:工艺增加监控接口

tags/freeze
白凤吉 3 months ago
parent
commit
664764292d
  1. 32
      src/main/java/com/iflytop/gd/app/controller/CraftsController.java
  2. 3
      src/main/java/com/iflytop/gd/app/model/dto/StartCraftsDTO.java
  3. 27
      src/main/java/com/iflytop/gd/app/model/vo/CraftStatusVO.java
  4. 38
      src/main/java/com/iflytop/gd/app/service/CraftsService.java

32
src/main/java/com/iflytop/gd/app/controller/CraftsController.java

@ -5,7 +5,9 @@ import com.iflytop.gd.app.model.dto.ResumeCraftsDto;
import com.iflytop.gd.app.model.dto.StartCraftsDTO;
import com.iflytop.gd.app.model.dto.StopCraftsDto;
import com.iflytop.gd.app.model.entity.Crafts;
import com.iflytop.gd.app.model.vo.CraftStatusVO;
import com.iflytop.gd.app.service.CraftsService;
import com.iflytop.gd.app.model.bo.CraftsStep;
import com.iflytop.gd.common.result.Result;
import com.iflytop.gd.common.result.ResultCode;
import io.swagger.v3.oas.annotations.Operation;
@ -20,7 +22,7 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@Tag(name = "工艺管理")
@RestController
@ -33,9 +35,10 @@ public class CraftsController {
@Operation(summary = "根据矿石id获取工艺列表")
@GetMapping("/list/{oresId}")
public Result<List<Crafts>> getAllCrafts(@NotNull(message = "矿石ID 不能为空")
@Min(value = 1, message = "矿石ID 必须大于等于 1")
@Parameter(description = "矿石ID") @PathVariable Long oresId) {
public Result<List<Crafts>> getAllCrafts(
@NotNull(message = "矿石ID 不能为空")
@Min(value = 1, message = "矿石ID 必须大于等于 1")
@Parameter(description = "矿石ID") @PathVariable Long oresId) {
List<Crafts> craftList = craftsService.selectAllByOresId(oresId);
return Result.success(craftList);
}
@ -67,7 +70,8 @@ public class CraftsController {
@Operation(summary = "删除工艺")
@DeleteMapping("/{ids}")
public Result<String> deleteCrafts(@Parameter(description = "工艺ID,多个以英文逗号(,)分割") @PathVariable String ids) {
public Result<String> deleteCrafts(
@Parameter(description = "工艺ID,多个以英文逗号(,)分割") @PathVariable String ids) {
boolean isSuccess = craftsService.deleteCrafts(ids);
if (isSuccess) {
return Result.success();
@ -108,4 +112,22 @@ public class CraftsController {
}
return Result.failed();
}
@Operation(summary = "获取某个加热区工艺状态")
@GetMapping("/status/{heatId}")
public Result<CraftStatusVO> getStatus(
@NotNull @PathVariable String heatId) {
CraftStatusVO vo = craftsService.getStatus(heatId);
if (vo == null) {
return Result.failed(ResultCode.NOT_FOUND, "未找到执行任务");
}
return Result.success(vo);
}
@Operation(summary = "获取所有加热区工艺状态列表")
@GetMapping("/status")
public Result<List<CraftStatusVO>> getAllStatuses() {
List<CraftStatusVO> list = craftsService.getAllStatuses();
return Result.success(list);
}
}

3
src/main/java/com/iflytop/gd/app/model/dto/StartCraftsDTO.java

@ -2,12 +2,13 @@ package com.iflytop.gd.app.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Positive;
import lombok.Data;
@Schema(description = "开始工艺")
@Data
public class StartCraftsDTO {
@NotBlank
@Positive(message = "工艺ID 必须是正数")
@Schema(description = "工艺id")
private Long craftId;
@NotBlank

27
src/main/java/com/iflytop/gd/app/model/vo/CraftStatusVO.java

@ -0,0 +1,27 @@
package com.iflytop.gd.app.model.vo;
import com.iflytop.gd.app.common.enums.CraftStates;
import com.iflytop.gd.app.model.bo.CraftsStep;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.List;
/**
* 工艺执行状态视图对象
*/
@Data
public class CraftStatusVO {
@Schema(description = "加热区 ID")
private String heatId;
@Schema(description = "当前状态")
private CraftStates state;
@Schema(description = "当前执行到的步骤索引")
private int currentIndex;
@Schema(description = "工艺所有步骤")
private List<CraftsStep> steps;
}

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

@ -7,6 +7,7 @@ import com.iflytop.gd.app.common.enums.CraftStates;
import com.iflytop.gd.app.core.CraftsContext;
import com.iflytop.gd.app.mapper.CraftsMapper;
import com.iflytop.gd.app.model.entity.Crafts;
import com.iflytop.gd.app.model.vo.CraftStatusVO;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.statemachine.config.StateMachineFactory;
@ -27,7 +28,6 @@ public class CraftsService extends ServiceImpl<CraftsMapper, Crafts> {
private final WebSocketService webSocketService;
private ExecutorService executor;
private final ConcurrentHashMap<String, CraftsContext> contextMap = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Future<?>> futureMap = new ConcurrentHashMap<>();
@ -40,7 +40,7 @@ public class CraftsService extends ServiceImpl<CraftsMapper, Crafts> {
* 开始执行工艺
*/
public synchronized boolean startCrafts(Long craftId, String heatId) {
if (futureMap.containsKey(heatId)) { // 已有任务在执行不重复启动
if (futureMap.containsKey(heatId)) {
return false;
}
Crafts craft = this.getById(craftId);
@ -97,7 +97,37 @@ public class CraftsService extends ServiceImpl<CraftsMapper, Crafts> {
return false;
}
// TODO: 如需提供查询当前执行状态可添加 getStatus(heatId) 方法返回 ctx.getCurrentIndex(), ctx.getSm().getState().getId(), ctx.getRemainingSteps()
/**
* 查询指定 heatId 的执行状态
*/
public synchronized CraftStatusVO getStatus(String heatId) {
CraftsContext ctx = contextMap.get(heatId);
if (ctx == null) {
return null;
}
CraftStatusVO vo = new CraftStatusVO();
vo.setHeatId(heatId);
vo.setState(ctx.getSm().getState().getId());
vo.setCurrentIndex(ctx.getCurrentIndex());
vo.setSteps(ctx.getCraftsStepList());
return vo;
}
/**
* 查询所有正在执行的工艺状态
*/
public synchronized List<CraftStatusVO> getAllStatuses() {
return contextMap.entrySet().stream().map(entry -> {
String heatId = entry.getKey();
CraftsContext ctx = entry.getValue();
CraftStatusVO vo = new CraftStatusVO();
vo.setHeatId(heatId);
vo.setState(ctx.getSm().getState().getId());
vo.setCurrentIndex(ctx.getCurrentIndex());
vo.setSteps(ctx.getCraftsStepList());
return vo;
}).collect(Collectors.toList());
}
public List<Crafts> selectAllByOresId(Long oresId) {
return this.baseMapper.selectAllByOresId(oresId);
@ -121,4 +151,4 @@ public class CraftsService extends ServiceImpl<CraftsMapper, Crafts> {
.collect(Collectors.toList());
return this.removeByIds(ids);
}
}
}
Loading…
Cancel
Save