#
Junjie
2024-10-17 d835d1b51f832889929cdf69010034a30ef44d02
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
package com.zy.asrs.wcs.rcs.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.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.framework.common.DateUtils;
import com.zy.asrs.framework.common.SpringUtils;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wcs.common.ExecuteSupport;
import com.zy.asrs.wcs.core.domain.dto.BasLiftStaDto;
import com.zy.asrs.wcs.core.entity.BasLift;
import com.zy.asrs.wcs.core.model.command.LiftCommand;
import com.zy.asrs.wcs.core.model.enums.LiftCommandModeType;
import com.zy.asrs.wcs.core.model.enums.MotionCtgType;
import com.zy.asrs.wcs.core.service.BasLiftService;
import com.zy.asrs.wcs.core.utils.RedisUtil;
import com.zy.asrs.wcs.core.utils.Utils;
import com.zy.asrs.wcs.rcs.News;
import com.zy.asrs.wcs.rcs.cache.OutputQueue;
import com.zy.asrs.wcs.rcs.entity.Device;
import com.zy.asrs.wcs.rcs.entity.DeviceDataLog;
import com.zy.asrs.wcs.rcs.model.enums.LiftProtocolStatusType;
import com.zy.asrs.wcs.rcs.model.protocol.LiftProtocol;
import com.zy.asrs.wcs.rcs.service.DeviceDataLogService;
import com.zy.asrs.wcs.rcs.thread.LiftThread;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
 
import java.text.MessageFormat;
import java.util.*;
 
@Slf4j
@SuppressWarnings("all")
public class JxgtLiftThread implements LiftThread {
 
    private Device device;
    private RedisUtil redisUtil;
    private LiftProtocol liftProtocol;
    private SiemensS7Net siemensS7Net;
    private List<BasLiftStaDto> staList = new ArrayList<>();
 
    public JxgtLiftThread(Device device, RedisUtil redisUtil) {
        this.device = device;
        this.redisUtil = redisUtil;
    }
 
