自动化立体仓库 - WCS系统
Junjie
2024-12-21 42f3380a98fff04674cf4ef95ea5e99f5c54f1fd
src/main/java/com/zy/common/utils/NavigateMapData.java
@@ -5,11 +5,15 @@
import com.core.common.SpringUtils;
import com.zy.asrs.entity.BasMap;
import com.zy.asrs.entity.LocMast;
import com.zy.asrs.service.BasShuttleService;
import com.zy.asrs.service.LocMastService;
import com.zy.common.model.MapNode;
import com.zy.common.model.NavigateNode;
import com.zy.common.model.enums.NavigationMapType;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.ShuttleTaskModeType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import java.io.*;
@@ -23,26 +27,52 @@
@Component
public class NavigateMapData {
    private Integer lev;//地图楼层
//    private Integer lev;//地图楼层
//
//    public Integer getLev() {
//        return lev;
//    }
//
//    public void setLev(Integer lev) {
//        this.lev = lev;
//    }
    public NavigateMapData() {
        this.lev = 1;
    }
    public NavigateMapData(Integer lev) {
        this.lev = lev;
    }
    public int[][] getData() {
        return getData(NavigationMapType.NONE.id, null, null);//默认读取无过滤的全部地图数据
    }
    public int[][] getData(Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
    public int[][] getData(Integer lev, Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
        try {
            String mapFilename = "map_" + lev + ".json";
//            RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
//            Object object = redisUtil.get(RedisKeyType.BASIC_MAP.key + lev);
//            if (object == null) {
//                return null;
//            }
//
//            //解析json地图数据
//            ArrayList arrayList = JSON.parseObject(object.toString(), 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;
            String fileName = this.getClass().getClassLoader().getResource(mapFilename).getPath();//获取文件路径
            File file = new File(fileName);
            String mapFilename = "map_" + lev + ".json";
            ClassPathResource classPathResource = new ClassPathResource(mapFilename);
            InputStream inputStream = classPathResource.getInputStream();
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            File file = File.createTempFile("prefix", "suffix");
            try (OutputStream outStream = new FileOutputStream(file)) {
                outStream.write(buffer);
            }
            StringBuffer stringBuffer = new StringBuffer();
            if (file.isFile() && file.exists()) {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
@@ -69,12 +99,15 @@
                    map[j++] = tmp;
                }
                if (!file.delete()) {
                    System.out.println("临时文件删除失败");
                }
                return map;
            } else {
                System.out.println("文件不存在!");
            }
        } catch (IOException ioException) {
            ioException.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
@@ -82,21 +115,21 @@
    /**
     * 尝试从redis获取数据
     */
    public int[][] getDataFromRedis(Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
    public int[][] getDataFromRedis(Integer lev, Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
        RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
        Object o = redisUtil.get("realtimeBasMap_" + lev);
        Object o = redisUtil.get(RedisKeyType.MAP.key + lev);
        if (o == null) {
            return null;
        }
        BasMap basMap = JSON.parseObject(o.toString(), BasMap.class);
        return this.getDataFormString(basMap.getData(), mapType, whitePoints, shuttlePoints);
        return this.getDataFormString(lev, basMap.getData(), mapType, whitePoints, shuttlePoints);
    }
    /**
     * 从List数据中获取地图
     */
    public int[][] getDataFormString(String data, Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
    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()][];
@@ -115,13 +148,83 @@
        return map;
    }
    //获取JSON格式数据
    public List<List<MapNode>> getJsonData(Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
        try {
            String mapFilename = "map_" + lev + ".json";
//    //获取JSON格式数据
//    public List<List<MapNode>> getJsonData(Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
//        try {
////            RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
////            Object object = redisUtil.get(RedisKeyType.BASIC_MAP.key + lev);
////            if (object == null) {
////                return null;
////            }
////
////            //解析json地图数据
////            ArrayList arrayList = JSON.parseObject(object.toString(), ArrayList.class);
////            List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//过滤地图数据
////            return lists;
//
//
//            String mapFilename = "map_" + lev + ".json";
//            ClassPathResource classPathResource = new ClassPathResource(mapFilename);
//            InputStream inputStream = classPathResource.getInputStream();
//            byte[] buffer = new byte[inputStream.available()];
//            inputStream.read(buffer);
//            File file = File.createTempFile("prefix","suffix");
//            try (OutputStream outStream = new FileOutputStream(file)) {
//                outStream.write(buffer);
//            }
//
//            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, lev, whitePoints, shuttlePoints);//过滤地图数据
//
//                if (!file.delete()) {
//                    System.out.println("临时文件删除失败");
//                }
//
//                return lists;
//            } else {
//                System.out.println("文件不存在!");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
            String fileName = this.getClass().getClassLoader().getResource(mapFilename).getPath();//获取文件路径
            File file = new File(fileName);
    //获取JSON格式数据
    public List<List<MapNode>> getJsonData(Integer lev, Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
        try {
//            RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
//            Object object = redisUtil.get(RedisKeyType.BASIC_MAP.key + lev);
//            if (object == null) {
//                return null;
//            }
//
//            //解析json地图数据
//            ArrayList arrayList = JSON.parseObject(object.toString(), ArrayList.class);
//            List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//过滤地图数据
//            return lists;
            String mapFilename = "map_" + lev + ".json";
            ClassPathResource classPathResource = new ClassPathResource(mapFilename);
            InputStream inputStream = classPathResource.getInputStream();
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            File file = File.createTempFile("prefix","suffix");
            try (OutputStream outStream = new FileOutputStream(file)) {
                outStream.write(buffer);
            }
            StringBuffer stringBuffer = new StringBuffer();
            if (file.isFile() && file.exists()) {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
@@ -136,12 +239,16 @@
                ArrayList arrayList = JSON.parseObject(stringBuffer.toString(), ArrayList.class);
                List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//过滤地图数据
                if (!file.delete()) {
                    System.out.println("临时文件删除失败");
                }
                return lists;
            } else {
                System.out.println("文件不存在!");
            }
        } catch (IOException ioException) {
            ioException.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
@@ -232,58 +339,27 @@
            }
        }
        return lists;
    }
        //加载白名单节点
        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);
    /**
     * 写入路径节点数据到redis地图中
     * lock为true 禁用库位,lock为false恢复库位
     */
    public boolean writeNavigateNodeToRedisMap(List<NavigateNode> nodes, boolean lock) {
        RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
        try {
            if (!redisUtil.tryLock("realtimeBasMap_" + lev)) {
                return false;//加锁失败
                List<MapNode> list = lists.get(x);
                MapNode mapNode = list.get(y);
                mapNode.setValue(col.getValue());//恢复原始节点
                //更新list
                list.set(y, mapNode);
                lists.set(x, list);
            }
            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(NavigationMapType.NONE.id, arrayList, lev, null, null);//获取全部地图数据
            NavigateMapData mapData = new NavigateMapData(nodes.get(0).getZ());
            List<List<MapNode>> realMap = mapData.getJsonData(-1, null, null);//获取完整地图(包括入库出库)
            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 {
                    //获取原始节点数据
                    List<MapNode> rows = realMap.get(node.getX());
                    MapNode col = rows.get(node.getY());
                    mapNode.setValue(col.getValue());//恢复库位
                }
                listX.set(node.getY(), mapNode);
                lists.set(node.getX(), listX);
            }
            basMap.setData(JSON.toJSONString(lists));
            basMap.setUpdateTime(new Date());
            //将数据库地图数据存入redis
            redisUtil.set("realtimeBasMap_" + lev, JSON.toJSONString(basMap));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
        return lists;
    }
}