自动化立体仓库 - WCS系统
Junjie
4 天以前 8a0442fe48ef5a8d314fd8426475ae44de81a047
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
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.cache.OutputQueue;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.*;
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 LfdZyForkLiftSlaveThread implements ForkLiftThread {
 
    private Integer masterId;
    private ForkLiftSlave slave;
    private ForkLiftProtocol forkLiftProtocol;
    private RedisUtil redisUtil;
    private List<ForkLiftStaProtocol> forkLiftStaProtocols = new ArrayList<>();
    private LfdZyForkLiftMasterThread masterThread;
 
    public LfdZyForkLiftSlaveThread(ForkLiftSlave slave, RedisUtil redisUtil, Integer masterId) {
        this.slave = slave;
        this.redisUtil = redisUtil;
        this.masterId = masterId;
        //初始化站点
        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;
        LfdZyForkLiftMasterThread masterThread = (LfdZyForkLiftMasterThread) SlaveConnection.get(SlaveType.ForkLiftMaster, masterId);
        if(masterThread != null) {
            result = true;
            this.masterThread = masterThread;
        }
        return result;
    }
 
    @Override
    public void close() {
 
    }
 
    @Override
    public void run() {
        News.info("{}号货叉提升机线程启动", slave.getId());
        this.connect();
        while (true) {
            try {
                if (this.masterThread == null) {
                    this.connect();
                    continue;
                }
 
                read();
                Thread.sleep(300);
                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 = this.masterThread.read(this.slave.getId(), "status");
            SiemensS7Net siemensS7Net = this.masterThread.getSiemensS7Net();
            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, 2));
                //任务状态
                forkLiftProtocol.setProtocolStatus((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 4));
                //任务模式
                forkLiftProtocol.setTaskMode((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 6));
                //取货数据
                forkLiftProtocol.setPick((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 8));
                //放货数据
                forkLiftProtocol.setPut((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 10));
                //出入库模式
                forkLiftProtocol.setIOMode((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 12));
                //故障码
                forkLiftProtocol.setErrorCode((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 14));
                //当前层
                forkLiftProtocol.setLev((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 16));
 
                //************补充扩展字段*************
                InnerForkLiftExtend forkLiftExtend = (InnerForkLiftExtend) forkLiftProtocol.getExtend();
                forkLiftProtocol.setExtend(forkLiftExtend);
 
            }else {
                OutputQueue.FORKLIFT.offer(MessageFormat.format("【{0}】{1}读取货叉提升机状态信息失败", DateUtils.convert(new Date()), slave.getId()));
            }
 
            OperateResultExOne<byte[]> result2 = this.masterThread.read(this.slave.getId(), "staStatusTray");
            if (result2.IsSuccess) {
                for (int i = 0; i < this.slave.getSta().size(); i++) {
                    ForkLiftSlave.Sta sta = this.slave.getSta().get(i);
                    if (forkLiftStaProtocols.isEmpty()) {
                        continue;
                    }
 
                    short val = siemensS7Net.getByteTransform().TransInt16(result2.Content, i * 2);
                    ForkLiftStaProtocol forkLiftStaProtocol = forkLiftStaProtocols.get(i);
                    forkLiftStaProtocol.setHasTray(val == 1);
                }
            }
 
            OperateResultExOne<byte[]> result3 = this.masterThread.read(this.slave.getId(), "staStatusCar");
            if (result3.IsSuccess) {
                for (int i = 0; i < this.slave.getSta().size(); i++) {
                    ForkLiftSlave.Sta sta = this.slave.getSta().get(i);
                    if (forkLiftStaProtocols.isEmpty()) {
                        continue;
                    }
 
                    short val = siemensS7Net.getByteTransform().TransInt16(result3.Content, i * 2);
                    ForkLiftStaProtocol forkLiftStaProtocol = forkLiftStaProtocols.get(i);
                    forkLiftStaProtocol.setHasCar(val == 1);
                }
            }
 
            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);
 
                //保存数据记录
                DeviceDataLog deviceDataLog2 = new DeviceDataLog();
                deviceDataLog2.setOriginData(Base64.getEncoder().encodeToString(result2.Content));
                deviceDataLog2.setWcsData(JSON.toJSONString(forkLiftStaProtocols));
                deviceDataLog2.setType("forkLiftStaProtocols");
                deviceDataLog2.setDeviceNo(forkLiftProtocol.getLiftNo());
                deviceDataLog2.setCreateTime(new Date());
                deviceDataLogService.insert(deviceDataLog2);
 
                //更新采集时间
                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 = this.masterThread.write(this.slave.getId(), "write", array);
        if (result.IsSuccess) {
            OperateResult result2 = this.masterThread.write(this.slave.getId(), "writeConfirm", 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 = this.masterThread.write(this.slave.getId(), "write", array);
        if (result.IsSuccess) {
            OperateResult result2 = this.masterThread.write(this.slave.getId(), "writeConfirm", command.getConfirm());
            if (result2.IsSuccess) {
                response.setResult(true);
            }
        }
        return response;
    }
 
    @Override
    public CommandResponse move(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 = this.masterThread.write(this.slave.getId(), "write", array);
        if (result.IsSuccess) {
            OperateResult result2 = this.masterThread.write(this.slave.getId(), "writeConfirm", command.getConfirm());
            if (result2.IsSuccess) {
                response.setResult(true);
            }
        }
        return response;
    }
 
    @Override
    public CommandResponse reset() {
        CommandResponse response = new CommandResponse(false);
        OperateResult result = this.masterThread.write(this.slave.getId(), "confirm", (short) 1);
        if (result.IsSuccess) {
            News.info("货叉提升机确认命令下发成功,提升机号={}", forkLiftProtocol.getLiftNo());
            response.setResult(true);
        }
        return response;
    }
 
    @Override
    public boolean isIdle() {
        if (this.forkLiftProtocol.getTaskNo() == null
                || this.forkLiftProtocol.getProtocolStatus() == null
                || this.forkLiftProtocol.getModel() == null
                || this.forkLiftProtocol.getErrorCode() == null
        ) {
            return false;
        }
 
        boolean res = this.forkLiftProtocol.getProtocolStatus() == ForkLiftProtocolStatusType.IDLE.id
                && this.forkLiftProtocol.getWrkNo() == 0
                && this.forkLiftProtocol.getTaskNo() == 0
                && this.forkLiftProtocol.getModel() == 2
                && this.forkLiftProtocol.getErrorCode() == 0
                ;
        return res;
    }
 
    @Override
    public boolean isDeviceIdle() {
        return isDeviceIdle(null);
    }
 
    @Override
    public boolean isDeviceIdle(ExecuteSupport support) {
        if (null != support) {
            Boolean judgement = support.judgement();
            if (judgement != null && !judgement) {
                return true;
            }
        }
 
        if (this.forkLiftProtocol.getTaskNo() == null
                || this.forkLiftProtocol.getProtocolStatus() == null
                || this.forkLiftProtocol.getModel() == null
                || this.forkLiftProtocol.getErrorCode() == null
        ) {
            return false;
        }
 
        boolean res = this.forkLiftProtocol.getProtocolStatus() == ForkLiftProtocolStatusType.IDLE.id
                && this.forkLiftProtocol.getWrkNo() == 0
                && this.forkLiftProtocol.getModel() == 2
                && this.forkLiftProtocol.getErrorCode() == 0
                ;
        return res;
    }
 
    @Override
    public boolean setProtocolStatus(ForkLiftProtocolStatusType status) {
        return false;
    }
 
    @Override
    public boolean setSyncTaskNo(Integer taskNo) {
        this.forkLiftProtocol.setSyncTaskNo(taskNo);
        return true;
    }
 
    @Override
    public boolean switchIOMode(ForkLiftIoModeType type) {
        OperateResult result = this.masterThread.write(this.slave.getId(), "switchIOMode", type.id.shortValue());
        if (result.IsSuccess) {
            return true;
        }
        return false;
    }
 
    @Override
    public int generateDeviceTaskNo(int taskNo, ForkLiftTaskModeType type) {
        return taskNo;
    }
 
    @Override
    public List<ForkLiftCommand> getPickAndPutCommand(Integer taskNo, Integer pick, Integer put) {
        Integer realPick = pick % 1000;
        Integer realPut = put % 1000;
 
        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(realPick.shortValue());
        command.setPut(realPut.shortValue());
        command.setConfirm((short) 1);
 
        commands.add(command);
        return commands;
    }
 
    @Override
    public List<ForkLiftCommand> getShuttleSwitchCommand(Integer taskNo, Integer pick, Integer put) {
        Integer realPick = pick % 1000;
        Integer realPut = put % 1000;
 
        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(realPick.shortValue());
        command.setPut(realPut.shortValue());
        command.setConfirm((short) 1);
 
        commands.add(command);
        return commands;
    }
 
    @Override
    public List<ForkLiftCommand> getMoveCommand(Integer taskNo, Integer pick, Integer put) {
        Integer realPick = pick % 1000;
        Integer realPut = put % 1000;
 
        List<ForkLiftCommand> commands = new ArrayList<>();
        ForkLiftCommand command = new ForkLiftCommand();
        command.setLiftNo(slave.getId());
        command.setTaskNo(taskNo.shortValue());
        command.setMode(ForkLiftTaskModeType.MOVE.id.shortValue());
        command.setPick(realPick.shortValue());
        command.setPut(realPut.shortValue());
        command.setConfirm((short) 1);
 
        commands.add(command);
        return commands;
    }
 
    /**
     * 扩展字段
     */
    @Data
    private class InnerForkLiftExtend {
 
    }
}