自动化立体仓库 - WCS系统
Junjie
2023-05-11 79b3bca0befd3d247d9b9c17ea4287ed19b97920
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.zy.common.utils;
 
import com.alibaba.fastjson.JSON;
import com.core.common.SpringUtils;
import com.zy.asrs.entity.BasMap;
import com.zy.common.model.MapNode;
import com.zy.common.model.NavigateNode;
import com.zy.core.enums.ShuttleTaskModeType;
import org.springframework.stereotype.Component;
 
import java.io.*;
import java.util.ArrayList;
import java.util.List;
 
/**
 * A*算法地图获取类
 */
@Component
public class NavigateMapData {
 
    private Integer lev;//地图楼层
 
    public NavigateMapData() {
        this.lev = 1;
    }
 
    public NavigateMapData(Integer lev) {
        this.lev = lev;
    }
 
    public int[][] getData() {
        return getData(ShuttleTaskModeType.PAK_IN.id);
    }
 
    public int[][] getData(Integer mapType) {
        try {
            String mapFilename = "map_" + lev + ".json";
 
            String fileName = this.getClass().getClassLoader().getResource(mapFilename).getPath();//获取文件路径
            File file = new File(fileName);
            StringBuffer stringBuffer = new StringBuffer();
            if (file.isFile() && file.exists()) {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
                BufferedReader br = new BufferedReader(isr);
                String lineTxt = null;
                while ((lineTxt = br.readLine()) != null) {
                    stringBuffer.append(lineTxt);
                }
                br.close();
 
                //解析json地图数据
                ArrayList arrayList = JSON.parseObject(stringBuffer.toString(), ArrayList.class);
                List<List<MapNode>> lists = filterMap(mapType, arrayList);//过滤地图数据
                int[][] map = new int[lists.size()][];
                int j = 0;
                for (List<MapNode> list : lists) {
                    int[] tmp = new int[list.size()];
                    int i = 0;
                    for (MapNode mapNode : list) {
                        //将数据添加进二维数组
                        tmp[i++] = mapNode.getValue();
                    }
                    //数据添加进一维数组
                    map[j++] = tmp;
                }
 
                return map;
            } else {
                System.out.println("文件不存在!");
            }
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        return null;
    }
 
    /**
     * 尝试从redis获取数据
     */
    public int[][] getDataFromRedis(Integer mapType) {
        RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
        Object o = redisUtil.get("realtimeBasMap_" + lev);
        if (o == null) {
            return null;
        }
 
        BasMap basMap = JSON.parseObject(o.toString(), BasMap.class);
        return this.getDataFormString(basMap.getData(), mapType);
    }
 
    /**
     * 从List数据中获取地图
     */
    public int[][] getDataFormString(String data, Integer mapType) {
        ArrayList arrayList = JSON.parseObject(data, ArrayList.class);
        List<List<MapNode>> lists = filterMap(mapType, arrayList);//过滤地图数据
        int[][] map = new int[lists.size()][];
        int j = 0;
        for (List<MapNode> list : lists) {
            int[] tmp = new int[list.size()];
            int i = 0;
            for (MapNode mapNode : list) {
                //将数据添加进二维数组
                tmp[i++] = mapNode.getValue();
            }
            //数据添加进一维数组
            map[j++] = tmp;
        }
 
        return map;
    }
 
    //获取JSON格式数据
    public List<List<MapNode>> getJsonData(Integer mapType) {
        try {
            String mapFilename = "map_" + lev + ".json";
 
            String fileName = this.getClass().getClassLoader().getResource(mapFilename).getPath();//获取文件路径
            File file = new File(fileName);
            StringBuffer stringBuffer = new StringBuffer();
            if (file.isFile() && file.exists()) {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
                BufferedReader br = new BufferedReader(isr);
                String lineTxt = null;
                while ((lineTxt = br.readLine()) != null) {
                    stringBuffer.append(lineTxt);
                }
                br.close();
 
                //解析json地图数据
                ArrayList arrayList = JSON.parseObject(stringBuffer.toString(), ArrayList.class);
                List<List<MapNode>> lists = filterMap(mapType, arrayList);//过滤地图数据
 
                return lists;
            } else {
                System.out.println("文件不存在!");
            }
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        return null;
    }
 
    /**
     * 过滤地图数据,入库操作则过滤出库的禁用库位,出库操作则过滤入库的禁用库位
     */
    public List<List<MapNode>> filterMap(Integer mapType,List arrayList) {
        List<List<MapNode>> lists = new ArrayList<>();
        for (int i = 0; i < arrayList.size(); i++) {
            Object obj = arrayList.get(i);
            List<MapNode> list = JSON.parseArray(obj.toString(), MapNode.class);
            for (int j = 0; j < list.size(); j++) {
                MapNode mapNode = list.get(j);
                if (mapType == ShuttleTaskModeType.PAK_IN.id) {
                    //入库地图
                    if (mapNode.getData().equals("IN_X")) {
                        //禁止使用的库位,设置为-1
                        mapNode.setValue(-1);
                    }
                }else if(mapType == ShuttleTaskModeType.PAK_OUT.id){
                    //出库地图
                    if (mapNode.getData().equals("OUT_X")) {
                        //禁止使用的库位,设置为-1
                        mapNode.setValue(-1);
                    }
                }
                list.set(j, mapNode);
            }
            lists.add(list);
        }
 
        return lists;
    }
 
    /**
     * 写入路径节点数据到redis地图中
     * lock为true 禁用库位,lock为false恢复库位
     */
    public boolean writeNavigateNodeToRedisMap(List<NavigateNode> nodes, boolean lock) {
        RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
        Object o = redisUtil.get("realtimeBasMap_" + lev);
        if (o == null) {
            return false;
        }
 
        BasMap basMap = JSON.parseObject(o.toString(), BasMap.class);
        ArrayList arrayList = JSON.parseObject(basMap.getData(), ArrayList.class);
        List<List<MapNode>> lists = filterMap(-1, arrayList);//获取全部地图数据
 
        for (NavigateNode node : nodes) {
            if (node.getZ() != lev) {
                continue;
            }
 
            List<MapNode> listX = lists.get(node.getX());
            MapNode mapNode = listX.get(node.getY());
            if (lock) {
                mapNode.setValue(-999);//禁用库位
            }else {
                if (node.getX() == 11 || node.getX() == 19) {
                    mapNode.setValue(3);//恢复库位,母轨道
                }else {
                    mapNode.setValue(0);//恢复库位,普通库位
                }
            }
 
            listX.set(node.getY(), mapNode);
            lists.set(node.getX(), listX);
        }
        basMap.setData(JSON.toJSONString(lists));
        //将数据库地图数据存入redis
        redisUtil.set("realtimeBasMap_" + lev, JSON.toJSONString(basMap));
        return true;
    }
 
}