春风项目四线(合箱线、总装线)
wujian
2024-04-22 2b59de22fbd48ad356b79499722e948d7817c9c3
提交 | 用户 | 时间
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();
145         if (null != SNCodeObject){
146             String SNCode = SNCodeObject.toString();
147             passingStationCollectionServiceImpl.sendMessage(SNCode);
3e03af 148         }
C 149     }
150
059083 151     public void SNRetrieval(String Node, String value) throws Exception {
W 152         String[] parts = Node.split("[.]");
153         if(value.equals("1")) {
154             //SN号检索
155             Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code").getValue();
156             if(null != SNCodeObject) {
157                 String SNCode=SNCodeObject.toString();
2b59de 158 //                String a=passingStationCollectionServiceImpl.SelectSN(SNCode,parts[1]);
W 159                 String a="1";
059083 160                 // 1:OK可生产 2:NG不可生产 3:NG可返工 4:PC检索失败(无记录)5:PC检索失败(软件)
9e6d86 161                 if (StrUtil.isNotBlank(a)){
W 162                     int input = Integer.parseInt(a);
163                     ReadWriteEntity entity = new ReadWriteEntity(parts[0]+"."+parts[1]+".CodeCheckFeed", input);
164                     log.info("-------监听到,{}的CodeCheck的信号",Node);
165                     //首站传输订单号
166                     if (OPCElement.OP050_HX_CODE_CHECK.equals(Node)){
167                         //查询订单号
168                         BsOrderScheduling bsOrderScheduling = new BsOrderScheduling();
169                         bsOrderScheduling.setEngineNo(SNCode);
170                         List<BsOrderScheduling> tempList = bsOrderSchedulingService.selectBsOrderSchedulingList(bsOrderScheduling);
171                         String orderNum = "";
172                         if (CollUtil.isNotEmpty(tempList)){
173                             BsOrderScheduling bsOrderScheduling1 = tempList.get(0);
174                             orderNum = bsOrderScheduling1.getOrderNo();
175                         }
176
177                         ReadWriteEntity entity2 = new ReadWriteEntity(parts[0]+"."+parts[1]+".OrderNumber", orderNum);
2b59de 178                         miloService.writeToOpcUa(entity2);
9e6d86 179                     }
W 180                     //如果是返修工位需要传输返修工位号
181                     if (OPCElement.OP465_ZZ_CODE_CHECK.equals(Node) || OPCElement.OP355_ZZ_CODE_CHECK.equals(Node)){
182                         rework(SNCode);
183                     }
f6b9ee 184                 miloService.writeToOpcByte(entity);
9e6d86 185                 }
W 186
059083 187             }
9e6d86 188         }
W 189     }
190
f6b9ee 191     private void rework(String SNCode)  throws Exception{
9e6d86 192         BsOrderScheduling bsOrderScheduling = new BsOrderScheduling();
W 193         bsOrderScheduling.setEngineNo(SNCode);
194         List<String> allProcessCoed = new ArrayList<>();
195         List<String> collect = bsOrderSchedulingService.selectBsOrderSchedulingList(bsOrderScheduling).stream().map(BsOrderScheduling::getModel).collect(Collectors.toList());
196         if (CollUtil.isNotEmpty(collect)){
197             String s = collect.get(0);
198             //查找次机型的完整工序
15ef33 199             List<BsTechnologyRouteChildInfo> bsTechnologyRouteChildInfos = bsTechnologyRouteChildInfoService.selectAllTechnologyRouteByProductCode(s);
9e6d86 200             if (CollUtil.isNotEmpty(bsTechnologyRouteChildInfos)){
W 201                 allProcessCoed = bsTechnologyRouteChildInfos.stream().map(BsTechnologyRouteChildInfo::getProcessesCode).collect(Collectors.toList());
202             }
203         }
204         RmRepairRecord rmRepairRecord = new RmRepairRecord();
205         rmRepairRecord.setBoxCode(SNCode);
206         List<ReadWriteEntity> writeList = new ArrayList<>();
207         List<RmRepairRecord> rmRepairRecords = rmRepairRecordService.selectRmRepairRecordList(rmRepairRecord);
208         //所有反工工位
209         List<String> collect1 = rmRepairRecords.stream().map(RmRepairRecord::getProcessesCode).collect(Collectors.toList());
210         //所有放行工位
211         List<String> collect2 = allProcessCoed;
212         if (CollUtil.isNotEmpty(rmRepairRecords)){
213             //数据库查询出的工位
214             rmRepairRecords.forEach(x -> {
215                 ReadWriteEntity readWriteEntity = new ReadWriteEntity();
216                 readWriteEntity.setIdentifier(x.getProcessesCode());
217                 readWriteEntity.setValue(1);
218                 writeList.add(readWriteEntity);
219             });
220             collect2 = allProcessCoed.stream().filter(x -> !collect1.contains(x)).collect(Collectors.toList());
221         }
222         if (CollUtil.isNotEmpty(collect2)){
223             collect2.forEach(x -> {
224                 ReadWriteEntity readWriteEntity = new ReadWriteEntity();
225                 readWriteEntity.setIdentifier(x);
226                 readWriteEntity.setValue(2);
227                 writeList.add(readWriteEntity);
228             });
229         }
f6b9ee 230         ReadWriteEntity readWriteEntity = new ReadWriteEntity();
W 231         readWriteEntity.setIdentifier("Repair_sign");
232         readWriteEntity.setValue(1);
233         writeList.add(readWriteEntity);
234         miloService.writeToOpcByte(writeList);
059083 235     }
W 236
237
3e03af 238     public void SaveData(String Node) throws Exception {
059083 239         String[] parts = Node.split("[.]");
3e03af 240         Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code1").getValue();
C 241
242         if(null != SNCodeObject)
243         {
244             String SNCode = SNCodeObject.toString();
245             List<DaCollectionParamConf> list;
246             DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf();
247             daCollectionParamConf.setGatherAddress(parts[0] + "." + parts[1]);
2b59de 248             daCollectionParamConf.setRemarks("1");
3e03af 249             list = collectionParamConfService.selectDaCollectionParamConfList(daCollectionParamConf);
C 250
251             List<String> nodeIdList = list.stream().map(info -> {
252                 String nodeid = info.getGatherAddress();
253                 return nodeid;
254             }).collect(Collectors.toList());
255
256             if(!nodeIdList.isEmpty()){
257                 List<ReadWriteEntity> readWriteEntityList = miloService.readFromOpcUa(nodeIdList);
258                 List<DaParamCollection> daParamCollectionlist = new ArrayList<>();
259                 for(int i=0;i<nodeIdList.size();i++){
260                     if(!readWriteEntityList.get(i).getValue().toString().equals("0.0")){
2b59de 261                         DaParamCollection ParamCollection = new DaParamCollection();
3e03af 262                         ParamCollection.setParamCode(readWriteEntityList.get(i).getIdentifier().toString().split("[.]")[2]);
C 263                         ParamCollection.setLocationCode(parts[1]);
264                         ParamCollection.setParamValue(readWriteEntityList.get(i).getValue().toString());
265                         ParamCollection.setSfcCode(SNCode);
266                         ParamCollection.setParamName(list.get(i).getCollectParameterName());
267                         ParamCollection.setParamUpper(list.get(i).getParamUpper());
268                         ParamCollection.setParamLower(list.get(i).getParamLower());
269                         ParamCollection.setUnit(list.get(i).getCollectParameterUnit());
270                         ParamCollection.setState("合格");
271                         ParamCollection.setType(list.get(i).getCollectParameterType());
272                         ParamCollection.setCollectionTime(new Date());
273                         daParamCollectionlist.add(ParamCollection);
274                     }
275                 }
2b59de 276                 System.out.println("11111111111111");
3e03af 277                 daParamCollectionService.saveBeachDaParamCollection(daParamCollectionlist);
C 278             }
9e6d86 279
W 280             Object object = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Runtime").getValue();
281             Long beatTime = 0L;
282             if (ObjectUtil.isNotEmpty(object)){
9f6314 283                 String str = object.toString();
W 284                 beatTime = Float.valueOf(str).longValue();
9e6d86 285             }
W 286             DaPassingStationCollection PassingStationCollection=new DaPassingStationCollection();
287             PassingStationCollection.setSfcCode(SNCode);
288             PassingStationCollection.setLocationCode(parts[1]);
289             PassingStationCollection.setOutboundTime(new Date());
290             PassingStationCollection.setInboundTime(new Date(new Date().getTime()-beatTime*1000));
291             PassingStationCollection.setCollectionTime(new Date());
292             PassingStationCollection.setCreateTime(new Date());
293             PassingStationCollection.setBeatTime(beatTime.toString());
294             PassingStationCollection.setOutRsSign("合格");
295             passingStationCollectionServiceImpl.insertDaPassingStationCollection(PassingStationCollection);
296
297
059083 298         }
3e03af 299     }
C 300 }