| | |
| | | STATION_RUN_BLOCK_TASK_LOOP_STATE_("station_run_block_task_loop_state_"), |
| | | CHECK_STATION_IDLE_RECOVER_LIMIT_("check_station_idle_recover_limit_"), |
| | | STATION_COMMAND_DISPATCH_DEDUP_("station_command_dispatch_dedup_"), |
| | | STATION_MOVE_SESSION_("station_move_session_"), |
| | | CHECK_SHALLOW_LOC_STATUS_LIMIT("check_shallow_loc_status_limit_"), |
| | | GENERATE_ENABLE_IN_STATION_DATA_LIMIT("generate_enable_in_station_data_limit_"), |
| | | GENERATE_STATION_BACK_LIMIT("generate_station_back_limit_"), |
| | |
| | | |
| | | private Integer traceVersion; |
| | | |
| | | // 同一 task 的 MOVE 路由版本,用于控制重下发和旧路径失效 |
| | | private Integer routeVersion; |
| | | |
| | | } |
| New file |
| | |
| | | package com.zy.core.move; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.serializer.SerializerFeature; |
| | | import com.zy.common.utils.RedisUtil; |
| | | import com.zy.core.enums.RedisKeyType; |
| | | import com.zy.core.model.command.StationCommand; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.security.MessageDigest; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | |
| | | @Component |
| | | public class StationMoveCoordinator { |
| | | |
| | | private static final int SESSION_EXPIRE_SECONDS = 60 * 60 * 24; |
| | | |
| | | @Autowired |
| | | private StationMoveSessionRegistry sessionRegistry; |
| | | @Autowired |
| | | private RedisUtil redisUtil; |
| | | |
| | | public StationMoveSession loadSession(Integer taskNo) { |
| | | if (sessionRegistry == null) { |
| | | return null; |
| | | } |
| | | return sessionRegistry.load(taskNo); |
| | | } |
| | | |
| | | public boolean isActiveRoute(Integer taskNo, Integer routeVersion) { |
| | | return sessionRegistry != null && sessionRegistry.isActiveRoute(taskNo, routeVersion); |
| | | } |
| | | |
| | | public void markSegmentIssued(Integer taskNo, Integer routeVersion) { |
| | | if (sessionRegistry != null) { |
| | | sessionRegistry.markSegmentIssued(taskNo, routeVersion); |
| | | } |
| | | } |
| | | |
| | | public void updateCurrentStation(Integer taskNo, Integer routeVersion, Integer currentStationId) { |
| | | if (sessionRegistry != null) { |
| | | sessionRegistry.updateCurrentStation(taskNo, routeVersion, currentStationId); |
| | | } |
| | | } |
| | | |
| | | public void markCancelled(Integer taskNo, Integer routeVersion, Integer currentStationId, String cancelReason) { |
| | | updateTerminal(taskNo, routeVersion, currentStationId, StationMoveSession.STATUS_CANCELLED, cancelReason); |
| | | } |
| | | |
| | | public void markBlocked(Integer taskNo, Integer routeVersion, Integer currentStationId) { |
| | | updateTerminal(taskNo, routeVersion, currentStationId, StationMoveSession.STATUS_BLOCKED, null); |
| | | } |
| | | |
| | | public void markTimeout(Integer taskNo, Integer routeVersion, Integer currentStationId) { |
| | | updateTerminal(taskNo, routeVersion, currentStationId, StationMoveSession.STATUS_TIMEOUT, null); |
| | | } |
| | | |
| | | public void markFinished(Integer taskNo, Integer routeVersion, Integer currentStationId) { |
| | | updateTerminal(taskNo, routeVersion, currentStationId, StationMoveSession.STATUS_FINISHED, null); |
| | | } |
| | | |
| | | public void markCancelPending(Integer taskNo, String cancelReason) { |
| | | StationMoveSession session = loadSession(taskNo); |
| | | if (session == null || !session.isActive()) { |
| | | return; |
| | | } |
| | | session.setStatus(StationMoveSession.STATUS_CANCEL_PENDING); |
| | | session.setCancelReason(cancelReason); |
| | | saveSession(session); |
| | | } |
| | | |
| | | public boolean shouldSuppressDispatch(Integer taskNo, Integer currentStationId, StationCommand candidateCommand) { |
| | | if (taskNo == null || taskNo <= 0 || currentStationId == null || candidateCommand == null) { |
| | | return false; |
| | | } |
| | | |
| | | StationMoveSession session = loadSession(taskNo); |
| | | if (session == null || !session.isActive()) { |
| | | return false; |
| | | } |
| | | |
| | | String candidateSignature = buildPathSignature(candidateCommand); |
| | | if (!isBlank(candidateSignature) && Objects.equals(candidateSignature, session.getPathSignature())) { |
| | | return true; |
| | | } |
| | | |
| | | if (Objects.equals(currentStationId, session.getNextDecisionStationId())) { |
| | | return false; |
| | | } |
| | | |
| | | return session.containsStation(currentStationId); |
| | | } |
| | | |
| | | public StationMoveSession recordDispatch(Integer taskNo, |
| | | Integer dispatchStationId, |
| | | String triggerName, |
| | | StationCommand command, |
| | | boolean circleRoute) { |
| | | if (taskNo == null || taskNo <= 0 || command == null) { |
| | | return null; |
| | | } |
| | | |
| | | StationMoveSession current = loadSession(taskNo); |
| | | long now = System.currentTimeMillis(); |
| | | String pathSignature = buildPathSignature(command); |
| | | List<Integer> fullPathStationIds = resolveFullPathStationIds(command); |
| | | boolean reuseCurrent = current != null |
| | | && current.isActive() |
| | | && Objects.equals(current.getDispatchStationId(), dispatchStationId) |
| | | && Objects.equals(current.getNextDecisionStationId(), command.getTargetStaNo()) |
| | | && Objects.equals(current.getPathSignature(), pathSignature); |
| | | |
| | | StationMoveSession session = reuseCurrent ? current : new StationMoveSession(); |
| | | if (!reuseCurrent) { |
| | | session.setRouteVersion(current == null || current.getRouteVersion() == null |
| | | ? 1 |
| | | : current.getRouteVersion() + 1); |
| | | session.setCreatedAt(now); |
| | | } else if (session.getRouteVersion() == null) { |
| | | session.setRouteVersion(1); |
| | | } |
| | | |
| | | session.setTaskNo(taskNo); |
| | | session.setThreadImpl(resolveThreadImpl(triggerName)); |
| | | session.setCurrentStationId(dispatchStationId); |
| | | session.setBusinessTargetStationId(command.getTargetStaNo()); |
| | | session.setCurrentRouteTargetStationId(command.getTargetStaNo()); |
| | | session.setTriggerType(resolveTriggerType(triggerName, circleRoute)); |
| | | session.setDispatchMode(resolveDispatchMode(triggerName, circleRoute)); |
| | | session.setStatus(StationMoveSession.STATUS_RUNNING); |
| | | session.setDispatchStationId(dispatchStationId); |
| | | session.setNextDecisionStationId(command.getTargetStaNo()); |
| | | session.setFullPathStationIds(fullPathStationIds); |
| | | session.setPathSignature(pathSignature); |
| | | session.setCancelReason(null); |
| | | session.setUpdatedAt(now); |
| | | session.setLastIssuedAt(now); |
| | | |
| | | command.setRouteVersion(session.getRouteVersion()); |
| | | saveSession(session); |
| | | |
| | | if (circleRoute) { |
| | | saveLegacyCircleCommand(taskNo, command); |
| | | } else { |
| | | clearLegacyCircleCommand(taskNo); |
| | | } |
| | | |
| | | return session; |
| | | } |
| | | |
| | | public StationMoveSession cancelSession(Integer taskNo) { |
| | | StationMoveSession session = loadSession(taskNo); |
| | | if (session == null) { |
| | | clearLegacyCircleCommand(taskNo); |
| | | return null; |
| | | } |
| | | session.setStatus(StationMoveSession.STATUS_CANCELLED); |
| | | session.setCancelReason("reroute_cancelled"); |
| | | saveSession(session); |
| | | clearLegacyCircleCommand(taskNo); |
| | | return session; |
| | | } |
| | | |
| | | public StationMoveSession finishSession(Integer taskNo) { |
| | | StationMoveSession session = loadSession(taskNo); |
| | | if (session == null) { |
| | | clearLegacyCircleCommand(taskNo); |
| | | return null; |
| | | } |
| | | session.setStatus(StationMoveSession.STATUS_FINISHED); |
| | | saveSession(session); |
| | | clearLegacyCircleCommand(taskNo); |
| | | return session; |
| | | } |
| | | |
| | | public String buildPathSignature(StationCommand command) { |
| | | if (command == null) { |
| | | return ""; |
| | | } |
| | | Map<String, Object> signature = new LinkedHashMap<>(); |
| | | signature.put("commandType", command.getCommandType() == null ? null : command.getCommandType().name()); |
| | | signature.put("stationId", command.getStationId()); |
| | | signature.put("targetStaNo", command.getTargetStaNo()); |
| | | signature.put("navigatePath", copyIntegerList(command.getNavigatePath())); |
| | | signature.put("liftTransferPath", copyIntegerList(command.getLiftTransferPath())); |
| | | signature.put("originalNavigatePath", copyIntegerList(command.getOriginalNavigatePath())); |
| | | return JSON.toJSONString(signature, SerializerFeature.DisableCircularReferenceDetect); |
| | | } |
| | | |
| | | public String buildPathSignatureHash(StationCommand command) { |
| | | String signature = buildPathSignature(command); |
| | | if (isBlank(signature)) { |
| | | return ""; |
| | | } |
| | | return digest(signature); |
| | | } |
| | | |
| | | private void updateTerminal(Integer taskNo, |
| | | Integer routeVersion, |
| | | Integer currentStationId, |
| | | String status, |
| | | String cancelReason) { |
| | | if (sessionRegistry == null) { |
| | | return; |
| | | } |
| | | StationMoveSession session = sessionRegistry.load(taskNo); |
| | | if (session == null || !Objects.equals(session.getRouteVersion(), routeVersion)) { |
| | | return; |
| | | } |
| | | session.setCurrentStationId(currentStationId); |
| | | session.setStatus(status); |
| | | session.setCancelReason(cancelReason); |
| | | saveSession(session); |
| | | if (StationMoveSession.STATUS_CANCELLED.equals(status) |
| | | || StationMoveSession.STATUS_BLOCKED.equals(status) |
| | | || StationMoveSession.STATUS_TIMEOUT.equals(status) |
| | | || StationMoveSession.STATUS_FINISHED.equals(status)) { |
| | | clearLegacyCircleCommand(taskNo); |
| | | } |
| | | } |
| | | |
| | | private void saveSession(StationMoveSession session) { |
| | | if (sessionRegistry != null) { |
| | | sessionRegistry.save(session); |
| | | } |
| | | } |
| | | |
| | | private void saveLegacyCircleCommand(Integer taskNo, StationCommand command) { |
| | | if (redisUtil == null || taskNo == null || taskNo <= 0 || command == null) { |
| | | return; |
| | | } |
| | | redisUtil.set(RedisKeyType.WATCH_CIRCLE_STATION_.key + taskNo, |
| | | JSON.toJSONString(command, SerializerFeature.DisableCircularReferenceDetect), |
| | | SESSION_EXPIRE_SECONDS); |
| | | } |
| | | |
| | | private void clearLegacyCircleCommand(Integer taskNo) { |
| | | if (redisUtil == null || taskNo == null || taskNo <= 0) { |
| | | return; |
| | | } |
| | | redisUtil.del(RedisKeyType.WATCH_CIRCLE_STATION_.key + taskNo); |
| | | } |
| | | |
| | | private List<Integer> resolveFullPathStationIds(StationCommand command) { |
| | | if (command == null) { |
| | | return new ArrayList<>(); |
| | | } |
| | | List<Integer> source = command.getOriginalNavigatePath(); |
| | | if (source == null || source.isEmpty()) { |
| | | source = command.getNavigatePath(); |
| | | } |
| | | if (source != null && !source.isEmpty()) { |
| | | return new ArrayList<>(source); |
| | | } |
| | | List<Integer> path = new ArrayList<>(); |
| | | if (command.getStationId() != null) { |
| | | path.add(command.getStationId()); |
| | | } |
| | | if (command.getTargetStaNo() != null && !Objects.equals(command.getTargetStaNo(), command.getStationId())) { |
| | | path.add(command.getTargetStaNo()); |
| | | } |
| | | return path; |
| | | } |
| | | |
| | | private List<Integer> copyIntegerList(List<Integer> source) { |
| | | if (source == null || source.isEmpty()) { |
| | | return Collections.emptyList(); |
| | | } |
| | | return new ArrayList<>(source); |
| | | } |
| | | |
| | | private String resolveThreadImpl(String triggerName) { |
| | | if (triggerName == null) { |
| | | return null; |
| | | } |
| | | if (triggerName.contains("crn")) { |
| | | return "crn"; |
| | | } |
| | | if (triggerName.contains("watchCircle")) { |
| | | return "station"; |
| | | } |
| | | if (triggerName.contains("checkStationRunBlock")) { |
| | | return "station"; |
| | | } |
| | | if (triggerName.contains("checkStationIdleRecover")) { |
| | | return "station"; |
| | | } |
| | | if (triggerName.contains("checkStationOutOrder")) { |
| | | return "station"; |
| | | } |
| | | return triggerName; |
| | | } |
| | | |
| | | private StationMoveTriggerType resolveTriggerType(String triggerName, boolean circleRoute) { |
| | | if (circleRoute) { |
| | | return StationMoveTriggerType.WATCH_CIRCLE; |
| | | } |
| | | if (triggerName == null) { |
| | | return StationMoveTriggerType.INITIAL_OUTBOUND; |
| | | } |
| | | if (triggerName.contains("checkStationOutOrder")) { |
| | | return StationMoveTriggerType.OUT_ORDER; |
| | | } |
| | | if (triggerName.contains("watchCircle")) { |
| | | return StationMoveTriggerType.WATCH_CIRCLE; |
| | | } |
| | | if (triggerName.contains("checkStationRunBlock")) { |
| | | return StationMoveTriggerType.RUN_BLOCK; |
| | | } |
| | | if (triggerName.contains("checkStationIdleRecover")) { |
| | | return StationMoveTriggerType.IDLE_RECOVER; |
| | | } |
| | | return StationMoveTriggerType.INITIAL_OUTBOUND; |
| | | } |
| | | |
| | | private StationMoveDispatchMode resolveDispatchMode(String triggerName, boolean circleRoute) { |
| | | if (circleRoute) { |
| | | return StationMoveDispatchMode.CIRCLE; |
| | | } |
| | | if (triggerName != null && triggerName.contains("checkStationRunBlock")) { |
| | | return StationMoveDispatchMode.RUN_BLOCK_REROUTE; |
| | | } |
| | | if (triggerName != null && triggerName.contains("checkStationIdleRecover")) { |
| | | return StationMoveDispatchMode.IDLE_RECOVER_REROUTE; |
| | | } |
| | | return StationMoveDispatchMode.DIRECT; |
| | | } |
| | | |
| | | private boolean isBlank(String value) { |
| | | return value == null || value.trim().isEmpty(); |
| | | } |
| | | |
| | | private String digest(String value) { |
| | | try { |
| | | MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
| | | byte[] bytes = digest.digest(value.getBytes(StandardCharsets.UTF_8)); |
| | | StringBuilder builder = new StringBuilder(bytes.length * 2); |
| | | for (byte b : bytes) { |
| | | String hex = Integer.toHexString(b & 0xff); |
| | | if (hex.length() == 1) { |
| | | builder.append('0'); |
| | | } |
| | | builder.append(hex); |
| | | } |
| | | return builder.toString(); |
| | | } catch (Exception ignore) { |
| | | return Integer.toHexString(value.hashCode()); |
| | | } |
| | | } |
| | | } |
| New file |
| | |
| | | package com.zy.core.move; |
| | | |
| | | public enum StationMoveDispatchMode { |
| | | DIRECT, |
| | | CIRCLE, |
| | | RUN_BLOCK_REROUTE, |
| | | IDLE_RECOVER_REROUTE |
| | | } |
| New file |
| | |
| | | package com.zy.core.move; |
| | | |
| | | import lombok.Data; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | @Data |
| | | public class StationMoveSession { |
| | | |
| | | public static final String STATUS_WAITING = "WAITING"; |
| | | public static final String STATUS_RUNNING = "RUNNING"; |
| | | public static final String STATUS_CANCEL_PENDING = "CANCEL_PENDING"; |
| | | public static final String STATUS_CANCELLED = "CANCELLED"; |
| | | public static final String STATUS_BLOCKED = "BLOCKED"; |
| | | public static final String STATUS_TIMEOUT = "TIMEOUT"; |
| | | public static final String STATUS_FINISHED = "FINISHED"; |
| | | |
| | | private Integer taskNo; |
| | | |
| | | private Integer routeVersion; |
| | | |
| | | private String threadImpl; |
| | | |
| | | private Integer currentStationId; |
| | | |
| | | private StationMoveTriggerType triggerType; |
| | | |
| | | private StationMoveDispatchMode dispatchMode; |
| | | |
| | | private String status; |
| | | |
| | | private Integer dispatchStationId; |
| | | |
| | | private Integer businessTargetStationId; |
| | | |
| | | private Integer currentRouteTargetStationId; |
| | | |
| | | private Integer nextDecisionStationId; |
| | | |
| | | private List<Integer> fullPathStationIds = new ArrayList<>(); |
| | | |
| | | private String pathSignature; |
| | | |
| | | private String cancelReason; |
| | | |
| | | private Long createdAt; |
| | | |
| | | private Long updatedAt; |
| | | |
| | | private Long lastIssuedAt; |
| | | |
| | | public boolean isActive() { |
| | | return STATUS_WAITING.equals(status) |
| | | || STATUS_RUNNING.equals(status) |
| | | || STATUS_CANCEL_PENDING.equals(status); |
| | | } |
| | | |
| | | public boolean containsStation(Integer stationId) { |
| | | return stationId != null |
| | | && fullPathStationIds != null |
| | | && fullPathStationIds.contains(stationId); |
| | | } |
| | | } |
| New file |
| | |
| | | package com.zy.core.move; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.serializer.SerializerFeature; |
| | | import com.zy.common.utils.RedisUtil; |
| | | import com.zy.core.enums.RedisKeyType; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | @Component |
| | | public class StationMoveSessionRegistry { |
| | | |
| | | private static final int SESSION_EXPIRE_SECONDS = 60 * 60 * 24; |
| | | |
| | | @Autowired |
| | | private RedisUtil redisUtil; |
| | | |
| | | public synchronized StationMoveSession load(Integer taskNo) { |
| | | if (taskNo == null || taskNo <= 0 || redisUtil == null) { |
| | | return null; |
| | | } |
| | | Object sessionObj = redisUtil.get(buildKey(taskNo)); |
| | | if (sessionObj == null) { |
| | | return null; |
| | | } |
| | | try { |
| | | return JSON.parseObject(String.valueOf(sessionObj), StationMoveSession.class); |
| | | } catch (Exception ignore) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | public synchronized void save(StationMoveSession session) { |
| | | if (session == null || session.getTaskNo() == null || session.getTaskNo() <= 0 || redisUtil == null) { |
| | | return; |
| | | } |
| | | long now = System.currentTimeMillis(); |
| | | if (session.getCreatedAt() == null || session.getCreatedAt() <= 0L) { |
| | | session.setCreatedAt(now); |
| | | } |
| | | session.setUpdatedAt(now); |
| | | redisUtil.set(buildKey(session.getTaskNo()), |
| | | JSON.toJSONString(session, SerializerFeature.DisableCircularReferenceDetect), |
| | | SESSION_EXPIRE_SECONDS); |
| | | } |
| | | |
| | | public synchronized void delete(Integer taskNo) { |
| | | if (taskNo == null || taskNo <= 0 || redisUtil == null) { |
| | | return; |
| | | } |
| | | redisUtil.del(buildKey(taskNo)); |
| | | } |
| | | |
| | | public synchronized boolean isActiveRoute(Integer taskNo, Integer routeVersion) { |
| | | StationMoveSession session = load(taskNo); |
| | | return session != null |
| | | && session.isActive() |
| | | && routeVersion != null |
| | | && Objects.equals(routeVersion, session.getRouteVersion()); |
| | | } |
| | | |
| | | public synchronized boolean shouldSkipOutOrderDecision(Integer taskNo, Integer currentStationId) { |
| | | StationMoveSession session = load(taskNo); |
| | | if (session == null || !session.isActive() || currentStationId == null) { |
| | | return false; |
| | | } |
| | | List<Integer> fullPathStationIds = session.getFullPathStationIds(); |
| | | if (fullPathStationIds == null || !fullPathStationIds.contains(currentStationId)) { |
| | | return false; |
| | | } |
| | | if (StationMoveDispatchMode.CIRCLE == session.getDispatchMode()) { |
| | | return true; |
| | | } |
| | | return !Objects.equals(currentStationId, session.getCurrentRouteTargetStationId()); |
| | | } |
| | | |
| | | public synchronized boolean isCircleDecisionStation(Integer taskNo, Integer currentStationId) { |
| | | StationMoveSession session = load(taskNo); |
| | | return session != null |
| | | && session.isActive() |
| | | && StationMoveDispatchMode.CIRCLE == session.getDispatchMode() |
| | | && currentStationId != null |
| | | && Objects.equals(currentStationId, session.getNextDecisionStationId()); |
| | | } |
| | | |
| | | public synchronized boolean isCircleTransitStation(Integer taskNo, Integer currentStationId) { |
| | | StationMoveSession session = load(taskNo); |
| | | if (session == null |
| | | || !session.isActive() |
| | | || StationMoveDispatchMode.CIRCLE != session.getDispatchMode() |
| | | || currentStationId == null |
| | | || Objects.equals(currentStationId, session.getNextDecisionStationId())) { |
| | | return false; |
| | | } |
| | | List<Integer> fullPathStationIds = session.getFullPathStationIds(); |
| | | return fullPathStationIds != null && fullPathStationIds.contains(currentStationId); |
| | | } |
| | | |
| | | public synchronized StationMoveSession registerPlan(Integer taskNo, |
| | | String threadImpl, |
| | | Integer currentStationId, |
| | | Integer businessTargetStationId, |
| | | StationMoveTriggerType triggerType, |
| | | StationMoveDispatchMode dispatchMode, |
| | | Integer currentRouteTargetStationId, |
| | | Integer nextDecisionStationId, |
| | | List<Integer> fullPathStationIds, |
| | | boolean cancelActive, |
| | | String cancelReason) { |
| | | if (taskNo == null || taskNo <= 0) { |
| | | return null; |
| | | } |
| | | StationMoveSession current = load(taskNo); |
| | | StationMoveSession next = new StationMoveSession(); |
| | | next.setTaskNo(taskNo); |
| | | next.setThreadImpl(threadImpl); |
| | | next.setRouteVersion(current == null || current.getRouteVersion() == null ? 1 : current.getRouteVersion() + 1); |
| | | next.setCurrentStationId(currentStationId); |
| | | next.setBusinessTargetStationId(businessTargetStationId); |
| | | next.setTriggerType(triggerType); |
| | | next.setDispatchMode(dispatchMode); |
| | | next.setCurrentRouteTargetStationId(currentRouteTargetStationId); |
| | | next.setNextDecisionStationId(nextDecisionStationId); |
| | | next.setStatus(StationMoveSession.STATUS_WAITING); |
| | | next.setCancelReason(cancelReason); |
| | | next.setFullPathStationIds(copyIntegerList(fullPathStationIds)); |
| | | next.setPathSignature(buildPathSignature(fullPathStationIds)); |
| | | save(next); |
| | | return next; |
| | | } |
| | | |
| | | public synchronized boolean isSameActivePath(Integer taskNo, List<Integer> fullPathStationIds) { |
| | | StationMoveSession session = load(taskNo); |
| | | if (session == null || !session.isActive()) { |
| | | return false; |
| | | } |
| | | return Objects.equals(session.getPathSignature(), buildPathSignature(fullPathStationIds)); |
| | | } |
| | | |
| | | public synchronized void markSegmentIssued(Integer taskNo, Integer routeVersion) { |
| | | StationMoveSession session = load(taskNo); |
| | | if (session == null || !Objects.equals(session.getRouteVersion(), routeVersion)) { |
| | | return; |
| | | } |
| | | session.setStatus(StationMoveSession.STATUS_RUNNING); |
| | | session.setLastIssuedAt(System.currentTimeMillis()); |
| | | save(session); |
| | | } |
| | | |
| | | public synchronized void updateCurrentStation(Integer taskNo, Integer routeVersion, Integer currentStationId) { |
| | | StationMoveSession session = load(taskNo); |
| | | if (session == null || !Objects.equals(session.getRouteVersion(), routeVersion)) { |
| | | return; |
| | | } |
| | | session.setCurrentStationId(currentStationId); |
| | | if (!StationMoveSession.STATUS_CANCEL_PENDING.equals(session.getStatus())) { |
| | | session.setStatus(StationMoveSession.STATUS_RUNNING); |
| | | } |
| | | save(session); |
| | | } |
| | | |
| | | public synchronized void markCancelPending(Integer taskNo, String cancelReason) { |
| | | StationMoveSession session = load(taskNo); |
| | | if (session == null || !session.isActive()) { |
| | | return; |
| | | } |
| | | session.setStatus(StationMoveSession.STATUS_CANCEL_PENDING); |
| | | session.setCancelReason(cancelReason); |
| | | save(session); |
| | | } |
| | | |
| | | public synchronized void markCancelled(Integer taskNo, Integer routeVersion, Integer currentStationId, String cancelReason) { |
| | | updateTerminal(taskNo, routeVersion, currentStationId, StationMoveSession.STATUS_CANCELLED, cancelReason); |
| | | } |
| | | |
| | | public synchronized void markBlocked(Integer taskNo, Integer routeVersion, Integer currentStationId) { |
| | | updateTerminal(taskNo, routeVersion, currentStationId, StationMoveSession.STATUS_BLOCKED, null); |
| | | } |
| | | |
| | | public synchronized void markTimeout(Integer taskNo, Integer routeVersion, Integer currentStationId) { |
| | | updateTerminal(taskNo, routeVersion, currentStationId, StationMoveSession.STATUS_TIMEOUT, null); |
| | | } |
| | | |
| | | public synchronized void markFinished(Integer taskNo, Integer routeVersion, Integer currentStationId) { |
| | | updateTerminal(taskNo, routeVersion, currentStationId, StationMoveSession.STATUS_FINISHED, null); |
| | | } |
| | | |
| | | public synchronized StationMoveSession buildCircleSessionCommandSource(Integer taskNo) { |
| | | StationMoveSession session = load(taskNo); |
| | | if (session == null || !session.isActive() || StationMoveDispatchMode.CIRCLE != session.getDispatchMode()) { |
| | | return null; |
| | | } |
| | | return session; |
| | | } |
| | | |
| | | private void updateTerminal(Integer taskNo, |
| | | Integer routeVersion, |
| | | Integer currentStationId, |
| | | String status, |
| | | String cancelReason) { |
| | | StationMoveSession session = load(taskNo); |
| | | if (session == null || !Objects.equals(session.getRouteVersion(), routeVersion)) { |
| | | return; |
| | | } |
| | | session.setCurrentStationId(currentStationId); |
| | | session.setStatus(status); |
| | | session.setCancelReason(cancelReason); |
| | | save(session); |
| | | } |
| | | |
| | | private String buildKey(Integer taskNo) { |
| | | return RedisKeyType.STATION_MOVE_SESSION_.key + taskNo; |
| | | } |
| | | |
| | | private String buildPathSignature(List<Integer> fullPathStationIds) { |
| | | if (fullPathStationIds == null || fullPathStationIds.isEmpty()) { |
| | | return ""; |
| | | } |
| | | StringBuilder builder = new StringBuilder(); |
| | | for (Integer stationId : fullPathStationIds) { |
| | | if (stationId == null) { |
| | | continue; |
| | | } |
| | | if (builder.length() > 0) { |
| | | builder.append("->"); |
| | | } |
| | | builder.append(stationId); |
| | | } |
| | | return builder.toString(); |
| | | } |
| | | |
| | | private List<Integer> copyIntegerList(List<Integer> source) { |
| | | List<Integer> result = new ArrayList<>(); |
| | | if (source != null) { |
| | | result.addAll(source); |
| | | } |
| | | return result; |
| | | } |
| | | } |
| New file |
| | |
| | | package com.zy.core.move; |
| | | |
| | | public enum StationMoveTriggerType { |
| | | INITIAL_OUTBOUND, |
| | | OUT_ORDER, |
| | | WATCH_CIRCLE, |
| | | RUN_BLOCK, |
| | | IDLE_RECOVER |
| | | } |
| | |
| | | import com.zy.core.network.DeviceConnectPool; |
| | | import com.zy.core.network.ZyStationConnectDriver; |
| | | import com.zy.core.network.entity.ZyStationStatusEntity; |
| | | import com.zy.core.thread.impl.v5.StationMoveSegmentExecutor; |
| | | import com.zy.core.utils.DeviceLogRedisKeyBuilder; |
| | | import com.zy.system.entity.Config; |
| | | import com.zy.system.service.ConfigService; |
| | |
| | | private DeviceConfig deviceConfig; |
| | | private RedisUtil redisUtil; |
| | | private ZyStationConnectDriver zyStationConnectDriver; |
| | | private StationMoveSegmentExecutor segmentExecutor; |
| | | private int deviceLogCollectTime = 200; |
| | | private boolean initStatus = false; |
| | | private long deviceDataLogTime = System.currentTimeMillis(); |
| | |
| | | public ZyStationV4Thread(DeviceConfig deviceConfig, RedisUtil redisUtil) { |
| | | this.deviceConfig = deviceConfig; |
| | | this.redisUtil = redisUtil; |
| | | this.segmentExecutor = new StationMoveSegmentExecutor(deviceConfig, redisUtil, this::sendCommand); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | if (step == 2) { |
| | | StationCommand cmd = (StationCommand) task.getData(); |
| | | executor.submit(() -> executeMoveWithSeg(cmd)); |
| | | executor.submit(() -> segmentExecutor.execute(cmd)); |
| | | } |
| | | Thread.sleep(100); |
| | | } catch (Exception e) { |
| New file |
| | |
| | | package com.zy.core.thread.impl.station; |
| | | |
| | | import com.zy.core.model.command.StationCommand; |
| | | import lombok.Data; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | @Data |
| | | public class StationSegmentExecutionPlan { |
| | | |
| | | private List<Integer> fullPathStationIds = new ArrayList<>(); |
| | | |
| | | private List<StationCommand> segmentCommands = new ArrayList<>(); |
| | | } |
| New file |
| | |
| | | package com.zy.core.thread.impl.station; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.core.common.Cools; |
| | | import com.core.common.SpringUtils; |
| | | import com.zy.asrs.domain.vo.StationTaskTraceSegmentVo; |
| | | import com.zy.asrs.entity.DeviceConfig; |
| | | import com.zy.common.utils.RedisUtil; |
| | | import com.zy.core.cache.SlaveConnection; |
| | | import com.zy.core.enums.RedisKeyType; |
| | | import com.zy.core.enums.SlaveType; |
| | | import com.zy.core.enums.StationCommandType; |
| | | import com.zy.core.model.CommandResponse; |
| | | import com.zy.core.model.command.StationCommand; |
| | | import com.zy.core.model.protocol.StationProtocol; |
| | | import com.zy.core.move.StationMoveCoordinator; |
| | | import com.zy.core.trace.StationTaskTraceRegistry; |
| | | import com.zy.system.entity.Config; |
| | | import com.zy.system.service.ConfigService; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.function.Function; |
| | | |
| | | public class StationSegmentExecutor { |
| | | |
| | | private static final String CFG_STATION_COMMAND_SEGMENT_ADVANCE_RATIO = "stationCommandSegmentAdvanceRatio"; |
| | | private static final double DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO = 0.3d; |
| | | private static final long CURRENT_STATION_TIMEOUT_MS = 1000L * 60L; |
| | | |
| | | private final DeviceConfig deviceConfig; |
| | | private final RedisUtil redisUtil; |
| | | private final Function<StationCommand, CommandResponse> commandSender; |
| | | private final StationSegmentPlanner segmentPlanner = new StationSegmentPlanner(); |
| | | |
| | | public StationSegmentExecutor(DeviceConfig deviceConfig, |
| | | RedisUtil redisUtil, |
| | | Function<StationCommand, CommandResponse> commandSender) { |
| | | this.deviceConfig = deviceConfig; |
| | | this.redisUtil = redisUtil; |
| | | this.commandSender = commandSender; |
| | | } |
| | | |
| | | public void execute(StationCommand original) { |
| | | if (original == null) { |
| | | return; |
| | | } |
| | | if (original.getCommandType() != StationCommandType.MOVE) { |
| | | commandSender.apply(original); |
| | | return; |
| | | } |
| | | |
| | | StationSegmentExecutionPlan localPlan = segmentPlanner.buildPlan(original); |
| | | if (localPlan.getSegmentCommands().isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | StationTaskTraceRegistry traceRegistry = SpringUtils.getBean(StationTaskTraceRegistry.class); |
| | | StationTaskTraceRegistry.TraceRegistration traceRegistration = traceRegistry == null |
| | | ? new StationTaskTraceRegistry.TraceRegistration() |
| | | : traceRegistry.registerPlan(original.getTaskNo(), deviceConfig.getThreadImpl(), |
| | | original.getStationId(), original.getStationId(), original.getTargetStaNo(), |
| | | localPlan.getFullPathStationIds(), buildTraceSegments(localPlan.getSegmentCommands())); |
| | | int traceVersion = traceRegistration.getTraceVersion() == null ? 1 : traceRegistration.getTraceVersion(); |
| | | int pathOffset = traceRegistration.getPathOffset() == null ? 0 : traceRegistration.getPathOffset(); |
| | | bindCommands(localPlan.getSegmentCommands(), traceVersion, pathOffset, original.getRouteVersion()); |
| | | List<Integer> effectiveFullPath = traceRegistration.getFullPathStationIds() == null |
| | | || traceRegistration.getFullPathStationIds().isEmpty() |
| | | ? copyIntegerList(localPlan.getFullPathStationIds()) |
| | | : copyIntegerList(traceRegistration.getFullPathStationIds()); |
| | | |
| | | StationCommand firstCommand = localPlan.getSegmentCommands().get(0); |
| | | if (!sendSegmentWithRetry(firstCommand, traceRegistry, traceVersion, null)) { |
| | | return; |
| | | } |
| | | if (traceRegistry != null) { |
| | | traceRegistry.markSegmentIssued(original.getTaskNo(), traceVersion, firstCommand, |
| | | "FIRST_SEGMENT_SENT", "输送任务首段下发成功", buildSegmentDetails(firstCommand)); |
| | | } |
| | | |
| | | long lastSeenAt = System.currentTimeMillis(); |
| | | int segCursor = 0; |
| | | Integer lastCurrentStationId = null; |
| | | boolean firstRun = true; |
| | | while (true) { |
| | | try { |
| | | if (!isRouteActive(original.getTaskNo(), original.getRouteVersion())) { |
| | | if (traceRegistry != null) { |
| | | traceRegistry.markCancelled(original.getTaskNo(), traceVersion, lastCurrentStationId, |
| | | buildDetails("reason", "route_version_replaced", "routeVersion", original.getRouteVersion())); |
| | | } |
| | | markCancelled(original.getTaskNo(), original.getRouteVersion(), lastCurrentStationId, "route_version_replaced"); |
| | | break; |
| | | } |
| | | |
| | | Object cancel = redisUtil.get(RedisKeyType.DEVICE_STATION_MOVE_RESET.key + original.getTaskNo()); |
| | | if (cancel != null) { |
| | | if (traceRegistry != null) { |
| | | traceRegistry.markCancelled(original.getTaskNo(), traceVersion, lastCurrentStationId, |
| | | buildDetails("reason", "redis_cancel_signal", "routeVersion", original.getRouteVersion())); |
| | | } |
| | | markCancelled(original.getTaskNo(), original.getRouteVersion(), lastCurrentStationId, "redis_cancel_signal"); |
| | | break; |
| | | } |
| | | |
| | | StationProtocol currentStation = findCurrentStationByTask(original.getTaskNo()); |
| | | if (currentStation == null) { |
| | | if (System.currentTimeMillis() - lastSeenAt > CURRENT_STATION_TIMEOUT_MS) { |
| | | if (traceRegistry != null) { |
| | | traceRegistry.markTimeout(original.getTaskNo(), traceVersion, lastCurrentStationId, |
| | | buildDetails("timeoutMs", CURRENT_STATION_TIMEOUT_MS, "routeVersion", original.getRouteVersion())); |
| | | } |
| | | markTimeout(original.getTaskNo(), original.getRouteVersion(), lastCurrentStationId); |
| | | break; |
| | | } |
| | | Thread.sleep(500L); |
| | | continue; |
| | | } |
| | | |
| | | lastSeenAt = System.currentTimeMillis(); |
| | | Integer previousCurrentStationId = lastCurrentStationId; |
| | | updateCurrentStation(original.getTaskNo(), original.getRouteVersion(), currentStation.getStationId()); |
| | | if (traceRegistry != null) { |
| | | traceRegistry.updateProgress(original.getTaskNo(), traceVersion, currentStation.getStationId(), |
| | | equalsInteger(previousCurrentStationId, currentStation.getStationId()) ? null : "CURRENT_STATION_CHANGE", |
| | | "输送任务当前位置已更新", |
| | | buildDetails("stationId", currentStation.getStationId(), "routeVersion", original.getRouteVersion())); |
| | | } |
| | | lastCurrentStationId = currentStation.getStationId(); |
| | | if (!firstRun && currentStation.isRunBlock()) { |
| | | if (traceRegistry != null) { |
| | | traceRegistry.markBlocked(original.getTaskNo(), traceVersion, currentStation.getStationId(), |
| | | buildDetails("blockedStationId", currentStation.getStationId(), "routeVersion", original.getRouteVersion())); |
| | | } |
| | | markBlocked(original.getTaskNo(), original.getRouteVersion(), currentStation.getStationId()); |
| | | break; |
| | | } |
| | | |
| | | int currentIndex = effectiveFullPath.indexOf(currentStation.getStationId()); |
| | | if (currentIndex < 0) { |
| | | Thread.sleep(500L); |
| | | firstRun = false; |
| | | continue; |
| | | } |
| | | |
| | | int remaining = effectiveFullPath.size() - currentIndex - 1; |
| | | if (remaining <= 0) { |
| | | if (traceRegistry != null) { |
| | | traceRegistry.markFinished(original.getTaskNo(), traceVersion, currentStation.getStationId(), |
| | | buildDetails("targetStationId", original.getTargetStaNo(), "routeVersion", original.getRouteVersion())); |
| | | } |
| | | markFinished(original.getTaskNo(), original.getRouteVersion(), currentStation.getStationId()); |
| | | break; |
| | | } |
| | | |
| | | StationCommand currentSegmentCommand = localPlan.getSegmentCommands().get(segCursor); |
| | | int currentSegEndIndex = safeIndex(currentSegmentCommand.getSegmentEndIndex()); |
| | | int currentSegStartIndex = segCursor == 0 |
| | | ? 0 |
| | | : safeIndex(localPlan.getSegmentCommands().get(segCursor - 1).getSegmentEndIndex()); |
| | | int segLen = currentSegEndIndex - currentSegStartIndex + 1; |
| | | int remainingSegment = Math.max(0, currentSegEndIndex - currentIndex); |
| | | int thresholdSegment = (int) Math.ceil(segLen * loadSegmentAdvanceRatio()); |
| | | thresholdSegment = Math.max(1, thresholdSegment); |
| | | if (remainingSegment <= thresholdSegment |
| | | && segCursor < localPlan.getSegmentCommands().size() - 1) { |
| | | StationCommand nextCommand = localPlan.getSegmentCommands().get(segCursor + 1); |
| | | if (sendSegmentWithRetry(nextCommand, traceRegistry, traceVersion, currentStation.getStationId())) { |
| | | segCursor++; |
| | | if (traceRegistry != null) { |
| | | traceRegistry.markSegmentIssued(original.getTaskNo(), traceVersion, nextCommand, |
| | | "NEXT_SEGMENT_SENT", "输送任务下一段已提前下发", |
| | | buildSegmentDetails(nextCommand)); |
| | | } |
| | | } else { |
| | | break; |
| | | } |
| | | } |
| | | Thread.sleep(500L); |
| | | firstRun = false; |
| | | } catch (Exception ignore) { |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | |
| | | private boolean sendSegmentWithRetry(StationCommand command, |
| | | StationTaskTraceRegistry traceRegistry, |
| | | Integer traceVersion, |
| | | Integer currentStationId) { |
| | | while (true) { |
| | | if (!isRouteActive(command == null ? null : command.getTaskNo(), command == null ? null : command.getRouteVersion())) { |
| | | if (traceRegistry != null && command != null) { |
| | | traceRegistry.markCancelled(command.getTaskNo(), traceVersion, currentStationId, |
| | | buildDetails("reason", "route_version_replaced", "routeVersion", command.getRouteVersion())); |
| | | } |
| | | markCancelled(command == null ? null : command.getTaskNo(), |
| | | command == null ? null : command.getRouteVersion(), |
| | | currentStationId, |
| | | "route_version_replaced"); |
| | | return false; |
| | | } |
| | | if (isTaskMoveReset(command == null ? null : command.getTaskNo())) { |
| | | if (traceRegistry != null && command != null) { |
| | | traceRegistry.markCancelled(command.getTaskNo(), traceVersion, currentStationId, |
| | | buildDetails("reason", "redis_cancel_signal", "routeVersion", command.getRouteVersion())); |
| | | } |
| | | markCancelled(command == null ? null : command.getTaskNo(), |
| | | command == null ? null : command.getRouteVersion(), |
| | | currentStationId, |
| | | "redis_cancel_signal"); |
| | | return false; |
| | | } |
| | | CommandResponse commandResponse = commandSender.apply(command); |
| | | if (commandResponse == null) { |
| | | sleepQuietly(200L); |
| | | continue; |
| | | } |
| | | if (commandResponse.getResult()) { |
| | | markSegmentIssued(command == null ? null : command.getTaskNo(), command == null ? null : command.getRouteVersion()); |
| | | return true; |
| | | } |
| | | sleepQuietly(200L); |
| | | } |
| | | } |
| | | |
| | | private double loadSegmentAdvanceRatio() { |
| | | try { |
| | | ConfigService configService = SpringUtils.getBean(ConfigService.class); |
| | | if (configService == null) { |
| | | return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO; |
| | | } |
| | | Config config = configService.getOne(new QueryWrapper<Config>() |
| | | .eq("code", CFG_STATION_COMMAND_SEGMENT_ADVANCE_RATIO)); |
| | | if (config == null || Cools.isEmpty(config.getValue())) { |
| | | return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO; |
| | | } |
| | | return normalizeSegmentAdvanceRatio(config.getValue()); |
| | | } catch (Exception ignore) { |
| | | return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO; |
| | | } |
| | | } |
| | | |
| | | private double normalizeSegmentAdvanceRatio(String valueText) { |
| | | if (valueText == null) { |
| | | return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO; |
| | | } |
| | | String text = valueText.trim(); |
| | | if (text.isEmpty()) { |
| | | return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO; |
| | | } |
| | | if (text.endsWith("%")) { |
| | | text = text.substring(0, text.length() - 1).trim(); |
| | | } |
| | | try { |
| | | double ratio = Double.parseDouble(text); |
| | | if (ratio > 1d && ratio <= 100d) { |
| | | ratio = ratio / 100d; |
| | | } |
| | | if (ratio < 0d) { |
| | | return 0d; |
| | | } |
| | | if (ratio > 1d) { |
| | | return 1d; |
| | | } |
| | | return ratio; |
| | | } catch (Exception ignore) { |
| | | return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO; |
| | | } |
| | | } |
| | | |
| | | private boolean isTaskMoveReset(Integer taskNo) { |
| | | if (taskNo == null || redisUtil == null) { |
| | | return false; |
| | | } |
| | | Object cancel = redisUtil.get(RedisKeyType.DEVICE_STATION_MOVE_RESET.key + taskNo); |
| | | return cancel != null; |
| | | } |
| | | |
| | | private StationProtocol findCurrentStationByTask(Integer taskNo) { |
| | | try { |
| | | com.zy.asrs.service.DeviceConfigService deviceConfigService = SpringUtils.getBean(com.zy.asrs.service.DeviceConfigService.class); |
| | | if (deviceConfigService == null) { |
| | | return null; |
| | | } |
| | | List<DeviceConfig> devpList = deviceConfigService.list(new QueryWrapper<DeviceConfig>() |
| | | .eq("device_type", String.valueOf(SlaveType.Devp))); |
| | | for (DeviceConfig dc : devpList) { |
| | | com.zy.core.thread.StationThread thread = (com.zy.core.thread.StationThread) SlaveConnection.get(SlaveType.Devp, dc.getDeviceNo()); |
| | | if (thread == null) { |
| | | continue; |
| | | } |
| | | Map<Integer, StationProtocol> statusMap = thread.getStatusMap(); |
| | | if (statusMap == null || statusMap.isEmpty()) { |
| | | continue; |
| | | } |
| | | for (StationProtocol protocol : statusMap.values()) { |
| | | if (protocol.getTaskNo() != null && protocol.getTaskNo().equals(taskNo) && protocol.isLoading()) { |
| | | return protocol; |
| | | } |
| | | } |
| | | } |
| | | } catch (Exception ignore) { |
| | | return null; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private List<StationTaskTraceSegmentVo> buildTraceSegments(List<StationCommand> segmentCommands) { |
| | | List<StationTaskTraceSegmentVo> result = new ArrayList<>(); |
| | | if (segmentCommands == null) { |
| | | return result; |
| | | } |
| | | for (StationCommand command : segmentCommands) { |
| | | if (command == null) { |
| | | continue; |
| | | } |
| | | StationTaskTraceSegmentVo item = new StationTaskTraceSegmentVo(); |
| | | item.setSegmentNo(command.getSegmentNo()); |
| | | item.setSegmentCount(command.getSegmentCount()); |
| | | item.setStationId(command.getStationId()); |
| | | item.setTargetStationId(command.getTargetStaNo()); |
| | | item.setSegmentStartIndex(command.getSegmentStartIndex()); |
| | | item.setSegmentEndIndex(command.getSegmentEndIndex()); |
| | | item.setSegmentPath(copyIntegerList(command.getNavigatePath())); |
| | | item.setIssued(Boolean.FALSE); |
| | | result.add(item); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | private void bindCommands(List<StationCommand> segmentCommands, int traceVersion, int pathOffset, Integer routeVersion) { |
| | | if (segmentCommands == null) { |
| | | return; |
| | | } |
| | | for (StationCommand command : segmentCommands) { |
| | | if (command == null) { |
| | | continue; |
| | | } |
| | | command.setTraceVersion(traceVersion); |
| | | command.setRouteVersion(routeVersion); |
| | | if (command.getSegmentStartIndex() != null) { |
| | | command.setSegmentStartIndex(command.getSegmentStartIndex() + pathOffset); |
| | | } |
| | | if (command.getSegmentEndIndex() != null) { |
| | | command.setSegmentEndIndex(command.getSegmentEndIndex() + pathOffset); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private Map<String, Object> buildSegmentDetails(StationCommand command) { |
| | | Map<String, Object> details = new LinkedHashMap<>(); |
| | | if (command != null) { |
| | | details.put("segmentNo", command.getSegmentNo()); |
| | | details.put("segmentCount", command.getSegmentCount()); |
| | | details.put("segmentPath", copyIntegerList(command.getNavigatePath())); |
| | | details.put("segmentStartIndex", command.getSegmentStartIndex()); |
| | | details.put("segmentEndIndex", command.getSegmentEndIndex()); |
| | | details.put("traceVersion", command.getTraceVersion()); |
| | | details.put("routeVersion", command.getRouteVersion()); |
| | | } |
| | | return details; |
| | | } |
| | | |
| | | private Map<String, Object> buildDetails(Object... keyValues) { |
| | | Map<String, Object> details = new LinkedHashMap<>(); |
| | | if (keyValues == null) { |
| | | return details; |
| | | } |
| | | for (int i = 0; i + 1 < keyValues.length; i += 2) { |
| | | Object key = keyValues[i]; |
| | | if (key != null) { |
| | | details.put(String.valueOf(key), keyValues[i + 1]); |
| | | } |
| | | } |
| | | return details; |
| | | } |
| | | |
| | | private List<Integer> copyIntegerList(List<Integer> source) { |
| | | List<Integer> result = new ArrayList<>(); |
| | | if (source != null) { |
| | | result.addAll(source); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | private int safeIndex(Integer value) { |
| | | return value == null ? -1 : value; |
| | | } |
| | | |
| | | private boolean equalsInteger(Integer a, Integer b) { |
| | | return a != null && a.equals(b); |
| | | } |
| | | |
| | | private void sleepQuietly(long millis) { |
| | | try { |
| | | Thread.sleep(millis); |
| | | } catch (Exception ignore) { |
| | | } |
| | | } |
| | | |
| | | private boolean isRouteActive(Integer taskNo, Integer routeVersion) { |
| | | // Legacy direct-enqueue commands (for example FakeProcess/stationInExecute) |
| | | // do not register a move session and therefore have no routeVersion. |
| | | // They should keep the historical behavior and execute normally. |
| | | if (taskNo == null || routeVersion == null) { |
| | | return true; |
| | | } |
| | | StationMoveCoordinator moveCoordinator = SpringUtils.getBean(StationMoveCoordinator.class); |
| | | return moveCoordinator == null || moveCoordinator.isActiveRoute(taskNo, routeVersion); |
| | | } |
| | | |
| | | private void markSegmentIssued(Integer taskNo, Integer routeVersion) { |
| | | StationMoveCoordinator moveCoordinator = SpringUtils.getBean(StationMoveCoordinator.class); |
| | | if (moveCoordinator != null) { |
| | | moveCoordinator.markSegmentIssued(taskNo, routeVersion); |
| | | } |
| | | } |
| | | |
| | | private void updateCurrentStation(Integer taskNo, Integer routeVersion, Integer currentStationId) { |
| | | StationMoveCoordinator moveCoordinator = SpringUtils.getBean(StationMoveCoordinator.class); |
| | | if (moveCoordinator != null) { |
| | | moveCoordinator.updateCurrentStation(taskNo, routeVersion, currentStationId); |
| | | } |
| | | } |
| | | |
| | | private void markCancelled(Integer taskNo, Integer routeVersion, Integer currentStationId, String cancelReason) { |
| | | StationMoveCoordinator moveCoordinator = SpringUtils.getBean(StationMoveCoordinator.class); |
| | | if (moveCoordinator != null) { |
| | | moveCoordinator.markCancelled(taskNo, routeVersion, currentStationId, cancelReason); |
| | | } |
| | | } |
| | | |
| | | private void markBlocked(Integer taskNo, Integer routeVersion, Integer currentStationId) { |
| | | StationMoveCoordinator moveCoordinator = SpringUtils.getBean(StationMoveCoordinator.class); |
| | | if (moveCoordinator != null) { |
| | | moveCoordinator.markBlocked(taskNo, routeVersion, currentStationId); |
| | | } |
| | | } |
| | | |
| | | private void markTimeout(Integer taskNo, Integer routeVersion, Integer currentStationId) { |
| | | StationMoveCoordinator moveCoordinator = SpringUtils.getBean(StationMoveCoordinator.class); |
| | | if (moveCoordinator != null) { |
| | | moveCoordinator.markTimeout(taskNo, routeVersion, currentStationId); |
| | | } |
| | | } |
| | | |
| | | private void markFinished(Integer taskNo, Integer routeVersion, Integer currentStationId) { |
| | | StationMoveCoordinator moveCoordinator = SpringUtils.getBean(StationMoveCoordinator.class); |
| | | if (moveCoordinator != null) { |
| | | moveCoordinator.markFinished(taskNo, routeVersion, currentStationId); |
| | | } |
| | | } |
| | | } |
| New file |
| | |
| | | package com.zy.core.thread.impl.station; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.serializer.SerializerFeature; |
| | | import com.zy.core.model.command.StationCommand; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | public class StationSegmentPlanner { |
| | | |
| | | public StationSegmentExecutionPlan buildPlan(StationCommand original) { |
| | | StationSegmentExecutionPlan plan = new StationSegmentExecutionPlan(); |
| | | if (original == null) { |
| | | return plan; |
| | | } |
| | | |
| | | List<Integer> path = copyIntegerList(original.getNavigatePath()); |
| | | List<Integer> liftTransferPath = copyIntegerList(original.getLiftTransferPath()); |
| | | Integer startStationId = original.getStationId(); |
| | | Integer targetStationId = original.getTargetStaNo(); |
| | | |
| | | if ((path == null || path.isEmpty()) && Objects.equals(startStationId, targetStationId) && startStationId != null) { |
| | | path = new ArrayList<>(); |
| | | path.add(startStationId); |
| | | } |
| | | if (path == null || path.isEmpty()) { |
| | | return plan; |
| | | } |
| | | |
| | | plan.setFullPathStationIds(copyIntegerList(path)); |
| | | |
| | | int total = path.size(); |
| | | List<Integer> segmentEndIndices = new ArrayList<>(); |
| | | if (liftTransferPath != null) { |
| | | for (Integer liftTransferStationId : liftTransferPath) { |
| | | int endIndex = path.indexOf(liftTransferStationId); |
| | | if (endIndex <= 0) { |
| | | continue; |
| | | } |
| | | if (segmentEndIndices.isEmpty() || endIndex > segmentEndIndices.get(segmentEndIndices.size() - 1)) { |
| | | segmentEndIndices.add(endIndex); |
| | | } |
| | | } |
| | | } |
| | | if (segmentEndIndices.isEmpty() || !Objects.equals(segmentEndIndices.get(segmentEndIndices.size() - 1), total - 1)) { |
| | | segmentEndIndices.add(total - 1); |
| | | } |
| | | |
| | | List<StationCommand> segmentCommands = new ArrayList<>(); |
| | | int buildStartIdx = 0; |
| | | for (Integer endIdx : segmentEndIndices) { |
| | | if (endIdx == null || endIdx < buildStartIdx) { |
| | | continue; |
| | | } |
| | | List<Integer> segmentPath = new ArrayList<>(path.subList(buildStartIdx, endIdx + 1)); |
| | | if (segmentPath.isEmpty()) { |
| | | buildStartIdx = endIdx + 1; |
| | | continue; |
| | | } |
| | | |
| | | StationCommand segmentCommand = new StationCommand(); |
| | | segmentCommand.setTaskNo(original.getTaskNo()); |
| | | segmentCommand.setCommandType(original.getCommandType()); |
| | | segmentCommand.setPalletSize(original.getPalletSize()); |
| | | segmentCommand.setBarcode(original.getBarcode()); |
| | | segmentCommand.setOriginalNavigatePath(copyIntegerList(path)); |
| | | segmentCommand.setNavigatePath(segmentPath); |
| | | segmentCommand.setStationId(segmentPath.get(0)); |
| | | segmentCommand.setTargetStaNo(segmentPath.get(segmentPath.size() - 1)); |
| | | segmentCommand.setSegmentStartIndex(buildStartIdx); |
| | | segmentCommand.setSegmentEndIndex(endIdx); |
| | | segmentCommand.setRouteVersion(original.getRouteVersion()); |
| | | segmentCommands.add(segmentCommand); |
| | | |
| | | buildStartIdx = endIdx; |
| | | } |
| | | |
| | | int segmentCount = segmentCommands.size(); |
| | | for (int i = 0; i < segmentCommands.size(); i++) { |
| | | StationCommand segmentCommand = segmentCommands.get(i); |
| | | segmentCommand.setSegmentNo(i + 1); |
| | | segmentCommand.setSegmentCount(segmentCount); |
| | | } |
| | | plan.setSegmentCommands(segmentCommands); |
| | | return plan; |
| | | } |
| | | |
| | | private List<Integer> copyIntegerList(List<Integer> source) { |
| | | if (source == null) { |
| | | return new ArrayList<>(); |
| | | } |
| | | return JSON.parseArray(JSON.toJSONString(source, SerializerFeature.DisableCircularReferenceDetect), Integer.class); |
| | | } |
| | | } |
| New file |
| | |
| | | package com.zy.core.thread.impl.v5; |
| | | |
| | | import com.zy.asrs.entity.DeviceConfig; |
| | | import com.zy.common.utils.RedisUtil; |
| | | import com.zy.core.model.CommandResponse; |
| | | import com.zy.core.model.command.StationCommand; |
| | | import com.zy.core.thread.impl.station.StationSegmentExecutor; |
| | | |
| | | import java.util.function.Function; |
| | | |
| | | public class StationMoveSegmentExecutor extends StationSegmentExecutor { |
| | | |
| | | public StationMoveSegmentExecutor(DeviceConfig deviceConfig, |
| | | RedisUtil redisUtil, |
| | | Function<StationCommand, CommandResponse> commandSender) { |
| | | super(deviceConfig, redisUtil, commandSender); |
| | | } |
| | | } |
| | |
| | | package com.zy.core.thread.impl.v5; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.core.common.Cools; |
| | | import com.core.common.SpringUtils; |
| | | import com.zy.asrs.domain.vo.StationTaskTraceSegmentVo; |
| | | |
| | | import com.zy.asrs.entity.DeviceConfig; |
| | | import com.zy.common.utils.RedisUtil; |
| | | import com.zy.core.cache.SlaveConnection; |
| | | import com.zy.core.enums.RedisKeyType; |
| | | import com.zy.core.enums.SlaveType; |
| | | import com.zy.core.enums.StationCommandType; |
| | | import com.zy.core.model.CommandResponse; |
| | | import com.zy.core.model.command.StationCommand; |
| | | import com.zy.core.model.protocol.StationProtocol; |
| | | import com.zy.core.trace.StationTaskTraceRegistry; |
| | | import com.zy.system.entity.Config; |
| | | import com.zy.system.service.ConfigService; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.function.Function; |
| | | |
| | | public class StationV5SegmentExecutor { |
| | | |
| | | private static final int NEXT_SEGMENT_ADVANCE_NODE_COUNT = 1; |
| | | private static final long CURRENT_STATION_TIMEOUT_MS = 1000L * 60L; |
| | | |
| | | private final DeviceConfig deviceConfig; |
| | | private final RedisUtil redisUtil; |
| | | private final Function<StationCommand, CommandResponse> commandSender; |
| | | private final StationV5SegmentPlanner segmentPlanner = new StationV5SegmentPlanner(); |
| | | public class StationV5SegmentExecutor extends StationMoveSegmentExecutor { |
| | | |
| | | public StationV5SegmentExecutor(DeviceConfig deviceConfig, |
| | | RedisUtil redisUtil, |
| | | Function<StationCommand, CommandResponse> commandSender) { |
| | | this.deviceConfig = deviceConfig; |
| | | this.redisUtil = redisUtil; |
| | | this.commandSender = commandSender; |
| | | } |
| | | |
| | | public void execute(StationCommand original) { |
| | | if (original == null) { |
| | | return; |
| | | } |
| | | if (original.getCommandType() != StationCommandType.MOVE) { |
| | | commandSender.apply(original); |
| | | return; |
| | | } |
| | | |
| | | StationV5SegmentExecutionPlan localPlan = segmentPlanner.buildPlan(original); |
| | | if (localPlan.getSegmentCommands().isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | StationTaskTraceRegistry traceRegistry = SpringUtils.getBean(StationTaskTraceRegistry.class); |
| | | StationTaskTraceRegistry.TraceRegistration traceRegistration = traceRegistry == null |
| | | ? new StationTaskTraceRegistry.TraceRegistration() |
| | | : traceRegistry.registerPlan(original.getTaskNo(), deviceConfig.getThreadImpl(), |
| | | original.getStationId(), original.getStationId(), original.getTargetStaNo(), |
| | | localPlan.getFullPathStationIds(), buildTraceSegments(localPlan.getSegmentCommands())); |
| | | int traceVersion = traceRegistration.getTraceVersion() == null ? 1 : traceRegistration.getTraceVersion(); |
| | | int pathOffset = traceRegistration.getPathOffset() == null ? 0 : traceRegistration.getPathOffset(); |
| | | bindCommands(localPlan.getSegmentCommands(), traceVersion, pathOffset); |
| | | List<Integer> effectiveFullPath = traceRegistration.getFullPathStationIds() == null |
| | | || traceRegistration.getFullPathStationIds().isEmpty() |
| | | ? copyIntegerList(localPlan.getFullPathStationIds()) |
| | | : copyIntegerList(traceRegistration.getFullPathStationIds()); |
| | | |
| | | StationCommand firstCommand = localPlan.getSegmentCommands().get(0); |
| | | if (!sendSegmentWithRetry(firstCommand)) { |
| | | return; |
| | | } |
| | | if (traceRegistry != null) { |
| | | traceRegistry.markSegmentIssued(original.getTaskNo(), traceVersion, firstCommand, |
| | | "FIRST_SEGMENT_SENT", "输送任务首段下发成功", buildSegmentDetails(firstCommand)); |
| | | } |
| | | |
| | | long lastSeenAt = System.currentTimeMillis(); |
| | | int segCursor = 0; |
| | | Integer lastCurrentStationId = null; |
| | | boolean firstRun = true; |
| | | while (true) { |
| | | try { |
| | | Object cancel = redisUtil.get(RedisKeyType.DEVICE_STATION_MOVE_RESET.key + original.getTaskNo()); |
| | | if (cancel != null) { |
| | | if (traceRegistry != null) { |
| | | traceRegistry.markCancelled(original.getTaskNo(), traceVersion, lastCurrentStationId, |
| | | buildDetails("reason", "redis_cancel_signal")); |
| | | } |
| | | break; |
| | | } |
| | | |
| | | StationProtocol currentStation = findCurrentStationByTask(original.getTaskNo()); |
| | | if (currentStation == null) { |
| | | if (System.currentTimeMillis() - lastSeenAt > CURRENT_STATION_TIMEOUT_MS) { |
| | | if (traceRegistry != null) { |
| | | traceRegistry.markTimeout(original.getTaskNo(), traceVersion, lastCurrentStationId, |
| | | buildDetails("timeoutMs", CURRENT_STATION_TIMEOUT_MS)); |
| | | } |
| | | break; |
| | | } |
| | | Thread.sleep(500L); |
| | | continue; |
| | | } |
| | | |
| | | lastSeenAt = System.currentTimeMillis(); |
| | | Integer previousCurrentStationId = lastCurrentStationId; |
| | | if (traceRegistry != null) { |
| | | traceRegistry.updateProgress(original.getTaskNo(), traceVersion, currentStation.getStationId(), |
| | | equalsInteger(previousCurrentStationId, currentStation.getStationId()) ? null : "CURRENT_STATION_CHANGE", |
| | | "输送任务当前位置已更新", |
| | | buildDetails("stationId", currentStation.getStationId())); |
| | | } |
| | | lastCurrentStationId = currentStation.getStationId(); |
| | | if (!firstRun && currentStation.isRunBlock()) { |
| | | if (traceRegistry != null) { |
| | | traceRegistry.markBlocked(original.getTaskNo(), traceVersion, currentStation.getStationId(), |
| | | buildDetails("blockedStationId", currentStation.getStationId())); |
| | | } |
| | | break; |
| | | } |
| | | |
| | | int currentIndex = effectiveFullPath.indexOf(currentStation.getStationId()); |
| | | if (currentIndex < 0) { |
| | | Thread.sleep(500L); |
| | | firstRun = false; |
| | | continue; |
| | | } |
| | | |
| | | int remaining = effectiveFullPath.size() - currentIndex - 1; |
| | | if (remaining <= 0) { |
| | | if (traceRegistry != null) { |
| | | traceRegistry.markFinished(original.getTaskNo(), traceVersion, currentStation.getStationId(), |
| | | buildDetails("targetStationId", original.getTargetStaNo())); |
| | | } |
| | | break; |
| | | } |
| | | |
| | | StationCommand currentSegmentCommand = localPlan.getSegmentCommands().get(segCursor); |
| | | int currentSegEndIndex = safeIndex(currentSegmentCommand.getSegmentEndIndex()); |
| | | int remainingSegment = Math.max(0, currentSegEndIndex - currentIndex); |
| | | if (remainingSegment <= NEXT_SEGMENT_ADVANCE_NODE_COUNT |
| | | && segCursor < localPlan.getSegmentCommands().size() - 1) { |
| | | StationCommand nextCommand = localPlan.getSegmentCommands().get(segCursor + 1); |
| | | if (sendSegmentWithRetry(nextCommand)) { |
| | | segCursor++; |
| | | if (traceRegistry != null) { |
| | | traceRegistry.markSegmentIssued(original.getTaskNo(), traceVersion, nextCommand, |
| | | "NEXT_SEGMENT_SENT", "输送任务下一段已提前下发", |
| | | buildSegmentDetails(nextCommand)); |
| | | } |
| | | } |
| | | } |
| | | Thread.sleep(500L); |
| | | firstRun = false; |
| | | } catch (Exception ignore) { |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | |
| | | private boolean sendSegmentWithRetry(StationCommand command) { |
| | | while (true) { |
| | | if (isTaskMoveReset(command == null ? null : command.getTaskNo())) { |
| | | return false; |
| | | } |
| | | CommandResponse commandResponse = commandSender.apply(command); |
| | | if (commandResponse == null) { |
| | | sleepQuietly(200L); |
| | | continue; |
| | | } |
| | | if (commandResponse.getResult()) { |
| | | return true; |
| | | } |
| | | sleepQuietly(200L); |
| | | } |
| | | } |
| | | |
| | | private boolean isTaskMoveReset(Integer taskNo) { |
| | | if (taskNo == null || redisUtil == null) { |
| | | return false; |
| | | } |
| | | Object cancel = redisUtil.get(RedisKeyType.DEVICE_STATION_MOVE_RESET.key + taskNo); |
| | | return cancel != null; |
| | | } |
| | | |
| | | private StationProtocol findCurrentStationByTask(Integer taskNo) { |
| | | try { |
| | | com.zy.asrs.service.DeviceConfigService deviceConfigService = SpringUtils.getBean(com.zy.asrs.service.DeviceConfigService.class); |
| | | if (deviceConfigService == null) { |
| | | return null; |
| | | } |
| | | List<DeviceConfig> devpList = deviceConfigService.list(new QueryWrapper<DeviceConfig>() |
| | | .eq("device_type", String.valueOf(SlaveType.Devp))); |
| | | for (DeviceConfig dc : devpList) { |
| | | com.zy.core.thread.StationThread thread = (com.zy.core.thread.StationThread) SlaveConnection.get(SlaveType.Devp, dc.getDeviceNo()); |
| | | if (thread == null) { |
| | | continue; |
| | | } |
| | | Map<Integer, StationProtocol> statusMap = thread.getStatusMap(); |
| | | if (statusMap == null || statusMap.isEmpty()) { |
| | | continue; |
| | | } |
| | | for (StationProtocol protocol : statusMap.values()) { |
| | | if (protocol.getTaskNo() != null && protocol.getTaskNo().equals(taskNo) && protocol.isLoading()) { |
| | | return protocol; |
| | | } |
| | | } |
| | | } |
| | | } catch (Exception ignore) { |
| | | return null; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private List<StationTaskTraceSegmentVo> buildTraceSegments(List<StationCommand> segmentCommands) { |
| | | List<StationTaskTraceSegmentVo> result = new ArrayList<>(); |
| | | if (segmentCommands == null) { |
| | | return result; |
| | | } |
| | | for (StationCommand command : segmentCommands) { |
| | | if (command == null) { |
| | | continue; |
| | | } |
| | | StationTaskTraceSegmentVo item = new StationTaskTraceSegmentVo(); |
| | | item.setSegmentNo(command.getSegmentNo()); |
| | | item.setSegmentCount(command.getSegmentCount()); |
| | | item.setStationId(command.getStationId()); |
| | | item.setTargetStationId(command.getTargetStaNo()); |
| | | item.setSegmentStartIndex(command.getSegmentStartIndex()); |
| | | item.setSegmentEndIndex(command.getSegmentEndIndex()); |
| | | item.setSegmentPath(copyIntegerList(command.getNavigatePath())); |
| | | item.setIssued(Boolean.FALSE); |
| | | result.add(item); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | private void bindCommands(List<StationCommand> segmentCommands, int traceVersion, int pathOffset) { |
| | | if (segmentCommands == null) { |
| | | return; |
| | | } |
| | | for (StationCommand command : segmentCommands) { |
| | | if (command == null) { |
| | | continue; |
| | | } |
| | | command.setTraceVersion(traceVersion); |
| | | if (command.getSegmentStartIndex() != null) { |
| | | command.setSegmentStartIndex(command.getSegmentStartIndex() + pathOffset); |
| | | } |
| | | if (command.getSegmentEndIndex() != null) { |
| | | command.setSegmentEndIndex(command.getSegmentEndIndex() + pathOffset); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private Map<String, Object> buildSegmentDetails(StationCommand command) { |
| | | Map<String, Object> details = new LinkedHashMap<>(); |
| | | if (command != null) { |
| | | details.put("segmentNo", command.getSegmentNo()); |
| | | details.put("segmentCount", command.getSegmentCount()); |
| | | details.put("segmentPath", copyIntegerList(command.getNavigatePath())); |
| | | details.put("segmentStartIndex", command.getSegmentStartIndex()); |
| | | details.put("segmentEndIndex", command.getSegmentEndIndex()); |
| | | details.put("traceVersion", command.getTraceVersion()); |
| | | } |
| | | return details; |
| | | } |
| | | |
| | | private Map<String, Object> buildDetails(Object... keyValues) { |
| | | Map<String, Object> details = new LinkedHashMap<>(); |
| | | if (keyValues == null) { |
| | | return details; |
| | | } |
| | | for (int i = 0; i + 1 < keyValues.length; i += 2) { |
| | | Object key = keyValues[i]; |
| | | if (key != null) { |
| | | details.put(String.valueOf(key), keyValues[i + 1]); |
| | | } |
| | | } |
| | | return details; |
| | | } |
| | | |
| | | private List<Integer> copyIntegerList(List<Integer> source) { |
| | | List<Integer> result = new ArrayList<>(); |
| | | if (source == null) { |
| | | return result; |
| | | } |
| | | result.addAll(source); |
| | | return result; |
| | | } |
| | | |
| | | private int safeIndex(Integer value) { |
| | | return value == null ? -1 : value; |
| | | } |
| | | |
| | | private boolean equalsInteger(Integer a, Integer b) { |
| | | return a != null && a.equals(b); |
| | | } |
| | | |
| | | private void sleepQuietly(long millis) { |
| | | try { |
| | | Thread.sleep(millis); |
| | | } catch (Exception ignore) { |
| | | } |
| | | super(deviceConfig, redisUtil, commandSender); |
| | | } |
| | | } |
| | |
| | | import com.zy.common.service.CommonService; |
| | | import com.zy.common.utils.NavigateUtils; |
| | | import com.zy.common.utils.RedisUtil; |
| | | import com.zy.core.move.StationMoveCoordinator; |
| | | import com.zy.core.move.StationMoveSession; |
| | | import com.zy.core.News; |
| | | import com.zy.core.cache.MessageQueue; |
| | | import com.zy.core.cache.SlaveConnection; |
| | |
| | | private StationTaskLoopService stationTaskLoopService; |
| | | @Autowired |
| | | private WrkAnalysisService wrkAnalysisService; |
| | | @Autowired |
| | | private StationMoveCoordinator stationMoveCoordinator; |
| | | |
| | | //执行输送站点入库任务 |
| | | public synchronized void stationInExecute() { |
| | |
| | | wrkMast.setModiTime(now); |
| | | if (wrkMastService.updateById(wrkMast)) { |
| | | wrkAnalysisService.markOutboundStationStart(wrkMast, now); |
| | | offerDevpCommandWithDedup(stationObjModel.getDeviceNo(), command, "crnStationOutExecute"); |
| | | boolean offered = offerDevpCommandWithDedup(stationObjModel.getDeviceNo(), command, "crnStationOutExecute"); |
| | | if (offered && stationMoveCoordinator != null) { |
| | | stationMoveCoordinator.recordDispatch( |
| | | wrkMast.getWrkNo(), |
| | | stationProtocol.getStationId(), |
| | | "crnStationOutExecute", |
| | | command, |
| | | false |
| | | ); |
| | | } |
| | | News.info("输送站点出库命令下发成功,站点号={},工作号={},命令数据={}", stationProtocol.getStationId(), wrkMast.getWrkNo(), JSON.toJSONString(command)); |
| | | redisUtil.set(RedisKeyType.STATION_OUT_EXECUTE_LIMIT.key + stationProtocol.getStationId(), "lock", 5); |
| | | redisUtil.del(RedisKeyType.CRN_OUT_TASK_COMPLETE_STATION_INFO.key + wrkMast.getWrkNo()); |
| | |
| | | if (wrkMast == null || wrkMast.getWrkNo() == null) { |
| | | return; |
| | | } |
| | | if (stationMoveCoordinator != null) { |
| | | stationMoveCoordinator.finishSession(wrkMast.getWrkNo()); |
| | | } |
| | | Date now = new Date(); |
| | | wrkMast.setWrkSts(WrkStsType.STATION_RUN_COMPLETE.sts); |
| | | wrkMast.setIoTime(now); |
| | |
| | | } |
| | | |
| | | if (complete) { |
| | | if (stationMoveCoordinator != null) { |
| | | stationMoveCoordinator.finishSession(wrkNo); |
| | | } |
| | | wrkMast.setWrkSts(WrkStsType.COMPLETE_OUTBOUND.sts); |
| | | wrkMast.setIoTime(new Date()); |
| | | wrkMastService.updateById(wrkMast); |
| | |
| | | stationProtocol.getTaskNo(), |
| | | currentTaskBufferCommandCount); |
| | | continue; |
| | | } |
| | | if (stationMoveCoordinator != null) { |
| | | stationMoveCoordinator.cancelSession(wrkMast.getWrkNo()); |
| | | } |
| | | //运行堵塞,重新申请任务 |
| | | String response = wmsOperateUtils.applyReassignTaskLocNo(wrkMast.getWrkNo(), stationProtocol.getStationId()); |
| | |
| | | } |
| | | |
| | | if (wrkMastService.updateById(wrkMast)) { |
| | | offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "checkStationRunBlock_direct"); |
| | | boolean offered = offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "checkStationRunBlock_direct"); |
| | | if (!offered) { |
| | | continue; |
| | | } |
| | | if (stationMoveCoordinator != null) { |
| | | stationMoveCoordinator.recordDispatch( |
| | | wrkMast.getWrkNo(), |
| | | stationProtocol.getStationId(), |
| | | "checkStationRunBlock_direct", |
| | | command, |
| | | false |
| | | ); |
| | | } |
| | | } |
| | | } else { |
| | | News.error("请求WMS接口失败!!!response:{}", response); |
| | |
| | | continue; |
| | | } |
| | | |
| | | if (stationMoveCoordinator != null) { |
| | | stationMoveCoordinator.cancelSession(wrkMast.getWrkNo()); |
| | | } |
| | | resetSegmentMoveCommandsBeforeReroute(wrkMast.getWrkNo()); |
| | | offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "checkStationRunBlock_reroute"); |
| | | boolean offered = offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "checkStationRunBlock_reroute"); |
| | | if (!offered) { |
| | | continue; |
| | | } |
| | | syncOutOrderWatchState(wrkMast, stationProtocol.getStationId(), outOrderStationIds, dispatchDecision, command); |
| | | if (stationMoveCoordinator != null) { |
| | | stationMoveCoordinator.recordDispatch( |
| | | wrkMast.getWrkNo(), |
| | | stationProtocol.getStationId(), |
| | | "checkStationRunBlock_reroute", |
| | | command, |
| | | dispatchDecision != null && dispatchDecision.isCircle() |
| | | ); |
| | | } |
| | | News.info("输送站点堵塞后重新计算路径命令下发成功,站点号={},工作号={},命令数据={}", stationProtocol.getStationId(), wrkMast.getWrkNo(), JSON.toJSONString(command)); |
| | | } |
| | | } |
| | |
| | | if (countCurrentTaskBufferCommands(stationProtocol.getTaskBufferItems(), stationProtocol.getTaskNo()) > 0) { |
| | | continue; |
| | | } |
| | | if (isWatchingCircleTransit(wrkMast.getWrkNo(), stationProtocol.getStationId())) { |
| | | continue; |
| | | } |
| | | |
| | | if (isWatchingCircleArrival(wrkMast.getWrkNo(), stationProtocol.getStationId())) { |
| | | continue; |
| | | } |
| | | |
| | | Double pathLenFactor = resolveOutboundPathLenFactor(wrkMast); |
| | | OutOrderDispatchDecision dispatchDecision = resolveOutboundDispatchDecision( |
| | |
| | | News.taskInfo(wrkMast.getWrkNo(), "获取输送线命令失败"); |
| | | continue; |
| | | } |
| | | if (stationMoveCoordinator != null |
| | | && stationMoveCoordinator.shouldSuppressDispatch(wrkMast.getWrkNo(), stationProtocol.getStationId(), command)) { |
| | | continue; |
| | | } |
| | | if (!tryAcquireOutOrderDispatchLock(wrkMast.getWrkNo(), stationProtocol.getStationId())) { |
| | | continue; |
| | | } |
| | | resetSegmentMoveCommandsBeforeReroute(wrkMast.getWrkNo()); |
| | | boolean offered = offerDevpCommandWithDedup(stationObjModel.getDeviceNo(), command, "checkStationOutOrder"); |
| | | if (!offered) { |
| | | continue; |
| | | } |
| | | syncOutOrderWatchState(wrkMast, stationProtocol.getStationId(), outOrderStationIds, dispatchDecision, command); |
| | | offerDevpCommandWithDedup(stationObjModel.getDeviceNo(), command, "checkStationOutOrder"); |
| | | if (stationMoveCoordinator != null) { |
| | | stationMoveCoordinator.recordDispatch( |
| | | wrkMast.getWrkNo(), |
| | | stationProtocol.getStationId(), |
| | | "checkStationOutOrder", |
| | | command, |
| | | dispatchDecision != null && dispatchDecision.isCircle() |
| | | ); |
| | | } |
| | | News.info(dispatchDecision.isCircle() ? "{}任务进行绕圈" : "{}任务直接去目标点", wrkMast.getWrkNo()); |
| | | } |
| | | } |
| | |
| | | News.taskInfo(wrkMast.getWrkNo(), "获取输送线命令失败"); |
| | | continue; |
| | | } |
| | | if (stationMoveCoordinator != null |
| | | && stationMoveCoordinator.shouldSuppressDispatch(wrkMast.getWrkNo(), stationProtocol.getStationId(), command)) { |
| | | continue; |
| | | } |
| | | if (!tryAcquireOutOrderDispatchLock(wrkMast.getWrkNo(), stationProtocol.getStationId())) { |
| | | continue; |
| | | } |
| | | resetSegmentMoveCommandsBeforeReroute(wrkMast.getWrkNo()); |
| | | boolean offered = offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "watchCircleStation"); |
| | | if (!offered) { |
| | | continue; |
| | | } |
| | | syncOutOrderWatchState(wrkMast, stationProtocol.getStationId(), outOrderList, dispatchDecision, command); |
| | | offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "watchCircleStation"); |
| | | if (stationMoveCoordinator != null) { |
| | | stationMoveCoordinator.recordDispatch( |
| | | wrkMast.getWrkNo(), |
| | | stationProtocol.getStationId(), |
| | | "watchCircleStation", |
| | | command, |
| | | dispatchDecision != null && dispatchDecision.isCircle() |
| | | ); |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | private boolean isWatchingCircleArrival(Integer wrkNo, Integer stationId) { |
| | | if (stationMoveCoordinator != null) { |
| | | StationMoveSession session = stationMoveCoordinator.loadSession(wrkNo); |
| | | if (session != null && session.isActive() && stationId != null) { |
| | | if (stationId.equals(session.getNextDecisionStationId())) { |
| | | return true; |
| | | } |
| | | if (session.containsStation(stationId)) { |
| | | return false; |
| | | } |
| | | } |
| | | } |
| | | StationCommand command = getWatchCircleCommand(wrkNo); |
| | | return command != null && stationId != null && stationId.equals(command.getTargetStaNo()); |
| | | } |
| | | |
| | | private boolean isWatchingCircleTransit(Integer wrkNo, Integer stationId) { |
| | | if (stationMoveCoordinator != null) { |
| | | StationMoveSession session = stationMoveCoordinator.loadSession(wrkNo); |
| | | if (session != null && session.isActive() && stationId != null) { |
| | | if (stationId.equals(session.getNextDecisionStationId())) { |
| | | return false; |
| | | } |
| | | if (session.containsStation(stationId)) { |
| | | return true; |
| | | } |
| | | } |
| | | } |
| | | StationCommand command = getWatchCircleCommand(wrkNo); |
| | | if (command == null || stationId == null || Objects.equals(stationId, command.getTargetStaNo())) { |
| | | return false; |
| | |
| | | return; |
| | | } |
| | | |
| | | if (stationMoveCoordinator != null) { |
| | | stationMoveCoordinator.cancelSession(stationProtocol.getTaskNo()); |
| | | } |
| | | redisUtil.set(RedisKeyType.CHECK_STATION_IDLE_RECOVER_LIMIT_.key + stationProtocol.getTaskNo(), "lock", STATION_IDLE_RECOVER_LIMIT_SECONDS); |
| | | resetSegmentMoveCommandsBeforeReroute(stationProtocol.getTaskNo()); |
| | | int clearedCommandCount = clearIssuedMoveCommandsDuringIdleStay(idleTrack, stationProtocol.getTaskNo(), stationProtocol.getStationId()); |
| | |
| | | return; |
| | | } |
| | | |
| | | offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "checkStationIdleRecover"); |
| | | boolean offered = offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "checkStationIdleRecover"); |
| | | if (!offered) { |
| | | return; |
| | | } |
| | | syncOutOrderWatchState(wrkMast, stationProtocol.getStationId(), outOrderList, dispatchDecision, command); |
| | | if (stationMoveCoordinator != null) { |
| | | stationMoveCoordinator.recordDispatch( |
| | | wrkMast.getWrkNo(), |
| | | stationProtocol.getStationId(), |
| | | "checkStationIdleRecover", |
| | | command, |
| | | dispatchDecision != null && dispatchDecision.isCircle() |
| | | ); |
| | | } |
| | | saveStationTaskIdleTrack(new StationTaskIdleTrack(wrkMast.getWrkNo(), stationProtocol.getStationId(), System.currentTimeMillis())); |
| | | News.info("输送站点任务停留{}秒未运行,已重新计算路径并重启运行,站点号={},目标站={},工作号={},清理旧分段命令数={},命令数据={}", |
| | | STATION_IDLE_RECOVER_SECONDS, stationProtocol.getStationId(), moveStaNo, wrkMast.getWrkNo(), clearedCommandCount, JSON.toJSONString(command)); |
| | |
| | | |
| | | private String buildStationCommandDispatchDedupKey(Integer deviceNo, StationCommand command) { |
| | | return RedisKeyType.STATION_COMMAND_DISPATCH_DEDUP_.key |
| | | + deviceNo + "_" |
| | | + command.getTaskNo() + "_" |
| | | + command.getStationId(); |
| | | + command.getStationId() + "_" |
| | | + (stationMoveCoordinator == null ? Integer.toHexString(buildFallbackPathSignature(command).hashCode()) |
| | | : stationMoveCoordinator.buildPathSignatureHash(command)); |
| | | } |
| | | |
| | | private String buildFallbackPathSignature(StationCommand command) { |
| | | if (command == null) { |
| | | return ""; |
| | | } |
| | | return String.valueOf(command.getCommandType()) |
| | | + "_" + command.getStationId() |
| | | + "_" + command.getTargetStaNo() |
| | | + "_" + command.getNavigatePath() |
| | | + "_" + command.getLiftTransferPath() |
| | | + "_" + command.getOriginalNavigatePath(); |
| | | } |
| | | |
| | | private int clearIssuedMoveCommandsDuringIdleStay(StationTaskIdleTrack idleTrack, |