#
qlsxk
2025-09-27 6c8d62614b055111f20435d47319a69859a9f749
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
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.alibaba.fastjson.JSONObject;
import com.zy.common.SpringUtils;
import com.zy.common.utils.DateUtils;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
import com.zy.core.cache.OutputQueue;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.DeviceCommandMsgModel;
import com.zy.core.model.DeviceMsgModel;
import com.zy.core.properties.DeviceConfig;
import com.zy.core.thread.LiftThread;
import com.zy.core.utils.DeviceMsgUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
 
import java.text.MessageFormat;
import java.util.*;
 
@Slf4j
@SuppressWarnings("all")
public class NyLiftThread implements LiftThread {
 
    private DeviceConfig deviceConfig;
    private RedisUtil redisUtil;
    private SiemensS7Net siemensS7Net;
    private boolean connect = false;
 
    public NyLiftThread(DeviceConfig deviceConfig, RedisUtil redisUtil) {
        this.deviceConfig = deviceConfig;
        this.redisUtil = redisUtil;
    }
 
    @Override
    public boolean connect() {
        if (deviceConfig.getFake()) {
            this.connect = true;
            OutputQueue.LIFT.offer(MessageFormat.format( "【{0}】提升机连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort(), 0, 0));
            News.info("【{}】提升机连接成功 ===>> [id:{}] [ip:{}] [port:{}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
            return true;
        }
 
        boolean result = false;
        siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, deviceConfig.getIp());
        siemensS7Net.setRack(Integer.valueOf(0).byteValue());
        siemensS7Net.setSlot(Integer.valueOf(0).byteValue());
        OperateResult connect = siemensS7Net.ConnectServer();
        if(connect.IsSuccess){
            result = true;
            OutputQueue.LIFT.offer(MessageFormat.format( "【{0}】提升机连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort(), 0, 0));
            News.info("【{}】提升机连接成功 ===>> [id:{}] [ip:{}] [port:{}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
        } else {
            OutputQueue.LIFT.offer(MessageFormat.format( "【{0}】提升机连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}]  [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort(), 0, 0));
            News.error("【{}】提升机连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
        }
        // siemensS7Net.ConnectClose();
        this.connect = result;
        return result;
    }
 
    @Override
    public void close() {
 
    }
 
    @Override
    public void run() {
        News.info("{}号提升机线程启动", deviceConfig.getDeviceNo());
        this.connect();
        while (true) {
            try {
                DeviceMsgUtils deviceMsgUtils = null;
                try {
                    deviceMsgUtils = SpringUtils.getBean(DeviceMsgUtils.class);
                }catch (Exception e){}
                if (deviceMsgUtils == null) {
                    continue;
                }
                DeviceCommandMsgModel deviceCommandMsg = deviceMsgUtils.getDeviceCommandMsg(SlaveType.Lift, deviceConfig.getDeviceNo());
                if (deviceCommandMsg == null) {
                    continue;
                }
                executeCommand(deviceCommandMsg, deviceMsgUtils);
 
                Thread.sleep(200);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    private void executeCommand(DeviceCommandMsgModel deviceCommandMsg, DeviceMsgUtils deviceMsgUtils) {
        if (deviceConfig.getFake()) {
            redisUtil.set(RedisKeyType.FAKE_DEVICE_LIFT_COMMAND_MSG_KEY.key + deviceConfig.getDeviceNo(), deviceCommandMsg, 60 * 60);
            return;
        }
 
        String command = JSON.toJSONString(deviceCommandMsg.getCommand());
        JSONObject commandObj = JSON.parseObject(command);
 
        HashMap<String, Object> resultData = new HashMap<>();
        resultData.put("result", "success");
 
        String deviceMsgType = "command";
        if (commandObj.getInteger("mode") == 1) {
            //取放货
            short[] array = new short[4];
            array[0] = 1;//任务类型
            array[1] = commandObj.getShort("pick");//源站
            array[2] = commandObj.getShort("put");//目标站
            array[3] = commandObj.getShort("taskNo");//任务号
            resultData.put("commandData", array);
 
            OperateResult result = siemensS7Net.Write("DB83.0", array);
            if (result.IsSuccess) {
                resultData.put("result", "success");
            }
        } else if (commandObj.getInteger("mode") == 2) {
            //小车换层
            short[] array = new short[4];
            array[0] = 2;//任务类型
            array[1] = commandObj.getShort("pick");//源站
            array[2] = commandObj.getShort("put");//目标站
            array[3] = commandObj.getShort("taskNo");//任务号
            resultData.put("commandData", array);
 
            OperateResult result = siemensS7Net.Write("DB83.0", array);
            if (result.IsSuccess) {
                resultData.put("result", "success");
            }
        } else if (commandObj.getInteger("mode") == 3) {
            //提升机移动
            short[] array = new short[4];
            array[0] = 2;//任务类型
            array[1] = commandObj.getShort("pick");//源站
            array[2] = commandObj.getShort("put");//目标站
            array[3] = commandObj.getShort("taskNo");//任务号
            resultData.put("commandData", array);
 
            OperateResult result = siemensS7Net.Write("DB83.0", array);
            if (result.IsSuccess) {
                resultData.put("result", "success");
            }
        } else if (commandObj.getInteger("mode") == 9996) {
            //复位
            short[] array = new short[1];
            array[0] = 1;
            resultData.put("commandData", array);
 
            OperateResult result = siemensS7Net.Write("DB103.10", array);
            if (result.IsSuccess) {
                resultData.put("result", "success");
            }
        } else if (commandObj.getInteger("mode") == 9997) {
            //切换入库模式
            short[] array = new short[1];
            array[0] = 1;
            resultData.put("commandData", array);
 
            OperateResult result = siemensS7Net.Write("DB103.12", array);
            if (result.IsSuccess) {
                resultData.put("result", "success");
            }
        } else if (commandObj.getInteger("mode") == 9998) {
            //切换出库模式
            short[] array = new short[1];
            array[0] = 2;
            resultData.put("commandData", array);
 
            OperateResult result = siemensS7Net.Write("DB103.12", array);
            if (result.IsSuccess) {
                resultData.put("result", "success");
            }
        } else if (commandObj.getInteger("mode") == 9999) {
            //读取状态
            JSONObject device = new JSONObject();
 
            OperateResultExOne<byte[]> readResult1 = siemensS7Net.Read("DB82.0", (short) 14);
            if (readResult1.IsSuccess) {
 
                //读取4.0-4.7数据
                boolean[] status1 = siemensS7Net.getByteTransform().TransBool(readResult1.Content, 4, 1);
                //读取5.0-5.7数据
                boolean[] status2 = siemensS7Net.getByteTransform().TransBool(readResult1.Content, 5, 1);
 
                //模式
                device.put("model", status1[0] ? 2 : 1);
                //PLC任务号
                device.put("plcTaskNo", (int) siemensS7Net.getByteTransform().TransInt16(readResult1.Content, 6));
                //设备状态
                device.put("deviceStatus", status1[1] ? 0 : 1);
                //任务模式
                device.put("taskMode", 0);
                //取货数据
                device.put("pick", 0);
                //放货数据
                device.put("put", 0);
                //出入库模式
                device.put("iOMode", 0);
                //有托盘
                device.put("hasTray", status2[5] ? 1 : 0);
                //有小车
                device.put("hasCar", status2[6] ? 1 : 0);
                //故障码
                device.put("errorCode", status2[7] ? 1 : 0);
                //层
                device.put("lev", (int) siemensS7Net.getByteTransform().TransInt16(readResult1.Content, 12));
 
                JSONObject extend = new JSONObject();
                device.put("extend", extend);
                //前超限
                extend.put("frontOverrun", status1[4] ? 1 : 0);
                //后超限
                extend.put("backOverrun", status1[5] ? 1 : 0);
                //左超限
                extend.put("leftOverrun", status1[6] ? 1 : 0);
                //右超限
                extend.put("rightOverrun", status1[7] ? 1 : 0);
                //超高
                extend.put("overHeight", status2[0] ? 1 : 0);
                //超重
                extend.put("overWeight", status2[1] ? 1 : 0);
                //PLC已完成任务号
                extend.put("plcTaskNoComplete", (int) siemensS7Net.getByteTransform().TransInt16(readResult1.Content, 10));
 
                resultData.put("deviceStatus", device);
            }
 
            //站点个数
            List<Integer> staList = new ArrayList<>();
            staList.add(101);
            staList.add(102);
            staList.add(103);
            staList.add(1001);
            staList.add(1002);
            staList.add(1003);
 
            List<JSONObject> stationList = new ArrayList<>();
            OperateResultExOne<byte[]> readResult2 = siemensS7Net.Read("DB82.14", (short) 156);
            if(readResult2.IsSuccess) {
                int i = 0;
                for (Integer siteId : staList) {
                    //读取4.0-4.7数据
                    boolean[] status1 = siemensS7Net.getByteTransform().TransBool(readResult2.Content, i * 26, 1);
                    //读取5.0-5.7数据
                    boolean[] status2 = siemensS7Net.getByteTransform().TransBool(readResult2.Content, i * 26 + 1, 1);
                    String barcode = siemensS7Net.getByteTransform().TransString(readResult2.Content, i * 26 + 10, 12, "UTF-8");
 
                    JSONObject station = new JSONObject();
                    station.put("siteId", siteId);
                    station.put("model", status1[0] ? 1 : 0);
                    station.put("busy", status1[1] ? 1 : 0);
                    station.put("hasTray", status1[2] ? 1 : 0);
                    station.put("inMode", status1[3] ? 1 : 0);
                    station.put("outMode", status1[4] ? 1 : 0);
                    station.put("allowShuttleTake", status2[5] ? 1 : 0);
                    station.put("allowShuttlePut", status2[6] ? 1 : 0);
                    station.put("deviceError", status2[7] ? 1 : 0);
                    station.put("taskNo", (int) siemensS7Net.getByteTransform().TransInt16(readResult2.Content, i * 26 + 2));
                    station.put("staNo", (int) siemensS7Net.getByteTransform().TransInt16(readResult2.Content, i * 28 + 2));
                    station.put("barcode", barcode);
                    stationList.add(station);
                    i++;
                }
            }
 
            device.put("stationList", stationList);
 
            resultData.put("commandResult1", readResult1.Content);
            resultData.put("commandResult2", readResult2.Content);
 
            deviceMsgType = "status";
        } else if (commandObj.getInteger("mode") == 10000) {
            //写入输送线数据
 
            Integer siteId = commandObj.getInteger("siteId");
            Integer staNo = commandObj.getInteger("staNo");
            Integer taskNo = commandObj.getInteger("taskNo");
            String address = commandObj.getString("address");
 
            short[] array = new short[2];
            array[0] = staNo.shortValue();//目标站
            array[1] = taskNo.shortValue();//任务号
 
            resultData.put("commandData", array);
 
            OperateResult result = siemensS7Net.Write(address, array);
            if (result.IsSuccess) {
                resultData.put("result", "success");
            }
        }
 
        if (deviceMsgType.equals("command")) {
            log.info("收到Rcs Lift Command Data: {}", JSON.toJSONString(deviceCommandMsg));
        }
        DeviceMsgModel deviceMsgModel = new DeviceMsgModel();
        deviceMsgModel.setDeviceId(deviceConfig.getDeviceNo());
        deviceMsgModel.setDeviceMsgType(deviceMsgType);
        deviceMsgModel.setDeviceMsg(resultData);
        deviceMsgModel.setDeviceOriginMsg(JSON.toJSONString(resultData));
        deviceMsgModel.setResultKey(deviceCommandMsg.getResultKey());
        deviceMsgUtils.sendDeviceMsg(SlaveType.Lift, deviceConfig.getDeviceNo(), deviceMsgModel);
    }
 
    @Override
    public DeviceConfig getDeviceConfig() {
        return this.deviceConfig;
    }
 
    /**
     * 扩展字段
     */
    @Data
    private class InnerLiftExtend {
 
        /**
         * 前超限
         */
        private Boolean frontOverrun;
 
        /**
         * 后超限
         */
        private Boolean backOverrun;
 
        /**
         * 左超限
         */
        private Boolean leftOverrun;
 
        /**
         * 右超限
         */
        private Boolean rightOverrun;
 
        /**
         * 超高
         */
        private Boolean overHeight;
 
        /**
         * 超重
         */
        private Boolean overWeight;
 
    }
}