#
zjj
2025-04-08 3df03c486fde77ab36b9298a94bdbb0aa065a7e2
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
package com.zy.asrs.wcs.core.action;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.wcs.core.entity.Task;
import com.zy.asrs.wcs.core.model.command.*;
import com.zy.asrs.wcs.core.model.enums.LiftCommandModeType;
import com.zy.asrs.wcs.core.service.TaskService;
import com.zy.asrs.wcs.core.utils.LiftDispatcher;
import com.zy.asrs.wcs.core.utils.RedisUtil;
import com.zy.asrs.wcs.rcs.News;
import com.zy.asrs.wcs.rcs.cache.SlaveConnection;
import com.zy.asrs.wcs.rcs.constant.DeviceRedisConstant;
import com.zy.asrs.wcs.rcs.entity.BasLiftOpt;
import com.zy.asrs.wcs.rcs.entity.Device;
import com.zy.asrs.wcs.rcs.model.CommandResponse;
import com.zy.asrs.wcs.rcs.model.enums.LiftProtocolStatusType;
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.BasLiftOptService;
import com.zy.asrs.wcs.rcs.thread.LiftThread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.Date;
import java.util.List;
 
@Component
public class LiftAction {
 
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private TaskService taskService;
    @Autowired
    private BasLiftOptService basLiftOptService;
    @Autowired
    private LiftDispatcher liftDispatcher;
 
    public synchronized boolean assignWork(Device device, LiftAssignCommand assignCommand) {
        LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, device.getId().intValue());
        if (liftThread == null) {
            return false;
        }
 
        LiftProtocol liftProtocol = liftThread.getStatus();
        if (liftProtocol == null) {
            return false;
        }
 
