package com.zy.acs.manager.core.scheduler;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.zy.acs.common.enums.AgvStatusType;
|
import com.zy.acs.common.utils.RedisSupport;
|
import com.zy.acs.framework.common.DateUtils;
|
import com.zy.acs.manager.core.service.MainLockWrapService;
|
import com.zy.acs.manager.core.service.MainService;
|
import com.zy.acs.manager.manager.entity.*;
|
import com.zy.acs.manager.manager.enums.*;
|
import com.zy.acs.manager.manager.service.*;
|
import com.zy.acs.manager.system.service.ConfigService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Component;
|
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Optional;
|
|
/**
|
* judge whether agv go to funcSta which be charging or standby
|
* Created by vincent on 2023/9/9
|
*/
|
@Slf4j
|
@Component
|
public class MaintainScheduler {
|
|
private final RedisSupport redis = RedisSupport.defaultRedisSupport;
|
|
@Autowired
|
private AgvService agvService;
|
@Autowired
|
private AgvDetailService agvDetailService;
|
@Autowired
|
private FuncStaService funcStaService;
|
@Autowired
|
private MainService mainService;
|
@Autowired
|
private MainLockWrapService mainLockWrapService;
|
@Autowired
|
private TaskService taskService;
|
@Autowired
|
private ConfigService configService;
|
@Autowired
|
private AgvModelService agvModelService;
|
@Autowired
|
private SegmentService segmentService;
|
|
@Scheduled(cron = "0/5 * * * * ? ")
|
private synchronized void autoCharge(){
|
List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, StatusType.ENABLE.val));
|
for (Agv agv : agvList) {
|
AgvDetail agvDetail = agvDetailService.selectByAgvId(agv.getId());
|
AgvModel agvModel = agvModelService.getByAgvId(agv.getId());
|
if (null == agvDetail || null == agvDetail.getVol() || null == agvDetail.getAgvStatus()) {
|
continue;
|
}
|
if (agvDetail.getAgvStatus().equals(AgvStatusType.CHARGE)) {
|
continue;
|
}
|
if (agvDetailService.isPowerLoss(agv, agvDetail, agvModel)) {
|
if (0 < taskService.count(new LambdaQueryWrapper<Task>()
|
.eq(Task::getAgvId, agv.getId())
|
.and(i -> {
|
i.eq(Task::getTaskSts, TaskStsType.WAITING.val())
|
.or().eq(Task::getTaskSts, TaskStsType.ASSIGN.val())
|
.or().eq(Task::getTaskSts, TaskStsType.PROGRESS.val());
|
})
|
)) {
|
continue;
|
}
|
if (0 < segmentService.count(new LambdaQueryWrapper<Segment>()
|
.eq(Segment::getAgvId, agv.getId())
|
.and( i -> {
|
// i.eq(Segment::getState, SegmentStateType.WAITING.toString()).or()
|
i.eq(Segment::getState, SegmentStateType.RUNNING.toString());
|
})
|
)) {
|
continue;
|
}
|
|
mainLockWrapService.buildMinorTask(agv.getId(), TaskTypeType.TO_CHARGE, null, null);
|
}
|
}
|
}
|
|
|
@Scheduled(cron = "0/1 * * * * ? ")
|
// @Scheduled(cron = "0 */2 * * * ? ")
|
private synchronized void autoStandby(){
|
if (!configService.getVal("automaticStandbyPosition", Boolean.class)) { return; }
|
|
List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, StatusType.ENABLE.val));
|
for (Agv agv : agvList) {
|
AgvDetail agvDetail = agvDetailService.selectByAgvId(agv.getId());
|
AgvModel agvModel = agvModelService.getByAgvId(agv.getId());
|
if (null == agvDetail || null == agvDetail.getVol() || null == agvDetail.getAgvStatus() || null == agvDetail.getRecentCode()) {
|
continue;
|
}
|
// low battery status, that need to go to charge
|
if (agvDetailService.isPowerLoss(agv, agvDetail, agvModel)) {
|
continue;
|
}
|
// is charging ?
|
if (agvDetail.getAgvStatus().equals(AgvStatusType.CHARGE)) {
|
if (agvDetail.getVol() < agvModel.getQuaBattery()) {
|
continue;
|
}
|
}
|
// already on standby code ?
|
if (0 < funcStaService.count(new LambdaQueryWrapper<FuncSta>()
|
.eq(FuncSta::getType, FuncStaType.STANDBY.toString())
|
.eq(FuncSta::getCode, agvDetail.getRecentCode())
|
)) {
|
continue;
|
}
|
// has running tasks ?
|
if (0 < taskService.count(new LambdaQueryWrapper<Task>()
|
.eq(Task::getAgvId, agv.getId())
|
.and(i -> {
|
i.eq(Task::getTaskSts, TaskStsType.WAITING.val())
|
.or().eq(Task::getTaskSts, TaskStsType.ASSIGN.val())
|
.or().eq(Task::getTaskSts, TaskStsType.PROGRESS.val());
|
})
|
)) {
|
continue;
|
}
|
// the time between the latest task and now that be must more that @{param} seconds
|
// if (!Optional.ofNullable((Boolean) redis.getObject(RedisConstant.AGV_TO_STANDBY_FLAG, agv.getUuid())).orElse(false)) {
|
Integer intervalOfAutoStandby = configService.getVal("intervalOfAutoStandby", Integer.class);
|
if (null != intervalOfAutoStandby && intervalOfAutoStandby > 0) {
|
Task latestTask = taskService.findLatestTask(agv.getId(), null);
|
if (null != latestTask) {
|
long seconds = DateUtils.diffToSeconds(
|
Optional.ofNullable(latestTask.getEndTime()).orElse(latestTask.getUpdateTime())
|
, new Date()
|
);
|
if (seconds < intervalOfAutoStandby) { continue; }
|
}
|
} else {
|
continue;
|
}
|
// }
|
|
mainLockWrapService.buildMinorTask(agv.getId(), TaskTypeType.TO_STANDBY, null, null);
|
}
|
}
|
|
@Scheduled(cron = "0/5 * * * * ? ")
|
private synchronized void releaseFuncSta(){
|
List<FuncSta> funcStaList = funcStaService.list(new LambdaQueryWrapper<FuncSta>().eq(FuncSta::getState, FuncStaStateType.OCCUPIED.toString()));
|
for (FuncSta funcSta : funcStaList) {
|
boolean beIdle = funcStaService.isCanBeIdle(funcSta);
|
if (beIdle) {
|
funcSta.setState(FuncStaStateType.IDLE.toString());
|
funcSta.setUpdateTime(new Date());
|
if (!funcStaService.updateById(funcSta)) {
|
log.error("FuncSta [{}] failed to be idle !!!", funcSta.getName());
|
}
|
}
|
}
|
}
|
|
}
|