自动化立体仓库 - WCS系统
#
taisheng
6 天以前 4453cdecbc6e7a925ae76e3223418654f1cf0b18
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package com.zy.common.utils;
 
import com.alibaba.fastjson.JSON;
import com.core.common.SpringUtils;
import com.zy.asrs.entity.BasMap;
import com.zy.asrs.entity.LocMast;
import com.zy.asrs.service.BasMapService;
import com.zy.asrs.service.LocMastService;
import com.zy.asrs.utils.Utils;
import com.zy.common.model.MapNode;
import com.zy.common.model.enums.NavigationMapType;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.MapNodeType;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.ForkLiftSlave;
import com.zy.core.model.protocol.ForkLiftStaProtocol;
import com.zy.core.properties.SlaveProperties;
import com.zy.core.thread.ForkLiftThread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.io.*;
import java.util.ArrayList;
import java.util.List;
 
/**
 * A*算法地图获取类
 */
@Component
public class NavigateMapData {
 
    @Autowired
    private SlaveProperties slaveProperties;
    @Autowired
    private BasMapService basMapService;
 
    public int[][] getData(Integer lev, Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
        try {
            BasMap basMap = basMapService.selectLatestMap(lev);
            String originData = basMap.getOriginData();
 
            //解析json地图数据
            ArrayList arrayList = JSON.parseObject(originData, ArrayList.class);
            List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//过滤地图数据
            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;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 尝试从redis获取数据
     */
    public int[][] getDataFromRedis(Integer lev, Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
        RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
        Object o = redisUtil.get(RedisKeyType.MAP.key + lev);
        if (o == null) {
            return null;
        }
 
        BasMap basMap = JSON.parseObject(o.toString(), BasMap.class);
        return this.getDataFormString(lev, basMap.getData(), mapType, whitePoints, shuttlePoints);
    }
 
    /**
     * 从List数据中获取地图
     */
    public int[][] getDataFormString(Integer lev, String data, Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
        ArrayList arrayList = JSON.parseObject(data, ArrayList.class);
        List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//过滤地图数据
        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 lev, Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
        try {
            BasMap basMap = basMapService.selectLatestMap(lev);
            String originData = basMap.getOriginData();
            //解析json地图数据
            ArrayList arrayList = JSON.parseObject(originData, ArrayList.class);
            //重建数据格式
            List<List<MapNode>> lists = rebuildData(arrayList);
            return lists;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public int[][] parseJsonDataArr(List<List<MapNode>> lists) {
        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;
    }
 
    /**
     * 过滤地图数据
     * mapType -1=>无过滤,1=》过滤库位状态DFX,2=》过滤库位状态X
     *
     * @param whitePoints 白名单节点,不需要被过滤
     * @param shuttlePoints 穿梭车节点,需要加载进地图
     */
    public List<List<MapNode>> filterMap(Integer mapType, List arrayList, Integer lev, List<int[]> whitePoints, List<int[]> shuttlePoints) {
        //重建数据格式
        List<List<MapNode>> lists = rebuildData(arrayList);
 
        //载入库位信息
        lists = loadLocMast(mapType, lists, lev, whitePoints);
 
        //加载车辆
        lists = loadShuttle(lists, shuttlePoints);
 
        //加载货叉提升机点位
        lists = loadForkLift(lists, mapType, lev);
 
        //加载白名单节点
        lists = loadWhite(lists, lev, whitePoints);
 
        return lists;
    }
 
    //重建数据格式
    public List<List<MapNode>> rebuildData(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);
                list.set(j, mapNode);
            }
            lists.add(list);
        }
 
        return lists;
    }
 
    //载入库位信息
    public List<List<MapNode>> loadLocMast(Integer mapType, List<List<MapNode>> lists, Integer lev, List<int[]> whitePoints) {
        //过滤数据
        LocMastService locMastService = SpringUtils.getBean(LocMastService.class);
        try {
            //获取当前楼层库位数据
            List<LocMast> locMasts = locMastService.selectLocByLev(lev);
            for (LocMast locMast : locMasts) {
                Integer row = locMast.getRow1();
                Integer bay = locMast.getBay1();
 
                boolean whiteFlag = false;//默认不存在白名单
                if (whitePoints != null) {
                    for (int[] whitePoint : whitePoints) {
                        if (whitePoint[0] == row && whitePoint[1] == bay) {
                            //存在白名单
                            whiteFlag = true;
                            break;
                        }
                    }
                }
                if (whiteFlag) {
                    continue;//存在白名单,不执行下列过滤方案
                }
 
 
                List<MapNode> list = lists.get(row);
                MapNode mapNode = list.get(bay);
 
                if (mapType == NavigationMapType.NONE.id) {
                    //不过滤任何数据
                } else if (mapType == NavigationMapType.DFX.id) {
                    //车辆有货
                    //读取对应库位数据,将DFX库位状态的节点置为-1(障碍物)
                    if (locMast.getLocSts().equals("F")
                            || locMast.getLocSts().equals("D")
                            || locMast.getLocSts().equals("X")
                            || locMast.getLocSts().equals("R")
                            || locMast.getLocSts().equals("P")
                    ) {
                        mapNode.setValue(MapNodeType.DISABLE.id);//禁用节点
                    }
                } else if (mapType == NavigationMapType.NORMAL.id) {
                    //过滤库位状态X
                    if (locMast.getLocSts().equals("X")) {
                        mapNode.setValue(MapNodeType.DISABLE.id);//禁用节点
                    }
                }
 
                //更新list
                list.set(bay, mapNode);
                lists.set(row, list);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
 
        return lists;
    }
 
    //加载车辆
    public List<List<MapNode>> loadShuttle(List<List<MapNode>> lists, List<int[]> shuttlePoints) {
        //加载车辆坐标到地图中
        if (shuttlePoints != null) {
            for (int[] points : shuttlePoints) {
                int x = points[0];
                int y = points[1];
                List<MapNode> list = lists.get(x);
                MapNode mapNode = list.get(y);
                mapNode.setValue(MapNodeType.CAR.id);//设置为车辆代码66
                //更新list
                list.set(y, mapNode);
                lists.set(x, list);
            }
        }
 
        return lists;
    }
 
    //加载白名单节点
    public List<List<MapNode>> loadWhite(List<List<MapNode>> lists, Integer lev, List<int[]> whitePoints) {
        //加载白名单节点
        if (whitePoints != null) {
            List<List<MapNode>> realMap = getJsonData(lev, -1, null, null);//获取完整地图
            for (int[] points : whitePoints) {
                //获取原始节点数据
                int x = points[0];
                int y = points[1];
                List<MapNode> rows = realMap.get(x);
                MapNode col = rows.get(y);
 
                List<MapNode> list = lists.get(x);
                MapNode mapNode = list.get(y);
                mapNode.setValue(col.getValue());//恢复原始节点
 
                //更新list
                list.set(y, mapNode);
                lists.set(x, list);
            }
        }
        return lists;
    }
 
    //加载货叉提升机点位
    public List<List<MapNode>> loadForkLift(List<List<MapNode>> lists, Integer mapType, Integer lev) {
        try {
            //加载货叉提升机放货点位数据
            for (ForkLiftSlave forkLiftSlave : slaveProperties.getForkLift()) {
                ForkLiftThread forkLiftThread = (ForkLiftThread) SlaveConnection.get(SlaveType.ForkLift, forkLiftSlave.getId());
                if (forkLiftThread == null) {
                    continue;
                }
 
                for (ForkLiftStaProtocol staProtocol : forkLiftThread.getForkLiftStaProtocols()) {
                    if (staProtocol.getLev() != lev) {
                        continue;
                    }
                    int row = Utils.getRow(staProtocol.getLocNo());
                    int bay = Utils.getBay(staProtocol.getLocNo());
 
                    List<MapNode> list = lists.get(row);
                    MapNode mapNode = list.get(bay);
 
                    if (mapType == NavigationMapType.DFX.id) {
                        //车辆有货
                        if (staProtocol.getHasTray() != null && staProtocol.getHasTray()) {
                            mapNode.setValue(MapNodeType.DISABLE.id);
                        }
                    } else {
                        if (staProtocol.getHasCar() != null && staProtocol.getHasCar()) {
                            mapNode.setValue(MapNodeType.CAR.id);
                        }
                    }
 
                    //更新list
                    list.set(bay, mapNode);
                    lists.set(row, list);
                }
 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lists;
    }
 
}