#
luxiaotao1123
2024-12-17 db576395617232b8526ff59cbb29014437af7c8c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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<String> 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);
        }
 
    }
 
}