| | |
| | | package com.zy.core.network.fake; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.core.common.SpringUtils; |
| | | import com.zy.asrs.entity.DeviceConfig; |
| | | import com.zy.common.model.NavigateNode; |
| | | import com.zy.common.utils.NavigateUtils; |
| | | import com.zy.core.model.CommandResponse; |
| | | import com.zy.core.model.command.StationCommand; |
| | | import com.zy.core.network.api.ZyStationConnectApi; |
| | | import com.zy.core.network.entity.ZyStationStatusEntity; |
| | | import java.util.Collections; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | import java.util.concurrent.ExecutorService; |
| | |
| | | */ |
| | | public class ZyStationFakeConnect implements ZyStationConnectApi { |
| | | |
| | | private ZyStationStatusEntity status; |
| | | private List<ZyStationStatusEntity> statusList = new ArrayList<>(); |
| | | private final DeviceConfig deviceConfig; |
| | | private final ExecutorService executor = Executors.newSingleThreadExecutor(); |
| | | // 允许并行执行多个命令任务(固定线程池)。如需更高并发可调整大小。 |
| | | private final ExecutorService executor = Executors |
| | | .newFixedThreadPool(Math.max(2, Runtime.getRuntime().availableProcessors())); |
| | | |
| | | public ZyStationFakeConnect(DeviceConfig deviceConfig) { |
| | | this.deviceConfig = deviceConfig; |
| | | this.status = JSON.parseObject(deviceConfig.getFakeInitStatus(), ZyStationStatusEntity.class); |
| | | if (this.status == null) { |
| | | this.status = new ZyStationStatusEntity(); |
| | | this.status.setStationId(deviceConfig.getDeviceNo()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | @Override |
| | | public boolean disconnect() { |
| | | executor.shutdownNow(); |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public List<ZyStationStatusEntity> getStatus() { |
| | | return Collections.singletonList(status); |
| | | if (this.statusList.isEmpty()) { |
| | | this.statusList = JSON.parseArray(deviceConfig.getFakeInitStatus(), ZyStationStatusEntity.class); |
| | | |
| | | for (ZyStationStatusEntity status : this.statusList) { |
| | | status.setAutoing(true);// 模拟自动运行 |
| | | status.setLoading(false);// 模拟有物 |
| | | status.setInEnable(true);// 模拟可入 |
| | | status.setOutEnable(true);// 模拟可出 |
| | | status.setEmptyMk(false);// 模拟空板信号 |
| | | status.setFullPlt(false);// 模拟满托盘 |
| | | status.setPalletHeight(0);// 模拟托盘高度为0 |
| | | status.setError(0);// 模拟无报警 |
| | | status.setBarcode("");// 模拟无条码 |
| | | } |
| | | } |
| | | |
| | | return this.statusList; |
| | | } |
| | | |
| | | @Override |
| | | public CommandResponse sendCommand(StationCommand command) { |
| | | CommandResponse response = new CommandResponse(false); |
| | | executor.submit(() -> handleCommand(command)); |
| | | response.setResult(true); |
| | | return response; |
| | | executor.submit(() -> { |
| | | try { |
| | | handleCommand(command); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | }); |
| | | return new CommandResponse(true, "命令已受理(异步执行)"); |
| | | } |
| | | |
| | | private void handleCommand(StationCommand command) { |
| | | // 简单的模拟:设置工作号和目标站,并模拟有物/可入/可出状态切换 |
| | | this.status.setTaskNo(command.getTaskNo()); |
| | | this.status.setTargetStaNo(command.getTargetStaNo()); |
| | | NavigateUtils navigateUtils = SpringUtils.getBean(NavigateUtils.class); |
| | | if (navigateUtils == null) { |
| | | return; |
| | | } |
| | | |
| | | // 模拟到站过程 |
| | | this.status.setAutoing(true); |
| | | this.status.setLoading(true); |
| | | Integer taskNo = command.getTaskNo(); |
| | | Integer stationId = command.getStationId(); |
| | | Integer targetStationId = command.getTargetStaNo(); |
| | | |
| | | List<NavigateNode> navigateNodes = null; |
| | | |
| | | try { |
| | | navigateNodes = navigateUtils.calcByStationId(stationId, targetStationId); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | if (navigateNodes == null) { |
| | | return; |
| | | } |
| | | |
| | | Integer lastStationId = null; |
| | | for (int i = 0; i < navigateNodes.size(); i++) { |
| | | NavigateNode navigateNode = navigateNodes.get(i); |
| | | JSONObject valueObject = JSON.parseObject(navigateNode.getNodeValue()); |
| | | Integer currentStationId = valueObject.getInteger("stationId"); |
| | | ZyStationStatusEntity status = statusList.stream() |
| | | .filter(item -> item.getStationId().equals(currentStationId)).findFirst().orElse(null); |
| | | if (status == null) { |
| | | continue; |
| | | } |
| | | |
| | | try { |
| | | while (true) { |
| | | ZyStationStatusEntity nextStatus = statusList.stream() |
| | | .filter(item -> item.getStationId().equals(currentStationId)).findFirst().orElse(null); |
| | | if (nextStatus == null) { |
| | | continue; |
| | | } |
| | | |
| | | if (nextStatus.getTaskNo() == 0) { |
| | | break; |
| | | } |
| | | |
| | | sleep(100); |
| | | } |
| | | } catch (Exception e) { |
| | | continue; |
| | | } |
| | | |
| | | synchronized (status) { |
| | | status.setTaskNo(taskNo); |
| | | status.setTargetStaNo(targetStationId); |
| | | status.setLoading(true); |
| | | } |
| | | |
| | | if (lastStationId != null) { |
| | | Integer finalLastStationId = lastStationId; |
| | | ZyStationStatusEntity lastStatus = statusList.stream() |
| | | .filter(item -> item.getStationId().equals(finalLastStationId)).findFirst().orElse(null); |
| | | if (lastStatus != null) { |
| | | synchronized (lastStatus) { |
| | | lastStatus.setTaskNo(0); |
| | | lastStatus.setTargetStaNo(0); |
| | | lastStatus.setLoading(false); |
| | | } |
| | | } |
| | | } |
| | | lastStationId = currentStationId; |
| | | sleep(1000); |
| | | } |
| | | |
| | | // 模拟放下托盘 |
| | | this.status.setInEnable(true); |
| | | this.status.setOutEnable(false); |
| | | sleep(1000); |
| | | |
| | | // 完成任务,复位状态 |
| | | this.status.setLoading(false); |
| | | this.status.setAutoing(false); |
| | | this.status.setTaskNo(0); |
| | | this.status.setInEnable(false); |
| | | this.status.setOutEnable(true); |
| | | sleep(10000); |
| | | if (lastStationId != null) { |
| | | Integer finalLastStationId = lastStationId; |
| | | ZyStationStatusEntity lastStatus = statusList.stream() |
| | | .filter(item -> item.getStationId().equals(finalLastStationId)).findFirst().orElse(null); |
| | | if (lastStatus != null) { |
| | | synchronized (lastStatus) { |
| | | lastStatus.setTaskNo(0); |
| | | lastStatus.setTargetStaNo(0); |
| | | lastStatus.setLoading(false); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | private void sleep(long ms) { |