    @Override
    public void run() {
        News.info("{}号提升机线程启动", device.getDeviceNo());
        this.connect();
        while (true) {
            try {
                read();
                Thread.sleep(500);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    private void read() {
        try {
            readStatus();
 
            //提升机处于运行状态,将标记置为true
            if (liftProtocol.getRun()) {
                liftProtocol.setPakMk(true);
            }
        } catch (Exception e) {
            OutputQueue.LIFT.offer(MessageFormat.format("【{0}】读取提升机状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort()));
        }
    }
 
    private void readStatus() {
        try {
            //获取提升机数据
            OperateResultExOne<byte[]> result1 = siemensS7Net.Read("DB100.0", (short) 20);
            if (result1.IsSuccess) {
                if (null == liftProtocol) {
                    liftProtocol = new LiftProtocol();
                    liftProtocol.setLiftNo(Integer.valueOf(device.getDeviceNo()));
                    liftProtocol.setProtocolStatus(LiftProtocolStatusType.IDLE);
                    liftProtocol.setDevice(device);
 
                    InnerLiftExtend innerLiftExtend = new InnerLiftExtend();
                    liftProtocol.setExtend(innerLiftExtend);
                }
 
                //----------读取提升机状态-----------
                //模式
                liftProtocol.setModel(siemensS7Net.getByteTransform().TransInt16(result1.Content, 0) == 1);
                //忙闲
                liftProtocol.setRun(siemensS7Net.getByteTransform().TransInt16(result1.Content, 2) == 0);
//                //前超限
//                liftProtocol.setFrontOverrun(status1[4]);
//                //后超限
//                liftProtocol.setBackOverrun(status1[5]);
//                //左超限
//                liftProtocol.setLeftOverrun(status1[6]);
//                //右超限
//                liftProtocol.setRightOverrun(status1[7]);
//                //超高
//                liftProtocol.setOverHeight(status2[0]);
//                //超重
//                liftProtocol.setOverWeight(status2[1]);
                //有托盘
                liftProtocol.setHasTray(siemensS7Net.getByteTransform().TransInt16(result1.Content, 4) == 1);
                //有小车
                liftProtocol.setHasCar(siemensS7Net.getByteTransform().TransInt16(result1.Content, 6) == 1);
                //当前楼层
                liftProtocol.setLev((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 8));
//                //工作号
//                liftProtocol.setTaskNo(siemensS7Net.getByteTransform().TransInt32(result1.Content, 12));
                //设备故障
                liftProtocol.setError(siemensS7Net.getByteTransform().TransInt16(result1.Content, 16) == 1);
                //故障码
                liftProtocol.setErrorCode(String.valueOf(siemensS7Net.getByteTransform().TransInt16(result1.Content, 18)));
 
                //************补充扩展字段*************
                InnerLiftExtend liftExtend = (InnerLiftExtend) liftProtocol.getExtend();
                //锁定
                liftExtend.setLock(siemensS7Net.getByteTransform().TransInt16(result1.Content, 10) == 1);
                liftProtocol.setExtend(liftExtend);
 
                boolean ready = true;
                if (!liftProtocol.getModel() || liftProtocol.getRun()) {
                    ready = false;
                }
                liftProtocol.setReady(ready);//就绪状态
 
 
            }else {
                OutputQueue.LIFT.offer(MessageFormat.format("【{0}】{1}读取提升机状态信息失败", DateUtils.convert(new Date()), device.getId()));
                throw new CoolException(MessageFormat.format( "读取提升机状态信息失败 ===>> [id:{0}] [ip:{1}] [port:{2}]", device.getId(), device.getIp(), device.getPort()));
            }
 
            if (System.currentTimeMillis() - liftProtocol.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(liftProtocol));
                deviceDataLog.setType("lift");
                deviceDataLog.setDeviceNo(String.valueOf(liftProtocol.getLiftNo()));
                deviceDataLog.setCreateTime(new Date());
                deviceDataLog.setHostId(device.getHostId());
                deviceDataLogService.save(deviceDataLog);
 
                //更新采集时间
                liftProtocol.setDeviceDataLog(System.currentTimeMillis());
            }
 
            //将提升机状态保存至数据库
            BasLiftService basLiftService = SpringUtils.getBean(BasLiftService.class);
            BasLift basLift = basLiftService.getOne(new LambdaQueryWrapper<BasLift>()
                    .eq(BasLift::getLiftNo, device.getDeviceNo())
                    .eq(BasLift::getHostId, device.getHostId()));
            if (basLift == null) {
                basLift = new BasLift();
                //提升机号
                basLift.setLiftNo(Integer.valueOf(device.getDeviceNo()));
                basLift.setStatus(1);
                basLift.setDeleted(0);
                basLift.setHostId(device.getHostId());
                basLift.setDeviceId(device.getId());
                basLiftService.save(basLift);
            }
            //任务号
            basLift.setTaskNo(liftProtocol.getTaskNo().intValue());
            //修改时间
            basLift.setUpdateTime(new Date());
            //设备状态
            basLift.setProtocol(JSON.toJSONString(liftProtocol));
            if (basLiftService.updateById(basLift)) {
                OutputQueue.LIFT.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), device.getDeviceNo()));
            }
 
        } catch (Exception e) {
            OutputQueue.LIFT.offer(MessageFormat.format("【{0}】读取提升机状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort()));
        }
    }
 
    @Override
    public LiftProtocol getStatus() {
        return this.liftProtocol.clone();
    }
 
    @Override
    public Device getDevice() {
        return this.device;
    }
 
    @Override
    public boolean move(LiftCommand command) {
        return write(command);
    }
 
    @Override
    public boolean palletInOut(LiftCommand command) {
        return write(command);
    }
 
    private boolean write(LiftCommand command) {
        if (null == command) {
            News.error("提升机写入命令为空");
            return false;
        }
 
        List<Integer> list = JSON.parseArray(command.getBody(), Integer.class);
        int[] array = new int[list.size()];//获取命令报文
        for (int i = 0; i < list.size(); i++) {
            array[i] = list.get(i);
        }
 
        OperateResult result = null;
        if (command.getMode() == LiftCommandModeType.MOVE.id) {
            //移动
            result = siemensS7Net.Write("DB101.2", (short) array[0]);
            result = siemensS7Net.Write("DB101.4", (int) array[1]);
        } else if (command.getMode() == LiftCommandModeType.PALLET_INOUT.id) {
            //托盘出入
            result = siemensS7Net.Write("DB101.8", array);
        } else if (command.getMode() == LiftCommandModeType.LOCK.id || command.getMode() == LiftCommandModeType.UNLOCK.id) {
            //提升机锁定/解锁
            result = siemensS7Net.Write("DB101.0", (short) array[0]);
        }
 
        if (result != null && result.IsSuccess) {
            liftProtocol.setSendTime(System.currentTimeMillis());//指令下发时间
            News.info("提升机命令下发[id:{}] >>>>> {}", device.getId(), JSON.toJSON(command));
            OutputQueue.LIFT.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), device.getId(), JSON.toJSON(command)));
            return true;
        } else {
            OutputQueue.LIFT.offer(MessageFormat.format("【{0}】写入提升机plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort()));
            News.error("写入提升机plc数据失败 ===>> [id:{}] [ip:{}] [port:{}]", device.getId(), device.getIp(), device.getPort());
            return false;
        }
    }
 
    @Override
    public boolean lock(LiftCommand command) {
        return write(command);
    }
 
    @Override
    public boolean unlock(LiftCommand command) {
        return write(command);
    }
 
    @Override
    public boolean reset(LiftCommand command) {
        return false;
    }
 
    @Override
    public boolean isIdle() {
        return isIdle(null);
    }
 
    @Override
    public boolean isIdle(MotionCtgType flag) {
        // 判断提升机是否自动、就绪、空闲
        if (this.liftProtocol.getModel()
                && !this.liftProtocol.getRun()
                && this.liftProtocol.getReady()
                && this.liftProtocol.getPakMk()
                && !this.liftProtocol.getError()
                && this.liftProtocol.getErrorCode().equals("0")
                && (this.liftProtocol.getProtocolStatusType().equals(LiftProtocolStatusType.IDLE)
                || this.liftProtocol.getProtocolStatusType().equals(LiftProtocolStatusType.WAITING))
        ) {
            return true;
        }
        return false;
    }
 
    @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.liftProtocol.getModel()
                && !this.liftProtocol.getRun()
                && this.liftProtocol.getReady()
                && !this.liftProtocol.getError()
                && this.liftProtocol.getErrorCode().equals("0")
        ) {
            return true;
        }
        return false;
    }
 
