#
Junjie
4 天以前 96cfc17f191fee0a05070ec8e7bb6fb099f0231c
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
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.DeviceConfig;
import com.zy.asrs.service.BasCrnpOptService;
import com.zy.asrs.utils.Utils;
import com.zy.core.News;
import com.zy.core.cache.OutputQueue;
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("SiemensDualCrn 双工位堆垛机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("SiemensDualCrn 双工位堆垛机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) 66);
            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.setWalkPos((int) siemensNet.getByteTransform().TransInt16(result.Content, 26));
 
                //工位2
                crnStatus.setTaskNoTwo((int) siemensNet.getByteTransform().TransInt16(result.Content, 14));
                crnStatus.setStatusTwo((int) siemensNet.getByteTransform().TransInt16(result.Content, 16));
                crnStatus.setBayTwo((int) siemensNet.getByteTransform().TransInt16(result.Content, 18));
                crnStatus.setLevelTwo((int) siemensNet.getByteTransform().TransInt16(result.Content, 20));
                crnStatus.setForkPosTwo((int) siemensNet.getByteTransform().TransInt16(result.Content, 22));
                crnStatus.setLoadedTwo((int) siemensNet.getByteTransform().TransInt16(result.Content, 24));
                crnStatus.setWalkPosTwo((int) siemensNet.getByteTransform().TransInt16(result.Content, 28));
 
                crnStatus.setAlarm((int) siemensNet.getByteTransform().TransInt16(result.Content, 18));
                crnStatus.setTemp1((int) siemensNet.getByteTransform().TransInt16(result.Content, 20));
                crnStatus.setTemp2((int) siemensNet.getByteTransform().TransInt16(result.Content, 22));
                crnStatus.setTemp3((int) siemensNet.getByteTransform().TransInt16(result.Content, 24));
                crnStatus.setTemp4((int) siemensNet.getByteTransform().TransInt16(result.Content, 26));
                crnStatus.setXSpeed((int) siemensNet.getByteTransform().TransInt16(result.Content, 28));
                crnStatus.setYSpeed((int) siemensNet.getByteTransform().TransInt16(result.Content, 32));
                crnStatus.setZSpeed((int) siemensNet.getByteTransform().TransInt16(result.Content, 36));
                crnStatus.setXDistance((int) siemensNet.getByteTransform().TransInt16(result.Content, 40));
                crnStatus.setYDistance((int) siemensNet.getByteTransform().TransInt16(result.Content, 44));
                crnStatus.setXDuration((int) siemensNet.getByteTransform().TransInt16(result.Content, 48));
                crnStatus.setYDuration((int) siemensNet.getByteTransform().TransInt16(result.Content, 52));
 
                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("SiemensCrn 读取双工位堆垛机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("SiemensCrn 读取双工位堆垛机plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
        }
        return null;
    }
 
    @Override
    public CommandResponse sendCommand(DualCrnCommand command) {
        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.20";
            }
 
            int writeAck = 0;
            boolean ackResult = false;
            do {
                OperateResult resultAck = siemensNet.Write(address, (short) 0);
                if (resultAck.IsSuccess) {
                    Thread.sleep(200);
                    OperateResultExOne<byte[]> resultRead = siemensNet.Read(address, (short) 2);
                    short ack = siemensNet.getByteTransform().TransInt16(resultRead.Content, 0);
                    if (ack != 0) {
                        writeAck++;
                    } else {
                        News.info("双工位堆垛机命令下发[id:{}] >>>>> {}", command.getCrnNo(), "ack复位完成");
                        ackResult = true;
                        break;
                    }
                }
            } while (writeAck < 5);
 
            if (!ackResult) {
                response.setMessage("双工位堆垛机命令下发[id:{}] >>>>> {}" + command.getCrnNo() + "ack复位失败");
                return response;
            }
 
            short[] array = new short[10];
            array[0] = command.getAckFinish();
            array[1] = command.getTaskNo();
            array[2] = command.getTaskMode();
            array[3] = command.getSourcePosX();
            array[4] = command.getSourcePosY();
            array[5] = command.getSourcePosZ();
            array[6] = command.getDestinationPosX();
            array[7] = command.getDestinationPosY();
            array[8] = command.getDestinationPosZ();
            array[9] = command.getCommand();
 
            OperateResult result = null;
            int idx = 0;
            do {
                OperateResultExOne<byte[]> resultRead = siemensNet.Read(address, (short) 20);
                if (resultRead.IsSuccess) {
                    if (command.getAckFinish() == 0) {
                        short taskNo = siemensNet.getByteTransform().TransInt16(resultRead.Content, 2);
                        short taskMode = siemensNet.getByteTransform().TransInt16(resultRead.Content, 4);
                        short sourcePosX = siemensNet.getByteTransform().TransInt16(resultRead.Content, 6);
                        short sourcePosY = siemensNet.getByteTransform().TransInt16(resultRead.Content, 8);
                        short sourcePosZ = siemensNet.getByteTransform().TransInt16(resultRead.Content, 10);
                        short destinationPosX = siemensNet.getByteTransform().TransInt16(resultRead.Content, 12);
                        short destinationPosY = siemensNet.getByteTransform().TransInt16(resultRead.Content, 14);
                        short destinationPosZ = siemensNet.getByteTransform().TransInt16(resultRead.Content, 16);
                        if (taskNo == 0 || taskMode == 0 || sourcePosX == 0 || sourcePosY == 0 || sourcePosZ == 0 || destinationPosX == 0 || destinationPosY == 0 || destinationPosZ == 0) {
                            result = siemensNet.Write(address, array);
                        } else {
                            break;
                        }
                    } else {
                        short ackFinish = siemensNet.getByteTransform().TransInt16(resultRead.Content, 0);
                        if (ackFinish != command.getAckFinish()) {
                            result = siemensNet.Write(address, array);
                        } else {
                            break;
                        }
                    }
                }
                idx++;
                Thread.sleep(500);
            } while (idx < 5);
 
            if (command.getAckFinish() == 0) {
                short commandFinish = 1;
                int i = 0;
                do {
                    OperateResultExOne<byte[]> resultRead = siemensNet.Read(address, (short) 4);
                    OperateResultExOne<byte[]> resultReadConfirm = siemensNet.Read(address + 18, (short) 2);
                    if (resultRead.IsSuccess && resultReadConfirm.IsSuccess) {
                        short taskNo = siemensNet.getByteTransform().TransInt16(resultRead.Content, 2);
                        short confirm = siemensNet.getByteTransform().TransInt16(resultReadConfirm.Content, 0);
                        if (taskNo != 0 && confirm == 0) {
                            result = siemensNet.Write(address + 18, commandFinish);
                        }
                    }
                    i++;
                    Thread.sleep(500);
                } while (i < 5);
            }
 
            if (result != null && result.IsSuccess) {
                News.info("SiemensDualCrn 双工位堆垛机命令下发[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("SiemensDualCrn 双工位堆垛机写入堆垛机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();
        }finally {
            String sourceLocNo = Utils.getLocNo(command.getSourcePosX(), command.getSourcePosY(), command.getSourcePosZ());
            String targetLocNo = Utils.getLocNo(command.getDestinationPosX(), command.getDestinationPosY(), command.getDestinationPosZ());
 
            // 日志记录
            BasCrnpOptService bean = SpringUtils.getBean(BasCrnpOptService.class);
            BasCrnpOpt basCrnpOpt = new BasCrnpOpt(
                    command.getTaskNo().intValue(),    // 任务号
                    command.getCrnNo(),    // 堆垛机[非空]
                    new Date(),    // 下发时间
                    String.valueOf(command.getTaskMode()),    // 模式
                    sourceLocNo, //源库位
                    targetLocNo, //目标库位
                    null,    // 修改时间
                    null,    // 修改人员
                    null,    // 备注
                    JSON.toJSONString(command),    // 指令
                    JSON.toJSONString(getStatus()),    // 系统状态
                    1,    // 下发状态{0:未下发,1:已下发}
                    JSON.toJSONString(response)    // 响应
            );
            bean.insert(basCrnpOpt);
        }
        return response;
    }
}