#
Junjie
9 天以前 2df6d2c5d13a2a7909e802c5f190afeeba70f111
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
package com.zy.asrs.wcs.core.utils;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.framework.common.Cools;
import com.zy.asrs.wcs.core.domain.dto.BasLiftLevOffsetDto;
import com.zy.asrs.wcs.core.entity.BasLift;
import com.zy.asrs.wcs.core.entity.ShuttleStandby;
import com.zy.asrs.wcs.core.model.NavigateNode;
import com.zy.asrs.wcs.core.model.enums.DeviceCtgType;
import com.zy.asrs.wcs.core.model.enums.NavigationMapType;
import com.zy.asrs.wcs.core.service.BasLiftService;
import com.zy.asrs.wcs.core.service.ShuttleStandbyService;
import com.zy.asrs.wcs.rcs.cache.SlaveConnection;
import com.zy.asrs.wcs.rcs.entity.Device;
import com.zy.asrs.wcs.rcs.model.enums.SlaveType;
import com.zy.asrs.wcs.rcs.model.protocol.LiftProtocol;
import com.zy.asrs.wcs.rcs.service.DeviceService;
import com.zy.asrs.wcs.rcs.thread.LiftThread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
/**
 * Created by vincent on 2023/10/16
 */
@Service
public class LiftDispatcher {
 
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private BasLiftService basLiftService;
    @Autowired
    private ShuttleStandbyService shuttleStandbyService;
    @Autowired
    private NavigateUtils navigateUtils;
 
    /**
     * 根据目标位置搜索提升机
     * transfer: 是否可换层
     */
    public LiftThread searchLift(String locNo, Long hostId, Boolean transfer) {
        LiftThread recentLiftThread = null;
        Integer finalDistance = Integer.MAX_VALUE;
        List<Device> list = deviceService.list(new LambdaQueryWrapper<Device>()
                .eq(Device::getDeviceType, DeviceCtgType.LIFT.val())
                .eq(Device::getHostId, hostId)
                .eq(Device::getStatus, 1));
        for (Device device : list) {
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, device.getId().intValue());
            if (liftThread == null) {
                continue;
            }
 
            LiftProtocol liftProtocol = liftThread.getStatus();
            if (liftProtocol == null) {
                continue;
            }
 
            if (transfer) {
                BasLift basLift = basLiftService.getOne(new LambdaQueryWrapper<BasLift>().eq(BasLift::getLiftNo, device.getDeviceNo()).eq(BasLift::getHostId, hostId));
                if (basLift == null) {
                    continue;
                }
                if (basLift.getTransfer() != 1) {
                    continue;//提升机被设置成不可换层
                }
            }
 
            ShuttleStandby standby = shuttleStandbyService.getOne(new LambdaQueryWrapper<ShuttleStandby>()
                    .eq(ShuttleStandby::getDeviceId, device.getId())
                    .eq(ShuttleStandby::getDeviceLev, Utils.getLev(locNo))
                    .eq(ShuttleStandby::getStatus, 1));
            if (standby == null) {
                continue;
            }
 
            String liftLocNo = Utils.getLocNo(Utils.getRow(standby.getDeviceStandbyLoc()), Utils.getBay(standby.getDeviceStandbyLoc()), Utils.getLev(locNo));
            List<NavigateNode> nodeList = navigateUtils.calc(locNo, liftLocNo, NavigationMapType.NONE.id, null);
            if (nodeList == null) {
                continue;
            }
            Integer originPathAllDistance = navigateUtils.getOriginPathAllDistance(nodeList);//总距离
            if (originPathAllDistance < finalDistance) {
                finalDistance = originPathAllDistance;
                recentLiftThread = liftThread;
            }
        }
 
