#
Junjie
2025-11-05 8b4f5b2b23023986db813242cd04f4650537decd
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
package com.zy.core.network.real;
 
import HslCommunication.Core.Types.OperateResult;
import HslCommunication.Core.Types.OperateResultExOne;
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.CrnCommand;
import com.zy.core.network.api.ZyCrnConnectApi;
import com.zy.core.network.entity.ZyCrnStatusEntity;
 
import java.text.MessageFormat;
import java.util.Date;
 
public class ZyCrnRealConnect implements ZyCrnConnectApi {
 
    private SiemensS7Net siemensNet;
    private DeviceConfig deviceConfig;
 
    public ZyCrnRealConnect(SiemensS7Net siemensS7Net, DeviceConfig deviceConfig) {
        this.siemensNet = siemensS7Net;
        this.deviceConfig = deviceConfig;
    }
 
    public boolean connect() {
        boolean connected = false;
        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("SiemensCrn"+" - 1"+" - 堆垛机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("SiemensCrn"+" - 2"+" - 堆垛机plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
        }
//        siemensNet.ConnectClose();
        return connected;
    }
 
    @Override
    public boolean disconnect() {
        siemensNet.ConnectClose();
        return true;
    }
 
    @Override
    public ZyCrnStatusEntity getStatus() {
        try {
            OperateResultExOne<byte[]> result = siemensNet.Read("DB101.0", (short) 56);
            if (result.IsSuccess) {
                ZyCrnStatusEntity crnStatus = new ZyCrnStatusEntity();
                crnStatus.setCrnNo(deviceConfig.getDeviceNo());
                crnStatus.setMode((int) siemensNet.getByteTransform().TransInt16(result.Content, 0));
                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.setLiftPos((int) siemensNet.getByteTransform().TransInt16(result.Content, 12));
                crnStatus.setWalkPos((int) siemensNet.getByteTransform().TransInt16(result.Content, 14));
                crnStatus.setLoaded((int) siemensNet.getByteTransform().TransInt16(result.Content, 16));
                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"+" - 4"+" - 读取堆垛机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"+" - 5"+" - 读取堆垛机plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}]", deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort());
        }
        return null;
    }
 
    @Override
    public CommandResponse sendCommand(CrnCommand command) {
        CommandResponse response = new CommandResponse(false);
        try {
            if (null == command) {
                News.error("堆垛机写入命令为空");
                response.setMessage("堆垛机写入命令为空");
                return response;
            }
 
            int writeAck = 0;
            boolean ackResult = false;
            do {
                OperateResult resultAck = siemensNet.Write("DB100.0", (short) 0);
                if (resultAck.IsSuccess) {
                    Thread.sleep(200);
                    OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (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("DB100.0", (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("DB100.0", array);
                        } else {
                            break;
                        }
                    } else {
                        short ackFinish = siemensNet.getByteTransform().TransInt16(resultRead.Content, 0);
                        if (ackFinish != command.getAckFinish()) {
                            result = siemensNet.Write("DB100.0", array);
                        } else {
                            break;
                        }
                    }
                }
                idx++;
                Thread.sleep(500L);
            } while (idx < 5);
 
            if (command.getAckFinish() == 0) {
                short commandFinish = 1;
                int i = 0;
                do {
                    OperateResultExOne<byte[]> resultRead = siemensNet.Read("DB100.0", (short) 4);
                    OperateResultExOne<byte[]> resultReadConfirm = siemensNet.Read("DB100.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("DB100.18", commandFinish);
                        }
                    }
                    i++;
                    Thread.sleep(500L);
                } while (i < 5);
            }
 
            if (result != null && result.IsSuccess) {
                News.info("SiemensCrn" + " - 7" + " - 堆垛机命令下发[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("SiemensCrn" + " - 8" + " - 写入堆垛机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(command),    // 系统状态
                    1,    // 下发状态{0:未下发,1:已下发}
                    JSON.toJSONString(response)    // 响应
            );
            bean.insert(basCrnpOpt);
        }
        return response;
    }
}