#
Junjie
2024-06-18 63bdaac47e9717cec820343874a782de1735d9d7
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
package com.zy.asrs.wcs.rcs.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.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.framework.common.Cools;
import com.zy.asrs.framework.common.DateUtils;
import com.zy.asrs.framework.common.SpringUtils;
import com.zy.asrs.wcs.core.entity.BasConveyor;
import com.zy.asrs.wcs.core.entity.BasConveyorSta;
import com.zy.asrs.wcs.core.service.BasConveyorService;
import com.zy.asrs.wcs.core.service.BasConveyorStaService;
import com.zy.asrs.wcs.core.utils.RedisUtil;
import com.zy.asrs.wcs.rcs.News;
import com.zy.asrs.wcs.rcs.cache.OutputQueue;
import com.zy.asrs.wcs.rcs.entity.Device;
import com.zy.asrs.wcs.rcs.model.protocol.StaProtocol;
import com.zy.asrs.wcs.rcs.thread.DevpThread;
import lombok.extern.slf4j.Slf4j;
 
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
@Slf4j
public class SuopaiSiemensDevpThread implements DevpThread, Runnable {
 
    private Device device;
    private RedisUtil redisUtil;
    private SiemensS7Net siemensS7Net;
 
    private Map<Integer, StaProtocol> station = new ConcurrentHashMap<>();
 
    public static ArrayList<BasConveyorSta> stationList = new ArrayList<>();
 
    /**
     * 条码数量
     */
    private int barcodeSize = 2;
 
    public SuopaiSiemensDevpThread(Device device, RedisUtil redisUtil) {
        this.device = device;
        this.redisUtil = redisUtil;
    }
 
    private ArrayList<BasConveyorSta> getStaNo() {
        try {
            if (stationList.isEmpty()) {
                BasConveyorService basConveyorService = SpringUtils.getBean(BasConveyorService.class);
                BasConveyorStaService basConveyorStaService = SpringUtils.getBean(BasConveyorStaService.class);
                BasConveyor basConveyor = basConveyorService.getOne(new LambdaQueryWrapper<BasConveyor>()
                        .eq(BasConveyor::getDeviceId, device.getId())
                        .eq(BasConveyor::getHostId, device.getHostId()));
                if(basConveyor != null) {
                    List<BasConveyorSta> stations = basConveyorStaService.list(new LambdaQueryWrapper<BasConveyorSta>()
                            .eq(BasConveyorSta::getConveyorId, basConveyor.getId())
                            .eq(BasConveyorSta::getHostId, device.getHostId()));
                    stationList.addAll(stations);
                }
            }
            return stationList;
        } catch (Exception e) {
            return stationList;
        }
    }
 
    @Override
    @SuppressWarnings("InfiniteLoopStatement")
    public void run() {
        News.info("{}号输送线线程启动", device.getDeviceNo());
        this.connect();
        while (true) {
            try {
                read();
                Thread.sleep(500);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    private void read() throws InterruptedException {
        ArrayList<BasConveyorSta> staNos = getStaNo();
        int staNoSize = staNos.size();
        OperateResultExOne<byte[]> result = siemensS7Net.Read("DB1000.100", (short) (staNoSize * 40));
        if (result.IsSuccess) {
            for (int i = 0; i < staNoSize; i++) {
                BasConveyorSta siteStation = staNos.get(i);
                int siteId = siteStation.getSiteNo();// 站点编号
                StaProtocol staProtocol = station.get(siteId);
                if (null == staProtocol) {
                    staProtocol = new StaProtocol();
                    staProtocol.setSiteId(siteId);
                    station.put(siteId, staProtocol);
                }
                staProtocol.setWorkNo((short) siemensS7Net.getByteTransform().TransInt32(result.Content, i * 40 + 2));     // 工作号
                staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result.Content, i * 40 + 4));   // 目标站
                staProtocol.setAutoing(siemensS7Net.getByteTransform().TransInt32(result.Content, i * 40 + 6) == 2);  // 自动
                staProtocol.setLoading(siemensS7Net.getByteTransform().TransInt32(result.Content, i * 40 + 6) == 2);  // 有物
//                staProtocol.setInEnable(status[2]); // 可入
//                staProtocol.setOutEnable(status[3]);// 可出
//                staProtocol.setEmptyMk(status[4]);  // 空板信号
//                staProtocol.setFullPlt(status[5]);  // 满托盘
//                staProtocol.setHigh(status[6]);     // 高库位
//                staProtocol.setLow(status[7]);      // 低库位
 
                if (!staProtocol.isPakMk() && !staProtocol.isLoading()) {
                    staProtocol.setPakMk(true);
                }
            }
        }
 
        if (!Cools.isEmpty(result) && result.IsSuccess) {
 
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), device.getId()));
 
