春风项目四线(合箱线、总装线)
wujian
2024-04-28 929bdcf5898f03820d9d2ba62005e32a0a0ded14
提交 | 用户 | 时间
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;
c76a3f 24 import javafx.animation.Timeline;
059083 25 import lombok.extern.slf4j.Slf4j;
3e03af 26 import org.springframework.stereotype.Component;
C 27
9f6314 28 import java.math.BigDecimal;
c76a3f 29 import java.sql.Time;
9e6d86 30 import java.time.LocalDateTime;
c76a3f 31 import java.util.*;
W 32 import java.util.concurrent.TimeUnit;
3e03af 33 import java.util.stream.Collectors;
C 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
c76a3f 58     public HashMap<String,List<DaCollectionParamConf>> allCollectParamList = new HashMap<>();
W 59
3e03af 60     public OPCUaSubscription(MiloService miloService,
C 61                              IDaCollectionParamConfService collectionParamConfService,
059083 62                              IDaParamCollectionService daParamCollectionService,
W 63                              DaPassingStationCollectionServiceImpl passingStationCollectionServiceImpl,
9e6d86 64                              IBsOrderSchedulingService bsOrderSchedulingService,
W 65                              List<DaOpcuaConfig> lists,
66                              IRmRepairRecordService rmRepairRecordService,
67                              IBsTechnologyRouteChildInfoService bsTechnologyRouteChildInfoService) {
3e03af 68         OPCUaSubscription.miloService = miloService;
C 69         this.collectionParamConfService = collectionParamConfService;
70         this.daParamCollectionService = daParamCollectionService;
059083 71         this.passingStationCollectionServiceImpl = passingStationCollectionServiceImpl;
9e6d86 72         this.bsOrderSchedulingService = bsOrderSchedulingService;
059083 73         this.lists = lists;
9e6d86 74         this.rmRepairRecordService = rmRepairRecordService;
W 75         this.bsTechnologyRouteChildInfoService = bsTechnologyRouteChildInfoService;
c76a3f 76
W 77
3e03af 78     }
C 79     @Override
80     public void onSubscribe(String identifier, Object value) {
81         String ecpStr = "";//异常记录标记
82         try {
9e6d86 83             if(null != value && "1".equals(value.toString())) {
c76a3f 84 //                if (OPCElement.OP121_ZZ_CODE_CHECK.equals(identifier)){
W 85 //                    rework("2V91Y RD25D080",identifier);
86 //                }
059083 87                 //1、检索SN号
W 88                 //2、过站参数采集
89                 //3、扫码枪数据回传
90                 List<String> collect1 = lists.stream().filter(x -> OPCElement.SN_CHECK.equals(x.getrFunction()))
91                         .map(DaOpcuaConfig::getNode).collect(Collectors.toList());
92                 List<String> collect2 = lists.stream().filter(x -> OPCElement.SAVE_DATA.equals(x.getrFunction()))
93                         .map(DaOpcuaConfig::getNode).collect(Collectors.toList());
94                 if (collect1.contains(identifier)){
c76a3f 95                     log.info("-------监听到,{}的CODE_CHECK的信号",identifier);
W 96                     log.info("-------time--------------,{}",LocalDateTime.now());
059083 97                     //sn
W 98                     this.SNRetrieval(identifier,value.toString());
99                     if (identifier.equals(OPCElement.OP120_ZZ_CODE_CHECK) && "1".equals(value.toString())){
100                         //总装上线扫码传输数据
101                         log.info("-------监听到,{}的扫码枪扫码的CODE_CHECK的信号",identifier);
9e6d86 102                         scannerGunMessage();
W 103                     }
104                 }
105                 if (collect2.contains(identifier)){
106                     if ("1".equals(value.toString())){
c76a3f 107                         log.info("-------监听到,{}的SAVE_REQUEST_LAST的信号",identifier);
W 108                         log.info("-------time--------------,{}",LocalDateTime.now());
9e6d86 109                         //save
W 110                         this.SaveData(identifier);
c76a3f 111
059083 112                     }
W 113                 }
114
3e03af 115             }
C 116         } catch (Exception e) {
9e6d86 117             System.out.println(e.getMessage());
3e03af 118         } finally {
C 119             if (!"".equals(ecpStr)) {
120                 System.out.println(ecpStr + "\r\n");
121             }
9e6d86 122         }
W 123     }
124
125     private void scannerGunMessage() throws Exception {
126         String[] parts = OPCElement.OP120_ZZ_CODE_CHECK.split("[.]");
127         Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code1").getValue();
3946f8 128         if (null == SNCodeObject){
W 129             SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code").getValue();
130         }
9e6d86 131         if (null != SNCodeObject){
W 132             String SNCode = SNCodeObject.toString();
133             passingStationCollectionServiceImpl.sendMessage(SNCode);
3e03af 134         }
C 135     }
136
059083 137     public void SNRetrieval(String Node, String value) throws Exception {
W 138         String[] parts = Node.split("[.]");
139         if(value.equals("1")) {
140             //SN号检索
141             Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code").getValue();
142             if(null != SNCodeObject) {
143                 String SNCode=SNCodeObject.toString();
929bdc 144 //                String a=passingStationCollectionServiceImpl.SelectSN(SNCode,parts[1]);
W 145                 String a;
146                 BsOrderScheduling bsOrderSchedulingQuery = new BsOrderScheduling();
147                 bsOrderSchedulingQuery.setEngineNo(SNCode);
148                 List<BsOrderScheduling> queryOrderList = bsOrderSchedulingService.selectBsOrderSchedulingList(bsOrderSchedulingQuery);
149                 if (CollUtil.isNotEmpty(queryOrderList)){
150                     a = "1";
151                 }else {
152                     a = "5";
153                 }
154                 //如果是返修工位需要传输返修工位号
155                 if (OPCElement.OP465_ZZ_CODE_CHECK.equals(Node) || OPCElement.OP355_ZZ_CODE_CHECK.equals(Node)
156                         || OPCElement.OP695_ZZ_CODE_CHECK.equals(Node) || OPCElement.OP755_ZZ_CODE_CHECK.equals(Node)){
157                     //过站记录
158                     DaPassingStationCollection PassingStationCollection=new DaPassingStationCollection();
159                     PassingStationCollection.setSfcCode(SNCode);
160                     List<DaPassingStationCollection> daPassingStationCollections = passingStationCollectionServiceImpl.selectDaPassingStationCollectionList(PassingStationCollection);
161                     if (CollUtil.isNotEmpty(daPassingStationCollections)){
162                         DaPassingStationCollection lastOne = daPassingStationCollections.get(daPassingStationCollections.size() - 1);
163                         String outRsSign = lastOne.getOutRsSign();
164                         if ("合格".equals(outRsSign)){
165                             a = "1";
166                         }else {
167                             a = "2";
168                         }
169                     }
170                 }
c76a3f 171                 log.info("-----返回codeCheckFeed-----,{}",a);
3946f8 172 //                String a="1";
059083 173                 // 1:OK可生产 2:NG不可生产 3:NG可返工 4:PC检索失败(无记录)5:PC检索失败(软件)
9e6d86 174                 if (StrUtil.isNotBlank(a)){
W 175                     int input = Integer.parseInt(a);
176                     ReadWriteEntity entity = new ReadWriteEntity(parts[0]+"."+parts[1]+".CodeCheckFeed", input);
177                     log.info("-------监听到,{}的CodeCheck的信号",Node);
cffac1 178                     miloService.writeToOpcByte(entity);
W 179                     log.info("监听到返回codecheckfeed信号,{}",entity);
9e6d86 180                     //首站传输订单号
W 181                     if (OPCElement.OP050_HX_CODE_CHECK.equals(Node)){
182                         //查询订单号
183                         BsOrderScheduling bsOrderScheduling = new BsOrderScheduling();
184                         bsOrderScheduling.setEngineNo(SNCode);
185                         List<BsOrderScheduling> tempList = bsOrderSchedulingService.selectBsOrderSchedulingList(bsOrderScheduling);
186                         String orderNum = "";
187                         if (CollUtil.isNotEmpty(tempList)){
188                             BsOrderScheduling bsOrderScheduling1 = tempList.get(0);
189                             orderNum = bsOrderScheduling1.getOrderNo();
190                         }
191
192                         ReadWriteEntity entity2 = new ReadWriteEntity(parts[0]+"."+parts[1]+".OrderNumber", orderNum);
2b59de 193                         miloService.writeToOpcUa(entity2);
9e6d86 194                     }
W 195                     //如果是返修工位需要传输返修工位号
3946f8 196                     if (OPCElement.OP465_ZZ_CODE_CHECK.equals(Node) || OPCElement.OP355_ZZ_CODE_CHECK.equals(Node)
W 197                     || OPCElement.OP695_ZZ_CODE_CHECK.equals(Node) || OPCElement.OP755_ZZ_CODE_CHECK.equals(Node)){
929bdc 198
3946f8 199                         rework(SNCode,Node);
9e6d86 200                     }
3946f8 201                     System.out.println(entity);
cffac1 202
9e6d86 203                 }
W 204
059083 205             }
9e6d86 206         }
W 207     }
208
3946f8 209     private void rework(String SNCode,String Node)  throws Exception{
9e6d86 210         BsOrderScheduling bsOrderScheduling = new BsOrderScheduling();
W 211         bsOrderScheduling.setEngineNo(SNCode);
212         List<String> allProcessCoed = new ArrayList<>();
929bdc 213         List<BsTechnologyRouteChildInfo> bsTechnologyRouteChildInfos = new ArrayList<>();
9e6d86 214         List<String> collect = bsOrderSchedulingService.selectBsOrderSchedulingList(bsOrderScheduling).stream().map(BsOrderScheduling::getModel).collect(Collectors.toList());
W 215         if (CollUtil.isNotEmpty(collect)){
216             String s = collect.get(0);
217             //查找次机型的完整工序
929bdc 218             bsTechnologyRouteChildInfos = bsTechnologyRouteChildInfoService.selectAllTechnologyRouteByProductCode(s);
9e6d86 219             if (CollUtil.isNotEmpty(bsTechnologyRouteChildInfos)){
W 220                 allProcessCoed = bsTechnologyRouteChildInfos.stream().map(BsTechnologyRouteChildInfo::getProcessesCode).collect(Collectors.toList());
221             }
222         }
223         RmRepairRecord rmRepairRecord = new RmRepairRecord();
224         rmRepairRecord.setBoxCode(SNCode);
225         List<ReadWriteEntity> writeList = new ArrayList<>();
226         List<RmRepairRecord> rmRepairRecords = rmRepairRecordService.selectRmRepairRecordList(rmRepairRecord);
3946f8 227         //过站记录
W 228         DaPassingStationCollection PassingStationCollection=new DaPassingStationCollection();
229         PassingStationCollection.setSfcCode(SNCode);
230         //过站记录
231         List<DaPassingStationCollection> daPassingStationCollections = passingStationCollectionServiceImpl.selectDaPassingStationCollectionList(PassingStationCollection);
232
9e6d86 233         //所有反工工位
W 234         List<String> collect1 = rmRepairRecords.stream().map(RmRepairRecord::getProcessesCode).collect(Collectors.toList());
3946f8 235         if (CollUtil.isNotEmpty(collect1)){
929bdc 236             //查询出所有需要返修工位的最小op块
W 237             Integer minOP = 0;
238             if (CollUtil.isNotEmpty(collect1)){
3946f8 239
929bdc 240                 List<Integer> sortList = new ArrayList<>();
W 241                 collect1.stream().forEach(x ->{
242                     String op = x.replace("OP", "");
243                     if (StrUtil.isNotBlank(op)){
244                         int i = Integer.parseInt(op);
245                         sortList.add(i);
246                     }
247                 });
248                 if (CollUtil.isNotEmpty(sortList)){
249                     List<Integer> collect2 = sortList.stream().filter(Objects::nonNull).sorted().collect(Collectors.toList());
250                     minOP = collect2.get(0);
3946f8 251                 }
W 252             }
929bdc 253             //获取过站记录的最大工位
W 254             String maxCode = null;
255             if (CollUtil.isNotEmpty(daPassingStationCollections)){
256                 DaPassingStationCollection lastOne = daPassingStationCollections.get(daPassingStationCollections.size() - 1);
257                 maxCode = lastOne.getLocationCode();
28860f 258             }
W 259
260
929bdc 261             //所有放行工位
W 262             List<String> passStation = new ArrayList<>();
263             //所有生产工位
264             List<String> productStation = new ArrayList<>();
265             //最大工位编号
266             Integer maxCodeNum = 0;
267             if (StrUtil.isNotEmpty(maxCode)){
268                 String finalMaxCode = maxCode;
269                 //获取当前工位对应工步号
270                 List<String> collect2 = bsTechnologyRouteChildInfos.stream()
271                         .filter(x -> x.getProcessesCode().equals(finalMaxCode))
272                         .map(BsTechnologyRouteChildInfo::getStepNo).collect(Collectors.toList());
273                 if (CollUtil.isNotEmpty(collect2)){
274                     String s = collect2.get(0);
275                     if (StrUtil.isNotBlank(s)){
276                         maxCodeNum = Integer.parseInt(s);
277                     }
278                 }
279                 if (CollUtil.isNotEmpty(bsTechnologyRouteChildInfos)){
280                     bsTechnologyRouteChildInfos.forEach(x -> {
281                         x.setStepNoNum(Integer.parseInt(x.getStepNo()));
282                     });
283                     Integer finalMaxCodeNum = maxCodeNum;
284                     //勾选的返修工位中最大的工位之后的工位----1
285                     List<BsTechnologyRouteChildInfo> collect3 = bsTechnologyRouteChildInfos.stream()
286                             .filter(x -> x.getStepNoNum() >= finalMaxCodeNum).collect(Collectors.toList());
287                     if (CollUtil.isNotEmpty(collect3)){
288                         //需要生产
289                         productStation.addAll(collect3.stream().map(BsTechnologyRouteChildInfo::getProcessesCode).collect(Collectors.toList()));
290                     }
291                     //从第一个工位到返修的最大工位之间,
292                     //勾选了返修标记的需要生产---1,未勾选的不生产---2
293                     productStation.addAll(collect1);
294                 }
295             }
296             if (CollUtil.isNotEmpty(productStation)){
297                 List<String> collect2 = allProcessCoed.stream().filter(x -> !productStation.contains(x)).collect(Collectors.toList());
298                 passStation.addAll(collect2);
299             }
300             String[] parts = Node.split("[.]");
301             String par = parts[0]+"."+parts[1];
302             if (CollUtil.isNotEmpty(passStation)){
303                 passStation.forEach(x -> {
304                     ReadWriteEntity readWriteEntity = new ReadWriteEntity();
305                     readWriteEntity.setIdentifier(par+"."+x);
306                     readWriteEntity.setValue(2);
307                     writeList.add(readWriteEntity);
308                 });
309             }
310             if (CollUtil.isNotEmpty(productStation)){
311                 productStation.forEach(x -> {
312                     ReadWriteEntity readWriteEntity = new ReadWriteEntity();
313                     readWriteEntity.setIdentifier(par+"."+x);
314                     readWriteEntity.setValue(1);
315                     writeList.add(readWriteEntity);
316                 });
317             }
318             ReadWriteEntity readWriteEntity = new ReadWriteEntity();
319             readWriteEntity.setIdentifier(parts[0]+"."+parts[1]+".Repair_sign");
320             readWriteEntity.setValue(minOP);
321             miloService.writeToOpcShort(readWriteEntity);
322             miloService.writeToOpcByte(writeList);
323             log.info("返修写入:,{}",writeList);
324         }
325
059083 326     }
W 327
328
3e03af 329     public void SaveData(String Node) throws Exception {
28860f 330
059083 331         String[] parts = Node.split("[.]");
3e03af 332         Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code1").getValue();
3946f8 333         if (null == SNCodeObject){
W 334             SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code").getValue();
335         }
3e03af 336         if(null != SNCodeObject)
C 337         {
338             String SNCode = SNCodeObject.toString();
c76a3f 339             Object object = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Runtime").getValue();
W 340             String gatherAddress = parts[0] + "." + parts[1];
341             List<DaCollectionParamConf> list = new ArrayList<>();
342             if (allCollectParamList.containsKey(gatherAddress)){
343                 list = allCollectParamList.get(gatherAddress);
344             }else {
345                 DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf();
346                 daCollectionParamConf.setGatherAddress(gatherAddress);
347                 daCollectionParamConf.setRemarks("1");
348                 list = collectionParamConfService.selectDaCollectionParamConfList(daCollectionParamConf);
349                 if (CollUtil.isNotEmpty(list)){
350                     //加入hashmap
351                     allCollectParamList.put(gatherAddress,list);
352                 }
353             }
3e03af 354
C 355             List<String> nodeIdList = list.stream().map(info -> {
356                 String nodeid = info.getGatherAddress();
357                 return nodeid;
358             }).collect(Collectors.toList());
359
f4cb4d 360             List<ReadWriteEntity> readWriteEntityList = new ArrayList<>();
W 361             if(CollUtil.isNotEmpty(nodeIdList)){
c76a3f 362                 long time1 = new Date().getTime();
f4cb4d 363                 readWriteEntityList = miloService.readFromOpcUa(nodeIdList);
c76a3f 364                 long time2 = new Date().getTime();
W 365                 log.info("读取点位花费时间:{},",time2-time1);
f4cb4d 366             }
c76a3f 367                 //返回plc保存成功
W 368                 if (parts.length==3){
369                     if ("SaveRequest".equals(parts[2])){
370                         ReadWriteEntity entity = new ReadWriteEntity(parts[0] + "." + parts[1] + ".SaveFeed", 1);
371                                 miloService.writeToOpcByte(entity);
372                     }else if ("SaveRequestLast".equals(parts[2])){
373                         ReadWriteEntity entity = new ReadWriteEntity(parts[0] + "." + parts[1] + ".SaveFeedLast", 1);
374                                 miloService.writeToOpcByte(entity);
375                     }
376                     log.info("-------请求返回,{}的SaveFeedLast的信号",Node);
377                     log.info("-------请求返回-----,{}",LocalDateTime.now());
378                 }
3e03af 379                 List<DaParamCollection> daParamCollectionlist = new ArrayList<>();
f4cb4d 380                 if (CollUtil.isNotEmpty(nodeIdList)){
W 381                     for(int i=0;i<nodeIdList.size();i++){
382                         if(!readWriteEntityList.get(i).getValue().toString().equals("0.0")){
383                             DaParamCollection ParamCollection = new DaParamCollection();
384                             ParamCollection.setParamCode(readWriteEntityList.get(i).getIdentifier().toString().split("[.]")[2]);
385                             ParamCollection.setLocationCode(parts[1]);
386                             if (readWriteEntityList.get(i) == null){
387                                 ParamCollection.setParamValue("0");
388                             }else {
389                                 ParamCollection.setParamValue(readWriteEntityList.get(i).getValue().toString());
390                             }
391                             ParamCollection.setSfcCode(SNCode);
392                             ParamCollection.setParamName(list.get(i).getCollectParameterName());
393                             ParamCollection.setParamUpper(list.get(i).getParamUpper());
394                             ParamCollection.setParamLower(list.get(i).getParamLower());
395                             ParamCollection.setUnit(list.get(i).getCollectParameterUnit());
396                             ParamCollection.setState("合格");
397                             ParamCollection.setType(list.get(i).getCollectParameterType());
398                             ParamCollection.setCollectionTime(new Date());
399                             daParamCollectionlist.add(ParamCollection);
c6aaad 400                         }
3e03af 401                     }
f4cb4d 402                     daParamCollectionService.saveBeachDaParamCollection(daParamCollectionlist);
W 403                     log.info("-------保存数据完成---------");
3e03af 404                 }
f4cb4d 405
W 406
9e6d86 407
c76a3f 408
9e6d86 409             Long beatTime = 0L;
W 410             if (ObjectUtil.isNotEmpty(object)){
9f6314 411                 String str = object.toString();
W 412                 beatTime = Float.valueOf(str).longValue();
9e6d86 413             }
W 414             DaPassingStationCollection PassingStationCollection=new DaPassingStationCollection();
415             PassingStationCollection.setSfcCode(SNCode);
416             PassingStationCollection.setLocationCode(parts[1]);
417             PassingStationCollection.setOutboundTime(new Date());
418             PassingStationCollection.setInboundTime(new Date(new Date().getTime()-beatTime*1000));
419             PassingStationCollection.setCollectionTime(new Date());
420             PassingStationCollection.setCreateTime(new Date());
421             PassingStationCollection.setBeatTime(beatTime.toString());
422             PassingStationCollection.setOutRsSign("合格");
423             passingStationCollectionServiceImpl.insertDaPassingStationCollection(PassingStationCollection);
c76a3f 424             log.info("-------保存过站记录数据完成---------");
W 425
059083 426         }
3e03af 427     }
C 428 }