Junjie
2024-12-11 8ef2bb5e46d84594e6ed632c07ea0b47a1bf6c4d
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
package com.zy.asrs.wcs.core.kernel.command;
 
import com.alibaba.fastjson.JSON;
import com.zy.asrs.wcs.core.entity.Motion;
import com.zy.asrs.wcs.core.model.MapNode;
import com.zy.asrs.wcs.core.model.NavigateNode;
import com.zy.asrs.wcs.core.model.enums.MapNodeType;
import com.zy.asrs.wcs.core.model.enums.MotionCtgType;
import com.zy.asrs.wcs.core.model.enums.NavigationMapType;
import com.zy.asrs.wcs.core.utils.*;
import com.zy.asrs.wcs.rcs.cache.SlaveConnection;
import com.zy.asrs.wcs.rcs.constant.DeviceRedisConstant;
import com.zy.asrs.wcs.rcs.model.enums.SlaveType;
import com.zy.asrs.wcs.rcs.model.protocol.ShuttleProtocol;
import com.zy.asrs.wcs.rcs.thread.ShuttleThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
 
@Slf4j
@Service
public class MapCommandService {
 
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private NavigateMapUtils navigateMapUtils;
    @Autowired
    private NavigateMapData navigateMapData;
 
    public Boolean accept(Motion motion) {
        switch (Objects.requireNonNull(MotionCtgType.get(motion.getMotionCtgEl()))) {
            case MAP_LOCK_PATH:
                return lockPath(motion, true);
            case MAP_UNLOCK_PATH:
                return lockPath(motion, false);
            default:
                break;
        }
 
        return Boolean.TRUE;
    }
 
    public Boolean finish(Motion motion) {
        switch (Objects.requireNonNull(MotionCtgType.get(motion.getMotionCtgEl()))) {
            case MAP_LOCK_PATH:
                return checkLockPath(motion, true);
            case MAP_UNLOCK_PATH:
                return checkLockPath(motion, false);
            default:
                break;
        }
 
        return Boolean.TRUE;
    }
 
    private boolean lockPath(Motion motion, boolean lock) {
        Integer deviceNo = Integer.parseInt(motion.getDevice());
        ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, deviceNo);
        if (shuttleThread == null) {
            return false;
        }
        ShuttleProtocol shuttleProtocol = shuttleThread.getStatus();
        if (null == shuttleProtocol) {
            return false;
        }
 
        List<NavigateNode> nodes = new ArrayList<>();
 
        String target = motion.getTarget();
        List<String> lockPath = JSON.parseArray(target, String.class);
        Integer lev = null;
        for (String loc : lockPath) {
            NavigateNode navigateNode = NavigatePositionConvert.locNoToNode(loc);
            nodes.add(navigateNode);
 
            lev = Utils.getLev(loc);
        }
 
        //所使用的路径进行锁定/解锁
        boolean lockResult = navigateMapUtils.writeNavigateNodeToRedisMap(lev, shuttleProtocol.getShuttleNo(), nodes, lock);//所使用的路径进行锁定/解锁
        if (!lockResult) {
            return false;//锁定/解锁失败
        }
        return true;
    }
 
    private boolean checkLockPath(Motion motion, boolean lock) {
        List<NavigateNode> nodes = new ArrayList<>();
 
        String target = motion.getTarget();
        List<String> lockPath = JSON.parseArray(target, String.class);
        Integer lev = null;
        for (String loc : lockPath) {
            NavigateNode navigateNode = NavigatePositionConvert.locNoToNode(loc);
            nodes.add(navigateNode);
 
            lev = Utils.getLev(loc);
        }
 
        Object o = redisUtil.get(DeviceRedisConstant.MAP + lev);
        if (o == null) {
            return false;
        }
 
        Integer deviceNo = Integer.parseInt(motion.getDevice());
        ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, deviceNo);
        if (shuttleThread == null) {
            return false;
        }
        ShuttleProtocol shuttleProtocol = shuttleThread.getStatus();
        if (null == shuttleProtocol) {
            return false;
        }
 
        //获取小车节点
        List<int[]> shuttlePoints = Utils.getShuttlePoints(shuttleProtocol.getShuttleNo(), lev);
        navigateMapData.setLev(lev);
        List<List<MapNode>> map = navigateMapData.getJsonData(NavigationMapType.DFX.id, null, shuttlePoints);
 
        for (NavigateNode node : nodes) {
            List<MapNode> listX = map.get(node.getX());
            MapNode mapNode = listX.get(node.getY());
            if (mapNode.getLockDeviceNo() != null && !mapNode.getLockDeviceNo().equals(shuttleProtocol.getShuttleNo())) {
                return false;//路径所属权不是当前设备
            }
 
            if (lock) {//检测是否锁定
                if (!mapNode.getValue().equals(MapNodeType.LOCK.id)) {
                    return false;//路径未锁定
                }
            }else {//检测是否未锁定
                if(mapNode.getValue().equals(MapNodeType.LOCK.id)) {
                    return false;//路径已锁定
                }
            }
        }
        return true;
    }
 
}