自动化立体仓库 - WCS系统
#
Junjie
2025-01-07 fd5d26ae0d1dbf71f90b99ef84d09a34e2d10d83
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package com.zy.core.thread.impl;
 
import HslCommunication.Core.Types.OperateResult;
import HslCommunication.Core.Types.OperateResultExOne;
import HslCommunication.Profinet.Siemens.SiemensPLCS;
import HslCommunication.Profinet.Siemens.SiemensS7Net;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.zy.asrs.entity.BasLift;
import com.zy.asrs.entity.DeviceDataLog;
import com.zy.asrs.service.BasLiftService;
import com.zy.asrs.service.DeviceDataLogService;
import com.zy.asrs.utils.Utils;
import com.zy.common.ExecuteSupport;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
import com.zy.core.action.ForkLiftAction;
import com.zy.core.action.ShuttleAction;
import com.zy.core.cache.OutputQueue;
import com.zy.core.enums.ForkLiftProtocolStatusType;
import com.zy.core.enums.ForkLiftTaskModeType;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.model.CommandResponse;
import com.zy.core.model.ForkLiftSlave;
import com.zy.core.model.command.ForkLiftCommand;
import com.zy.core.model.protocol.ForkLiftProtocol;
import com.zy.core.model.protocol.ForkLiftStaProtocol;
import com.zy.core.thread.ForkLiftThread;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
 
import java.text.MessageFormat;
import java.util.*;
 
@Slf4j
@SuppressWarnings("all")
public class ZyForkLiftThread implements ForkLiftThread {
 
    private ForkLiftSlave slave;
    private ForkLiftProtocol forkLiftProtocol;
    private RedisUtil redisUtil;
    private SiemensS7Net siemensS7Net;
    private List<ForkLiftStaProtocol> forkLiftStaProtocols = new ArrayList<>();
 
    public ZyForkLiftThread(ForkLiftSlave slave, RedisUtil redisUtil) {
        this.slave = slave;
        this.redisUtil = redisUtil;
        //初始化站点
        for (ForkLiftSlave.Sta sta : this.slave.getSta()) {
            ForkLiftStaProtocol forkLiftStaProtocol = new ForkLiftStaProtocol();
            forkLiftStaProtocol.setStaNo(sta.getStaNo());//站点号
            forkLiftStaProtocol.setLev(sta.getLev());//站点楼层
            String locNo = Utils.getLocNo(this.slave.getStaRow(), this.slave.getStaBay(), sta.getLev());
            forkLiftStaProtocol.setLocNo(locNo);//站点库位号
            forkLiftStaProtocol.setLiftNo(sta.getLiftNo());//提升机号
            forkLiftStaProtocols.add(forkLiftStaProtocol);
        }
    }
 
