package com.zy.core.network.fake; import com.alibaba.fastjson.JSON; import com.zy.asrs.entity.DeviceConfig; import com.zy.core.enums.CrnStatusType; import com.zy.core.enums.DualCrnTaskModeType; import com.zy.core.model.CommandResponse; import com.zy.core.model.command.DualCrnCommand; import com.zy.core.network.api.ZyDualCrnConnectApi; import com.zy.core.network.entity.ZyDualCrnStatusEntity; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ZyDualCrnFakeConnect implements ZyDualCrnConnectApi { private ZyDualCrnStatusEntity crnStatus; private DeviceConfig deviceConfig; private final ExecutorService executor = Executors.newSingleThreadExecutor(); public ZyDualCrnFakeConnect(DeviceConfig deviceConfig) { this.deviceConfig = deviceConfig; this.crnStatus = JSON.parseObject(deviceConfig.getFakeInitStatus(), ZyDualCrnStatusEntity.class); } @Override public boolean connect() { return true; } @Override public boolean disconnect() { try { executor.shutdownNow(); } catch (Exception ignore) {} return true; } @Override public ZyDualCrnStatusEntity getStatus() { return this.crnStatus; } @Override public CommandResponse sendCommand(DualCrnCommand command) { CommandResponse response = new CommandResponse(false); if (command.getTaskMode().intValue() == DualCrnTaskModeType.TRANSFER.id) { //取放货 executor.submit(() -> commandTake(command)); } else if (command.getTaskMode().intValue() == DualCrnTaskModeType.PICK.id) { //取货 executor.submit(() -> commandPick(command)); } else if (command.getTaskMode().intValue() == DualCrnTaskModeType.PUT.id) { //放货 executor.submit(() -> commandPut(command)); } else if (command.getTaskMode().intValue() == DualCrnTaskModeType.MOVE.id) { //移动 executor.submit(() -> commandMove(command)); } else if (command.getTaskMode().intValue() == DualCrnTaskModeType.CONFIRM.id) { //复位 executor.submit(() -> commandTaskComplete(command)); } response.setResult(true); return response; } private void commandTaskComplete(DualCrnCommand command) { if(command.getStation() == 1) { this.crnStatus.setTaskNo(0); this.crnStatus.setStatus(CrnStatusType.IDLE.id); }else { this.crnStatus.setTaskNoTwo(0); this.crnStatus.setStatusTwo(CrnStatusType.IDLE.id); } } private void commandMove(DualCrnCommand command) { int destinationPosX = command.getDestinationPosX().intValue(); int destinationPosY = command.getDestinationPosY().intValue(); int destinationPosZ = command.getDestinationPosZ().intValue(); int taskMode = command.getTaskMode().intValue(); int taskNo = command.getTaskNo().intValue(); if(command.getStation() == 1) { this.crnStatus.setTaskNo(taskNo); this.crnStatus.setStatus(CrnStatusType.MOVING.id); moveY(this.crnStatus.getBay(), destinationPosY, command.getStation().intValue()); moveZ(this.crnStatus.getLevel(), destinationPosZ, command.getStation().intValue()); this.crnStatus.setStatus(CrnStatusType.WAITING.id); }else { this.crnStatus.setTaskNoTwo(taskNo); this.crnStatus.setStatusTwo(CrnStatusType.MOVING.id); moveY(this.crnStatus.getBayTwo(), destinationPosY, command.getStation().intValue()); moveZ(this.crnStatus.getLevelTwo(), destinationPosZ, command.getStation().intValue()); this.crnStatus.setStatusTwo(CrnStatusType.WAITING.id); } } private void commandTake(DualCrnCommand command) { int sourcePosX = command.getSourcePosX().intValue(); int sourcePosY = command.getSourcePosY().intValue(); int sourcePosZ = command.getSourcePosZ().intValue(); int destinationPosX = command.getDestinationPosX().intValue(); int destinationPosY = command.getDestinationPosY().intValue(); int destinationPosZ = command.getDestinationPosZ().intValue(); int taskMode = command.getTaskMode().intValue(); int taskNo = command.getTaskNo().intValue(); this.crnStatus.setMode(taskMode); if(command.getStation() == 1) { this.crnStatus.setTaskNo(taskNo); this.crnStatus.setStatus(CrnStatusType.FETCH_MOVING.id); moveY(this.crnStatus.getBay(), sourcePosY, command.getStation().intValue()); moveZ(this.crnStatus.getLevel(), sourcePosZ, command.getStation().intValue()); this.crnStatus.setStatus(CrnStatusType.FETCHING.id); sleep(2000); if (Thread.currentThread().isInterrupted()) { return; } this.crnStatus.setLoaded(1); this.crnStatus.setStatus(CrnStatusType.PUT_MOVING.id); moveY(this.crnStatus.getBay(), destinationPosY, command.getStation().intValue()); moveZ(this.crnStatus.getLevel(), destinationPosZ, command.getStation().intValue()); this.crnStatus.setStatus(CrnStatusType.PUTTING.id); sleep(2000); if (Thread.currentThread().isInterrupted()) { return; } this.crnStatus.setLoaded(0); this.crnStatus.setStatus(CrnStatusType.WAITING.id); }else { this.crnStatus.setTaskNoTwo(taskNo); this.crnStatus.setStatusTwo(CrnStatusType.FETCH_MOVING.id); moveY(this.crnStatus.getBayTwo(), sourcePosY, command.getStation().intValue()); moveZ(this.crnStatus.getLevelTwo(), sourcePosZ, command.getStation().intValue()); this.crnStatus.setStatusTwo(CrnStatusType.FETCHING.id); sleep(2000); if (Thread.currentThread().isInterrupted()) { return; } this.crnStatus.setLoadedTwo(1); this.crnStatus.setStatusTwo(CrnStatusType.PUT_MOVING.id); moveY(this.crnStatus.getBayTwo(), destinationPosY, command.getStation().intValue()); moveZ(this.crnStatus.getLevelTwo(), destinationPosZ, command.getStation().intValue()); this.crnStatus.setStatusTwo(CrnStatusType.PUTTING.id); sleep(2000); if (Thread.currentThread().isInterrupted()) { return; } this.crnStatus.setLoadedTwo(0); this.crnStatus.setStatusTwo(CrnStatusType.WAITING.id); } } private void commandPick(DualCrnCommand command) { int destinationPosX = command.getDestinationPosX().intValue(); int destinationPosY = command.getDestinationPosY().intValue(); int destinationPosZ = command.getDestinationPosZ().intValue(); int taskMode = command.getTaskMode().intValue(); int taskNo = command.getTaskNo().intValue(); this.crnStatus.setMode(taskMode); if(command.getStation() == 1) { this.crnStatus.setTaskNo(taskNo); this.crnStatus.setStatus(CrnStatusType.FETCH_MOVING.id); moveY(this.crnStatus.getBay(), destinationPosY, command.getStation().intValue()); moveZ(this.crnStatus.getLevel(), destinationPosZ, command.getStation().intValue()); this.crnStatus.setStatus(CrnStatusType.FETCHING.id); sleep(2000); if (Thread.currentThread().isInterrupted()) { return; } this.crnStatus.setLoaded(1); this.crnStatus.setStatus(CrnStatusType.WAITING.id); }else { this.crnStatus.setTaskNoTwo(taskNo); this.crnStatus.setStatusTwo(CrnStatusType.FETCH_MOVING.id); moveY(this.crnStatus.getBayTwo(), destinationPosY, command.getStation().intValue()); moveZ(this.crnStatus.getLevelTwo(), destinationPosZ, command.getStation().intValue()); this.crnStatus.setStatusTwo(CrnStatusType.FETCHING.id); sleep(2000); if (Thread.currentThread().isInterrupted()) { return; } this.crnStatus.setLoadedTwo(1); this.crnStatus.setStatusTwo(CrnStatusType.WAITING.id); } } private void commandPut(DualCrnCommand command) { int destinationPosX = command.getDestinationPosX().intValue(); int destinationPosY = command.getDestinationPosY().intValue(); int destinationPosZ = command.getDestinationPosZ().intValue(); int taskMode = command.getTaskMode().intValue(); int taskNo = command.getTaskNo().intValue(); this.crnStatus.setMode(taskMode); if(command.getStation() == 1) { this.crnStatus.setTaskNo(taskNo); this.crnStatus.setStatus(CrnStatusType.PUT_MOVING.id); moveY(this.crnStatus.getBay(), destinationPosY, command.getStation().intValue()); moveZ(this.crnStatus.getLevel(), destinationPosZ, command.getStation().intValue()); this.crnStatus.setStatus(CrnStatusType.PUTTING.id); sleep(2000); if (Thread.currentThread().isInterrupted()) { return; } this.crnStatus.setLoaded(0); this.crnStatus.setStatus(CrnStatusType.WAITING.id); }else { this.crnStatus.setTaskNoTwo(taskNo); this.crnStatus.setStatusTwo(CrnStatusType.PUT_MOVING.id); moveY(this.crnStatus.getBayTwo(), destinationPosY, command.getStation().intValue()); moveZ(this.crnStatus.getLevelTwo(), destinationPosZ, command.getStation().intValue()); this.crnStatus.setStatusTwo(CrnStatusType.PUTTING.id); sleep(2000); if (Thread.currentThread().isInterrupted()) { return; } this.crnStatus.setLoadedTwo(0); this.crnStatus.setStatusTwo(CrnStatusType.WAITING.id); } } private void moveZ(int sourcePosZ, int destinationPosZ, int station) { if(destinationPosZ - sourcePosZ > 0) { int moveLength = destinationPosZ - sourcePosZ; int initSourcePosZ = sourcePosZ; for(int i = 0; i < moveLength; i++) { initSourcePosZ++; if(station == 1) { this.crnStatus.setLevel(initSourcePosZ); }else { this.crnStatus.setLevelTwo(initSourcePosZ); } sleep(1000); if (Thread.currentThread().isInterrupted()) { return; } } }else { int moveLength = sourcePosZ - destinationPosZ; int initSourcePosZ = sourcePosZ; for(int i = 0; i < moveLength; i++) { initSourcePosZ--; if(station == 1) { this.crnStatus.setLevel(initSourcePosZ); }else { this.crnStatus.setLevelTwo(initSourcePosZ); } this.crnStatus.setLevel(initSourcePosZ); sleep(1000); if (Thread.currentThread().isInterrupted()) { return; } } } } private void moveY(int sourcePosY, int destinationPosY, int station) { if(destinationPosY - sourcePosY > 0) { int moveLength = destinationPosY - sourcePosY; int initSourcePosY = sourcePosY; for(int i = 0; i < moveLength; i++) { initSourcePosY++; if(station == 1) { this.crnStatus.setBay(initSourcePosY); }else { this.crnStatus.setBayTwo(initSourcePosY); } sleep(1000); if (Thread.currentThread().isInterrupted()) { return; } } }else { int moveLength = sourcePosY - destinationPosY; int initSourcePosY = sourcePosY; for(int i = 0; i < moveLength; i++) { initSourcePosY--; if(station == 1) { this.crnStatus.setBay(initSourcePosY); }else { this.crnStatus.setBayTwo(initSourcePosY); } sleep(1000); if (Thread.currentThread().isInterrupted()) { return; } } } } private void sleep(long ms) { try { Thread.sleep(ms); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }