#
zjj
2024-06-22 8a830f3e5f9ff3bca3161b5bf800abeb1a64e866
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/utils/NavigateMapUtils.java
@@ -1,7 +1,10 @@
package com.zy.asrs.wcs.core.utils;
import com.alibaba.fastjson.JSON;
import com.zy.asrs.framework.common.SpringUtils;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zy.asrs.wcs.core.domain.dto.RedisMapDto;
import com.zy.asrs.wcs.core.model.MapNode;
import com.zy.asrs.wcs.core.model.NavigateNode;
@@ -19,13 +22,16 @@
    @Autowired
    private NavigateMapData navigateMapData;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ObjectMapper objectMapper;
    /**
     * 写入路径节点数据到redis地图中
     * lock为true 禁用库位,lock为false恢复库位
     */
    public synchronized boolean writeNavigateNodeToRedisMap(Integer lev, Integer shuttleNo, List<NavigateNode> nodes, boolean lock) {
        RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
        try {
            if (nodes.isEmpty()) {
                return true;
@@ -85,7 +91,10 @@
            redisMap.setData(JSON.toJSONString(lists));
            redisMap.setUpdateTime(new Date());
            //将数据库地图数据存入redis
            redisUtil.set(DeviceRedisConstant.MAP + lev, JSON.toJSONString(redisMap));
            redisUtil.set(DeviceRedisConstant.MAP + lev, JSON.toJSONString(redisMap, SerializerFeature.DisableCircularReferenceDetect));
            //保存路径锁节点
            saveLockPath(lev, nodes, lock);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
@@ -93,4 +102,66 @@
        return false;
    }
    //保存路径锁节点
    public void saveLockPath(Integer lev, List<NavigateNode> nodes, boolean lock) {
        Object o = redisUtil.get(DeviceRedisConstant.LOCK_PATH + lev);
        List<NavigateNode> navigateNodes = new ArrayList<>();
        if (o != null) {
            try {
                navigateNodes = objectMapper.readValue(o.toString(), new TypeReference<List<NavigateNode>>() {});
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }
        String nodeStr = null;
        if (lock) {
            navigateNodes.addAll(nodes);
            try {
                nodeStr = objectMapper.writeValueAsString(navigateNodes);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
            redisUtil.set(DeviceRedisConstant.LOCK_PATH + lev, nodeStr);
        }else {
            List<NavigateNode> tmp = new ArrayList<>();
            for (NavigateNode navigateNode : navigateNodes) {
                boolean flag = true;
                for (NavigateNode node : nodes) {
                    if (navigateNode.getX() == node.getX()
                            && navigateNode.getY() == node.getY()
                            && navigateNode.getZ() == node.getZ()) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    tmp.add(navigateNode);
                }
            }
            try {
                nodeStr = objectMapper.writeValueAsString(tmp);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
            redisUtil.set(DeviceRedisConstant.LOCK_PATH + lev, nodeStr);
        }
    }
    //获取路径锁节点
    public List<NavigateNode> getLockPath(Integer lev) {
        Object obj = redisUtil.get(DeviceRedisConstant.LOCK_PATH + lev);
        List<NavigateNode> navigateNodes = new ArrayList<>();
        if (obj != null) {
            try {
                navigateNodes = objectMapper.readValue(obj.toString(), new TypeReference<List<NavigateNode>>() {});
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }
        return navigateNodes;
    }
}