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.CrnTaskModeType;
|
import com.zy.core.model.CommandResponse;
|
import com.zy.core.model.command.CrnCommand;
|
import com.zy.core.network.api.ZyCrnConnectApi;
|
import com.zy.core.network.entity.ZyCrnStatusEntity;
|
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.Executors;
|
|
public class ZyCrnFakeConnect implements ZyCrnConnectApi {
|
|
private ZyCrnStatusEntity crnStatus;
|
private DeviceConfig deviceConfig;
|
private final ExecutorService executor = Executors.newSingleThreadExecutor();
|
|
public ZyCrnFakeConnect(DeviceConfig deviceConfig) {
|
this.deviceConfig = deviceConfig;
|
this.crnStatus = JSON.parseObject(deviceConfig.getFakeInitStatus(), ZyCrnStatusEntity.class);
|
}
|
|
@Override
|
public boolean connect() {
|
return true;
|
}
|
|
@Override
|
public boolean disconnect() {
|
return true;
|
}
|
|
@Override
|
public ZyCrnStatusEntity getStatus() {
|
return this.crnStatus;
|
}
|
|
@Override
|
public CommandResponse sendCommand(CrnCommand command) {
|
CommandResponse response = new CommandResponse(false);
|
if (command.getTaskMode().intValue() == CrnTaskModeType.LOC_MOVE.id) {
|
//取放货
|
executor.submit(() -> commandTake(command));
|
} else if (command.getTaskMode().intValue() == CrnTaskModeType.CRN_MOVE.id) {
|
//移动
|
executor.submit(() -> commandMove(command));
|
} else if (command.getTaskMode().intValue() == CrnTaskModeType.NONE.id) {
|
//复位
|
executor.submit(() -> commandTaskComplete(command));
|
}
|
response.setResult(true);
|
return response;
|
}
|
|
private void commandTaskComplete(CrnCommand command) {
|
this.crnStatus.setTaskNo(0);
|
this.crnStatus.setStatus(CrnStatusType.IDLE.id);
|
}
|
|
private void commandMove(CrnCommand 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.setTaskNo(taskNo);
|
this.crnStatus.setStatus(CrnStatusType.MOVING.id);
|
moveY(this.crnStatus.getBay(), destinationPosY);
|
moveZ(this.crnStatus.getLevel(), destinationPosZ);
|
this.crnStatus.setStatus(CrnStatusType.WAITING.id);
|
}
|
|
private void commandTake(CrnCommand 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.setTaskNo(taskNo);
|
this.crnStatus.setMode(taskMode);
|
this.crnStatus.setStatus(CrnStatusType.FETCH_MOVING.id);
|
moveY(this.crnStatus.getBay(), sourcePosY);
|
moveZ(this.crnStatus.getLevel(), sourcePosZ);
|
this.crnStatus.setStatus(CrnStatusType.FETCHING.id);
|
sleep(2000);
|
|
this.crnStatus.setLoaded(1);
|
this.crnStatus.setStatus(CrnStatusType.PUT_MOVING.id);
|
moveY(this.crnStatus.getBay(), destinationPosY);
|
moveZ(this.crnStatus.getLevel(), destinationPosZ);
|
this.crnStatus.setStatus(CrnStatusType.PUTTING.id);
|
sleep(2000);
|
this.crnStatus.setLoaded(0);
|
this.crnStatus.setStatus(CrnStatusType.WAITING.id);
|
}
|
|
private void moveZ(int sourcePosZ, int destinationPosZ) {
|
if(destinationPosZ - sourcePosZ > 0) {
|
int moveLength = destinationPosZ - sourcePosZ;
|
int initSourcePosZ = sourcePosZ;
|
for(int i = 0; i < moveLength; i++) {
|
initSourcePosZ++;
|
this.crnStatus.setLevel(initSourcePosZ);
|
sleep(1000);
|
}
|
}else {
|
int moveLength = sourcePosZ - destinationPosZ;
|
int initSourcePosZ = sourcePosZ;
|
for(int i = 0; i < moveLength; i++) {
|
initSourcePosZ--;
|
this.crnStatus.setLevel(initSourcePosZ);
|
sleep(1000);
|
}
|
}
|
}
|
|
private void moveY(int sourcePosY, int destinationPosY) {
|
if(destinationPosY - sourcePosY > 0) {
|
int moveLength = destinationPosY - sourcePosY;
|
int initSourcePosY = sourcePosY;
|
for(int i = 0; i < moveLength; i++) {
|
initSourcePosY++;
|
this.crnStatus.setBay(initSourcePosY);
|
sleep(1000);
|
}
|
}else {
|
int moveLength = sourcePosY - destinationPosY;
|
int initSourcePosY = sourcePosY;
|
for(int i = 0; i < moveLength; i++) {
|
initSourcePosY--;
|
this.crnStatus.setBay(initSourcePosY);
|
sleep(1000);
|
}
|
}
|
}
|
|
private void sleep(long ms) {
|
try {
|
Thread.sleep(ms);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|