            // 根据实时信息更新数据库
            try {
                List<BasConveyorSta> stations = new ArrayList<>();
                for (BasConveyorSta sta : getStaNo()) {
                    StaProtocol staProtocol = station.get(sta.getSiteNo());
                    BasConveyorSta sqlModel = staProtocol.toSqlModel(sta);
                    stations.add(sqlModel);
                }
 
                if (!stations.isEmpty()) {
                    BasConveyorStaService basConveyorStaService = SpringUtils.getBean(BasConveyorStaService.class);
                    if (null != basConveyorStaService && !basConveyorStaService.updateBatchById(stations)) {
                        throw new Exception("更新数据库数据失败");
                    }
                }
 
            } catch (Exception e) {
                e.printStackTrace();
                OutputQueue.DEVP.offer(MessageFormat.format("【{0}】更新数据库数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort(), device.getRack(), device.getSlot()));
                News.error("更新数据库数据失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", device.getId(), device.getIp(), device.getPort(), device.getRack(), device.getSlot());
            }
 
        } else {
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】读取输送线plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort(), device.getRack(), device.getSlot()));
//            log.error("读取输送线plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}] [rack:{}] [slot:{}]", device.getId(), device.getIp(), device.getPort(), device.getRack(), device.getSlot());
        }
    }
 
    @Override
    public boolean connect() {
        boolean result = false;
        siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, device.getIp());
        siemensS7Net.setRack(device.getRack().byteValue());
        siemensS7Net.setSlot(device.getSlot().byteValue());
        OperateResult connect = siemensS7Net.ConnectServer();
        if(connect.IsSuccess){
            result = true;
            OutputQueue.DEVP.offer(MessageFormat.format( "【{0}】输送线plc连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort(), device.getRack(), device.getSlot()));
            News.info("输送线plc连接成功 ===>> [id:{}] [ip:{}] [port:{}]", device.getId(), device.getIp(), device.getPort());
        } else {
            OutputQueue.DEVP.offer(MessageFormat.format( "【{0}】输送线plc连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}]  [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), device.getId(), device.getIp(), device.getPort(), device.getRack(), device.getSlot()));
            News.error("输送线plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", device.getId(), device.getIp(), device.getPort());
        }
        // siemensS7Net.ConnectClose();
        return result;
    }
 
    @Override
    public void close() {
 
    }
 
    @Override
    public boolean writeWorkNo(int siteId, short workNo) {
        int index = findStaNosIndex(siteId);
 
        OperateResult write = siemensS7Net.Write("DB100." + index*6, workNo);    // 工作号
 
        if (!write.IsSuccess) {
            StaProtocol staProtocol = station.get(siteId);
            if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() ==0) {
                staProtocol.setPakMk(true);
            }
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线站点数据失败。输送线plc编号={1},站点数据={2}", device.getId(), JSON.toJSON(staProtocol)));
            log.error("写入输送线站点数据失败。输送线plc编号={},站点数据={}", device.getId(), JSON.toJSON(staProtocol));
        } else {
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】 输送线命令下发 [id:{1}] >>>>> {2}", DateUtils.convert(new Date()), device.getId(), JSON.toJSON(workNo)));
            log.info("输送线命令下发 [id:{}] >>>>> 命令下发: {}",  device.getId(), JSON.toJSON(workNo));
            return true;
        }
        return false;
    }
 
    @Override
    public boolean writeStaNo(int siteId,short staNo) {
        int index = findStaNosIndex(siteId);
 
        OperateResult write = siemensS7Net.Write("DB100." + (index*6+4), staNo);    // 目标站
 
        if (!write.IsSuccess) {
            StaProtocol staProtocol = station.get(siteId);
            if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() ==0) {
                staProtocol.setPakMk(true);
            }
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线站点数据失败。输送线plc编号={1},站点数据={2}", device.getId(), JSON.toJSON(staProtocol)));
            log.error("写入输送线站点数据失败。输送线plc编号={},站点数据={}", device.getId(), JSON.toJSON(staProtocol));
        } else {
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】 输送线命令下发 [id:{1}] >>>>> {2}", DateUtils.convert(new Date()), device.getId(), JSON.toJSON(staNo)));
            log.info("输送线命令下发 [id:{}] >>>>> 命令下发: {}",  device.getId(), JSON.toJSON(staNo));
            return true;
        }
        return false;
    }
 
    @Override
    public boolean writeWorkSta(int siteId, short staNo, short workNo) {
        int index = findStaNosIndex(siteId);
 
        OperateResult write1 = siemensS7Net.Write("DB100." + index*6, workNo);    // 工作号
        OperateResult write2 = siemensS7Net.Write("DB100." + (index*6+4), staNo);    // 目标站
 
        if (!(write1.IsSuccess && write2.IsSuccess)) {
            StaProtocol staProtocol = station.get(siteId);
            if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() ==0) {
                staProtocol.setPakMk(true);
            }
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线站点数据失败。输送线plc编号={1},站点数据={2}", device.getId(), JSON.toJSON(staProtocol)));
            log.error("写入输送线站点数据失败。输送线plc编号={},站点数据={}", device.getId(), JSON.toJSON(staProtocol));
        } else {
            OutputQueue.DEVP.offer(MessageFormat.format("【{0}】 输送线命令下发 [id:{1}] >>>>> {2}", DateUtils.convert(new Date()), device.getId(), JSON.toJSON(staNo)));
            log.info("输送线命令下发 [id:{}] >>>>> 命令下发: {}",  device.getId(), JSON.toJSON(staNo));
            return true;
        }
        return false;
    }
 
    @Override
    public Map<Integer, StaProtocol> getStation() {
        return this.station;
    }
 
    private int findStaNosIndex(int siteId) {
        ArrayList<BasConveyorSta> staNos = getStaNo();
 
        int index = -1;
        for (int i = 0; i < staNos.size(); i++) {
            BasConveyorSta sta = staNos.get(i);
            if (sta.getSiteNo() == siteId) {
                index = i;
                break;
            }
        }
        return index;
    }
 
    /**
     * 设置入库标记
     */
    @Override
    public void setPakMk(Integer siteId, boolean pakMk) {
        StaProtocol staProtocol = station.get(siteId);
        if (null != staProtocol) {
            staProtocol.setPakMk(pakMk);
        }
    }
}