|  |  |  | 
|---|
|  |  |  | import com.zy.acs.manager.manager.service.AgvService; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.service.CodeService; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.apache.commons.lang.time.StopWatch; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.context.annotation.DependsOn; | 
|---|
|  |  |  | import org.springframework.core.io.ClassPathResource; | 
|---|
|  |  |  | import org.springframework.core.io.Resource; | 
|---|
|  |  |  | import org.springframework.stereotype.Component; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.PostConstruct; | 
|---|
|  |  |  | import javax.annotation.PreDestroy; | 
|---|
|  |  |  | import java.io.*; | 
|---|
|  |  |  | import java.io.BufferedReader; | 
|---|
|  |  |  | import java.io.File; | 
|---|
|  |  |  | import java.io.InputStream; | 
|---|
|  |  |  | import java.io.InputStreamReader; | 
|---|
|  |  |  | import java.nio.file.Files; | 
|---|
|  |  |  | import java.nio.file.StandardCopyOption; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | @Component | 
|---|
|  |  |  | @DependsOn("getMapDataDispatcher") | 
|---|
|  |  |  | public class AvoidWaveCalculator { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static final ReentrantLock lock = new ReentrantLock(Boolean.TRUE); | 
|---|
|  |  |  | 
|---|
|  |  |  | public void execute() { | 
|---|
|  |  |  | this.singleThreadExecutor = Executors.newSingleThreadExecutor(); | 
|---|
|  |  |  | this.singleThreadExecutor.execute(() -> { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | try { Thread.sleep(200); } catch (InterruptedException ignore) {} | 
|---|
|  |  |  | try { Thread.sleep(500); } catch (InterruptedException ignore) {} | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.calcDynamicNodeWhenBoot(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //            while (!Thread.currentThread().isInterrupted()) { | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                this.calcWaveScope(); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                try { Thread.sleep(500); } catch (InterruptedException ignore) {} | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | Integer lev = MapDataDispatcher.MAP_DEFAULT_LEV; | 
|---|
|  |  |  | boolean lockAcquired = false; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | StopWatch stopWatch = new StopWatch(); | 
|---|
|  |  |  | stopWatch.start(); | 
|---|
|  |  |  | long startTime = System.currentTimeMillis(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if (!(lockAcquired = lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS))) { | 
|---|
|  |  |  | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return this.calcWaveScopeByPython(lev); | 
|---|
|  |  |  | //            return this.calcWaveScopeByJava(lev); | 
|---|
|  |  |  | //            return this.calcWaveScopeByPython(lev); | 
|---|
|  |  |  | return this.calcWaveScopeByJava(lev); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | lock.unlock(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | stopWatch.stop(); | 
|---|
|  |  |  | if (stopWatch.getTime() > 100) { | 
|---|
|  |  |  | log.info("滤波函数花费时间为:{}毫秒......", stopWatch.getTime()); | 
|---|
|  |  |  | long during = System.currentTimeMillis() - startTime; | 
|---|
|  |  |  | if (during > 50) { | 
|---|
|  |  |  | log.info("滤波函数花费时间为:{}毫秒......", during); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private boolean calcWaveScopeByJava(Integer lev) throws Exception { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | AgvModel agvModel = agvModelService.selectByType(AgvModelType.CTU_BOX_TRANSPORT_AGV.toString());    // can be optimized | 
|---|
|  |  |  | Double avoidDistance = MapDataUtils.getVehicleWaveSafeDistance(agvModel.getDiameter(), MapDataConstant.MAX_DISTANCE_BETWEEN_ADJACENT_AGV_FACTOR); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // java | 
|---|
|  |  |  | String[][] codeMatrix = mapDataDispatcher.getCodeMatrix(lev); | 
|---|
|  |  |  | String[][] waveMatrix = mapDataDispatcher.initWaveMatrix(lev); | 
|---|
|  |  |  | 
|---|
|  |  |  | DynamicNode dynamicNode = dynamicMatrix[i][j]; | 
|---|
|  |  |  | String vehicle = dynamicNode.getVehicle(); | 
|---|
|  |  |  | if (!DynamicNodeType.ACCESS.val.equals(vehicle) && !DynamicNodeType.BLOCK.val.equals(vehicle)) { | 
|---|
|  |  |  | AgvModel agvModel = agvModelService.getById(agvService.selectByUuid(vehicle).getAgvModel());    // can be optimized | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Double avoidDistance = MapDataUtils.getVehicleWaveSafeDistance(agvModel.getDiameter(), MapDataConstant.MAX_DISTANCE_BETWEEN_ADJACENT_AGV_FACTOR); | 
|---|
|  |  |  | List<NavigateNode> includeList = mapService.getWaveScopeByCode(lev, codeMatrix[i][j], avoidDistance); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (NavigateNode navigateNode : includeList) { | 
|---|
|  |  |  | 
|---|
|  |  |  | return scriptFile; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void syncWaveBySingleVeh(String agvNo, String codeData) { | 
|---|
|  |  |  | if (Cools.isEmpty(agvNo, codeData)) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | boolean lockAcquired = false; | 
|---|
|  |  |  | Integer lev = MapDataDispatcher.MAP_DEFAULT_LEV; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if (!(lockAcquired = lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS))) { | 
|---|
|  |  |  | log.warn("AvoidWaveCalculator syncWaveBySingleVeh fail, cause can not acquire lock ..."); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Agv agv = agvService.selectByUuid(agvNo); | 
|---|
|  |  |  | AgvModel agvModel = agvModelService.getById(agv.getAgvModel()); | 
|---|
|  |  |  | Double avoidDistance = MapDataUtils.getVehicleWaveSafeDistance(agvModel.getDiameter(), MapDataConstant.MAX_DISTANCE_BETWEEN_ADJACENT_AGV_FACTOR); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String[][] waveMatrix = mapDataDispatcher.getWaveMatrix(lev); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<NavigateNode> includeList = mapService.getWaveScopeByCode(lev, codeData, avoidDistance); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (NavigateNode navigateNode : includeList) { | 
|---|
|  |  |  | String waveNode = waveMatrix[navigateNode.getX()][navigateNode.getY()]; | 
|---|
|  |  |  | waveMatrix[navigateNode.getX()][navigateNode.getY()] = MapDataUtils.generateWaveNode(waveNode, agv.getUuid()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | mapDataDispatcher.setWaveMatrix(lev, waveMatrix); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | log.error("AvoidWaveCalculator.syncWaveBySingleVeh fail", e); | 
|---|
|  |  |  | } finally { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (lockAcquired) { | 
|---|
|  |  |  | lock.unlock(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void calcDynamicNodeWhenBoot() { | 
|---|
|  |  |  | List<Agv> agvList = agvService.list(new LambdaQueryWrapper<>()); | 
|---|
|  |  |  | DynamicNode[][] dynamicMatrix = mapDataDispatcher.getDynamicMatrix(null); | 
|---|
|  |  |  | 
|---|
|  |  |  | DynamicNode dynamicNode = dynamicMatrix[codeMatrixIdx[0]][codeMatrixIdx[1]]; | 
|---|
|  |  |  | String vehicle = dynamicNode.getVehicle(); | 
|---|
|  |  |  | if (vehicle.equals(DynamicNodeType.ACCESS.val)) { | 
|---|
|  |  |  | mapDataDispatcher.modifyDynamicMatrix(null, Utils.singletonList(code.getData()), agv.getUuid()); | 
|---|
|  |  |  | mapDataDispatcher.modifyDynamicMatrix(null, Utils.singletonList(codeMatrixIdx), agv.getUuid()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|