wujian
2024-05-24 09ef66abe6ffc1325c5d23055ed6f3f4cb88b625
提交 | 用户 | 时间
e6c837 1 package com.jcdm.main.plcserver.sub;
A 2
3
4 import cn.hutool.core.collection.CollUtil;
09ef66 5 import cn.hutool.core.util.ObjectUtil;
e6c837 6 import cn.hutool.core.util.StrUtil;
A 7 import cn.hutool.json.JSONObject;
8 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
9 import com.jcdm.framework.websocket.WebSocketUsers;
10 import com.jcdm.main.constant.Constants;
11 import com.jcdm.main.da.collectionParamConf.domain.DaCollectionParamConf;
12 import com.jcdm.main.da.collectionParamConf.service.IDaCollectionParamConfService;
13 import com.jcdm.main.da.paramCollection.domain.DaParamCollection;
14 import com.jcdm.main.da.paramCollection.service.IDaParamCollectionService;
15 import com.jcdm.main.da.passingStationCollection.domain.DaPassingStationCollection;
09ef66 16 import com.jcdm.main.da.passingStationCollection.query.ProductOrderChild;
W 17 import com.jcdm.main.da.passingStationCollection.query.ProductOrderQuery;
18 import com.jcdm.main.da.passingStationCollection.query.ProductOrderVO;
e6c837 19 import com.jcdm.main.da.passingStationCollection.service.IDaPassingStationCollectionService;
A 20 import com.jcdm.main.om.productionOrde.domain.OmProductionOrdeInfo;
21 import com.jcdm.main.om.productionOrde.service.IOmProductionOrdeInfoService;
22 import com.jcdm.main.plcserver.util.TimeUtil;
23 import com.jcdm.main.restful.factoryMes.service.RestfulService;
09ef66 24 import com.jcdm.main.work.domain.KeyCodeRelation;
W 25 import com.jcdm.main.work.domain.WorkOrder;
26 import com.jcdm.main.work.service.KeyCodeCheckService;
27 import com.jcdm.main.work.service.KeyCodeRelationService;
28 import com.jcdm.main.work.service.WorkOrderService;
e6c837 29 import com.kangaroohy.milo.model.ReadWriteEntity;
A 30 import com.kangaroohy.milo.runner.subscription.SubscriptionCallback;
31 import com.kangaroohy.milo.service.MiloService;
09ef66 32 import lombok.extern.slf4j.Slf4j;
e6c837 33 import org.springframework.stereotype.Component;
A 34
35 import javax.websocket.Session;
36 import java.text.SimpleDateFormat;
37 import java.util.*;
38 import java.util.stream.Collectors;
39
40
09ef66 41 @Slf4j
e6c837 42 @Component
A 43 public class OPCUaSubscription implements SubscriptionCallback {
44
45     public SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
46
47     //新自动工站
48     public List<String> automaticList = Arrays.asList("OP280","OP310A","OP310B", "OP300A","OP300B", "OP320A","OP320B", "OP340A","OP340B", "OP350A","OP350B", "OP360","OP365","OP370", "OP390", "OP470");
49
50     //采集模组工位 ModuleCode
51     public List<String> moduleCodeList = Arrays.asList("OP300A","OP300B","OP320A","OP320B","OP340A","OP340B","OP365", "OP350A", "OP350B", "OP330");
52
53     //区分模组型号工位
54     public List<String> distinguishingModules = Arrays.asList("OP300","OP310", "OP340", "OP350", "OP330");
55
56     //区分小车码
57     public List<String> agvId = Arrays.asList("OP360", "OP390", "OP470","OP280");
58
59
60     //区分pack型号的工位
61     public List<String> packIdList = Arrays.asList("OP280","OP360","OP390","OP470");
62
63     //测试设备手动工位
64     public List<String> testList = Arrays.asList("OP430-1","OP430-2","OP430-3","OP430-4","OP480-1","OP480-2","OP480-3","OP440");
65
66
67     //空的
68     public List<String> nullList = Arrays.asList("OP250","OP260");
69
70
71     public static MiloService miloService;
72
73     Map<String, Session> map = WebSocketUsers.getUsers();
74
75     public IDaPassingStationCollectionService daPassingStationCollectionService;
76
77     public static IDaCollectionParamConfService collectionParamConfService;
78
79     public static IDaParamCollectionService daParamCollectionService;
80
81     public static IOmProductionOrdeInfoService omProductionOrdeInfoService;
09ef66 82     public static WorkOrderService workOrderService;
W 83     public static KeyCodeCheckService keyCodeCheckService;
84     public static KeyCodeRelationService keyCodeRelationService;
e6c837 85
A 86     public OPCUaSubscription(MiloService miloService,
87                              IDaPassingStationCollectionService daPassingStationCollectionService,
88                              IDaCollectionParamConfService collectionParamConfService,
89                              IDaParamCollectionService daParamCollectionService,
09ef66 90                              IOmProductionOrdeInfoService omProductionOrdeInfoService,
W 91                              WorkOrderService workOrderService,
92                              KeyCodeCheckService keyCodeCheckService,
93                              KeyCodeRelationService keyCodeRelationService) {
e6c837 94         OPCUaSubscription.miloService = miloService;
A 95         this.daPassingStationCollectionService = daPassingStationCollectionService;
96         OPCUaSubscription.collectionParamConfService = collectionParamConfService;
97         OPCUaSubscription.daParamCollectionService = daParamCollectionService;
98         OPCUaSubscription.omProductionOrdeInfoService = omProductionOrdeInfoService;
09ef66 99         OPCUaSubscription.workOrderService = workOrderService;
W 100         OPCUaSubscription.keyCodeCheckService = keyCodeCheckService;
101         OPCUaSubscription.keyCodeRelationService = keyCodeRelationService;
102
e6c837 103     }
A 104
105
106     @Override
107     public void onSubscribe(String identifier, Object value) {
108
109         try {
09ef66 110             if(null != value && !Constants.ZERO.equals(value.toString())) {
e6c837 111                 String[] nodes = identifier.split("[.]");
A 112                 String thoroughfare = nodes[0];//通道
113                 String device = nodes[1];//设备
114                 String tab = nodes[2];//标记
09ef66 115                 String valueString = value.toString();//地址值
W 116                 if (Constants.RECORD_CHECK_CODE.equals(tab)){
117                     if (Constants.ONE.equals(valueString)){
118                         Integer scanResult = 11;
119                         if (Constants.OP010.equals(device) || Constants.OP030.equals(device)){
120                             //OP010工位电芯条码校验||OP030工位电芯条码校验
121                             Object value1 = miloService.readFromOpcUa(thoroughfare + "." + device + ".Scaner").getValue();
122                             if (ObjectUtil.isNotNull(value1)){
123                                 String keyCode = value1.toString();
124                                 log.info("读取到工位{}的Scaner数据:{}",device,keyCode);
125                                 //仅校验长度是否合格
126 //                                List<KeyCodeCheck> collect = keyCodeCheckService.list().stream().filter(x -> x.getKeyCode().contains(keyCode)).collect(Collectors.toList());
127 //                                if (CollUtil.isNotEmpty(collect)){
128 //                                    scanResult = 11;
129 //                                }
e6c837 130                             }
A 131                         }
09ef66 132                         miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".ScanerResult1").value(scanResult).build());
W 133                         log.info("写入到工位{}的ScanerResult1数据:{}",device,scanResult);
e6c837 134                     }
09ef66 135                 }else if (Constants.RECORD_DATA.equals(tab)){
W 136                     if (Constants.ONE.equals(valueString)){
137                         //1:告知MES托盘已到站,请求下发进站状态
138                         if (Constants.OP030.equals(device)){
139                             //反馈电芯ocv检测结果
140                             boolean b = OCVResultFeedBack(thoroughfare, device);
141                             if (b){
142                                 miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(11).build());
143                                 log.info("写入到工位{}的RecordDataDone数据:{}",device,11);
144                             }else {
145                                 miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(12).build());
146                                 log.info("写入到工位{}的RecordDataDone数据:{}",device,12);
147                             }
148                         } else {
149                             miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(11).build());
150                             log.info("写入到工位{}的RecordDataDone数据:{}",device,11);
e6c837 151                         }
09ef66 152                     }else if (Constants.TWO.equals(valueString)){
W 153                         //分段010-065段
154                         List<String> PART1 = new ArrayList<>();
155                         PART1.add(Constants.OP020);
156                         PART1.add(Constants.OP030);
157                         PART1.add(Constants.OP040);
158                         PART1.add(Constants.OP050);
159                         PART1.add(Constants.OP060);
160                         PART1.add(Constants.OP065);
161                         if (Constants.OP010.equals(device)){
162                             //010工位无过站记录,只给放行信号
163                             miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(21).build());
164                             log.info("写入到工位{}的RecordDataDone数据:{}",device,21);
165                         }else if (PART1.contains(device)){
166                             //020工位保存过站记录+参数数据
167                             List<DaPassingStationCollection> passingList = getDaPassingStationCollections(thoroughfare, device);
168                             if (CollUtil.isNotEmpty(passingList)){
169                                 daPassingStationCollectionService.insertBatch(passingList);
170                             }
171                             miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(21).build());
172                             log.info("写入到工位{}的RecordDataDone数据:{}",device,21);
173                         } else if (Constants.OP100_1.equals(device) || Constants.OP100_2.equals(device)){
174                             //下发刻码信息-为模组码
175                             ProductOrderQuery query = new ProductOrderQuery();
176                             query.setSiteCode("3983");
177                             query.setStationCode(device);
178                             List<WorkOrder> collect = workOrderService.list().stream()
179                                     .sorted(Comparator.comparing(WorkOrder::getCreateTime).reversed())
180                                     .collect(Collectors.toList());
181                             if (CollUtil.isNotEmpty(collect)){
182                                 query.setProductNum(collect.get(0).getProductNum());
183                             }
184                             ProductOrderVO vo = daPassingStationCollectionService.queryMessFromLine(query);
185                             if (Constants.SUCCESS.equals(vo.getCode())){
186                                 WorkOrder workOrder = new WorkOrder();
187                                 ProductOrderChild data = vo.getData();
188                                 workOrder.setProductNum(data.getProductNum());
189                                 workOrder.setProductionOrderNum(data.getProductionOrderNum());
190                                 workOrder.setStationCode(data.getStationCode());
191                                 workOrder.setMaterialCode(data.getMaterialCode());
192                                 workOrder.setModel(data.getModel());
193                                 workOrderService.save(workOrder);
194                                 //写入模组码、工单号、产品型号到plc
195                                 log.info("写入模组码、工单号、产品型号到plc");
196                                 miloService.writeToOpcUa(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".ModuleCode").value(data.getModel()).build());
197                                 miloService.writeToOpcUa(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".WorkOrderNumber").value(data.getProductionOrderNum()).build());
198                                 //这里plc录入的产品型号类型是int类型,工厂mes返回数据不是int类型,可能会出现问题
199                                 miloService.writeToOpcUa(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".ProductType").value(data.getProductNum()).build());
200                                 //绑定模组码和电芯码数据
201                                 List<DaCollectionParamConf> list = collectionParamConfService.list(new LambdaQueryWrapper<DaCollectionParamConf>()
202                                         .eq(DaCollectionParamConf::getProcessesCode, device)
203                                         .eq(DaCollectionParamConf::getRemarks,Constants.ONE));
204                                 List<String> collect3 = list.stream().filter(x -> Constants.INT_ZERO.equals(x.getKeyNum())).map(DaCollectionParamConf::getGatherAddress).collect(Collectors.toList());
205                                 List<String> collect1 = list.stream().filter(x -> Constants.INT_ONE.equals(x.getKeyNum())|| Constants.INT_TWO.equals(x.getKeyNum()))
206                                         .map(DaCollectionParamConf::getGatherAddress).collect(Collectors.toList());
207 //                                List<String> collect2 = list.stream().filter(x -> Constants.INT_TWO.equals(x.getKeyNum()))
208 //                                        .map(DaCollectionParamConf::getGatherAddress).collect(Collectors.toList());
209                                 List<ReadWriteEntity> paramCollectionList1 = miloService.readFromOpcUa(collect1);
210                                 List<ReadWriteEntity> paramCollectionList3 = miloService.readFromOpcUa(collect3);
211                                 if (CollUtil.isNotEmpty(paramCollectionList1)){
212                                     List<KeyCodeRelation> relationList = new ArrayList<>();
213                                     for (ReadWriteEntity readWriteEntity : paramCollectionList1) {
214                                         if (ObjectUtil.isNotNull(readWriteEntity.getValue())){
215                                             KeyCodeRelation keyCodeRelation = new KeyCodeRelation();
216                                             keyCodeRelation.setModuleCode(data.getModel());
217                                             keyCodeRelation.setKeyCode(readWriteEntity.getValue().toString());
218                                             keyCodeRelation.setProductionOrderNum(data.getProductionOrderNum());
219                                             keyCodeRelation.setProductNum(data.getProductNum());
220                                             keyCodeRelation.setCreateTime(new Date());
221                                             relationList.add(keyCodeRelation);
222                                         }
223                                     }
224                                     if (CollUtil.isNotEmpty(relationList)){
225                                         keyCodeRelationService.insertBatch(relationList);
226                                     }
227                                 }
228                                 if (CollUtil.isNotEmpty(paramCollectionList3)){
229                                     QueryPLCData(device,thoroughfare,collect3);
230                                 }
231                                 //保存过站记录
232                                 DaPassingStationCollection passingStationCollection = new DaPassingStationCollection();
233                                 passingStationCollection.setModuleCode(data.getModel());
234
235                                 passingStationCollection.setLocationCode(device);
236                                 passingStationCollection.setInboundTime(new Date());
237                                 String stationStatus = Constants.PASS;
238                                 ReadWriteEntity stationStatusRead = miloService.readFromOpcUa(thoroughfare + "." + device + ".StationStatus");
239                                 if (ObjectUtil.isNotNull(stationStatusRead.getValue())){
240                                     String string = stationStatusRead.getValue().toString();
241                                     log.info("读取工位{}StationStatus:{}",device,string);
242                                     if (Constants.TWO.equals(string)){
243                                         stationStatus = Constants.UN_PASS;
244                                     }
245                                 }
246                                 passingStationCollection.setOutRsSign(stationStatus);
247                                 passingStationCollection.setCreateTime(new Date());
248                                 daPassingStationCollectionService.save(passingStationCollection);
249                             }
250                             miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(21).build());
251                             log.info("写入到工位{}的RecordDataDone数据:{}",device,21);
252                         } else {
253                             //2:告知MES工位工作完成,请求记录工位数据
254                             //保存参数采集记录
255                             saveParamPassingData(device,thoroughfare);
256                             //保存过站记录
257                             DaPassingStationCollection passingStationCollection = new DaPassingStationCollection();
258                             Object value1 = miloService.readFromOpcUa(thoroughfare + "." + device + ".ModuleCode").getValue();
259                             String moduleCode = null;
260                             if (ObjectUtil.isNotNull(value1)){
261                                 moduleCode = value1.toString();
262                                 log.info("读取工位{}的ModuleCode数据:{}",device,moduleCode);
263                             }
264                             passingStationCollection.setModuleCode(moduleCode);
265                             passingStationCollection.setLocationCode(device);
266                             passingStationCollection.setInboundTime(new Date());
267                             String stationStatus = Constants.PASS;
268                             ReadWriteEntity stationStatusRead = miloService.readFromOpcUa(thoroughfare + "." + device + ".StationStatus");
269                             if (ObjectUtil.isNotNull(stationStatusRead.getValue())){
270                                 String string = stationStatusRead.getValue().toString();
271                                 if (Constants.TWO.equals(string)){
272                                     stationStatus = Constants.UN_PASS;
273                                 }
274                             }
275                             passingStationCollection.setOutRsSign(stationStatus);
276                             passingStationCollection.setCreateTime(new Date());
277                             daPassingStationCollectionService.save(passingStationCollection);
278                             miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(21).build());
279                             log.info("写入到工位{}的RecordDataDone数据:{}",device,21);
280                         }
281                     }
282                 } else if (Constants.RECORD_SN.equals(tab)){
283                     if (Constants.ONE.equals(valueString)){
284                         if (Constants.OP070.equals(device)){
285                             //OP070下发产品模组码
286                             String string = UUID.randomUUID().toString();
287                             miloService.writeToOpcUa(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".ModuleCode").value(string).build());
288                             miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordSNDone").value(1).build());
289                         }
290                     }
291                 } else if (Constants.RECORD_CHECK_CODE_1.equals(tab)) {
292                     if (Constants.OP100_1.equals(device)){
293                         //钢带条码校验--发送给工厂mes
294                         miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".ScanerResult11").value(11).build());
295                     } else if (Constants.OP100_2.equals(device)) {
296                         //PET带条码校验--发送给工厂mes
297                         miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".ScanerResult12").value(11).build());
e6c837 298                     }
A 299                 }
300             }
301         } catch (Exception e) {
09ef66 302             System.out.println(e.getMessage());
W 303             log.info(e.getMessage());
304         }
305     }
306
307     /**
308      * 保存电芯过站记录
309      * @param thoroughfare 通道
310      * @param device 工位
311      * @return list
312      * @throws Exception e
313      */
314     private static List<DaPassingStationCollection> getDaPassingStationCollections(String thoroughfare, String device) throws Exception {
315         List<String> readList = new ArrayList<>();
316         readList.add(thoroughfare + "." + device +".CellCode_1");
317         readList.add(thoroughfare + "." + device +".CellCode_2");
318         readList.add(thoroughfare + "." + device +".CellCode_3");
319         readList.add(thoroughfare + "." + device +".CellCode_4");
320         List<String> keyCodeList = new ArrayList<>();
321         List<ReadWriteEntity> readWriteEntityList = miloService.readFromOpcUa(readList);
322         List<DaPassingStationCollection> passingList = new ArrayList<>();
323         for (ReadWriteEntity readWriteEntity : readWriteEntityList) {
324             DaPassingStationCollection passingStationCollection = new DaPassingStationCollection();
325             passingStationCollection.setLocationCode(device);
326             passingStationCollection.setInboundTime(new Date());
327             if (ObjectUtil.isNotNull(readWriteEntity.getValue())){
328                 passingStationCollection.setKeyCode(readWriteEntity.getValue().toString());
329                 keyCodeList.add(readWriteEntity.getValue().toString());
330             }
331             String stationStatus = Constants.PASS;
332             ReadWriteEntity stationStatusRead = miloService.readFromOpcUa(thoroughfare + "." + device + ".StationStatus");
333             if (ObjectUtil.isNotNull(stationStatusRead.getValue())){
334                 String string = stationStatusRead.getValue().toString();
335                 if (Constants.TWO.equals(string)){
336                     stationStatus = Constants.UN_PASS;
337                 }
338             }
339             passingStationCollection.setOutRsSign(stationStatus);
340             passingStationCollection.setCreateTime(new Date());
341             passingList.add(passingStationCollection);
342         }
343         //参数采集
344         //保存参数采集数据
345         List<DaCollectionParamConf> list = collectionParamConfService.list(new LambdaQueryWrapper<DaCollectionParamConf>()
346                 .eq(DaCollectionParamConf::getProcessesCode, device)
347                 .eq(DaCollectionParamConf::getRemarks, Constants.ONE));
348         if (CollUtil.isNotEmpty(list)){
349             List<DaParamCollection> saveParamList = new ArrayList<>();
350             List<String> collect1 = list.stream().filter(x -> Constants.INT_ONE.equals(x.getKeyNum()))
351                     .map(DaCollectionParamConf::getGatherAddress).collect(Collectors.toList());
352             List<ReadWriteEntity> paramCollectionList1 = miloService.readFromOpcUa(collect1);
353             List<String> collect2 = list.stream().filter(x -> Constants.INT_TWO.equals(x.getKeyNum()))
354                     .map(DaCollectionParamConf::getGatherAddress).collect(Collectors.toList());
355             List<ReadWriteEntity> paramCollectionList2 = miloService.readFromOpcUa(collect2);
356             List<String> collect3 = list.stream().filter(x -> Constants.INT_THREE.equals(x.getKeyNum()))
357                     .map(DaCollectionParamConf::getGatherAddress).collect(Collectors.toList());
358             List<ReadWriteEntity> paramCollectionList3 = miloService.readFromOpcUa(collect3);
359             List<String> collect4 = list.stream().filter(x -> Constants.INT_FOUR.equals(x.getKeyNum()))
360                     .map(DaCollectionParamConf::getGatherAddress).collect(Collectors.toList());
361             List<ReadWriteEntity> paramCollectionList4 = miloService.readFromOpcUa(collect4);
362             List<String> collect0 = list.stream().filter(x -> Constants.INT_ZERO.equals(x.getKeyNum()))
363                     .map(DaCollectionParamConf::getGatherAddress).collect(Collectors.toList());
364             List<ReadWriteEntity> paramCollectionList0 = miloService.readFromOpcUa(collect0);
365             if (CollUtil.isNotEmpty(paramCollectionList1)){
366                 for (int i = 0; i < paramCollectionList1.size(); i++) {
367                     DaParamCollection daParamCollection = new DaParamCollection();
368                     if (CollUtil.isNotEmpty(keyCodeList) && keyCodeList.size() == 4){
369                         daParamCollection.setKeyCode(keyCodeList.get(0));
370                     }
371
372                     daParamCollection.setParamCode(paramCollectionList1.get(i).getIdentifier());
373                     String paramValue = "";
374                     if (ObjectUtil.isNotNull(paramCollectionList1.get(i).getValue())){
375                         paramValue = paramCollectionList1.get(i).getValue().toString();
376                     }
377                     daParamCollection.setParamValue(paramValue);
378                     daParamCollection.setLocationCode(device);
379                     daParamCollection.setCollectionTime(new Date());
380                     saveParamList.add(daParamCollection);
381                 }
382             }
383             if (CollUtil.isNotEmpty(paramCollectionList2)){
384                 for (int i = 0; i < paramCollectionList2.size(); i++) {
385                     DaParamCollection daParamCollection = new DaParamCollection();
386                     if (CollUtil.isNotEmpty(keyCodeList) && keyCodeList.size() == 4){
387                         daParamCollection.setKeyCode(keyCodeList.get(1));
388                     }
389
390                     daParamCollection.setParamCode(paramCollectionList2.get(i).getIdentifier());
391                     String paramValue = "";
392                     if (ObjectUtil.isNotNull(paramCollectionList2.get(i).getValue())){
393                         paramValue = paramCollectionList2.get(i).getValue().toString();
394                     }
395                     daParamCollection.setParamValue(paramValue);
396                     daParamCollection.setLocationCode(device);
397                     daParamCollection.setCollectionTime(new Date());
398                     saveParamList.add(daParamCollection);
399                 }
400             }
401             if (CollUtil.isNotEmpty(paramCollectionList3)){
402                 for (int i = 0; i < paramCollectionList3.size(); i++) {
403                     DaParamCollection daParamCollection = new DaParamCollection();
404                     if (CollUtil.isNotEmpty(keyCodeList) && keyCodeList.size() == 4){
405                         daParamCollection.setKeyCode(keyCodeList.get(2));
406                     }
407
408                     daParamCollection.setParamCode(paramCollectionList3.get(i).getIdentifier());
409                     String paramValue = "";
410                     if (ObjectUtil.isNotNull(paramCollectionList3.get(i).getValue())){
411                         paramValue = paramCollectionList3.get(i).getValue().toString();
412                     }
413                     daParamCollection.setParamValue(paramValue);
414                     daParamCollection.setLocationCode(device);
415                     daParamCollection.setCollectionTime(new Date());
416                     saveParamList.add(daParamCollection);
417                 }
418             }
419             if (CollUtil.isNotEmpty(paramCollectionList4)){
420                 for (int i = 0; i < paramCollectionList4.size(); i++) {
421                     DaParamCollection daParamCollection = new DaParamCollection();
422                     if (CollUtil.isNotEmpty(keyCodeList) && keyCodeList.size() == 4){
423                         daParamCollection.setKeyCode(keyCodeList.get(3));
424                     }
425
426                     daParamCollection.setParamCode(paramCollectionList4.get(i).getIdentifier());
427                     String paramValue = "";
428                     if (ObjectUtil.isNotNull(paramCollectionList4.get(i).getValue())){
429                         paramValue = paramCollectionList4.get(i).getValue().toString();
430                     }
431                     daParamCollection.setParamValue(paramValue);
432                     daParamCollection.setLocationCode(device);
433                     daParamCollection.setCollectionTime(new Date());
434                     saveParamList.add(daParamCollection);
435                 }
436             }
437             if (CollUtil.isNotEmpty(paramCollectionList0)){
438                 for (int i = 0; i < paramCollectionList0.size(); i++) {
439                     if (CollUtil.isNotEmpty(keyCodeList) && keyCodeList.size() == 4){
440                         for (String s : keyCodeList) {
441                             DaParamCollection daParamCollection = new DaParamCollection();
442                             daParamCollection.setKeyCode(s);
443                             daParamCollection.setParamCode(paramCollectionList0.get(i).getIdentifier());
444                             String paramValue = "";
445                             if (ObjectUtil.isNotNull(paramCollectionList0.get(i).getValue())){
446                                 paramValue = paramCollectionList0.get(i).getValue().toString();
447                             }
448                             daParamCollection.setParamValue(paramValue);
449                             daParamCollection.setLocationCode(device);
450                             daParamCollection.setCollectionTime(new Date());
451                             saveParamList.add(daParamCollection);
452                         }
453                     }
454                 }
455             }
456                 daParamCollectionService.insertBatch(saveParamList);
457             }
458         return passingList;
459     }
460
461     /**
462      * 030工位返回ocv测试结果
463      * @param thoroughfare
464      * @param device
465      * @throws Exception
466      */
467     private boolean OCVResultFeedBack(String thoroughfare, String device) throws Exception {
468         boolean flag = true;
469         Object value1 = miloService.readFromOpcUa(thoroughfare + "." + device + ".CellCode_1").getValue();
470         if (ObjectUtil.isNotNull(value1)){
471             String string = value1.toString();
472             List<DaParamCollection> list = daParamCollectionService.list(new LambdaQueryWrapper<DaParamCollection>()
473                     .eq(DaParamCollection::getKeyCode, string));
474             if (CollUtil.isNotEmpty(list)){
475                 DaParamCollection daParamCollection = list.get(0);
476                 if (Constants.ONE.equals(daParamCollection.getState())){
477                     miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".CellStatus_1").value(1).build());
478                 }else {
479                     flag = false;
480                     miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".CellStatus_1").value(2).build());
481                 }
482             }
483         }
484         Object value2 = miloService.readFromOpcUa(thoroughfare + "." + device + ".CellCode_2").getValue();
485         if (ObjectUtil.isNotNull(value2)){
486             String string = value2.toString();
487             List<DaParamCollection> list = daParamCollectionService.list(new LambdaQueryWrapper<DaParamCollection>()
488                     .eq(DaParamCollection::getKeyCode, string));
489             if (CollUtil.isNotEmpty(list)){
490                 DaParamCollection paramCollection = list.get(0);
491                 if (Constants.ONE.equals(paramCollection.getState())){
492                     miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".CellStatus_2").value(1).build());
493                 }else {
494                     flag = false;
495                     miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".CellStatus_2").value(2).build());
496                 }
497             }
498         }
499         Object value3 = miloService.readFromOpcUa(thoroughfare + "." + device + ".CellCode_3").getValue();
500         if (ObjectUtil.isNotNull(value3)){
501             String string = value3.toString();
502             List<DaParamCollection> list = daParamCollectionService.list(new LambdaQueryWrapper<DaParamCollection>()
503                     .eq(DaParamCollection::getKeyCode, string));
504             if (CollUtil.isNotEmpty(list)){
505                 DaParamCollection paramCollection = list.get(0);
506                 if (Constants.ONE.equals(paramCollection.getState())){
507                     miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".CellStatus_3").value(1).build());
508                 }else {
509                     flag = false;
510                     miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".CellStatus_3").value(2).build());
511                 }
512             }
513         }
514         Object value4 = miloService.readFromOpcUa(thoroughfare + "." + device + ".CellCode_4").getValue();
515         if (ObjectUtil.isNotNull(value4)){
516             String string = value4.toString();
517             List<DaParamCollection> list = daParamCollectionService.list(new LambdaQueryWrapper<DaParamCollection>()
518                     .eq(DaParamCollection::getKeyCode, string));
519             if (CollUtil.isNotEmpty(list)){
520                 DaParamCollection paramCollection = list.get(0);
521                 if (Constants.ONE.equals(paramCollection.getState())){
522                     miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".CellStatus_4").value(1).build());
523                 }else {
524                     flag = false;
525                     miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".CellStatus_4").value(2).build());
526                 }
527             }
528         }
529         return flag;
530     }
531
532     /**
533      * 保存过站数据和参数保存数据
534      * @param device 工位号
535      * @throws Exception
536      */
537     private void saveParamPassingData(String device,String thoroughfare) throws Exception {
538
539         //保存参数采集数据
540         List<DaCollectionParamConf> list = collectionParamConfService.list(new LambdaQueryWrapper<DaCollectionParamConf>()
541                 .eq(DaCollectionParamConf::getProcessesCode, device)
542                 .eq(DaCollectionParamConf::getRemarks, Constants.ONE));
543         if (CollUtil.isNotEmpty(list)){
544             List<String> collect = list.stream().map(DaCollectionParamConf::getGatherAddress)
545                     .filter(Objects::nonNull).distinct()
546                     .collect(Collectors.toList());
547             QueryPLCData(device,thoroughfare,collect);
e6c837 548
A 549         }
550     }
551
09ef66 552     /**
W 553      * 批量查询plc站点数据
554      * @param device 站点
555      * @param collect 点位
556      * @throws Exception
557      */
558     private static void QueryPLCData(String device,String thoroughfare,List<String> collect) throws Exception {
559         List<ReadWriteEntity> readWriteEntityList = miloService.readFromOpcUa(collect);
560
561         if (CollUtil.isNotEmpty(readWriteEntityList)){
562             List<DaParamCollection> collectionList = new ArrayList<>();
563             Object value1 = miloService.readFromOpcUa(thoroughfare + "." + device + ".ModuleCode").getValue();
564             String moduleCode = null;
565             if (ObjectUtil.isNotNull(value1)){
566                 moduleCode = value1.toString();
567             }
568             for (int i = 0; i < readWriteEntityList.size(); i++) {
569                 DaParamCollection daParamCollection = new DaParamCollection();
570                 if (StrUtil.isNotBlank(moduleCode)){
571                     daParamCollection.setModuleCode(moduleCode);
572                 }
573                 daParamCollection.setParamCode(collect.get(i));
574                 String paramValue = "";
575                 if (ObjectUtil.isNotNull(readWriteEntityList.get(i).getValue())){
576                     paramValue = readWriteEntityList.get(i).getValue().toString();
577                 }
578                 daParamCollection.setParamValue(paramValue);
579                 daParamCollection.setLocationCode(device);
580                 daParamCollection.setCollectionTime(new Date());
581                 collectionList.add(daParamCollection);
582             }
583             daParamCollectionService.insertBatch(collectionList);
584         }
585     }
e6c837 586
A 587
588     /**
589      * 获取SNCode
590      */
591     public String getSNCode(){
592         String SNCode = "";
593         return SNCode;
594     }
595
596     /**
597      * 出站保存数据
598      */
599     public String outSaveDate(String thoroughfare,String device) {
600         String snCode = "";
601         String result = "";
602         String read = thoroughfare + "." + device + ".";
603
604         try {
605             if(nullList.stream().noneMatch(s -> s.equals(device))){
606                 if(moduleCodeList.stream().anyMatch(s -> s.equals(device))){
607                     read = read + "ModuleCode";
608                 }else {
609                     read = read + "PACKCode";
610                 }
611             }
612             snCode = miloService.readFromOpcUa(read).getValue().toString();
613
614             if(null == snCode || "".equals(snCode)){
615                 result = "22";
616             }else{
617
618                 String workOrderNo = "";
619                 String productCode = "";
620                 //2、保存过站采集数据
621                 Object orderNumberObject = miloService.readFromOpcUa(thoroughfare + "." + device + "." + "WorkOrderNumber").getValue();
622                 if(orderNumberObject!=null){
623                     workOrderNo = orderNumberObject.toString();
624                 }
625
626                 Object productCodeObject = miloService.readFromOpcUa(thoroughfare + "." + device + "." + "ProductType").getValue();
627                 if(productCodeObject!=null){
628                     productCode = productCodeObject.toString();
629                 }
630
631                 saveStationInfo(snCode,thoroughfare,device,workOrderNo,productCode);
632                 //3、保存参数采集数据
633                 SaveParamData(snCode,thoroughfare,device,workOrderNo,productCode);
634
635                 //如果是末尾工站要报工
636                 if(device.equals("OP500")){
637                     RestfulService.getWorkReportResultFeedback(snCode,device,format.format(new Date()));
638                     //1、更新工单信息
639                     updateOrderInfo(snCode);
640                 }
641
642                 result = "21";
643
644             }
645
646         }catch (Exception e) {
647             System.out.println(e.getMessage());
648         }
649         return result;
650     }
651
652
653 //    /**
654 //     * 出站保存数据
655 //     */
656 //    public String outSaveDate(String thoroughfare,String device) {
657 //        String result = "";
658 //        try {
659 //            //读取SNCode
660 //            String PACKCode = thoroughfare + "." + device + ".PACKCode";
661 //            Object PACKCodeObject = miloService.readFromOpcUa(PACKCode).getValue();
662 //            if(null == PACKCodeObject || "".equals(PACKCodeObject)){
663 //               result = "22";
664 //            }else{
665 //                String PACKCodeParam = PACKCodeObject.toString();
666 //                //1、更新工单信息
667 //                //updateOrderInfo();
668 //                //2、保存过站采集数据
669 //                saveStationInfo(PACKCodeParam,thoroughfare,device);
670 //                //3、保存参数采集数据
671 //                SaveParamData(PACKCodeParam,thoroughfare,device,"","");
672 //
673 //                result = "21";
674 //
675 //            }
676 //
677 //        }catch (Exception e) {
678 //
679 //        }
680 //        return result;
681 //    }
682
683     /**
684      * 保存过站采集
685      */
686     public void saveStationInfo(String packCode,String thoroughfare,String device,String workOrderNo,String productCode) throws Exception {
687         SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
688         sdf.setTimeZone(TimeZone.getTimeZone("GMT+8")); // CST通常表示中国标准时间,即东八区
689         String prefix = thoroughfare+"."+device+".";
690         String startTime = miloService.readFromOpcUa(prefix + "StartTime").getValue().toString();
691         String stopTime = miloService.readFromOpcUa(prefix + "StopTime").getValue().toString();
692         String stationStatus = miloService.readFromOpcUa(prefix + "StationStatus").getValue().toString();
693
694         DaPassingStationCollection daPassingStationCollection = new DaPassingStationCollection();
695         daPassingStationCollection.setSfcCode(packCode);
696         daPassingStationCollection.setWorkOrderNo(workOrderNo);
697         daPassingStationCollection.setProductCode(productCode);
698         daPassingStationCollection.setLocationCode(device);
699         String strt = TimeUtil.stringProcessing(startTime);
700         String end = TimeUtil.stringProcessing(stopTime);
701         daPassingStationCollection.setInboundTime(sdf.parse(strt));//入站时间
702         daPassingStationCollection.setOutboundTime(sdf.parse(end));//出站时间
703         daPassingStationCollection.setOutRsSign(stationStatus);//出站是否合格
704         daPassingStationCollectionService.insertDaPassingStationCollection(daPassingStationCollection);
705     }
706
707     public static void SaveParamData(String packCode,String thoroughfare,String device,String workOrderNo,String productType) throws Exception {
708         List<DaCollectionParamConf> list;
709         DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf();
710         daCollectionParamConf.setGatherAddress(thoroughfare+ "." + device);
711         list = collectionParamConfService.selectDaCollectionParamConfList(daCollectionParamConf);
712
713         List<String> nodeIdList = list.stream().map(info -> {
714             String nodeid = info.getGatherAddress();
715             return nodeid;
716         }).collect(Collectors.toList());
717
718         if(!nodeIdList.isEmpty()){
719             List<ReadWriteEntity> readWriteEntityList = miloService.readFromOpcUa(nodeIdList);
720             for (int i = 0; i < readWriteEntityList.size(); i++) {
721                 if(readWriteEntityList.get(i).getValue() == null){
722                     readWriteEntityList.get(i).setValue(" ");
723                 }
724             }
725             List<DaParamCollection> daParamCollectionlist = new ArrayList<>();
726             for(int i=0;i<nodeIdList.size();i++){
727                 if(!readWriteEntityList.get(i).getValue().toString().equals("0.0")){
728                     String tt = readWriteEntityList.get(i).getValue().toString();
729                     DaParamCollection ParamCollection = new DaParamCollection();
730                     ParamCollection.setParamCode(list.get(i).getCollectParameterId());
731                     ParamCollection.setLocationCode(device);
732                     if(tt.contains("Time")){
733 //                        String str = TimeUtil.getTimestamp(TimeUtil.stringProcessing(tt));
734 //                        ParamCollection.setParamValue(str);
735                         ParamCollection.setParamValue(TimeUtil.getYyyyDdMm(TimeUtil.stringProcessing(tt)));
736                     }else {
737                         ParamCollection.setParamValue(readWriteEntityList.get(i).getValue().toString());
738                     }
739                     ParamCollection.setSfcCode(packCode);
740                     if(device.contains("OP360")){
741                         String node = nodeIdList.get(i);
742                         Object value = miloService.readFromOpcUa("PACK.OP360.ModuleSNCode"+node.charAt(node.length()-1)).getValue();
743                         if(value!=null){
744                             ParamCollection.setModuleCode(value.toString());
745                         }
746                     }
747                     ParamCollection.setParamName(list.get(i).getCollectParameterName());
748                     ParamCollection.setParamUpper(list.get(i).getParamUpper());
749                     ParamCollection.setParamLower(list.get(i).getParamLower());
750                     ParamCollection.setUnit(list.get(i).getCollectParameterUnit());
751                     //ParamCollection.setState("合格");
752                     ParamCollection.setType(list.get(i).getCollectParameterType());
753                     ParamCollection.setCollectionTime(new Date());
754                     ParamCollection.setWorkOrderNo(workOrderNo);
755                     ParamCollection.setProductCode(productType);
756                     daParamCollectionlist.add(ParamCollection);
757 //                    daParamCollectionService.insertDaParamCollection(ParamCollection);
758                 }
759             }
760             daParamCollectionService.saveBeachDaParamCollection(daParamCollectionlist);
761         }
762         addBaseData(workOrderNo,productType,device,packCode);
763
764     }
765
766     public static void addBaseData(String workOrderNo,String productCode,String locationCode,String packCode){
767         Map<String, String> map = new HashMap<>();
768         map.put("GC", "南浔工厂");
769         map.put("CXBH", "Pack线");
770         map.put("SBBH", "设备001");
771         map.put("YGBH", "员工001");
772         List<DaParamCollection> confList = new ArrayList<>();
773         map.forEach((key, value) -> {
774             List<DaCollectionParamConf> daCollectionParamConfs = collectionParamConfService.list(new LambdaQueryWrapper<DaCollectionParamConf>()
775                     .eq(DaCollectionParamConf::getProcessesCode,locationCode)
09ef66 776                     .like(DaCollectionParamConf::getCollectParameterId,key)
W 777                     .eq(DaCollectionParamConf::getRemarks,Constants.ONE));
e6c837 778             DaParamCollection saveData = new DaParamCollection();
A 779             saveData.setWorkOrderNo(workOrderNo);
780             saveData.setProductCode(productCode);
781             saveData.setLocationCode(locationCode);
782             saveData.setSfcCode(packCode);
783             saveData.setParamCode(daCollectionParamConfs.get(0).getCollectParameterId());
784             saveData.setParamName(daCollectionParamConfs.get(0).getCollectParameterName());
785             saveData.setCollectionTime(new Date());
786             saveData.setParamValue(value);
787             confList.add(saveData);
788         });
789         daParamCollectionService.insertBatch(confList);
790     }
791
792     public static void getFactoryOrder(String locationCode){
793         String productionWorkOrder = RestfulService.getProductionWorkOrderRequest("", locationCode);
794         JSONObject jsonObject = new JSONObject(productionWorkOrder);
795         JSONObject dataObject = jsonObject.getJSONObject("data");
796
797         String productNum = dataObject.getStr("productNum");
798         String stationCode = dataObject.getStr("stationCode");
799         String materialCode = dataObject.getStr("materialCode");
800         String productionOrderNum = dataObject.getStr("productionOrderNum");
801
802         OmProductionOrdeInfo omProductionOrdeInfo = new OmProductionOrdeInfo();
803         omProductionOrdeInfo.setProductNum(productNum);
804         omProductionOrdeInfo.setWorkOrderNo(productionOrderNum);
805         omProductionOrdeInfo.setStationCode(stationCode);
806         omProductionOrdeInfo.setProductCode(materialCode);
807         omProductionOrdeInfoService.save(omProductionOrdeInfo);
808     }
809
810     public static void updateOrderInfo(String packCode){
811         OmProductionOrdeInfo one = omProductionOrdeInfoService.getOne(new LambdaQueryWrapper<OmProductionOrdeInfo>().eq(OmProductionOrdeInfo::getProductNum, packCode));
812         one.setOrderStatus("5");
813         omProductionOrdeInfoService.saveOrUpdate(one);
814     }
815
816     public static void avgFunction(){
817
818     }
819
820     public static String TightenTheConversionOkNg(String param){
821         // 去除首尾的方括号,然后按照逗号分割字符串
822         String[] parts = param.substring(1, param.length() - 1).split(",");
823
824         // 创建一个新的StringBuilder来构建替换后的字符串
825         StringBuilder sb = new StringBuilder();
826         sb.append('['); // 添加左方括号
827
828         for (int i = 0; i < parts.length; i++) {
829             String part = parts[i].trim(); // 去除可能的空格
830             float value;
831             try {
832                 value = Float.parseFloat(part); // 尝试将字符串转换为浮点数
833                 String replacement;
834                 if (value == 1f) {
835                     replacement = "OK";
836                 } else if (value == 2f) {
837                     replacement = "NG";
838                 } else {
839                     replacement = part; // 如果不是1或2,则保持不变
840                 }
841                 sb.append(replacement);
842                 if (i < parts.length - 1) {
843                     sb.append(','); // 添加逗号(除了最后一个元素)
844                 }
845             } catch (NumberFormatException e) {
846                 // 如果转换失败,则保持原样(或进行其他错误处理)
847                 sb.append(part);
848                 if (i < parts.length - 1) {
849                     sb.append(',');
850                 }
851             }
852         }
853         sb.append(']'); // 添加右方括号
854         return sb.toString();
855     }
856
857
858     /**
859      * 校验是否存在NG
860      * @param packCode sfcCode
861      * @return boolean
862      */
863     public boolean checkIsNG(String packCode,String modelCode){
864         List<DaPassingStationCollection> list = daPassingStationCollectionService.list(new LambdaQueryWrapper<DaPassingStationCollection>()
865                 .eq(StrUtil.isNotBlank(packCode),DaPassingStationCollection::getSfcCode, packCode)
866                 .eq(DaPassingStationCollection::getOutRsSign, Constants.UN_PASS));
867         return CollUtil.isEmpty(list);
868     }
869
870 }