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();
|
|
private long fetchPutDurationMs() { return FakeConfigSupport.getLong(FakeConfigKeys.FAKE_CRN_FETCH_PUT_DURATION_MS); }
|
private long levelStepDurationMs() { return FakeConfigSupport.getLong(FakeConfigKeys.FAKE_CRN_LEVEL_STEP_DURATION_MS); }
|
private long bayStepDurationMs() { return FakeConfigSupport.getLong(FakeConfigKeys.FAKE_CRN_BAY_STEP_DURATION_MS); }
|
private long resetDurationMs() { return FakeConfigSupport.getLong(FakeConfigKeys.FAKE_CRN_RESET_DURATION_MS); }
|
private long bayMin() { return FakeConfigSupport.getLong(FakeConfigKeys.FAKE_CRN_BAY_MIN); }
|
private long bayMax() { return FakeConfigSupport.getLong(FakeConfigKeys.FAKE_CRN_BAY_MAX); }
|
private long laserMin() { return FakeConfigSupport.getLong(FakeConfigKeys.FAKE_CRN_LASER_MIN); }
|
private long laserMax() { return FakeConfigSupport.getLong(FakeConfigKeys.FAKE_CRN_LASER_MAX); }
|
|
public ZyCrnFakeConnect(DeviceConfig deviceConfig) {
|
this.deviceConfig = deviceConfig;
|
this.crnStatus = JSON.parseObject(deviceConfig.getFakeInitStatus(), ZyCrnStatusEntity.class);
|
if (this.crnStatus != null && this.crnStatus.getBay() != null) {
|
updateLaserByBay(this.crnStatus.getBay());
|
}
|
}
|
|
private void updateLaserByBay(int bay) {
|
long bayMin = bayMin();
|
long bayMax = bayMax();
|
long laserMin = laserMin();
|
long laserMax = laserMax();
|
if (bayMax <= bayMin || laserMax <= laserMin) {
|
this.crnStatus.setLaserValue((int) laserMin);
|
return;
|
}
|
long normalizedBay = Math.max(bayMin, Math.min(bayMax, bay));
|
double ratio = (double) (normalizedBay - bayMin) / (double) (bayMax - bayMin);
|
long computed = Math.round(laserMin + ratio * (laserMax - laserMin));
|
long clamped = Math.max(laserMin, Math.min(laserMax, computed));
|
this.crnStatus.setLaserValue((int) clamped);
|
}
|
|
@Override
|
public boolean connect() {
|
return true;
|
}
|
|
@Override
|
public boolean disconnect() {
|
try { executor.shutdownNow(); } catch (Exception ignore) {}
|
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.RESET.id) {
|
//复位
|
executor.submit(() -> commandTaskComplete(command));
|
}
|
response.setResult(true);
|
return response;
|
}
|
|
private void commandTaskComplete(CrnCommand command) {
|
sleep(resetDurationMs());
|
this.crnStatus.setLoaded(0);
|
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(fetchPutDurationMs());
|
if (Thread.currentThread().isInterrupted()) {
|
return;
|
}
|
|
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(fetchPutDurationMs());
|
if (Thread.currentThread().isInterrupted()) {
|
return;
|
}
|
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(levelStepDurationMs());
|
if (Thread.currentThread().isInterrupted()) {
|
return;
|
}
|
}
|
}else {
|
int moveLength = sourcePosZ - destinationPosZ;
|
int initSourcePosZ = sourcePosZ;
|
for(int i = 0; i < moveLength; i++) {
|
initSourcePosZ--;
|
this.crnStatus.setLevel(initSourcePosZ);
|
sleep(levelStepDurationMs());
|
if (Thread.currentThread().isInterrupted()) {
|
return;
|
}
|
}
|
}
|
}
|
|
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);
|
updateLaserByBay(initSourcePosY);
|
sleep(bayStepDurationMs());
|
if (Thread.currentThread().isInterrupted()) {
|
return;
|
}
|
}
|
}else {
|
int moveLength = sourcePosY - destinationPosY;
|
int initSourcePosY = sourcePosY;
|
for(int i = 0; i < moveLength; i++) {
|
initSourcePosY--;
|
this.crnStatus.setBay(initSourcePosY);
|
updateLaserByBay(initSourcePosY);
|
sleep(bayStepDurationMs());
|
if (Thread.currentThread().isInterrupted()) {
|
return;
|
}
|
}
|
}
|
}
|
|
private void sleep(long ms) {
|
try {
|
Thread.sleep(ms);
|
} catch (InterruptedException e) {
|
Thread.currentThread().interrupt();
|
}
|
}
|
}
|