package com.zy.acs.fake.service; import com.zy.acs.fake.config.RedisProperties; import com.zy.acs.fake.domain.DynamicNode; import com.zy.acs.framework.common.Cools; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.stereotype.Component; import org.springframework.util.StopWatch; 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.ArrayList; import java.util.List; /** * Created by vincent on 2023/6/14 */ @Slf4j @Component("mapService") public class MapService { @Autowired private MapDataDispatcher mapDataDispatcher; @Autowired private RedisProperties redisProperties; public synchronized void unlockPath0(String agvNo, String codeData) { try { Resource resource = new ClassPathResource("unlock.py"); File tempScript = null; InputStream is = resource.getInputStream(); tempScript = File.createTempFile("unlock", ".py"); tempScript.deleteOnExit(); Files.copy(is, tempScript.toPath(), StandardCopyOption.REPLACE_EXISTING); tempScript.setExecutable(true); ProcessBuilder processBuilder = new ProcessBuilder( "python" , tempScript.getAbsolutePath() , redisProperties.getHost() , redisProperties.getPassword() , String.valueOf(redisProperties.getPort()) , String.valueOf(redisProperties.getIndex()) , agvNo , codeData ); processBuilder.redirectErrorStream(true); Process process = processBuilder.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; StringBuilder builder = new StringBuilder(); while ((line = reader.readLine()) != null) { builder.append(line); } int exitCode = process.waitFor(); if (exitCode != 0) { log.error("Python script exited with error code: {}", exitCode); log.error("python error:{}", builder.toString()); return; } reader.close(); if (builder.length() <= 0) { return; } String result = builder.toString(); if (!Cools.isEmpty(result)) { if (!"1".equals(result)) { log.error("Fail python"); } } } catch (Exception e) { log.error("MapService.unlockPath", e); } } public synchronized void unlockPath(String agvNo, String codeData) { try { long startTime = System.currentTimeMillis(); if (Cools.isEmpty(agvNo, codeData)) { return; } Integer lev = null; String[][] codeMatrix = mapDataDispatcher.getCodeMatrix(null); int[] codeMatrixIdx = mapDataDispatcher.getCodeMatrixIdx(lev, codeData); DynamicNode[][] dynamicMatrix = mapDataDispatcher.getDynamicMatrix(lev); DynamicNode dynamicNode = dynamicMatrix[codeMatrixIdx[0]][codeMatrixIdx[1]]; Integer serial = dynamicNode.getSerial(); long time = dynamicNode.getTime(); List resetCodeList = new ArrayList<>(); for (int i = 0; i < dynamicMatrix.length; i++) { for (int j = 0; j < dynamicMatrix[i].length; j++) { if (i == codeMatrixIdx[0] && j == codeMatrixIdx[1]) { continue; } DynamicNode node = dynamicMatrix[i][j]; if (node.getVehicle().equals(agvNo)) { if (node.getSerial() < serial || node.getTime() != time) { resetCodeList.add(codeMatrix[i][j]); } } } } if (!Cools.isEmpty(resetCodeList)) { mapDataDispatcher.clearDynamicMatrixByCodeList(lev, resetCodeList); } log.info("解锁路径函数花费时间为:{}毫秒......", System.currentTimeMillis() - startTime); } catch (Exception e) { log.error("MapService.unlockPath", e); } } }