#
Junjie
3 天以前 2468af88e9ee9901f1d4d6f1cfdb6361c7400730
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
package com.zy.core.network.real;
 
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.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.zy.asrs.entity.BasCrnpOpt;
import com.zy.asrs.entity.BasDualCrnp;
import com.zy.asrs.entity.BasDualCrnpOpt;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.asrs.service.BasCrnpOptService;
import com.zy.asrs.service.BasDualCrnpOptService;
import com.zy.asrs.utils.Utils;
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.model.CommandResponse;
import com.zy.core.model.command.DualCrnCommand;
import com.zy.core.network.api.ZyDualCrnConnectApi;
import com.zy.core.network.entity.ZyDualCrnStatusEntity;
 
import java.text.MessageFormat;
import java.util.Date;
 
public class ZyDualCrnRealConnect implements ZyDualCrnConnectApi {
 
    private SiemensS7Net siemensNet;
    private DeviceConfig deviceConfig;
 
    public ZyDualCrnRealConnect(DeviceConfig deviceConfig) {
        this.deviceConfig = deviceConfig;
    }
 
    public boolean connect() {
        boolean connected = false;
        siemensNet = new SiemensS7Net(SiemensPLCS.S1200, deviceConfig.getIp());
        OperateResult connect = siemensNet.ConnectServer();
        if(connect.IsSuccess){
            connected = true;
            OutputQueue.CRN.offer(MessageFormat.format( "【{0}双工位堆垛机plc连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()));
            News.info("双工位堆垛机plc连接成功 ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
        } else {
            OutputQueue.CRN.offer(MessageFormat.format("【{0}双工位堆垛机plc连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()));
            News.error("双工位堆垛机plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
        }
//        siemensNet.ConnectClose();
        return connected;
    }
 
    @Override
    public boolean disconnect() {
        siemensNet.ConnectClose();
        return true;
    }
 
    @Override
    public ZyDualCrnStatusEntity getStatus() {
        try {
            OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 70);
            if (result.IsSuccess) {
                ZyDualCrnStatusEntity crnStatus = new ZyDualCrnStatusEntity();
                crnStatus.setCrnNo(deviceConfig.getDeviceNo());
                crnStatus.setMode((int) siemensNet.getByteTransform().TransInt16(result.Content, 0));
 
                //工位1
                crnStatus.setTaskNo((int) siemensNet.getByteTransform().TransInt16(result.Content, 2));
                crnStatus.setStatus((int) siemensNet.getByteTransform().TransInt16(result.Content, 4));
                crnStatus.setBay((int) siemensNet.getByteTransform().TransInt16(result.Content, 6));
                crnStatus.setLevel((int) siemensNet.getByteTransform().TransInt16(result.Content, 8));
                crnStatus.setForkPos((int) siemensNet.getByteTransform().TransInt16(result.Content, 10));
                crnStatus.setLoaded((int) siemensNet.getByteTransform().TransInt16(result.Content, 12));
                crnStatus.setTaskReceive((int) siemensNet.getByteTransform().TransInt16(result.Content, 14));
 
                //工位2
                crnStatus.setTaskNoTwo((int) siemensNet.getByteTransform().TransInt16(result.Content, 16));
                crnStatus.setStatusTwo((int) siemensNet.getByteTransform().TransInt16(result.Content, 18));
                crnStatus.setBayTwo((int) siemensNet.getByteTransform().TransInt16(result.Content, 20));
                crnStatus.setLevelTwo((int) siemensNet.getByteTransform().TransInt16(result.Content, 22));
                crnStatus.setForkPosTwo((int) siemensNet.getByteTransform().TransInt16(result.Content, 24));
                crnStatus.setLoadedTwo((int) siemensNet.getByteTransform().TransInt16(result.Content, 26));
                crnStatus.setTaskReceiveTwo((int) siemensNet.getByteTransform().TransInt16(result.Content, 28));
 
                crnStatus.setWalkPos((int) siemensNet.getByteTransform().TransInt16(result.Content, 30));
                crnStatus.setWalkPosTwo((int) siemensNet.getByteTransform().TransInt16(result.Content, 32));
                crnStatus.setLiftPos((int) siemensNet.getByteTransform().TransInt16(result.Content, 34));
                crnStatus.setAlarm((int) siemensNet.getByteTransform().TransInt16(result.Content, 36));
                crnStatus.setXSpeed((double) siemensNet.getByteTransform().TransSingle(result.Content, 38));
                crnStatus.setYSpeed((double) siemensNet.getByteTransform().TransSingle(result.Content, 42));
                crnStatus.setZSpeed((double) siemensNet.getByteTransform().TransSingle(result.Content, 46));
                crnStatus.setZSpeedTwo((double) siemensNet.getByteTransform().TransSingle(result.Content, 50));
                crnStatus.setXDistance((double) siemensNet.getByteTransform().TransSingle(result.Content, 54));
                crnStatus.setYDistance((double) siemensNet.getByteTransform().TransSingle(result.Content, 58));
                crnStatus.setXDuration((double) siemensNet.getByteTransform().TransSingle(result.Content, 62));
                crnStatus.setYDuration((double) siemensNet.getByteTransform().TransSingle(result.Content, 66));
 
                if ((crnStatus.getStatus() == 0 || crnStatus.getStatus() == 20) && crnStatus.getTaskReceive() == 0) {
                    OperateResultExOne<byte[]> taskResult = siemensNet.Read("DB100.0", (short) 18);
                    if (taskResult.IsSuccess) {
                        short taskNo = siemensNet.getByteTransform().TransInt16(taskResult.Content, 0);
                        short taskMode = siemensNet.getByteTransform().TransInt16(taskResult.Content, 2);
                        short sourcePosX = siemensNet.getByteTransform().TransInt16(taskResult.Content, 4);
                        short sourcePosY = siemensNet.getByteTransform().TransInt16(taskResult.Content, 6);
                        short sourcePosZ = siemensNet.getByteTransform().TransInt16(taskResult.Content, 8);
                        short destinationPosX = siemensNet.getByteTransform().TransInt16(taskResult.Content, 10);
                        short destinationPosY = siemensNet.getByteTransform().TransInt16(taskResult.Content, 12);
                        short destinationPosZ = siemensNet.getByteTransform().TransInt16(taskResult.Content, 14);
                        short confirm = siemensNet.getByteTransform().TransInt16(taskResult.Content, 16);
 
                        if(taskNo != 0 || taskMode != 0 || sourcePosX != 0 || sourcePosY != 0 || sourcePosZ != 0 || destinationPosX != 0 || destinationPosY != 0 || destinationPosZ != 0 || confirm != 0) {
                            short[] array = new short[9];
                            array[0] = (short) 0;
                            array[1] = (short) 0;
                            array[2] = (short) 0;
                            array[3] = (short) 0;
                            array[4] = (short) 0;
                            array[5] = (short) 0;
                            array[6] = (short) 0;
                            array[7] = (short) 0;
                            array[8] = (short) 0;
 
                            RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
                            Object lock = redisUtil.get(RedisKeyType.DUAL_CRN_SEND_COMMAND_LOCK.key + deviceConfig.getDeviceNo());
                            if (lock == null) {
                                OperateResult taskClearResult = siemensNet.Write("DB100.0", array);
                                redisUtil.set(RedisKeyType.CLEAR_DUAL_CRN_TASK_LIMIT.key + deviceConfig.getDeviceNo(), "lock", 3);
                            }
                        }
                    }
                }
 
                if ((crnStatus.getStatusTwo() == 0 || crnStatus.getStatusTwo() == 20) && crnStatus.getTaskReceiveTwo() == 0) {
                    OperateResultExOne<byte[]> taskResult = siemensNet.Read("DB100.18", (short) 18);
                    if (taskResult.IsSuccess) {
                        short taskNo = siemensNet.getByteTransform().TransInt16(taskResult.Content, 0);
                        short taskMode = siemensNet.getByteTransform().TransInt16(taskResult.Content, 2);
                        short sourcePosX = siemensNet.getByteTransform().TransInt16(taskResult.Content, 4);
                        short sourcePosY = siemensNet.getByteTransform().TransInt16(taskResult.Content, 6);
                        short sourcePosZ = siemensNet.getByteTransform().TransInt16(taskResult.Content, 8);
                        short destinationPosX = siemensNet.getByteTransform().TransInt16(taskResult.Content, 10);
                        short destinationPosY = siemensNet.getByteTransform().TransInt16(taskResult.Content, 12);
                        short destinationPosZ = siemensNet.getByteTransform().TransInt16(taskResult.Content, 14);
                        short confirm = siemensNet.getByteTransform().TransInt16(taskResult.Content, 16);
 
                        if(taskNo != 0 || taskMode != 0 || sourcePosX != 0 || sourcePosY != 0 || sourcePosZ != 0 || destinationPosX != 0 || destinationPosY != 0 || destinationPosZ != 0 || confirm != 0) {
                            short[] array = new short[9];
                            array[0] = (short) 0;
                            array[1] = (short) 0;
                            array[2] = (short) 0;
                            array[3] = (short) 0;
                            array[4] = (short) 0;
                            array[5] = (short) 0;
                            array[6] = (short) 0;
                            array[7] = (short) 0;
                            array[8] = (short) 0;
 
                            RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
                            Object lock = redisUtil.get(RedisKeyType.DUAL_CRN_SEND_COMMAND_LOCK.key + deviceConfig.getDeviceNo());
                            if (lock == null) {
                                OperateResult taskClearResult = siemensNet.Write("DB100.18", array);
                            }
                        }
                    }
                }
 
                return crnStatus;
            } else {
                OutputQueue.CRN.offer(MessageFormat.format("【{0}】读取双工位堆垛机plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()));
                News.error("读取双工位堆垛机plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
            }
        } catch (Exception e) {
            e.printStackTrace();
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】读取双工位堆垛机plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()));
            News.error("读取双工位堆垛机plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
        }
        return null;
    }
 
    @Override
    public CommandResponse sendCommand(DualCrnCommand command) {
        RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
        redisUtil.set(RedisKeyType.DUAL_CRN_SEND_COMMAND_LOCK.key + deviceConfig.getDeviceNo(), "lock", 5);
        CommandResponse response = new CommandResponse(false);
         try {
             if (null == command) {
                 News.error("双工位堆垛机写入命令为空");
                 response.setMessage("双工位堆垛机写入命令为空");
                 return response;
             }
 
             String address = "DB100.0";
             if (command.getStation() == 1) {
                 //工位1
                 address = "DB100.0";
             }else {
                 //工位2
                 address = "DB100.18";
             }
 
             String confirmAddress = "DB100.16";
             if (command.getStation() == 1) {
                 //工位1
                 confirmAddress = "DB100.16";
             }else {
                 //工位2
                 confirmAddress = "DB100.34";
             }
 
             short[] array = new short[9];
             array[0] = command.getTaskNo().shortValue();
             array[1] = command.getTaskMode().shortValue();
             array[2] = command.getSourcePosX().shortValue();
             array[3] = command.getSourcePosY().shortValue();
             array[4] = command.getSourcePosZ().shortValue();
             array[5] = command.getDestinationPosX().shortValue();
             array[6] = command.getDestinationPosY().shortValue();
             array[7] = command.getDestinationPosZ().shortValue();
             array[8] = (short) 0;
 
             OperateResult result = siemensNet.Write(address, array);
             if (!result.IsSuccess) {
                 News.error("双工位堆垛机写入堆垛机plc数据失败 ===>> [id:{}]", command.getCrnNo());
                 response.setMessage("双工位堆垛机写入堆垛机plc数据失败");
                 return response;
             }
 
             int idx = 0;
             do {
                 OperateResultExOne<byte[]> resultRead = siemensNet.Read(address, (short) 18);
                 if (resultRead.IsSuccess) {
                    short taskNo = siemensNet.getByteTransform().TransInt16(resultRead.Content, 0);
                    short taskMode = siemensNet.getByteTransform().TransInt16(resultRead.Content, 2);
                    short sourcePosX = siemensNet.getByteTransform().TransInt16(resultRead.Content, 4);
                    short sourcePosY = siemensNet.getByteTransform().TransInt16(resultRead.Content, 6);
                    short sourcePosZ = siemensNet.getByteTransform().TransInt16(resultRead.Content, 8);
                    short destinationPosX = siemensNet.getByteTransform().TransInt16(resultRead.Content, 10);
                    short destinationPosY = siemensNet.getByteTransform().TransInt16(resultRead.Content, 12);
                    short destinationPosZ = siemensNet.getByteTransform().TransInt16(resultRead.Content, 14);
                    if (taskNo == 0 || taskMode == 0 || sourcePosX == 0 || sourcePosY == 0 || sourcePosZ == 0 || destinationPosX == 0 || destinationPosY == 0 || destinationPosZ == 0) {
                        result = siemensNet.Write(address, array);
                    } else {
                        break;
                    }
                 }
                 idx++;
                 Thread.sleep(500);
             } while (idx < 5);
 
             OperateResult confirmResult = siemensNet.Write(confirmAddress, (short) 1);
             if (!confirmResult.IsSuccess) {
                 News.error("双工位堆垛机写入确认数据失败 ===>> [id:{}]", command.getCrnNo());
                 response.setMessage("双工位堆垛机写入确认数据失败");
                 return response;
             }
 
             if (result != null && result.IsSuccess) {
                 News.info("双工位堆垛机命令下发[id:{}] >>>>> {}", command.getCrnNo(), JSON.toJSON(command));
                 OutputQueue.CRN.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), command.getCrnNo(), JSON.toJSON(command)));
                 response.setResult(true);
                 response.setMessage("命令下发成功");
             } else {
                 News.error("双工位堆垛机写入堆垛机plc数据失败 ===>> [id:{}]", command.getCrnNo());
                 OutputQueue.CRN.offer(MessageFormat.format("【{0}】写入堆垛机plc数据失败 ===>> [id:{1}]", DateUtils.convert(new Date()), command.getCrnNo()));
                 response.setResult(false);
                 response.setMessage("命令下发失败");
             }
 
             return response;
         } catch (Exception e) {
             e.printStackTrace();
         }
        return response;
    }
}