#
Junjie
3 天以前 6d99081f3f6e0363b0bd28053e545a8e4589b979
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
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.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.zy.asrs.entity.BasDevp;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.asrs.service.BasDevpService;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
import com.zy.core.cache.OutputQueue;
import com.zy.core.enums.StationCommandType;
import com.zy.core.model.CommandResponse;
import com.zy.core.model.StationObjModel;
import com.zy.core.model.command.StationCommand;
import com.zy.core.network.api.ZyStationConnectApi;
import com.zy.core.network.entity.ZyStationStatusEntity;
import lombok.extern.slf4j.Slf4j;
 
import java.text.MessageFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
 
/**
 * 输送站真实连接(PLC)
 */
@Slf4j
public class ZyStationV3RealConnect implements ZyStationConnectApi {
 
    private List<ZyStationStatusEntity> statusList;
    private List<StationObjModel> barcodeOriginList;
    private SiemensS7Net siemensNet;
    private DeviceConfig deviceConfig;
    private RedisUtil redisUtil;
    private final static int taskAddressLength = 48;
    private final static int taskAddressLimit = 50;
 
    public ZyStationV3RealConnect(DeviceConfig deviceConfig, RedisUtil redisUtil) {
        this.deviceConfig = deviceConfig;
        this.redisUtil = redisUtil;
    }
 
