| | |
| | | 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.asrs.utils.Utils; |
| | | import com.zy.common.model.NavigateNode; |
| | | import com.zy.common.model.enums.NavigationMapType; |
| | | import com.zy.core.enums.RedisKeyType; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | |
| | | @Component |
| | | public class NavigateMapUtils { |
| | | |
| | | @Autowired |
| | | private RedisUtil redisUtil; |
| | | |
| | | /** |
| | | * 写入路径节点数据到redis地图中 |
| | | * lock为true 禁用库位,lock为false恢复库位 |
| | | */ |
| | | public synchronized boolean writeNavigateNodeToRedisMap(Integer lev, List<NavigateNode> nodes, boolean lock) { |
| | | RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class); |
| | | try { |
| | | NavigateMapData navigateMapData = new NavigateMapData(lev); |
| | | public synchronized boolean writeNavigateNodeToRedisMap(Integer lev, Integer shuttleNo, List<NavigateNode> nodes, boolean lock) { |
| | | if (lock) { |
| | | return lockNodes(lev, shuttleNo, nodes); |
| | | }else { |
| | | return unlockNodes(lev, shuttleNo, nodes); |
| | | } |
| | | } |
| | | |
| | | if (!redisUtil.tryLock("realtimeBasMap_" + lev)) { |
| | | return false;//加锁失败 |
| | | } |
| | | public synchronized boolean lockNodes(Integer lev, Integer shuttleNo, List<NavigateNode> nodes) { |
| | | if (nodes.isEmpty()) { |
| | | return false; |
| | | } |
| | | |
| | | Object o = redisUtil.get("realtimeBasMap_" + lev); |
| | | if (o == null) { |
| | | HashMap<String, Object> lockMap = new HashMap<>(); |
| | | Object o = redisUtil.get(RedisKeyType.LOCK_MAP_NODES.key + lev); |
| | | if (o != null) { |
| | | lockMap = (HashMap<String, Object>) o; |
| | | } |
| | | |
| | | List<String> shuttleList = new ArrayList<>(); |
| | | //获取小车节点-获取除白名单外的指定楼层全部穿梭车xy坐标点 |
| | | List<int[]> shuttlePoints = Utils.getShuttlePoints(shuttleNo, lev); |
| | | for (int[] point : shuttlePoints) { |
| | | String key = Utils.getLocNo(point[0], point[1], lev); |
| | | shuttleList.add(key); |
| | | } |
| | | |
| | | HashMap<String, Object> tmpMap = new HashMap<>(); |
| | | for (NavigateNode node : nodes) { |
| | | String key = Utils.getLocNo(node.getX(), node.getY(), lev); |
| | | |
| | | //判断节点是否有小车 |
| | | if(shuttleList.contains(key)) { |
| | | return false; |
| | | } |
| | | |
| | | BasMap basMap = JSON.parseObject(o.toString(), BasMap.class); |
| | | ArrayList arrayList = JSON.parseObject(basMap.getData(), ArrayList.class); |
| | | List<List<MapNode>> lists = navigateMapData.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); |
| | | //判断节点是否被锁定 |
| | | if(lockMap.containsKey(key)) { |
| | | return false; |
| | | } |
| | | basMap.setData(JSON.toJSONString(lists)); |
| | | basMap.setUpdateTime(new Date()); |
| | | //将数据库地图数据存入redis |
| | | redisUtil.set("realtimeBasMap_" + lev, JSON.toJSONString(basMap)); |
| | | //解锁 |
| | | redisUtil.unlock("realtimeBasMap_" + lev); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | |
| | | tmpMap.put(key, shuttleNo); |
| | | } |
| | | |
| | | lockMap.putAll(tmpMap); |
| | | |
| | | redisUtil.set(RedisKeyType.LOCK_MAP_NODES.key + lev, lockMap); |
| | | return true; |
| | | } |
| | | |
| | | public synchronized boolean unlockNodes(Integer lev, Integer shuttleNo, List<NavigateNode> nodes) { |
| | | if (nodes.isEmpty()) { |
| | | return false; |
| | | } |
| | | |
| | | HashMap<String, Object> lockMap = new HashMap<>(); |
| | | Object o = redisUtil.get(RedisKeyType.LOCK_MAP_NODES.key + lev); |
| | | if (o != null) { |
| | | lockMap = (HashMap<String, Object>) o; |
| | | } |
| | | |
| | | for (NavigateNode node : nodes) { |
| | | String key = Utils.getLocNo(node.getX(), node.getY(), lev); |
| | | |
| | | if(!lockMap.containsKey(key)) { |
| | | continue; |
| | | } |
| | | |
| | | Integer deviceNo = (Integer) lockMap.get(key); |
| | | if(deviceNo.equals(shuttleNo)) { |
| | | lockMap.remove(key); |
| | | } |
| | | } |
| | | lockMap.putAll(lockMap); |
| | | redisUtil.set(RedisKeyType.LOCK_MAP_NODES.key + lev, lockMap); |
| | | return true; |
| | | } |
| | | |