    @Override
    public synchronized boolean setProtocolStatus(LiftProtocolStatusType status) {
        this.liftProtocol.setProtocolStatus(status);
        return true;
    }
 
    @Override
    public synchronized boolean setSyncTaskNo(Integer taskNo) {
        this.liftProtocol.setTaskNo(taskNo);
        return true;
    }
 
    @Override
    public boolean isLock(ExecuteSupport support) {
        InnerLiftExtend extend = (InnerLiftExtend) this.liftProtocol.getExtend();
        return extend.getLock();
    }
 
    @Override
    public int generateDeviceTaskNo(int taskNo, MotionCtgType motionCtgType) {
        int deviceTaskNo = taskNo;
        try {
            deviceTaskNo = Utils.getTaskNo("LIFT_TASK_NO");
        } catch (Exception e) {
            return taskNo;
        }
        return deviceTaskNo;
    }
 
    @Override
    public List<LiftCommand> getMoveCommand(Integer taskNo, Integer sourceLev, Integer targetLev, LiftCommandModeType mode) {
        // 开始任务
        short[] array = new short[2];
        array[0] = targetLev.shortValue();//目标层
        array[1] = taskNo.shortValue();//工作号
 
        LiftCommand command = new LiftCommand();
        command.setLiftNo(Integer.valueOf(this.device.getDeviceNo()));
        command.setTaskNo(taskNo);
        command.setBody(JSON.toJSONString(array));
        command.setMode(LiftCommandModeType.MOVE.id);
        command.setOriginLev(sourceLev);
        command.setTargetLev(targetLev);
 
        ArrayList<LiftCommand> list = new ArrayList<>();
        list.add(command);
        return list;
    }
 