    @Override
    public boolean connect() {
        boolean connected = false;
        siemensNet = new SiemensS7Net(SiemensPLCS.S1500, deviceConfig.getIp());
        OperateResult connect = siemensNet.ConnectServer();
        if (connect.IsSuccess) {
            connected = true;
            OutputQueue.DEVP.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.DEVP.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 List<ZyStationStatusEntity> getStatus(Integer deviceNo) {
        if (statusList == null) {
            BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
            if (basDevpService == null) {
                return Collections.emptyList();
            }
 
            BasDevp basDevp = basDevpService
                    .selectOne(new EntityWrapper<BasDevp>().eq("devp_no", deviceConfig.getDeviceNo()));
            if (basDevp == null) {
                return Collections.emptyList();
            }
 
            statusList = JSONObject.parseArray(basDevp.getStationList(), ZyStationStatusEntity.class);
            if (statusList != null) {
                statusList.sort(Comparator.comparing(ZyStationStatusEntity::getStationId));
            }
            barcodeOriginList = basDevp.getBarcodeStationList$();
        }
 
        if (siemensNet == null) {
            return statusList;
        }
 
        OperateResultExOne<byte[]> result = siemensNet.Read("DB100.0", (short) (statusList.size() * 8));
        if (result.IsSuccess) {
            for (int i = 0; i < statusList.size(); i++) {
                ZyStationStatusEntity statusEntity = statusList.get(i); // 站点编号
                statusEntity.setTaskNo(siemensNet.getByteTransform().TransInt32(result.Content, i * 8)); // 工作号
                statusEntity.setTargetStaNo((int) siemensNet.getByteTransform().TransInt16(result.Content, i * 8 + 4)); // 目标站
 
                boolean[] status = siemensNet.getByteTransform().TransBool(result.Content, i * 8 + 6, 1);
                statusEntity.setAutoing(status[0]); // 自动
                statusEntity.setLoading(status[1]); // 有物
                statusEntity.setInEnable(status[2]); // 可入
                statusEntity.setOutEnable(status[3]);// 可出
                statusEntity.setEmptyMk(status[4]); // 空托盘
                statusEntity.setFullPlt(status[5]); // 满托盘
 
                boolean[] status2 = siemensNet.getByteTransform().TransBool(result.Content, i * 8 + 7, 1);
                statusEntity.setRunBlock(status2[1]);//重新规划路线
                statusEntity.setEnableIn(status2[3]);//启动入库
 
                Integer palletHeight = null;
                if (status[7]) {
                    palletHeight = 1;//低
                }
                if (status2[0]) {
                    palletHeight = 2;//中
                }
                if (status[6]) {
                    palletHeight = 3;//高
                }
                statusEntity.setPalletHeight(palletHeight);//高低信号
 
                statusEntity.setError(0);//默认无报警
            }
        }
 
        // 条码扫描器
        OperateResultExOne<byte[]> result2 = siemensNet.Read("DB101.0", (short) (barcodeOriginList.size() * 16));
        if (result2.IsSuccess) {
            for (int i = 0; i < barcodeOriginList.size(); i++) {
                ZyStationStatusEntity barcodeEntity = findStatusEntityByBarcodeIdx(i + 1);
                if (barcodeEntity == null) {
                    continue;
                }
                String barcode = siemensNet.getByteTransform().TransString(result2.Content, i * 16 + 2, 14, "UTF-8");
                barcode = barcode.trim();
                barcodeEntity.setBarcode(barcode);
            }
        }
 
        // 称重
        OperateResultExOne<byte[]> result3 = siemensNet.Read("DB102.0", (short) (barcodeOriginList.size() * 4));
        if (result3.IsSuccess) {
            for (int i = 0; i < barcodeOriginList.size(); i++) {
                ZyStationStatusEntity barcodeEntity = findStatusEntityByBarcodeIdx(i + 1);
                if (barcodeEntity == null) {
                    continue;
                }
                double weight = (double) siemensNet.getByteTransform().TransSingle(result3.Content, i * 4);
                barcodeEntity.setWeight(weight);
            }
        }
 
        // 报警信息
        OperateResultExOne<byte[]> result4 = siemensNet.Read("DB103.0", (short) (barcodeOriginList.size() * 2));
        if (result4.IsSuccess) {
            for (int i = 0; i < barcodeOriginList.size(); i++) {
                ZyStationStatusEntity barcodeEntity = findStatusEntityByBarcodeIdx(i + 1);
                if (barcodeEntity == null) {
                    continue;
                }
 
                StringBuilder sb = new StringBuilder();
                boolean[] status1 = siemensNet.getByteTransform().TransBool(result4.Content, i * 2, 1);
                boolean[] status2 = siemensNet.getByteTransform().TransBool(result4.Content, i * 2 + 1, 1);
 
                if(status1[0]){
                    sb.append("左超宽报警;");
                }
                if(status1[1]) {
                    sb.append("右超宽报警;");
                }
                if(status1[2]) {
                    sb.append("前超长报警;");
                }
                if(status1[3]) {
                    sb.append("后超长报警;");
                }
                if(status1[4]) {
                    sb.append("超高报警;");
                }
                if(status1[5]) {
                    sb.append("有货报警,空托入库时检测托盘上有无货物;");
                }
                if(status1[6]) {
                    sb.append("重量异常报警;");
                }
                if(status1[7]) {
                    sb.append("扫码异常;");
                }
 
                if(sb.length() > 0) {
                    barcodeEntity.setError(1);
                }else {
                    barcodeEntity.setError(0);
                }
                barcodeEntity.setErrorMsg(sb.toString());
            }
        }
 
        return statusList;
    }
 
    @Override
    public CommandResponse sendCommand(Integer deviceNo, StationCommand command) {
        CommandResponse commandResponse = new CommandResponse(false);
        if (null == command) {
            commandResponse.setMessage("命令为空");
            return commandResponse;
        }
 
        int maxTryCount = 999;
        int tryCount = 0;
        if (command.getCommandType().equals(StationCommandType.MOVE)) {
            int enableCommandIdx = -1;
            while (true) {
                enableCommandIdx = getEnableCommandIdx(command.getTaskNo());
                if(enableCommandIdx == -1) {
                    try {
                        Thread.sleep(300);
                    }catch (Exception e) {}
                }else {
                    break;
                }
                tryCount++;
                if (tryCount > maxTryCount) {
                    commandResponse.setMessage("命令下发超时,无法找到可用下发区域");
                    return commandResponse;
                }
            }
 
            List<Integer> pathList = command.getNavigatePath();
 
            short[] data = new short[21];
            data[0] = command.getTargetStaNo().shortValue();
 
            int dataIdx = 1;
            for (Integer path : pathList) {
                data[dataIdx++] = path.shortValue();
            }
 
            OperateResult writeTask = siemensNet.Write("DB23." + (enableCommandIdx * taskAddressLength), command.getTaskNo());
            try {
                Thread.sleep(50);
            }catch (Exception e) {}
 
            OperateResult writeData = siemensNet.Write("DB23." + (enableCommandIdx * taskAddressLength + 4), data);
            try {
                Thread.sleep(200);
            }catch (Exception e) {}
            OperateResult writeConfirm = siemensNet.Write("DB23." + (enableCommandIdx * taskAddressLength + 46), (short) 1);
 
            if(writeTask.IsSuccess &&  writeData.IsSuccess &&  writeConfirm.IsSuccess) {
                log.error("写入输送线命令成功。任务号={},站点数据={}", command.getTaskNo(), JSON.toJSON(command));
                commandResponse.setResult(true);
            }else {
                log.error("写入输送线命令失败。站点编号={},站点数据={}", command.getTaskNo(), JSON.toJSON(command));
                commandResponse.setResult(false);
            }
        } else if (command.getCommandType().equals(StationCommandType.WRITE_INFO)) {
            int enableCommandIdx = -1;
            while (true) {
                enableCommandIdx = getEnableCommandIdx(command.getTaskNo());
                if(enableCommandIdx == -1) {
                    try {
                        Thread.sleep(300);
                    }catch (Exception e) {}
                }else {
                    break;
                }
                tryCount++;
                if (tryCount > maxTryCount) {
                    commandResponse.setMessage("命令下发超时,无法找到可用下发区域");
                    return commandResponse;
                }
            }
 
            short[] data = new short[21];
            data[0] = command.getTargetStaNo().shortValue();
            data[1] = command.getTargetStaNo().shortValue();
 
            OperateResult writeTask = siemensNet.Write("DB23." + (enableCommandIdx * taskAddressLength), command.getTaskNo());
            try {
                Thread.sleep(50);
            }catch (Exception e) {}
 
            OperateResult writeData = siemensNet.Write("DB23." + (enableCommandIdx * taskAddressLength + 4), data);
            try {
                Thread.sleep(200);
            }catch (Exception e) {}
            OperateResult writeConfirm = siemensNet.Write("DB23." + (enableCommandIdx * taskAddressLength + 46), (short) 1);
 
            if(writeTask.IsSuccess &&  writeData.IsSuccess &&  writeConfirm.IsSuccess) {
                log.error("写入输送线命令成功。任务号={},站点数据={}", command.getTaskNo(), JSON.toJSON(command));
                commandResponse.setResult(true);
            }else {
                log.error("写入输送线命令失败。站点编号={},站点数据={}", command.getTaskNo(), JSON.toJSON(command));
                commandResponse.setResult(false);
            }
        }
        return commandResponse;
    }
 
    @Override
    public CommandResponse sendOriginCommand(String address, short[] data) {
        CommandResponse commandResponse = new CommandResponse(false);
        if (null == data || data.length == 0) {
            commandResponse.setMessage("数据为空");
            return commandResponse;
        }
 
        OperateResult write = siemensNet.Write(address, data);
        if (write.IsSuccess) {
            log.info("写入原始命令成功。地址={},数据={}", address, JSON.toJSON(data));
            commandResponse.setResult(true);
        } else {
            log.error("写入原始命令失败。地址={},数据={}", address, JSON.toJSON(data));
            commandResponse.setResult(false);
        }
        return commandResponse;
    }
 
    @Override
    public byte[] readOriginCommand(String address, int length) {
        OperateResultExOne<byte[]> result = siemensNet.Read(address, (short) length);
        if (result.IsSuccess) {
            return result.Content;
        }
        return new byte[0];
    }
 
    private ZyStationStatusEntity findStatusEntityByBarcodeIdx(Integer barcodeIdx) {
        Integer stationId = null;
        for (StationObjModel stationObjModel : barcodeOriginList) {
            if (stationObjModel.getBarcodeIdx().equals(barcodeIdx)) {
                stationId = stationObjModel.getStationId();
                break;
            }
        }
 
        for (ZyStationStatusEntity zyStationStatusEntity : statusList) {
            if(zyStationStatusEntity.getStationId().equals(stationId)) {
                return zyStationStatusEntity;
            }
        }
        return null;
    }
 
    private int getEnableCommandIdx(int taskNo) {
        int useIdx = -1;
        for (int i = 0; i < taskAddressLimit; i++) {
            OperateResultExOne<byte[]> resultTask = siemensNet.Read("DB23." + (i * taskAddressLength + 0), (short) 4);
            OperateResultExOne<byte[]> resultStatus = siemensNet.Read("DB23." + (i * taskAddressLength + 46), (short) 2);
            if(!resultTask.IsSuccess || !resultStatus.IsSuccess) {
                continue;
            }
 
            int commandAreaTaskNo = siemensNet.getByteTransform().TransInt32(resultTask.Content, 0);
            if(commandAreaTaskNo == taskNo) {
                return -1;
            }
 
            int taskStatus = siemensNet.getByteTransform().TransInt16(resultStatus.Content, 0);
            if (taskStatus == 1) {
                continue;
            }
 
            useIdx = i;
            break;
        }
        return  useIdx;
    }
 
}