    @Override
    public boolean connect() {
        boolean result = false;
        siemensS7Net = new SiemensS7Net(SiemensPLCS.S1500, slave.getIp());
        siemensS7Net.setRack(slave.getRack().byteValue());
        siemensS7Net.setSlot(slave.getSlot().byteValue());
        OperateResult connect = siemensS7Net.ConnectServer();
        if(connect.IsSuccess){
            result = true;
            OutputQueue.DEVP.offer(MessageFormat.format( "【{0}】货叉提升机连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
            News.info("货叉提升机连接成功 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
        } else {
            OutputQueue.DEVP.offer(MessageFormat.format( "【{0}】货叉提升机连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}]  [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
            News.error("货叉提升机连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
        }
        // siemensS7Net.ConnectClose();
        return result;
    }
 
    @Override
    public void close() {
 
    }
 
    @Override
    public void run() {
        News.info("{}号货叉提升机线程启动", slave.getId());
        this.connect();
        while (true) {
            try {
                read();
                Thread.sleep(200);
                execute();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    private void execute() {
        ForkLiftAction forkLiftAction = SpringUtils.getBean(ForkLiftAction.class);
        if (forkLiftAction == null) {
            return;
        }
 
        Object object = redisUtil.get(RedisKeyType.FORK_LIFT_FLAG.key + slave.getId());
        if (object == null) {
            return;
        }
 
        Integer taskNo = Integer.valueOf(String.valueOf(object));
        if (taskNo != 0) {
            //存在任务需要执行
            boolean result = forkLiftAction.executeWork(slave.getId(), taskNo);
        }
    }
 
    private void read() {
        try {
            readStatus();
 
            //提升机处于运行状态,将标记置为true
            if (forkLiftProtocol.getProtocolStatusType().equals(ForkLiftProtocolStatusType.PICK_UP)
                    || forkLiftProtocol.getProtocolStatusType().equals(ForkLiftProtocolStatusType.PUT_DOWN)
                    || forkLiftProtocol.getProtocolStatusType().equals(ForkLiftProtocolStatusType.WAITING)) {
                forkLiftProtocol.setPakMk(true);
            }
        } catch (Exception e) {
            OutputQueue.FORKLIFT.offer(MessageFormat.format("【{0}】读取提升机状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
        }
    }
 
    private void readStatus() {
        try {
            //获取提升机数据
            OperateResultExOne<byte[]> result1 = siemensS7Net.Read("DB101.0", (short) 32);
            if (result1.IsSuccess) {
                if (null == forkLiftProtocol) {
                    forkLiftProtocol = new ForkLiftProtocol();
                    forkLiftProtocol.setLiftNo(slave.getId());
                    forkLiftProtocol.setProtocolStatus(ForkLiftProtocolStatusType.NONE);
 
                    InnerForkLiftExtend innerForkLiftExtend = new InnerForkLiftExtend();
                }
 
                //----------读取提升机状态-----------
                //模式
                forkLiftProtocol.setModel((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 0));
                //PLC任务号
                forkLiftProtocol.setWrkNo((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 4));
                //任务状态
                forkLiftProtocol.setProtocolStatus((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 8));
                //任务模式
                forkLiftProtocol.setModel((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 12));
                //取货数据
                forkLiftProtocol.setPick((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 16));
                //放货数据
                forkLiftProtocol.setPut((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 20));
                //出入库模式
                forkLiftProtocol.setIOMode((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 24));
                //故障码
                forkLiftProtocol.setErrorCode((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 28));
 
                //************补充扩展字段*************
                InnerForkLiftExtend forkLiftExtend = (InnerForkLiftExtend) forkLiftProtocol.getExtend();
                forkLiftProtocol.setExtend(forkLiftExtend);
 
            }else {
                OutputQueue.FORKLIFT.offer(MessageFormat.format("【{0}】{1}读取货叉提升机状态信息失败", DateUtils.convert(new Date()), slave.getId()));
            }
 
            if (System.currentTimeMillis() - forkLiftProtocol.getDeviceDataLog() > 1000 * 5) {
                //采集时间超过5s,保存一次数据记录
                //保存数据记录
                DeviceDataLogService deviceDataLogService = SpringUtils.getBean(DeviceDataLogService.class);
                DeviceDataLog deviceDataLog = new DeviceDataLog();
                deviceDataLog.setOriginData(Base64.getEncoder().encodeToString(result1.Content));
                deviceDataLog.setWcsData(JSON.toJSONString(forkLiftProtocol));
                deviceDataLog.setType("forkLift");
                deviceDataLog.setDeviceNo(forkLiftProtocol.getLiftNo());
                deviceDataLog.setCreateTime(new Date());
                deviceDataLogService.insert(deviceDataLog);
 
                //更新采集时间
                forkLiftProtocol.setDeviceDataLog(System.currentTimeMillis());
            }
 
            //将提升机状态保存至数据库
            BasLiftService basLiftService = SpringUtils.getBean(BasLiftService.class);
            BasLift basLift = basLiftService.selectOne(new EntityWrapper<BasLift>()
                    .eq("lift_no", slave.getId()));
            if (basLift == null) {
                basLift = new BasLift();
                //提升机号
                basLift.setLiftNo(forkLiftProtocol.getLiftNo());
                basLift.setStatus(1);
                basLiftService.insert(basLift);
            }
            //任务号
            basLift.setWrkNo(forkLiftProtocol.getTaskNo());
            //修改时间
            basLift.setUpdateTime(new Date());
            //设备状态
            basLift.setDeviceStatus(JSON.toJSONString(forkLiftProtocol));
            if (basLiftService.updateById(basLift)) {
                OutputQueue.FORKLIFT.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), forkLiftProtocol.getLiftNo()));
            }
 
        } catch (Exception e) {
            OutputQueue.FORKLIFT.offer(MessageFormat.format("【{0}】读取货叉提升机状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
        }
    }
 
    @Override
    public ForkLiftProtocol getStatus(boolean clone) {
        if (this.forkLiftProtocol == null) {
            return null;
        }
        return clone ? this.forkLiftProtocol.clone() : this.forkLiftProtocol;
    }
 
    @Override
    public List<ForkLiftStaProtocol> getForkLiftStaProtocols() {
        return this.forkLiftStaProtocols;
    }
 
    @Override
    public ForkLiftProtocol getStatus() {
        return getStatus(true);
    }
 
    @Override
    public CommandResponse pickAndPut(ForkLiftCommand command) {
        CommandResponse response = new CommandResponse(false);
 
        short[] array = new short[4];
        array[0] = command.getTaskNo();//任务号
        array[1] = command.getMode();//任务模式
        array[2] = command.getPick();//取货数据
        array[3] = command.getPut();//放货数据
        OperateResult result = siemensS7Net.Write("DB103.0", array);
        if (result.IsSuccess) {
            OperateResult result2 = siemensS7Net.Write("DB103.16", command.getConfirm());
            if (result2.IsSuccess) {
                response.setResult(true);
            }
        }
        return response;
    }
 
    @Override
    public CommandResponse shuttleSwitch(ForkLiftCommand command) {
        CommandResponse response = new CommandResponse(false);
 
        short[] array = new short[4];
        array[0] = command.getTaskNo();//任务号
        array[1] = command.getMode();//任务模式
        array[2] = command.getPick();//取货数据
        array[3] = command.getPut();//放货数据
        OperateResult result = siemensS7Net.Write("DB103.0", array);
        if (result.IsSuccess) {
            OperateResult result2 = siemensS7Net.Write("DB103.16", command.getConfirm());
            if (result2.IsSuccess) {
                response.setResult(true);
            }
        }
        return response;
    }
 
    @Override
    public CommandResponse reset(ForkLiftCommand command) {
        CommandResponse response = new CommandResponse(false);
        return response;
    }
 
    @Override
    public boolean isIdle() {
        if (this.forkLiftProtocol.getTaskNo() == null
                || this.forkLiftProtocol.getProtocolStatus() == null
        ) {
            return false;
        }
 
        boolean res = this.forkLiftProtocol.getProtocolStatus() == ForkLiftProtocolStatusType.IDLE.id
                && this.forkLiftProtocol.getTaskNo() == 0;
        return res;
    }
 
    @Override
    public boolean isDeviceIdle() {
        return false;
    }
 
    @Override
    public boolean isDeviceIdle(ExecuteSupport support) {
        return false;
    }
 
    @Override
    public boolean setProtocolStatus(ForkLiftProtocolStatusType status) {
        return false;
    }
 
    @Override
    public boolean setSyncTaskNo(Integer taskNo) {
        this.forkLiftProtocol.setSyncTaskNo(taskNo);
        return true;
    }
 
    @Override
    public int generateDeviceTaskNo(int taskNo, ForkLiftTaskModeType type) {
        return taskNo;
    }
 
    @Override
    public List<ForkLiftCommand> getPickAndPutCommand(Integer taskNo, Integer pick, Integer put) {
        List<ForkLiftCommand> commands = new ArrayList<>();
        ForkLiftCommand command = new ForkLiftCommand();
        command.setLiftNo(slave.getId());
        command.setTaskNo(taskNo.shortValue());
        command.setMode(ForkLiftTaskModeType.PICK_PUT.id.shortValue());
        command.setPick(pick.shortValue());
        command.setPut(put.shortValue());
        command.setConfirm((short) 1);
 
        commands.add(command);
        return commands;
    }
 
    @Override
    public List<ForkLiftCommand> getShuttleSwitchCommand(Integer taskNo, Integer originLev, Integer targetLev) {
        List<ForkLiftCommand> commands = new ArrayList<>();
        ForkLiftCommand command = new ForkLiftCommand();
        command.setLiftNo(slave.getId());
        command.setTaskNo(taskNo.shortValue());
        command.setMode(ForkLiftTaskModeType.SHUTTLE_SWITCH.id.shortValue());
        command.setPick(originLev.shortValue());
        command.setPut(targetLev.shortValue());
        command.setConfirm((short) 1);
 
        commands.add(command);
        return commands;
    }
 
    /**
     * 扩展字段
     */
    @Data
    private class InnerForkLiftExtend {
 
    }
}