        return recentLiftThread;
    }
 
    /**
     * 根据目标位置搜索空闲提升机
     * transfer: 是否可换层
     */
    public LiftThread searchIdleLift(String locNo, Long hostId, Boolean transfer) {
        LiftThread recentLiftThread = null;
        Integer finalDistance = Integer.MAX_VALUE;
        List<Device> list = deviceService.list(new LambdaQueryWrapper<Device>()
                .eq(Device::getDeviceType, DeviceCtgType.LIFT.val())
                .eq(Device::getHostId, hostId)
                .eq(Device::getStatus, 1));
        for (Device device : list) {
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, device.getId().intValue());
            if (liftThread == null) {
                continue;
            }
 
            LiftProtocol liftProtocol = liftThread.getStatus();
            if (liftProtocol == null) {
                continue;
            }
 
            if (!liftThread.isIdle()) {
                continue;
            }
 
            if (transfer) {
                BasLift basLift = basLiftService.getOne(new LambdaQueryWrapper<BasLift>().eq(BasLift::getLiftNo, device.getDeviceNo()).eq(BasLift::getHostId, hostId));
                if (basLift == null) {
                    continue;
                }
                if (basLift.getTransfer() != 1) {
                    continue;//提升机被设置成不可换层
                }
            }
 
            ShuttleStandby standby = shuttleStandbyService.getOne(new LambdaQueryWrapper<ShuttleStandby>()
                    .eq(ShuttleStandby::getDeviceId, device.getId())
                    .eq(ShuttleStandby::getDeviceLev, Utils.getLev(locNo))
                    .eq(ShuttleStandby::getStatus, 1));
            if (standby == null) {
                continue;
            }
 
            String liftLocNo = Utils.getLocNo(Utils.getRow(standby.getDeviceStandbyLoc()), Utils.getBay(standby.getDeviceStandbyLoc()), Utils.getLev(locNo));
            List<NavigateNode> nodeList = navigateUtils.calc(locNo, liftLocNo, NavigationMapType.NONE.id, null);
            if (nodeList == null) {
                continue;
            }
            Integer originPathAllDistance = navigateUtils.getOriginPathAllDistance(nodeList);//总距离
            if (originPathAllDistance < finalDistance) {
                finalDistance = originPathAllDistance;
                recentLiftThread = liftThread;
            }
        }
 
        return recentLiftThread;
    }
 
    /**
     * 获取提升机位置
     */
    public String getLiftLocNo(LiftThread liftThread, Integer lev) {
        Device device = liftThread.getDevice();
        ShuttleStandby standby = shuttleStandbyService.getOne(new LambdaQueryWrapper<ShuttleStandby>()
                .eq(ShuttleStandby::getDeviceId, device.getId())
                .eq(ShuttleStandby::getDeviceLev, lev)
                .eq(ShuttleStandby::getStatus, 1));
        if (standby == null) {
            return null;
        }
 
        String liftLocNo = Utils.getLocNo(Utils.getRow(standby.getDeviceLoc()), Utils.getBay(standby.getDeviceLoc()), lev);
        return liftLocNo;
    }
 
    /**
     * 获取提升机待机位位置
     */
    public String getLiftStandByLocNo(LiftThread liftThread, Integer lev) {
        Device device = liftThread.getDevice();
        ShuttleStandby standby = shuttleStandbyService.getOne(new LambdaQueryWrapper<ShuttleStandby>()
                .eq(ShuttleStandby::getDeviceId, device.getId())
                .eq(ShuttleStandby::getDeviceLev, lev)
                .eq(ShuttleStandby::getStatus, 1));
        if (standby == null) {
            return null;
        }
 
        String liftLocNo = Utils.getLocNo(Utils.getRow(standby.getDeviceStandbyLoc()), Utils.getBay(standby.getDeviceStandbyLoc()), lev);
        return liftLocNo;
    }
 
    /**
     * 获取进提升机待机位位置
     */
    public String getInLiftStandByLocNo(LiftThread liftThread, Integer lev) {
        Device device = liftThread.getDevice();
        ShuttleStandby standby = shuttleStandbyService.getOne(new LambdaQueryWrapper<ShuttleStandby>()
                .eq(ShuttleStandby::getDeviceId, device.getId())
                .eq(ShuttleStandby::getDeviceLev, lev)
                .eq(ShuttleStandby::getStatus, 1));
        if (standby == null) {
            return null;
        }
 
        return standby.getMemo();
    }
 
    /**
     * 获取换层需要锁定的路径
     */
    public List<String> getLockPathByLocNo(LiftThread liftThread, Integer lev) {
        Device device = liftThread.getDevice();
        ShuttleStandby standby = shuttleStandbyService.getOne(new LambdaQueryWrapper<ShuttleStandby>()
                .eq(ShuttleStandby::getDeviceId, device.getId())
                .eq(ShuttleStandby::getDeviceLev, lev)
                .eq(ShuttleStandby::getStatus, 1));
        if (standby == null) {
            return null;
        }
 
        return standby.getLockPath$();
    }
 
    public Integer getLiftLevOffset(Integer deviceId,Integer lev) {
        BasLift basLift = basLiftService.getOne(new LambdaQueryWrapper<BasLift>().eq(BasLift::getDeviceId, deviceId));
        if (basLift != null) {
            if(Cools.isEmpty(basLift.getLevOffset())) {
                return lev;
            }
            List<BasLiftLevOffsetDto> levOffsetDtos = JSON.parseArray(basLift.getLevOffset(), BasLiftLevOffsetDto.class);
            for (BasLiftLevOffsetDto offsetDto : levOffsetDtos) {
                if(lev == offsetDto.getLogicLev()){
                    return offsetDto.getRealLev();
                }
            }
        }
        return lev;
    }
 
}