package com.zy.core.utils.station;
|
|
import com.alibaba.fastjson.JSON;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
import com.zy.asrs.domain.enums.NotifyMsgType;
|
import com.zy.asrs.entity.BasCrnp;
|
import com.zy.asrs.entity.BasDevp;
|
import com.zy.asrs.entity.BasStation;
|
import com.zy.asrs.entity.WrkMast;
|
import com.zy.asrs.service.*;
|
import com.zy.asrs.utils.NotifyUtils;
|
import com.zy.common.entity.FindCrnNoResult;
|
import com.zy.common.service.CommonService;
|
import com.zy.common.utils.RedisUtil;
|
import com.zy.core.News;
|
import com.zy.core.cache.SlaveConnection;
|
import com.zy.core.dispatch.StationCommandDispatchResult;
|
import com.zy.core.dispatch.StationCommandDispatcher;
|
import com.zy.core.enums.RedisKeyType;
|
import com.zy.core.enums.SlaveType;
|
import com.zy.core.enums.StationCommandType;
|
import com.zy.core.enums.WrkIoType;
|
import com.zy.core.enums.WrkStsType;
|
import com.zy.core.model.StationObjModel;
|
import com.zy.core.model.command.StationCommand;
|
import com.zy.core.model.protocol.StationProtocol;
|
import com.zy.core.model.protocol.StationTaskBufferItem;
|
import com.zy.core.move.StationMoveCoordinator;
|
import com.zy.core.thread.StationThread;
|
import com.zy.core.utils.station.model.DispatchLimitConfig;
|
import com.zy.core.utils.station.model.LoadGuardState;
|
import com.zy.core.utils.station.model.LoopHitResult;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Objects;
|
|
@Component
|
public class StationRegularDispatchProcessor {
|
|
@Autowired
|
private BasDevpService basDevpService;
|
@Autowired
|
private WrkMastService wrkMastService;
|
@Autowired
|
private CommonService commonService;
|
@Autowired
|
private RedisUtil redisUtil;
|
@Autowired
|
private WrkAnalysisService wrkAnalysisService;
|
@Autowired
|
private BasStationService basStationService;
|
@Autowired
|
private NotifyUtils notifyUtils;
|
@Autowired
|
private StationMoveCoordinator stationMoveCoordinator;
|
@Autowired(required = false)
|
private StationCommandDispatcher stationCommandDispatcher;
|
@Autowired
|
private StationDispatchLoadSupport stationDispatchLoadSupport;
|
@Autowired
|
private BasCrnpService basCrnpService;
|
|
public void stationOutExecuteFinish(StationObjModel stationObjModel) {
|
try {
|
if (stationObjModel == null) {
|
return;
|
}
|
Integer stationId = stationObjModel.getStationId();
|
if (stationId == null) {
|
return;
|
}
|
|
StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, stationObjModel.getDeviceNo());
|
if (stationThread == null) {
|
return;
|
}
|
Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
|
StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(stationId);
|
WrkMast wrkMast = findCurrentStationRunTask(stationProtocol);
|
boolean matchedByRecentArrival = false;
|
if (wrkMast == null) {
|
wrkMast = findRecentArrivalStationRunTask(stationThread, stationId);
|
matchedByRecentArrival = wrkMast != null;
|
}
|
if (wrkMast == null) {
|
return;
|
}
|
|
completeOutboundStationRun(stationObjModel, stationThread, wrkMast, matchedByRecentArrival);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
public void checkTaskToComplete(WrkMast wrkMast) {
|
try {
|
if (wrkMast == null || wrkMast.getWrkNo() == null || wrkMast.getStaNo() == null) {
|
return;
|
}
|
Integer wrkNo = wrkMast.getWrkNo();
|
Integer targetStaNo = wrkMast.getStaNo();
|
|
Object lock = redisUtil.get(RedisKeyType.STATION_OUT_EXECUTE_COMPLETE_LIMIT.key + wrkNo);
|
if (lock != null) {
|
return;
|
}
|
|
BasStation basStation = basStationService.getOne(new QueryWrapper<BasStation>().eq("station_id", targetStaNo));
|
if (basStation == null) {
|
return;
|
}
|
|
StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basStation.getDeviceNo());
|
if (stationThread == null) {
|
return;
|
}
|
|
Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
|
StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(basStation.getStationId());
|
if (stationProtocol == null) {
|
return;
|
}
|
|
if (!Objects.equals(stationProtocol.getTaskNo(), wrkNo)) {
|
if (stationMoveCoordinator != null) {
|
stationMoveCoordinator.finishSession(wrkNo);
|
}
|
wrkMast.setWrkSts(WrkStsType.COMPLETE_OUTBOUND.sts);
|
wrkMast.setIoTime(new Date());
|
wrkMastService.updateById(wrkMast);
|
clearOutboundDispatchCache(wrkMast);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
private void clearOutboundDispatchCache(WrkMast wrkMast) {
|
if (wrkMast == null || wrkMast.getWrkNo() == null) {
|
return;
|
}
|
redisUtil.del(RedisKeyType.DUAL_CRN_OUT_TASK_STATION_INFO.key + wrkMast.getWrkNo());
|
}
|
|
private WrkMast findCurrentStationRunTask(StationProtocol stationProtocol) {
|
if (stationProtocol == null || stationProtocol.getTaskNo() <= 0) {
|
return null;
|
}
|
WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
|
if (wrkMast == null || !Objects.equals(wrkMast.getWrkSts(), WrkStsType.STATION_RUN.sts)) {
|
return null;
|
}
|
return wrkMast;
|
}
|
|
private WrkMast findRecentArrivalStationRunTask(StationThread stationThread, Integer stationId) {
|
if (stationThread == null || stationId == null || stationId <= 0) {
|
return null;
|
}
|
List<WrkMast> stationRunTasks = wrkMastService.list(new QueryWrapper<WrkMast>()
|
.eq("io_type", WrkIoType.OUT.id)
|
.eq("sta_no", stationId)
|
.eq("wrk_sts", WrkStsType.STATION_RUN.sts)
|
.orderByAsc("io_time", "wrk_no"));
|
for (WrkMast candidate : stationRunTasks) {
|
Integer wrkNo = candidate == null ? null : candidate.getWrkNo();
|
if (wrkNo == null || wrkNo <= 0) {
|
continue;
|
}
|
if (stationThread.hasRecentArrival(stationId, wrkNo)) {
|
return candidate;
|
}
|
}
|
return null;
|
}
|
|
private void completeOutboundStationRun(StationObjModel stationObjModel,
|
StationThread stationThread,
|
WrkMast wrkMast,
|
boolean matchedByRecentArrival) {
|
if (stationObjModel == null || stationThread == null || wrkMast == null || wrkMast.getWrkNo() == null) {
|
return;
|
}
|
Date now = new Date();
|
boolean updated = wrkMastService.update(null, new UpdateWrapper<WrkMast>()
|
.set("wrk_sts", WrkStsType.STATION_RUN_COMPLETE.sts)
|
.set("io_time", now)
|
.set("modi_time", now)
|
.eq("wrk_no", wrkMast.getWrkNo())
|
.eq("wrk_sts", WrkStsType.STATION_RUN.sts));
|
if (!updated) {
|
return;
|
}
|
|
wrkMast.setWrkSts(WrkStsType.STATION_RUN_COMPLETE.sts);
|
wrkMast.setIoTime(now);
|
wrkMast.setModiTime(now);
|
if (stationMoveCoordinator != null) {
|
stationMoveCoordinator.finishSession(wrkMast.getWrkNo());
|
}
|
wrkAnalysisService.markOutboundStationComplete(wrkMast, now);
|
notifyUtils.notify(String.valueOf(SlaveType.Devp), stationObjModel.getDeviceNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.STATION_OUT_TASK_RUN_COMPLETE, null);
|
redisUtil.set(RedisKeyType.STATION_OUT_EXECUTE_COMPLETE_LIMIT.key + wrkMast.getWrkNo(), "lock", 60);
|
clearOutboundDispatchCache(wrkMast);
|
attemptClearTaskPath(stationThread, wrkMast.getWrkNo());
|
if (matchedByRecentArrival) {
|
News.info("输送站出库完成补偿命中最近到站缓存,目标站={},工作号={}", stationObjModel.getStationId(), wrkMast.getWrkNo());
|
}
|
}
|
|
public void attemptClearTaskPath(StationThread stationThread, Integer taskNo) {
|
if (stationThread == null || taskNo == null || taskNo <= 0) {
|
return;
|
}
|
try {
|
boolean cleared = stationThread.clearPath(taskNo);
|
if (cleared) {
|
News.info("输送站点任务运行完成后清理残留路径,工作号={}", taskNo);
|
}
|
} catch (Exception e) {
|
News.error("输送站点任务运行完成后清理残留路径异常,工作号={}", taskNo, e);
|
}
|
}
|
|
private boolean offerDevpCommandWithDedup(Integer deviceNo, StationCommand command, String scene) {
|
StationCommandDispatchResult dispatchResult = stationCommandDispatcher.dispatch(deviceNo, command, "station-operate-process", scene);
|
return dispatchResult.isAccepted();
|
}
|
|
public void stationInExecute(BasDevp basDevp, StationObjModel entity) {
|
if (basDevp == null || basDevp.getDevpNo() == null || entity == null || entity.getStationId() == null) {
|
return;
|
}
|
|
Integer stationId = entity.getStationId();
|
StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
|
if (stationThread == null) {
|
return;
|
}
|
|
Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
|
if (stationMap == null || !stationMap.containsKey(stationId)) {
|
return;
|
}
|
|
StationProtocol stationProtocol = stationMap.get(stationId);
|
if (stationProtocol == null) {
|
return;
|
}
|
|
Object lock = redisUtil.get(RedisKeyType.STATION_IN_EXECUTE_LIMIT.key + stationId);
|
if (lock != null) {
|
return;
|
}
|
|
if (!stationProtocol.isAutoing()
|
|| !stationProtocol.isLoading()
|
|| stationProtocol.getTaskNo() <= 0
|
|| !stationProtocol.isInEnable()
|
) {
|
return;
|
}
|
|
WrkMast wrkMast = wrkMastService.getOne(new QueryWrapper<WrkMast>().eq("barcode", stationProtocol.getBarcode()));
|
if (wrkMast == null || !Objects.equals(wrkMast.getWrkSts(), WrkStsType.NEW_INBOUND.sts)) {
|
return;
|
}
|
|
Integer crnNo = wrkMast.getCrnNo();
|
BasCrnp basCrnp = basCrnpService.getOne(new QueryWrapper<BasCrnp>().eq("crn_no", crnNo));
|
if (basCrnp == null) {
|
News.taskInfo(wrkMast.getWrkNo(), "{}工作,未找到堆垛机数据", wrkMast.getWrkNo());
|
return;
|
}
|
|
int maxInTask = 3;
|
if (basCrnp.getMaxInTask() != null) {
|
maxInTask = basCrnp.getMaxInTask();
|
}
|
|
long count = wrkMastService.count(new QueryWrapper<WrkMast>().eq("crn_no", crnNo).eq("wrk_sts", WrkStsType.INBOUND_STATION_RUN.sts));
|
if(count >= maxInTask) {
|
News.taskInfo(wrkMast.getWrkNo(), "{}工作,堆垛机到达任务上限,稍后执行", wrkMast.getWrkNo());
|
stationProtocol.setSystemWarning(wrkMast.getWrkNo() + "工作," + crnNo + "号堆垛机到达任务上限,稍后执行");
|
return;
|
}
|
|
String locNo = wrkMast.getLocNo();
|
FindCrnNoResult findCrnNoResult = commonService.findCrnNoByLocNo(locNo);
|
if (findCrnNoResult == null) {
|
News.taskInfo(wrkMast.getWrkNo(), "{}工作,未匹配到堆垛机", wrkMast.getWrkNo());
|
return;
|
}
|
|
Integer targetStationId = commonService.findInStationId(findCrnNoResult, stationId);
|
if (targetStationId == null) {
|
News.taskInfo(wrkMast.getWrkNo(), "{}站点,搜索入库站点失败", stationId);
|
return;
|
}
|
|
DispatchLimitConfig limitConfig = stationDispatchLoadSupport.getDispatchLimitConfig(stationProtocol.getStationId(), targetStationId);
|
int currentStationTaskCount = stationDispatchLoadSupport.countCurrentStationTask();
|
LoadGuardState loadGuardState = stationDispatchLoadSupport.buildLoadGuardState(limitConfig);
|
LoopHitResult loopHitResult = stationDispatchLoadSupport.findPathLoopHit(
|
limitConfig,
|
stationProtocol.getStationId(),
|
targetStationId,
|
loadGuardState
|
);
|
if (stationDispatchLoadSupport.isDispatchBlocked(limitConfig, currentStationTaskCount, loadGuardState, loopHitResult.isThroughLoop())) {
|
return;
|
}
|
|
StationCommand command = stationThread.getCommand(StationCommandType.MOVE, wrkMast.getWrkNo(), stationId, targetStationId, 0);
|
if (command == null) {
|
News.taskInfo(wrkMast.getWrkNo(), "{}工作,获取输送线命令失败", wrkMast.getWrkNo());
|
return;
|
}
|
|
Date now = new Date();
|
wrkMast.setWrkSts(WrkStsType.INBOUND_STATION_RUN.sts);
|
wrkMast.setSourceStaNo(stationProtocol.getStationId());
|
wrkMast.setStaNo(targetStationId);
|
wrkMast.setSystemMsg("");
|
wrkMast.setIoTime(now);
|
wrkMast.setModiTime(now);
|
if (wrkMastService.updateById(wrkMast)) {
|
wrkAnalysisService.markInboundStationStart(wrkMast, now);
|
boolean offered = offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "stationInExecute");
|
if (offered && stationMoveCoordinator != null) {
|
stationMoveCoordinator.recordDispatch(
|
wrkMast.getWrkNo(),
|
stationProtocol.getStationId(),
|
"stationInExecute",
|
command,
|
false
|
);
|
}
|
News.info("输送站点入库命令下发成功,站点号={},工作号={},命令数据={}", stationId, wrkMast.getWrkNo(), JSON.toJSONString(command));
|
redisUtil.set(RedisKeyType.STATION_IN_EXECUTE_LIMIT.key + stationId, "lock", 5);
|
loadGuardState.reserveLoopTask(loopHitResult.getLoopNo());
|
stationDispatchLoadSupport.saveLoopLoadReserve(wrkMast.getWrkNo(), loopHitResult);
|
}
|
}
|
}
|