春风项目四线(合箱线、总装线)
wujian
2024-04-24 3946f893f84814185dfb97e89bfdb10b82eea906
提交 | 用户 | 时间
3e03af 1 package com.jcdm.main.plcserver.sub;
C 2
3
9e6d86 4 import cn.hutool.core.collection.CollUtil;
W 5 import cn.hutool.core.util.ObjectUtil;
6 import cn.hutool.core.util.StrUtil;
7 import com.jcdm.main.bs.domain.BsTechnologyRouteChildInfo;
8 import com.jcdm.main.bs.orderScheduling.domain.BsOrderScheduling;
9 import com.jcdm.main.bs.orderScheduling.service.IBsOrderSchedulingService;
10 import com.jcdm.main.bs.technologyRouteChild.service.IBsTechnologyRouteChildInfoService;
3e03af 11 import com.jcdm.main.da.collectionParamConf.domain.DaCollectionParamConf;
C 12 import com.jcdm.main.da.collectionParamConf.service.IDaCollectionParamConfService;
059083 13 import com.jcdm.main.da.opcuaconfig.domain.DaOpcuaConfig;
3e03af 14 import com.jcdm.main.da.paramCollection.domain.DaParamCollection;
C 15 import com.jcdm.main.da.paramCollection.service.IDaParamCollectionService;
059083 16 import com.jcdm.main.da.passingStationCollection.domain.DaPassingStationCollection;
W 17 import com.jcdm.main.da.passingStationCollection.service.impl.DaPassingStationCollectionServiceImpl;
3e03af 18 import com.jcdm.main.plcserver.conf.OPCElement;
9e6d86 19 import com.jcdm.main.rm.repairRecord.domain.RmRepairRecord;
W 20 import com.jcdm.main.rm.repairRecord.service.IRmRepairRecordService;
3e03af 21 import com.kangaroohy.milo.model.ReadWriteEntity;
C 22 import com.kangaroohy.milo.runner.subscription.SubscriptionCallback;
23 import com.kangaroohy.milo.service.MiloService;
059083 24 import lombok.extern.slf4j.Slf4j;
3e03af 25 import org.springframework.stereotype.Component;
C 26
9f6314 27 import java.math.BigDecimal;
9e6d86 28 import java.time.LocalDateTime;
3e03af 29 import java.util.ArrayList;
C 30 import java.util.Date;
059083 31 import java.util.HashMap;
3e03af 32 import java.util.List;
C 33 import java.util.stream.Collectors;
34
35
059083 36 @Slf4j
3e03af 37 @Component
C 38 public class OPCUaSubscription implements SubscriptionCallback {
39
40     public static MiloService miloService;
41
42     public IDaCollectionParamConfService collectionParamConfService;
43
44     public IDaParamCollectionService daParamCollectionService;
45
059083 46     public DaPassingStationCollectionServiceImpl passingStationCollectionServiceImpl;
W 47
48     public List<DaOpcuaConfig> lists;
49
50     public static final HashMap<String,Integer> map = new HashMap<>();
51
9e6d86 52     public IBsOrderSchedulingService bsOrderSchedulingService;
W 53
54     public IRmRepairRecordService rmRepairRecordService;
55
56     public IBsTechnologyRouteChildInfoService bsTechnologyRouteChildInfoService;
57
3e03af 58     public OPCUaSubscription(MiloService miloService,
C 59                              IDaCollectionParamConfService collectionParamConfService,
059083 60                              IDaParamCollectionService daParamCollectionService,
W 61                              DaPassingStationCollectionServiceImpl passingStationCollectionServiceImpl,
9e6d86 62                              IBsOrderSchedulingService bsOrderSchedulingService,
W 63                              List<DaOpcuaConfig> lists,
64                              IRmRepairRecordService rmRepairRecordService,
65                              IBsTechnologyRouteChildInfoService bsTechnologyRouteChildInfoService) {
3e03af 66         OPCUaSubscription.miloService = miloService;
C 67         this.collectionParamConfService = collectionParamConfService;
68         this.daParamCollectionService = daParamCollectionService;
059083 69         this.passingStationCollectionServiceImpl = passingStationCollectionServiceImpl;
9e6d86 70         this.bsOrderSchedulingService = bsOrderSchedulingService;
059083 71         this.lists = lists;
9e6d86 72         this.rmRepairRecordService = rmRepairRecordService;
W 73         this.bsTechnologyRouteChildInfoService = bsTechnologyRouteChildInfoService;
059083 74
3e03af 75
C 76     }
77
78
79     @Override
80     public void onSubscribe(String identifier, Object value) {
81         String ecpStr = "";//异常记录标记
82         try {
9e6d86 83             if(null != value && "1".equals(value.toString())) {
059083 84                 //1、检索SN号
W 85                 //2、过站参数采集
86                 //3、扫码枪数据回传
87                 List<String> collect1 = lists.stream().filter(x -> OPCElement.SN_CHECK.equals(x.getrFunction()))
88                         .map(DaOpcuaConfig::getNode).collect(Collectors.toList());
89                 List<String> collect2 = lists.stream().filter(x -> OPCElement.SAVE_DATA.equals(x.getrFunction()))
90                         .map(DaOpcuaConfig::getNode).collect(Collectors.toList());
91                 if (collect1.contains(identifier)){
92                     //sn
93                     this.SNRetrieval(identifier,value.toString());
94                     if (identifier.equals(OPCElement.OP120_ZZ_CODE_CHECK) && "1".equals(value.toString())){
95                         //总装上线扫码传输数据
96                         log.info("-------监听到,{}的扫码枪扫码的CODE_CHECK的信号",identifier);
9e6d86 97                         scannerGunMessage();
W 98                     }
99                 }
100                 if (collect2.contains(identifier)){
101                     if ("1".equals(value.toString())){
102                         //save
103                         this.SaveData(identifier);
104                         //返回plc保存成功
105                         String[] parts = identifier.split("[.]");
106                         if (parts.length==3){
107                             if ("SaveRequest".equals(parts[2])){
108                                 ReadWriteEntity entity = new ReadWriteEntity(parts[0] + "." + parts[1] + ".SaveFeed", 1);
109                                 log.info("-------监听到,{}的saveRequest的信号",identifier);
110                                 Integer i = map.getOrDefault(identifier + "的saveRequest的信号",0);
111                                 if (0==i){
112                                     map.put(identifier + "的saveRequest的信号",i+1);
113                                 }
f6b9ee 114                                 miloService.writeToOpcByte(entity);
9e6d86 115                             }else if ("SaveRequestLast".equals(parts[2])){
W 116                                 ReadWriteEntity entity = new ReadWriteEntity(parts[0] + "." + parts[1] + ".SaveFeedLast", 1);
117                                 log.info("-------监听到,{}的SaveRequestLast的信号",identifier);
118                                 Integer i = map.getOrDefault(identifier + "的SaveRequestLast的信号",0);
119                                 if (0==i){
120                                     map.put(identifier + "的SaveRequestLast的信号",i+1);
121                                 }
f6b9ee 122                                 miloService.writeToOpcByte(entity);
9e6d86 123                             }
059083 124                         }
W 125                     }
126                 }
127
3e03af 128             }
C 129
130
131
132         } catch (Exception e) {
9e6d86 133             System.out.println(e.getMessage());
3e03af 134         } finally {
C 135             if (!"".equals(ecpStr)) {
136                 System.out.println(ecpStr + "\r\n");
137
138             }
9e6d86 139         }
W 140     }
141
142     private void scannerGunMessage() throws Exception {
143         String[] parts = OPCElement.OP120_ZZ_CODE_CHECK.split("[.]");
144         Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code1").getValue();
3946f8 145         if (null == SNCodeObject){
W 146             SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code").getValue();
147         }
9e6d86 148         if (null != SNCodeObject){
W 149             String SNCode = SNCodeObject.toString();
150             passingStationCollectionServiceImpl.sendMessage(SNCode);
3e03af 151         }
C 152     }
153
059083 154     public void SNRetrieval(String Node, String value) throws Exception {
W 155         String[] parts = Node.split("[.]");
156         if(value.equals("1")) {
157             //SN号检索
158             Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code").getValue();
159             if(null != SNCodeObject) {
160                 String SNCode=SNCodeObject.toString();
3946f8 161                 String a=passingStationCollectionServiceImpl.SelectSN(SNCode,parts[1]);
W 162                 System.out.println("codeCheckFeed:"+a);
163 //                String a="1";
059083 164                 // 1:OK可生产 2:NG不可生产 3:NG可返工 4:PC检索失败(无记录)5:PC检索失败(软件)
9e6d86 165                 if (StrUtil.isNotBlank(a)){
W 166                     int input = Integer.parseInt(a);
167                     ReadWriteEntity entity = new ReadWriteEntity(parts[0]+"."+parts[1]+".CodeCheckFeed", input);
168                     log.info("-------监听到,{}的CodeCheck的信号",Node);
169                     //首站传输订单号
170                     if (OPCElement.OP050_HX_CODE_CHECK.equals(Node)){
171                         //查询订单号
172                         BsOrderScheduling bsOrderScheduling = new BsOrderScheduling();
173                         bsOrderScheduling.setEngineNo(SNCode);
174                         List<BsOrderScheduling> tempList = bsOrderSchedulingService.selectBsOrderSchedulingList(bsOrderScheduling);
175                         String orderNum = "";
176                         if (CollUtil.isNotEmpty(tempList)){
177                             BsOrderScheduling bsOrderScheduling1 = tempList.get(0);
178                             orderNum = bsOrderScheduling1.getOrderNo();
179                         }
180
181                         ReadWriteEntity entity2 = new ReadWriteEntity(parts[0]+"."+parts[1]+".OrderNumber", orderNum);
2b59de 182                         miloService.writeToOpcUa(entity2);
9e6d86 183                     }
W 184                     //如果是返修工位需要传输返修工位号
3946f8 185                     if (OPCElement.OP465_ZZ_CODE_CHECK.equals(Node) || OPCElement.OP355_ZZ_CODE_CHECK.equals(Node)
W 186                     || OPCElement.OP695_ZZ_CODE_CHECK.equals(Node) || OPCElement.OP755_ZZ_CODE_CHECK.equals(Node)){
187                         rework(SNCode,Node);
9e6d86 188                     }
3946f8 189                     System.out.println(entity);
f6b9ee 190                 miloService.writeToOpcByte(entity);
9e6d86 191                 }
W 192
059083 193             }
9e6d86 194         }
W 195     }
196
3946f8 197     private void rework(String SNCode,String Node)  throws Exception{
9e6d86 198         BsOrderScheduling bsOrderScheduling = new BsOrderScheduling();
W 199         bsOrderScheduling.setEngineNo(SNCode);
200         List<String> allProcessCoed = new ArrayList<>();
201         List<String> collect = bsOrderSchedulingService.selectBsOrderSchedulingList(bsOrderScheduling).stream().map(BsOrderScheduling::getModel).collect(Collectors.toList());
202         if (CollUtil.isNotEmpty(collect)){
203             String s = collect.get(0);
204             //查找次机型的完整工序
15ef33 205             List<BsTechnologyRouteChildInfo> bsTechnologyRouteChildInfos = bsTechnologyRouteChildInfoService.selectAllTechnologyRouteByProductCode(s);
9e6d86 206             if (CollUtil.isNotEmpty(bsTechnologyRouteChildInfos)){
W 207                 allProcessCoed = bsTechnologyRouteChildInfos.stream().map(BsTechnologyRouteChildInfo::getProcessesCode).collect(Collectors.toList());
208             }
209         }
210         RmRepairRecord rmRepairRecord = new RmRepairRecord();
211         rmRepairRecord.setBoxCode(SNCode);
212         List<ReadWriteEntity> writeList = new ArrayList<>();
213         List<RmRepairRecord> rmRepairRecords = rmRepairRecordService.selectRmRepairRecordList(rmRepairRecord);
3946f8 214         //过站记录
W 215         DaPassingStationCollection PassingStationCollection=new DaPassingStationCollection();
216         PassingStationCollection.setSfcCode(SNCode);
217         //过站记录
218         List<DaPassingStationCollection> daPassingStationCollections = passingStationCollectionServiceImpl.selectDaPassingStationCollectionList(PassingStationCollection);
219
9e6d86 220         //所有反工工位
W 221         List<String> collect1 = rmRepairRecords.stream().map(RmRepairRecord::getProcessesCode).collect(Collectors.toList());
3946f8 222         //查询出所有需要返修工位的最小op块
W 223         Integer minOP = 0;
224         if (CollUtil.isNotEmpty(collect1)){
225
226             List<Integer> sortList = new ArrayList<>();
227             collect1.stream().forEach(x ->{
228                 String op = x.replace("OP", "");
229                 if (StrUtil.isNotBlank(op)){
230                     int i = Integer.parseInt(op);
231                     sortList.add(i);
232                 }
9e6d86 233             });
3946f8 234             if (CollUtil.isNotEmpty(sortList)){
W 235                 List<Integer> collect2 = sortList.stream().sorted().collect(Collectors.toList());
236                 minOP = collect2.get(0);
237             }
9e6d86 238         }
3946f8 239
W 240         //所有放行工位
241         //过站记录除去返修记录就是传2,返修数据及后面占位数据传1
242         List<String> collect3 = daPassingStationCollections.stream().map(DaPassingStationCollection::getLocationCode).collect(Collectors.toList());
243         if (CollUtil.isNotEmpty(collect1)){
244             collect3 = collect3.stream().filter(x -> !collect1.contains(x)).collect(Collectors.toList());
245         }
246         if (CollUtil.isNotEmpty(collect3)){
247             collect3.forEach(x -> {
9e6d86 248                 ReadWriteEntity readWriteEntity = new ReadWriteEntity();
W 249                 readWriteEntity.setIdentifier(x);
250                 readWriteEntity.setValue(2);
251                 writeList.add(readWriteEntity);
252             });
253         }
3946f8 254         //需生产工位
W 255         List<String> collect4 = allProcessCoed;
256         if (CollUtil.isNotEmpty(collect3)){
257             List<String> finalCollect = collect3;
258             collect4 = allProcessCoed.stream().filter(x -> !finalCollect.contains(x)).collect(Collectors.toList());
259         }
260         if (CollUtil.isNotEmpty(collect4)){
261             //数据库查询出的工位
262             collect4.forEach(x -> {
263                 ReadWriteEntity readWriteEntity = new ReadWriteEntity();
264                 readWriteEntity.setIdentifier(x);
265                 readWriteEntity.setValue(1);
266                 writeList.add(readWriteEntity);
267             });
268         }
269 //        List<String> collect2 = allProcessCoed;
270 //        if (CollUtil.isNotEmpty(rmRepairRecords)){
271 //            //数据库查询出的工位
272 //            rmRepairRecords.forEach(x -> {
273 //                ReadWriteEntity readWriteEntity = new ReadWriteEntity();
274 //                readWriteEntity.setIdentifier(x.getProcessesCode());
275 //                readWriteEntity.setValue(1);
276 //                writeList.add(readWriteEntity);
277 //            });
278 //            collect2 = allProcessCoed.stream().filter(x -> !collect1.contains(x)).collect(Collectors.toList());
279 //        }
280 //        if (CollUtil.isNotEmpty(collect2)){
281 //            collect2.forEach(x -> {
282 //                ReadWriteEntity readWriteEntity = new ReadWriteEntity();
283 //                readWriteEntity.setIdentifier(x);
284 //                readWriteEntity.setValue(2);
285 //                writeList.add(readWriteEntity);
286 //            });
287 //        }
f6b9ee 288         ReadWriteEntity readWriteEntity = new ReadWriteEntity();
W 289         readWriteEntity.setIdentifier("Repair_sign");
3946f8 290         readWriteEntity.setValue(minOP);
f6b9ee 291         writeList.add(readWriteEntity);
W 292         miloService.writeToOpcByte(writeList);
059083 293     }
W 294
295
3e03af 296     public void SaveData(String Node) throws Exception {
059083 297         String[] parts = Node.split("[.]");
3e03af 298         Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code1").getValue();
3946f8 299         if (null == SNCodeObject){
W 300             SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code").getValue();
301         }
3e03af 302         if(null != SNCodeObject)
C 303         {
304             String SNCode = SNCodeObject.toString();
305             List<DaCollectionParamConf> list;
306             DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf();
307             daCollectionParamConf.setGatherAddress(parts[0] + "." + parts[1]);
2b59de 308             daCollectionParamConf.setRemarks("1");
3e03af 309             list = collectionParamConfService.selectDaCollectionParamConfList(daCollectionParamConf);
C 310
311             List<String> nodeIdList = list.stream().map(info -> {
312                 String nodeid = info.getGatherAddress();
313                 return nodeid;
314             }).collect(Collectors.toList());
315
316             if(!nodeIdList.isEmpty()){
317                 List<ReadWriteEntity> readWriteEntityList = miloService.readFromOpcUa(nodeIdList);
318                 List<DaParamCollection> daParamCollectionlist = new ArrayList<>();
319                 for(int i=0;i<nodeIdList.size();i++){
320                     if(!readWriteEntityList.get(i).getValue().toString().equals("0.0")){
2b59de 321                         DaParamCollection ParamCollection = new DaParamCollection();
3e03af 322                         ParamCollection.setParamCode(readWriteEntityList.get(i).getIdentifier().toString().split("[.]")[2]);
C 323                         ParamCollection.setLocationCode(parts[1]);
324                         ParamCollection.setParamValue(readWriteEntityList.get(i).getValue().toString());
325                         ParamCollection.setSfcCode(SNCode);
326                         ParamCollection.setParamName(list.get(i).getCollectParameterName());
327                         ParamCollection.setParamUpper(list.get(i).getParamUpper());
328                         ParamCollection.setParamLower(list.get(i).getParamLower());
329                         ParamCollection.setUnit(list.get(i).getCollectParameterUnit());
330                         ParamCollection.setState("合格");
331                         ParamCollection.setType(list.get(i).getCollectParameterType());
332                         ParamCollection.setCollectionTime(new Date());
333                         daParamCollectionlist.add(ParamCollection);
334                     }
335                 }
2b59de 336                 System.out.println("11111111111111");
3e03af 337                 daParamCollectionService.saveBeachDaParamCollection(daParamCollectionlist);
C 338             }
9e6d86 339
W 340             Object object = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Runtime").getValue();
341             Long beatTime = 0L;
342             if (ObjectUtil.isNotEmpty(object)){
9f6314 343                 String str = object.toString();
W 344                 beatTime = Float.valueOf(str).longValue();
9e6d86 345             }
W 346             DaPassingStationCollection PassingStationCollection=new DaPassingStationCollection();
347             PassingStationCollection.setSfcCode(SNCode);
348             PassingStationCollection.setLocationCode(parts[1]);
349             PassingStationCollection.setOutboundTime(new Date());
350             PassingStationCollection.setInboundTime(new Date(new Date().getTime()-beatTime*1000));
351             PassingStationCollection.setCollectionTime(new Date());
352             PassingStationCollection.setCreateTime(new Date());
353             PassingStationCollection.setBeatTime(beatTime.toString());
354             PassingStationCollection.setOutRsSign("合格");
355             passingStationCollectionServiceImpl.insertDaPassingStationCollection(PassingStationCollection);
356
357
059083 358         }
3e03af 359     }
C 360 }