春风项目四线(合箱线、总装线)
wujian
2024-04-27 c76a3fde9098df30b47a546ee90598c4ec3cfb54
提交 | 用户 | 时间
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();
3946f8 144                 String a=passingStationCollectionServiceImpl.SelectSN(SNCode,parts[1]);
c76a3f 145                 log.info("-----返回codeCheckFeed-----,{}",a);
3946f8 146 //                String a="1";
059083 147                 // 1:OK可生产 2:NG不可生产 3:NG可返工 4:PC检索失败(无记录)5:PC检索失败(软件)
9e6d86 148                 if (StrUtil.isNotBlank(a)){
W 149                     int input = Integer.parseInt(a);
150                     ReadWriteEntity entity = new ReadWriteEntity(parts[0]+"."+parts[1]+".CodeCheckFeed", input);
151                     log.info("-------监听到,{}的CodeCheck的信号",Node);
152                     //首站传输订单号
153                     if (OPCElement.OP050_HX_CODE_CHECK.equals(Node)){
154                         //查询订单号
155                         BsOrderScheduling bsOrderScheduling = new BsOrderScheduling();
156                         bsOrderScheduling.setEngineNo(SNCode);
157                         List<BsOrderScheduling> tempList = bsOrderSchedulingService.selectBsOrderSchedulingList(bsOrderScheduling);
158                         String orderNum = "";
159                         if (CollUtil.isNotEmpty(tempList)){
160                             BsOrderScheduling bsOrderScheduling1 = tempList.get(0);
161                             orderNum = bsOrderScheduling1.getOrderNo();
162                         }
163
164                         ReadWriteEntity entity2 = new ReadWriteEntity(parts[0]+"."+parts[1]+".OrderNumber", orderNum);
2b59de 165                         miloService.writeToOpcUa(entity2);
9e6d86 166                     }
W 167                     //如果是返修工位需要传输返修工位号
3946f8 168                     if (OPCElement.OP465_ZZ_CODE_CHECK.equals(Node) || OPCElement.OP355_ZZ_CODE_CHECK.equals(Node)
W 169                     || OPCElement.OP695_ZZ_CODE_CHECK.equals(Node) || OPCElement.OP755_ZZ_CODE_CHECK.equals(Node)){
170                         rework(SNCode,Node);
9e6d86 171                     }
3946f8 172                     System.out.println(entity);
f6b9ee 173                 miloService.writeToOpcByte(entity);
9e6d86 174                 }
W 175
059083 176             }
9e6d86 177         }
W 178     }
179
3946f8 180     private void rework(String SNCode,String Node)  throws Exception{
9e6d86 181         BsOrderScheduling bsOrderScheduling = new BsOrderScheduling();
W 182         bsOrderScheduling.setEngineNo(SNCode);
183         List<String> allProcessCoed = new ArrayList<>();
184         List<String> collect = bsOrderSchedulingService.selectBsOrderSchedulingList(bsOrderScheduling).stream().map(BsOrderScheduling::getModel).collect(Collectors.toList());
185         if (CollUtil.isNotEmpty(collect)){
186             String s = collect.get(0);
187             //查找次机型的完整工序
15ef33 188             List<BsTechnologyRouteChildInfo> bsTechnologyRouteChildInfos = bsTechnologyRouteChildInfoService.selectAllTechnologyRouteByProductCode(s);
9e6d86 189             if (CollUtil.isNotEmpty(bsTechnologyRouteChildInfos)){
W 190                 allProcessCoed = bsTechnologyRouteChildInfos.stream().map(BsTechnologyRouteChildInfo::getProcessesCode).collect(Collectors.toList());
191             }
192         }
193         RmRepairRecord rmRepairRecord = new RmRepairRecord();
194         rmRepairRecord.setBoxCode(SNCode);
195         List<ReadWriteEntity> writeList = new ArrayList<>();
196         List<RmRepairRecord> rmRepairRecords = rmRepairRecordService.selectRmRepairRecordList(rmRepairRecord);
3946f8 197         //过站记录
W 198         DaPassingStationCollection PassingStationCollection=new DaPassingStationCollection();
199         PassingStationCollection.setSfcCode(SNCode);
200         //过站记录
201         List<DaPassingStationCollection> daPassingStationCollections = passingStationCollectionServiceImpl.selectDaPassingStationCollectionList(PassingStationCollection);
202
9e6d86 203         //所有反工工位
W 204         List<String> collect1 = rmRepairRecords.stream().map(RmRepairRecord::getProcessesCode).collect(Collectors.toList());
3946f8 205         //查询出所有需要返修工位的最小op块
W 206         Integer minOP = 0;
207         if (CollUtil.isNotEmpty(collect1)){
208
209             List<Integer> sortList = new ArrayList<>();
210             collect1.stream().forEach(x ->{
211                 String op = x.replace("OP", "");
212                 if (StrUtil.isNotBlank(op)){
213                     int i = Integer.parseInt(op);
214                     sortList.add(i);
215                 }
9e6d86 216             });
3946f8 217             if (CollUtil.isNotEmpty(sortList)){
W 218                 List<Integer> collect2 = sortList.stream().sorted().collect(Collectors.toList());
219                 minOP = collect2.get(0);
220             }
9e6d86 221         }
3946f8 222
W 223         //所有放行工位
224         //过站记录除去返修记录就是传2,返修数据及后面占位数据传1
225         List<String> collect3 = daPassingStationCollections.stream().map(DaPassingStationCollection::getLocationCode).collect(Collectors.toList());
226         if (CollUtil.isNotEmpty(collect1)){
227             collect3 = collect3.stream().filter(x -> !collect1.contains(x)).collect(Collectors.toList());
228         }
229         if (CollUtil.isNotEmpty(collect3)){
230             collect3.forEach(x -> {
9e6d86 231                 ReadWriteEntity readWriteEntity = new ReadWriteEntity();
W 232                 readWriteEntity.setIdentifier(x);
233                 readWriteEntity.setValue(2);
234                 writeList.add(readWriteEntity);
235             });
236         }
3946f8 237         //需生产工位
W 238         List<String> collect4 = allProcessCoed;
239         if (CollUtil.isNotEmpty(collect3)){
240             List<String> finalCollect = collect3;
241             collect4 = allProcessCoed.stream().filter(x -> !finalCollect.contains(x)).collect(Collectors.toList());
242         }
243         if (CollUtil.isNotEmpty(collect4)){
244             //数据库查询出的工位
245             collect4.forEach(x -> {
246                 ReadWriteEntity readWriteEntity = new ReadWriteEntity();
247                 readWriteEntity.setIdentifier(x);
248                 readWriteEntity.setValue(1);
249                 writeList.add(readWriteEntity);
250             });
251         }
252 //        List<String> collect2 = allProcessCoed;
253 //        if (CollUtil.isNotEmpty(rmRepairRecords)){
254 //            //数据库查询出的工位
255 //            rmRepairRecords.forEach(x -> {
256 //                ReadWriteEntity readWriteEntity = new ReadWriteEntity();
257 //                readWriteEntity.setIdentifier(x.getProcessesCode());
258 //                readWriteEntity.setValue(1);
259 //                writeList.add(readWriteEntity);
260 //            });
261 //            collect2 = allProcessCoed.stream().filter(x -> !collect1.contains(x)).collect(Collectors.toList());
262 //        }
263 //        if (CollUtil.isNotEmpty(collect2)){
264 //            collect2.forEach(x -> {
265 //                ReadWriteEntity readWriteEntity = new ReadWriteEntity();
266 //                readWriteEntity.setIdentifier(x);
267 //                readWriteEntity.setValue(2);
268 //                writeList.add(readWriteEntity);
269 //            });
270 //        }
c76a3f 271         String[] parts = Node.split("[.]");
W 272         String a=passingStationCollectionServiceImpl.SelectSN(SNCode,parts[1]);
273         if (StrUtil.isNotBlank(a)){
274             int input = Integer.parseInt(a);
275             ReadWriteEntity entity = new ReadWriteEntity(parts[0]+"."+parts[1]+".CodeCheckFeed", input);
276             writeList.add(entity);
277         }
f6b9ee 278         ReadWriteEntity readWriteEntity = new ReadWriteEntity();
W 279         readWriteEntity.setIdentifier("Repair_sign");
3946f8 280         readWriteEntity.setValue(minOP);
f6b9ee 281         writeList.add(readWriteEntity);
W 282         miloService.writeToOpcByte(writeList);
059083 283     }
W 284
285
3e03af 286     public void SaveData(String Node) throws Exception {
059083 287         String[] parts = Node.split("[.]");
3e03af 288         Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code1").getValue();
3946f8 289         if (null == SNCodeObject){
W 290             SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code").getValue();
291         }
3e03af 292         if(null != SNCodeObject)
C 293         {
294             String SNCode = SNCodeObject.toString();
c76a3f 295             Object object = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Runtime").getValue();
W 296             String gatherAddress = parts[0] + "." + parts[1];
297             List<DaCollectionParamConf> list = new ArrayList<>();
298             if (allCollectParamList.containsKey(gatherAddress)){
299                 list = allCollectParamList.get(gatherAddress);
300             }else {
301                 DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf();
302                 daCollectionParamConf.setGatherAddress(gatherAddress);
303                 daCollectionParamConf.setRemarks("1");
304                 list = collectionParamConfService.selectDaCollectionParamConfList(daCollectionParamConf);
305                 if (CollUtil.isNotEmpty(list)){
306                     //加入hashmap
307                     allCollectParamList.put(gatherAddress,list);
308                 }
309             }
3e03af 310
C 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()){
c76a3f 317                 long time1 = new Date().getTime();
3e03af 318                 List<ReadWriteEntity> readWriteEntityList = miloService.readFromOpcUa(nodeIdList);
c76a3f 319                 long time2 = new Date().getTime();
W 320                 log.info("读取点位花费时间:{},",time2-time1);
321
322                 //返回plc保存成功
323                 if (parts.length==3){
324                     if ("SaveRequest".equals(parts[2])){
325                         ReadWriteEntity entity = new ReadWriteEntity(parts[0] + "." + parts[1] + ".SaveFeed", 1);
326                                 miloService.writeToOpcByte(entity);
327                     }else if ("SaveRequestLast".equals(parts[2])){
328                         ReadWriteEntity entity = new ReadWriteEntity(parts[0] + "." + parts[1] + ".SaveFeedLast", 1);
329                                 miloService.writeToOpcByte(entity);
330                     }
331                     log.info("-------请求返回,{}的SaveFeedLast的信号",Node);
332                     log.info("-------请求返回-----,{}",LocalDateTime.now());
333                 }
3e03af 334                 List<DaParamCollection> daParamCollectionlist = new ArrayList<>();
C 335                 for(int i=0;i<nodeIdList.size();i++){
336                     if(!readWriteEntityList.get(i).getValue().toString().equals("0.0")){
2b59de 337                         DaParamCollection ParamCollection = new DaParamCollection();
3e03af 338                         ParamCollection.setParamCode(readWriteEntityList.get(i).getIdentifier().toString().split("[.]")[2]);
C 339                         ParamCollection.setLocationCode(parts[1]);
c6aaad 340                         if (readWriteEntityList.get(i) == null){
W 341                             ParamCollection.setParamValue("0");
342                         }else {
343                             ParamCollection.setParamValue(readWriteEntityList.get(i).getValue().toString());
344                         }
3e03af 345                         ParamCollection.setSfcCode(SNCode);
C 346                         ParamCollection.setParamName(list.get(i).getCollectParameterName());
347                         ParamCollection.setParamUpper(list.get(i).getParamUpper());
348                         ParamCollection.setParamLower(list.get(i).getParamLower());
349                         ParamCollection.setUnit(list.get(i).getCollectParameterUnit());
350                         ParamCollection.setState("合格");
351                         ParamCollection.setType(list.get(i).getCollectParameterType());
352                         ParamCollection.setCollectionTime(new Date());
353                         daParamCollectionlist.add(ParamCollection);
354                     }
355                 }
356                 daParamCollectionService.saveBeachDaParamCollection(daParamCollectionlist);
c76a3f 357                 log.info("-------保存数据完成---------");
3e03af 358             }
9e6d86 359
c76a3f 360
9e6d86 361             Long beatTime = 0L;
W 362             if (ObjectUtil.isNotEmpty(object)){
9f6314 363                 String str = object.toString();
W 364                 beatTime = Float.valueOf(str).longValue();
9e6d86 365             }
W 366             DaPassingStationCollection PassingStationCollection=new DaPassingStationCollection();
367             PassingStationCollection.setSfcCode(SNCode);
368             PassingStationCollection.setLocationCode(parts[1]);
369             PassingStationCollection.setOutboundTime(new Date());
370             PassingStationCollection.setInboundTime(new Date(new Date().getTime()-beatTime*1000));
371             PassingStationCollection.setCollectionTime(new Date());
372             PassingStationCollection.setCreateTime(new Date());
373             PassingStationCollection.setBeatTime(beatTime.toString());
374             PassingStationCollection.setOutRsSign("合格");
375             passingStationCollectionServiceImpl.insertDaPassingStationCollection(PassingStationCollection);
c76a3f 376             log.info("-------保存过站记录数据完成---------");
W 377
059083 378         }
3e03af 379     }
C 380 }