        LiftRedisCommand redisCommand = new LiftRedisCommand();
        redisCommand.setLiftNo(assignCommand.getLiftNo());//提升机号
        redisCommand.setWrkNo(assignCommand.getTaskNo());//工作号
        redisCommand.setCommandStep(0);//命令执行步序
        redisCommand.setAssignCommand(assignCommand);//命令
        //任务数据保存到redis
        if (redisUtil.set(DeviceRedisConstant.LIFT_WORK_FLAG + assignCommand.getTaskNo(), JSON.toJSONString(redisCommand))) {
            liftThread.setSyncTaskNo(assignCommand.getTaskNo());
            return true;
        }
        return false;
    }
 
    public synchronized boolean executeWork(Device device, Integer taskNo) {
        Object obj = redisUtil.get(DeviceRedisConstant.LIFT_WORK_FLAG + taskNo);
        if (obj == null) {
            return false;
        }
 
        Task task = taskService.getOne(new LambdaQueryWrapper<Task>().eq(Task::getTaskNo, taskNo));
        if (task == null) {
            return false;
        }
 
        LiftRedisCommand redisCommand = JSON.parseObject(obj.toString(), LiftRedisCommand.class);
        if (redisCommand == null) {
            return false;
        }
 
        LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, device.getId().intValue());
        if (liftThread == null) {
            return false;
        }
 
        LiftProtocol liftProtocol = liftThread.getStatus();
        if (liftProtocol == null) {
            return false;
        }
 
        //判断提升机是否空闲
        if (!liftThread.isDeviceIdle()) {
            return false;
        }
 
        List<LiftCommand> commands = redisCommand.getAssignCommand().getCommands();
        if (commands.isEmpty()) {
            return false;
        }
 
        LiftAssignCommand assignCommand = redisCommand.getAssignCommand();
        int commandStep = redisCommand.getCommandStep();
 
        if (commandStep != 0) {
            LiftCommand command = commands.get(commandStep - 1);
            if (command.getMode() == LiftCommandModeType.MOVE.id) {
                //提升机升降
                Integer target = liftDispatcher.getLiftLevLogic(liftThread.getDevice().getId().intValue(), command.getTargetLev());
                if (liftProtocol.getLev() == target) {
                    command.setComplete(true);
                }
            } else if (command.getMode() == LiftCommandModeType.MOVE_CAR.id) {
                //提升机升降小车
                if (liftProtocol.getLev() == command.getTargetLev()) {
                    command.setComplete(true);
                }
            } else if (command.getMode() == LiftCommandModeType.PALLET_IN.id) {
                //托盘入
                Integer target = liftDispatcher.getLiftLevLogic(liftThread.getDevice().getId().intValue(), command.getTargetLev());
                if (liftProtocol.getLev() == target) {
                    command.setComplete(true);
                }
 
                //判断提升机托盘是否存在
                if (!liftProtocol.getHasTray()) {
                    return false;
                }
 
            } else if (command.getMode() == LiftCommandModeType.PALLET_OUT.id) {
                //托盘出
                Integer target = liftDispatcher.getLiftLevLogic(liftThread.getDevice().getId().intValue(), command.getTargetLev());
                if (liftProtocol.getLev() == target) {
                    command.setComplete(true);
                }
 
                //判断提升机托盘是否存在
                if (liftProtocol.getHasTray()) {
                    return false;
                }
            } else if (command.getMode() == LiftCommandModeType.RESET.id) {
                //复位
                command.setComplete(true);
            } else if (command.getMode() == LiftCommandModeType.LOCK.id) {
                //锁定
                command.setComplete(true);
            } else if (command.getMode() == LiftCommandModeType.UNLOCK.id) {
                //解锁
                command.setComplete(true);
            }
 
            // 更新redis数据
            redisUtil.set(DeviceRedisConstant.LIFT_WORK_FLAG + taskNo, JSON.toJSONString(redisCommand));
 
            if (!command.getComplete()) {
                return false;
            }
 
            //判断是否为最后一条命令且命令执行完成,抛出等待确认状态
            LiftCommand endCommand = commands.get(commands.size() - 1);
            if (endCommand.getComplete()) {
                News.info("提升机任务执行下发完成等待执行结束,提升机号={},任务数据={}", liftProtocol.getLiftNo(), JSON.toJSON(commands));
 
                //对主线程抛出等待确认状态waiting
                liftThread.setProtocolStatus(LiftProtocolStatusType.WAITING);
                redisUtil.del(DeviceRedisConstant.LIFT_WORK_FLAG + taskNo);
                return false;//禁止再下发命令
            }
        }
 
        //取出命令
        LiftCommand command = commands.get(commandStep);
 
        // 下发命令
        CommandResponse response = write(command, device);
 
        //保存命令日志
        BasLiftOpt basLiftOpt = new BasLiftOpt();
        basLiftOpt.setTaskNo(taskNo);
        basLiftOpt.setLiftNo(liftProtocol.getLiftNo());
        basLiftOpt.setMode(LiftCommandModeType.get(command.getMode()).desc);
        basLiftOpt.setCommand(JSON.toJSONString(command));
        basLiftOpt.setSystemStatus(JSON.toJSONString(liftProtocol));
        basLiftOpt.setDeviceId(device.getId());
        basLiftOpt.setDeviceWrk(String.valueOf(command.getTaskNo()));
        basLiftOpt.setResponse(response.getMessage());//获取响应
        basLiftOpt.setSendTime(new Date());//指令下发时间
        basLiftOpt.setSend(response.getResult() ? 1 : 0);
        basLiftOpt.setHostId(device.getHostId());
        //保存命令流水
        basLiftOptService.save(basLiftOpt);
 
        if (!response.getResult()) {
            News.error("提升机命令下发失败,提升机号={},任务数据={}", command.getLiftNo(), JSON.toJSON(command));
            return false;
        } else {
            News.info("提升机命令下发成功,提升机号={},任务数据={}", command.getLiftNo(), JSON.toJSON(command));
        }
 
        liftThread.setProtocolStatus(LiftProtocolStatusType.WORKING);
        commandStep++;
        //更新redis数据
        redisCommand.setCommandStep(commandStep);
        // 更新redis数据
        redisUtil.set(DeviceRedisConstant.LIFT_WORK_FLAG + taskNo, JSON.toJSONString(redisCommand));
        return true;
    }
 
    private synchronized CommandResponse write(LiftCommand command, Device device) {
        CommandResponse response = new CommandResponse(false);
        if (null == command) {
            News.error("提升机写入命令为空");
            response.setMessage("提升机写入命令为空");
            return response;
        }
 
        LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, device.getId().intValue());
        if (liftThread == null) {
            return response;
        }
 
        if (command.getMode() == LiftCommandModeType.MOVE.id) {
            response = liftThread.move(command);
        } else if (command.getMode() == LiftCommandModeType.PALLET_IN.id || command.getMode() == LiftCommandModeType.PALLET_OUT.id) {
            response = liftThread.palletInOut(command);
        } else if (command.getMode() == LiftCommandModeType.LOCK.id) {
            response = liftThread.lock(command);
        } else if (command.getMode() == LiftCommandModeType.UNLOCK.id) {
            response = liftThread.unlock(command);
        } else if (command.getMode() == LiftCommandModeType.RESET.id) {
            response = liftThread.reset(command);
        }
        return response;
    }
 
}