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