| | |
| | | import com.zy.core.model.protocol.StationProtocol; |
| | | import com.zy.core.network.ZyStationConnectDriver; |
| | | import com.zy.core.network.entity.ZyStationStatusEntity; |
| | | import com.zy.core.task.DeviceAsyncLogPublisher; |
| | | import com.zy.core.thread.support.RecentStationArrivalTracker; |
| | | import com.zy.core.thread.support.StationErrLogSupport; |
| | | import com.zy.core.utils.DeviceLogRedisKeyBuilder; |
| | | import com.zy.core.thread.support.StationTaskLocationRegistry; |
| | | |
| | | import java.text.MessageFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.LinkedHashSet; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | public class StationV5StatusReader { |
| | | |
| | | private final DeviceConfig deviceConfig; |
| | | private final RedisUtil redisUtil; |
| | | private final RecentStationArrivalTracker recentArrivalTracker; |
| | | private final DeviceAsyncLogPublisher devpAsyncLogPublisher; |
| | | private final StationTaskLocationRegistry stationTaskLocationRegistry; |
| | | private final List<StationProtocol> statusList = new ArrayList<>(); |
| | | private final Map<Integer, StationProtocol> statusMap = new HashMap<>(); |
| | | private volatile List<Integer> taskNoList = new ArrayList<>(); |
| | | private boolean initialized = false; |
| | | private long deviceDataLogTime = System.currentTimeMillis(); |
| | |
| | | this.deviceConfig = deviceConfig; |
| | | this.redisUtil = redisUtil; |
| | | this.recentArrivalTracker = recentArrivalTracker; |
| | | this.devpAsyncLogPublisher = SpringUtils.getBean(DeviceAsyncLogPublisher.class); |
| | | this.stationTaskLocationRegistry = SpringUtils.getBean(StationTaskLocationRegistry.class); |
| | | } |
| | | |
| | | public void readStatus(ZyStationConnectDriver zyStationConnectDriver) { |
| | |
| | | StationProtocol stationProtocol = new StationProtocol(); |
| | | stationProtocol.setStationId(entity.getStationId()); |
| | | statusList.add(stationProtocol); |
| | | statusMap.put(entity.getStationId(), stationProtocol); |
| | | } |
| | | initialized = true; |
| | | } |
| | | |
| | | int deviceLogCollectTime = initialized ? Utils.getDeviceLogCollectTime() : 200; |
| | | List<ZyStationStatusEntity> zyStationStatusEntities = zyStationConnectDriver.getStatus(); |
| | | if (zyStationStatusEntities == null || zyStationStatusEntities.isEmpty()) { |
| | | return; |
| | | } |
| | | LinkedHashSet<Integer> taskNoSet = new LinkedHashSet<>(); |
| | | LinkedHashSet<Integer> loadingTaskNoSet = new LinkedHashSet<>(); |
| | | long observeAt = System.currentTimeMillis(); |
| | | for (ZyStationStatusEntity statusEntity : zyStationStatusEntities) { |
| | | for (StationProtocol stationProtocol : statusList) { |
| | | if (stationProtocol.getStationId().equals(statusEntity.getStationId())) { |
| | | stationProtocol.setTaskNo(statusEntity.getTaskNo()); |
| | | stationProtocol.setTargetStaNo(statusEntity.getTargetStaNo()); |
| | | stationProtocol.setAutoing(statusEntity.isAutoing()); |
| | | stationProtocol.setLoading(statusEntity.isLoading()); |
| | | stationProtocol.setInEnable(statusEntity.isInEnable()); |
| | | stationProtocol.setOutEnable(statusEntity.isOutEnable()); |
| | | stationProtocol.setEmptyMk(statusEntity.isEmptyMk()); |
| | | stationProtocol.setFullPlt(statusEntity.isFullPlt()); |
| | | stationProtocol.setPalletHeight(statusEntity.getPalletHeight()); |
| | | stationProtocol.setError(statusEntity.getError()); |
| | | stationProtocol.setErrorMsg(statusEntity.getErrorMsg()); |
| | | stationProtocol.setBarcode(statusEntity.getBarcode()); |
| | | stationProtocol.setRunBlock(statusEntity.isRunBlock()); |
| | | stationProtocol.setEnableIn(statusEntity.isEnableIn()); |
| | | stationProtocol.setWeight(statusEntity.getWeight()); |
| | | stationProtocol.setTaskWriteIdx(statusEntity.getTaskWriteIdx()); |
| | | stationProtocol.setTaskBufferItems(statusEntity.getTaskBufferItems()); |
| | | stationProtocol.setIoMode(statusEntity.getIoMode()); |
| | | stationProtocol.setInBarcodeError(statusEntity.isInBarcodeError()); |
| | | if (statusEntity.getTaskNo() != null && statusEntity.getTaskNo() > 0) { |
| | | taskNoSet.add(statusEntity.getTaskNo()); |
| | | } |
| | | if (statusEntity.getTaskBufferItems() != null) { |
| | | statusEntity.getTaskBufferItems().forEach(item -> { |
| | | Integer bufferTaskNo = item == null ? null : item.getTaskNo(); |
| | | if (bufferTaskNo != null && bufferTaskNo > 0) { |
| | | taskNoSet.add(bufferTaskNo); |
| | | } |
| | | }); |
| | | } |
| | | recentArrivalTracker.observe(statusEntity.getStationId(), statusEntity.getTaskNo(), statusEntity.isLoading()); |
| | | if (statusEntity == null || statusEntity.getStationId() == null) { |
| | | continue; |
| | | } |
| | | StationProtocol stationProtocol = statusMap.get(statusEntity.getStationId()); |
| | | if (stationProtocol == null) { |
| | | continue; |
| | | } |
| | | stationProtocol.setTaskNo(statusEntity.getTaskNo()); |
| | | stationProtocol.setTargetStaNo(statusEntity.getTargetStaNo()); |
| | | stationProtocol.setAutoing(statusEntity.isAutoing()); |
| | | stationProtocol.setLoading(statusEntity.isLoading()); |
| | | stationProtocol.setInEnable(statusEntity.isInEnable()); |
| | | stationProtocol.setOutEnable(statusEntity.isOutEnable()); |
| | | stationProtocol.setEmptyMk(statusEntity.isEmptyMk()); |
| | | stationProtocol.setFullPlt(statusEntity.isFullPlt()); |
| | | stationProtocol.setPalletHeight(statusEntity.getPalletHeight()); |
| | | stationProtocol.setError(statusEntity.getError()); |
| | | stationProtocol.setErrorMsg(statusEntity.getErrorMsg()); |
| | | stationProtocol.setBarcode(statusEntity.getBarcode()); |
| | | stationProtocol.setRunBlock(statusEntity.isRunBlock()); |
| | | stationProtocol.setEnableIn(statusEntity.isEnableIn()); |
| | | stationProtocol.setWeight(statusEntity.getWeight()); |
| | | stationProtocol.setTaskWriteIdx(statusEntity.getTaskWriteIdx()); |
| | | stationProtocol.setTaskBufferItems(statusEntity.getTaskBufferItems()); |
| | | stationProtocol.setIoMode(statusEntity.getIoMode()); |
| | | stationProtocol.setInBarcodeError(statusEntity.isInBarcodeError()); |
| | | if (statusEntity.getTaskNo() != null && statusEntity.getTaskNo() > 0) { |
| | | taskNoSet.add(statusEntity.getTaskNo()); |
| | | if (statusEntity.isLoading()) { |
| | | loadingTaskNoSet.add(statusEntity.getTaskNo()); |
| | | } |
| | | |
| | | if (!Cools.isEmpty(stationProtocol.getSystemWarning())) { |
| | | if (stationProtocol.isAutoing() && !stationProtocol.isLoading()) { |
| | | stationProtocol.setSystemWarning(""); |
| | | } |
| | | if (statusEntity.getTaskBufferItems() != null) { |
| | | statusEntity.getTaskBufferItems().forEach(item -> { |
| | | Integer bufferTaskNo = item == null ? null : item.getTaskNo(); |
| | | if (bufferTaskNo != null && bufferTaskNo > 0) { |
| | | taskNoSet.add(bufferTaskNo); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | recentArrivalTracker.observe(statusEntity.getStationId(), statusEntity.getTaskNo(), statusEntity.isLoading()); |
| | | syncTaskLocation(statusEntity, observeAt); |
| | | if (!Cools.isEmpty(stationProtocol.getSystemWarning()) |
| | | && stationProtocol.isAutoing() |
| | | && !stationProtocol.isLoading()) { |
| | | stationProtocol.setSystemWarning(""); |
| | | } |
| | | } |
| | | taskNoList = new ArrayList<>(taskNoSet); |
| | | if (stationTaskLocationRegistry != null) { |
| | | stationTaskLocationRegistry.cleanupByDevice(deviceConfig.getDeviceNo(), loadingTaskNoSet); |
| | | } |
| | | |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功", |
| | | DateUtils.convert(new Date()), deviceConfig.getDeviceNo())); |
| | | StationErrLogSupport.sync(deviceConfig, redisUtil, statusList); |
| | | |
| | | if (System.currentTimeMillis() - deviceDataLogTime > deviceLogCollectTime) { |
| | | DeviceDataLog deviceDataLog = new DeviceDataLog(); |
| | | deviceDataLog.setOriginData(JSON.toJSONString(zyStationStatusEntities)); |
| | | deviceDataLog.setWcsData(JSON.toJSONString(statusList)); |
| | | deviceDataLog.setType(String.valueOf(SlaveType.Devp)); |
| | | deviceDataLog.setDeviceNo(deviceConfig.getDeviceNo()); |
| | | deviceDataLog.setCreateTime(new Date()); |
| | | |
| | | redisUtil.set(DeviceLogRedisKeyBuilder.build(deviceDataLog), deviceDataLog, 60 * 60 * 24); |
| | | Date createTime = new Date(); |
| | | HashMap<Integer, ZyStationStatusEntity> originDataMap = buildStationStatusMap(zyStationStatusEntities); |
| | | for (int i = 0; i < statusList.size(); i++) { |
| | | StationProtocol stationProtocol = statusList.get(i); |
| | | if (stationProtocol == null || stationProtocol.getStationId() == null) { |
| | | continue; |
| | | } |
| | | DeviceDataLog deviceDataLog = new DeviceDataLog(); |
| | | ZyStationStatusEntity originEntity = originDataMap.get(stationProtocol.getStationId()); |
| | | deviceDataLog.setOriginData(originEntity == null ? null : JSON.toJSONString(originEntity)); |
| | | deviceDataLog.setWcsData(JSON.toJSONString(stationProtocol)); |
| | | deviceDataLog.setType(String.valueOf(SlaveType.Devp)); |
| | | deviceDataLog.setDeviceNo(deviceConfig.getDeviceNo()); |
| | | deviceDataLog.setStationId(stationProtocol.getStationId()); |
| | | deviceDataLog.setCreateTime(createTime); |
| | | devpAsyncLogPublisher.publishLatest(deviceDataLog); |
| | | } |
| | | deviceDataLogTime = System.currentTimeMillis(); |
| | | } |
| | | } |
| | | |
| | | private HashMap<Integer, ZyStationStatusEntity> buildStationStatusMap(List<ZyStationStatusEntity> zyStationStatusEntities) { |
| | | HashMap<Integer, ZyStationStatusEntity> map = new HashMap<>(); |
| | | if (zyStationStatusEntities == null) { |
| | | return map; |
| | | } |
| | | for (ZyStationStatusEntity statusEntity : zyStationStatusEntities) { |
| | | if (statusEntity == null || statusEntity.getStationId() == null) { |
| | | continue; |
| | | } |
| | | map.put(statusEntity.getStationId(), statusEntity); |
| | | } |
| | | return map; |
| | | } |
| | | |
| | | public List<StationProtocol> getStatusList() { |
| | |
| | | public List<Integer> getTaskNoList() { |
| | | return taskNoList; |
| | | } |
| | | |
| | | private void syncTaskLocation(ZyStationStatusEntity statusEntity, long observeAt) { |
| | | if (stationTaskLocationRegistry == null || statusEntity == null) { |
| | | return; |
| | | } |
| | | Integer taskNo = statusEntity.getTaskNo(); |
| | | if (taskNo != null && taskNo > 0 && statusEntity.isLoading()) { |
| | | stationTaskLocationRegistry.update( |
| | | taskNo, |
| | | deviceConfig.getDeviceNo(), |
| | | statusEntity.getStationId(), |
| | | true, |
| | | statusEntity.isRunBlock(), |
| | | observeAt |
| | | ); |
| | | return; |
| | | } |
| | | stationTaskLocationRegistry.remove(taskNo, deviceConfig.getDeviceNo(), statusEntity.getStationId()); |
| | | } |
| | | } |