    @Override
    public List<LiftCommand> getMoveWithShuttleCommand(Integer taskNo, Integer sourceLev, Integer targetLev, LiftCommandModeType mode) {
        return getMoveCommand(taskNo, sourceLev, targetLev, mode);
    }
 
    @Override
    public List<LiftCommand> getPalletInOutCommand(Integer taskNo, Integer sourceLev, Integer targetLev, Integer originSta, Integer targetSta, LiftCommandModeType mode) {
        // 开始任务
        short[] array = new short[3];
        array[0] = originSta.shortValue();//起始站
        array[1] = targetSta.shortValue();//目标站
        array[2] = taskNo.shortValue();//工作号
 
        LiftCommand command = new LiftCommand();
        command.setLiftNo(Integer.valueOf(this.device.getDeviceNo()));
        command.setTaskNo(taskNo);
        command.setBody(JSON.toJSONString(array));
        command.setMode(LiftCommandModeType.PALLET_INOUT.id);
        command.setOriginLev(sourceLev);
        command.setTargetLev(targetLev);
 
        ArrayList<LiftCommand> list = new ArrayList<>();
        list.add(command);
        return list;
    }
 
    @Override
    public List<LiftCommand> getLockCommand(Integer taskNo, Boolean lock) {
        // 开始任务
        short[] array = new short[2];
        array[0] = lock ? (short) 1 : (short) 0;
 
        LiftCommand command = new LiftCommand();
        command.setLiftNo(Integer.valueOf(this.device.getDeviceNo()));
        command.setTaskNo(taskNo);
        command.setBody(JSON.toJSONString(array));
        command.setMode(lock ? LiftCommandModeType.LOCK.id : LiftCommandModeType.UNLOCK.id);
 
        ArrayList<LiftCommand> list = new ArrayList<>();
        list.add(command);
        return list;
    }
 
    @Override
    public List<LiftCommand> getShuttleSignalCommand(Integer taskNo, Boolean signal) {
        return null;
    }
 
    @Override
    public boolean connect() {
        boolean result = false;
        //-------------------------提升机连接方法------------------------//
        siemensS7Net = new SiemensS7Net(SiemensPLCS.S1500, device.getIp());
        OperateResult connect = siemensS7Net.ConnectServer();
        if(connect.IsSuccess){
            result = true;
            OutputQueue.LIFT.offer(MessageFormat.format( "【{0}】提升机plc连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort()));
            log.info("提升机plc连接成功 ===>> [id:{}] [ip:{}] [port:{}] ", device.getId(), device.getIp(), device.getPort());
        } else {
            OutputQueue.LIFT.offer(MessageFormat.format("【{0}】提升机plc连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort()));
            log.error("提升机plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}] ", device.getId(), device.getIp(), device.getPort());
        }
        siemensS7Net.ConnectClose();
        //-------------------------提升机连接方法------------------------//
        return result;
    }
 
    @Override
    public void close() {
        siemensS7Net.ConnectClose();
    }
 
    /**
     * 扩展字段
     */
    @Data
    private class InnerLiftExtend {
 
        /**
         * 提升机锁定
         */
        private Boolean lock = false;
 
    }
 
}