-
admin
2024-05-13 288790d3b02242a073769db87a20f0912adcdf56
提交 | 用户 | 时间
a5b351 1 package com.jcdm.main.plcserver.sub;
C 2
3
8f0f8d 4 import cn.hutool.core.date.DateUtil;
e70fb4 5 import cn.hutool.json.JSONObject;
8f0f8d 6 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
7 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
288790 8 import com.jcdm.common.utils.StringUtils;
49c784 9 import com.jcdm.framework.websocket.WebSocketUsers;
a5b351 10 import com.jcdm.main.da.collectionParamConf.domain.DaCollectionParamConf;
C 11 import com.jcdm.main.da.collectionParamConf.service.IDaCollectionParamConfService;
12 import com.jcdm.main.da.paramCollection.domain.DaParamCollection;
13 import com.jcdm.main.da.paramCollection.service.IDaParamCollectionService;
14 import com.jcdm.main.da.passingStationCollection.domain.DaPassingStationCollection;
15 import com.jcdm.main.da.passingStationCollection.service.IDaPassingStationCollectionService;
e70fb4 16 import com.jcdm.main.om.productionOrde.domain.OmProductionOrdeInfo;
A 17 import com.jcdm.main.om.productionOrde.service.IOmProductionOrdeInfoService;
a5b351 18 import com.jcdm.main.plcserver.conf.OPCElement;
c5e9c5 19 import com.jcdm.main.plcserver.util.TimeUtil;
e70fb4 20 import com.jcdm.main.restful.factoryMes.service.RestfulService;
a5b351 21 import com.kangaroohy.milo.model.ReadWriteEntity;
C 22 import com.kangaroohy.milo.runner.subscription.SubscriptionCallback;
23 import com.kangaroohy.milo.service.MiloService;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.stereotype.Component;
26
49c784 27 import javax.websocket.Session;
c5e9c5 28 import java.text.SimpleDateFormat;
32483a 29 import java.util.*;
a5b351 30 import java.util.stream.Collectors;
C 31
32
33 @Component
34 public class OPCUaSubscription implements SubscriptionCallback {
e70fb4 35
A 36     public SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
32483a 37
5316c5 38     //新自动工站
d545f1 39     public List<String> automaticList = Arrays.asList("OP280","OP310A","OP310B", "OP300A","OP300B", "OP320A","OP320B", "OP340A","OP340B", "OP350A","OP350B", "OP360","OP365","OP370", "OP390", "OP470");
5316c5 40
7a0867 41     //采集模组工位 ModuleCode
3581b1 42     public List<String> moduleCodeList = Arrays.asList("OP300A","OP300B","OP320A","OP320B","OP340A","OP340B","OP365", "OP350A", "OP350B", "OP330");
a5b351 43
7a0867 44     //区分模组型号工位
A 45     public List<String> distinguishingModules = Arrays.asList("OP300","OP310", "OP340", "OP350", "OP330");
46
4a5f2a 47     //区分小车码
A 48     public List<String> agvId = Arrays.asList("OP360", "OP390", "OP470","OP280");
49
5316c5 50
A 51     //区分pack型号的工位
7a0867 52     public List<String> packIdList = Arrays.asList("OP280","OP360","OP390","OP470");
A 53
288790 54     //测试设备手动工位
A 55     public List<String> testList = Arrays.asList("OP430-1","OP430-2","OP430-3","OP430-4","OP480-1","OP480-2","OP480-3","OP440");
56
8f0f8d 57
63b254 58     //空的
8f0f8d 59     public List<String> nullList = Arrays.asList("OP250","OP260");
60
61
a5b351 62     public static MiloService miloService;
49c784 63
64     Map<String, Session> map = WebSocketUsers.getUsers();
a5b351 65
C 66     public IDaPassingStationCollectionService daPassingStationCollectionService;
67
054a69 68     public static IDaCollectionParamConfService collectionParamConfService;
a5b351 69
054a69 70     public static IDaParamCollectionService daParamCollectionService;
a5b351 71
e70fb4 72     public static IOmProductionOrdeInfoService omProductionOrdeInfoService;
A 73
a5b351 74     public OPCUaSubscription(MiloService miloService,
C 75                              IDaPassingStationCollectionService daPassingStationCollectionService,
76                              IDaCollectionParamConfService collectionParamConfService,
e70fb4 77                              IDaParamCollectionService daParamCollectionService,
A 78                              IOmProductionOrdeInfoService omProductionOrdeInfoService) {
a5b351 79         OPCUaSubscription.miloService = miloService;
C 80         this.daPassingStationCollectionService = daPassingStationCollectionService;
8f0f8d 81         OPCUaSubscription.collectionParamConfService = collectionParamConfService;
82         OPCUaSubscription.daParamCollectionService = daParamCollectionService;
e70fb4 83         OPCUaSubscription.omProductionOrdeInfoService = omProductionOrdeInfoService;
a5b351 84     }
C 85
86
87     @Override
88     public void onSubscribe(String identifier, Object value) {
89
90         try {
70cc26 91             if(null != value && Integer.valueOf(value.toString())!= 0) {
a5b351 92                 String[] nodes = identifier.split("[.]");
C 93                 String thoroughfare = nodes[0];//通道
94                 String device = nodes[1];//设备
95                 String tab = nodes[2];//标记
96                 String tabVlaue = value.toString();//地址值
97
288790 98                 if (("RecordData").equals(tab)) {
a5b351 99                     String recordDataDoneValue = "";
C 100
101                     if("1".equals(tabVlaue)){
89f416 102
e70fb4 103                         //请求工单
A 104 //                        if(device.equals("OP230")){
105 //                            getFactoryOrder("OP230");
106 //                        }
107
108                         //自动工位
5316c5 109                         if(automaticList.stream().anyMatch(s -> s.equals(device))){
A 110                             //plc给我们一个模组码,拿模组码校验出型号
288790 111                             if(moduleCodeList.stream().anyMatch(s -> s.equals(device))) {
A 112                                 String RecordDataDoneAddress = thoroughfare + "." + device + ".RecordDataDone";
5316c5 113                                 //读模组码
288790 114                                 Object moduleCode = miloService.readFromOpcUa(thoroughfare + "." + device + ".ModuleCode").getValue();
A 115
116                                 if(moduleCode!=null){
06713a 117                                     miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(11).build());
288790 118                                 }else {
A 119                                     miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(12).build());
120                                 }
7a0867 121                             }else {
4a5f2a 122                                 if(agvId.stream().anyMatch(s -> s.equals(device))){
288790 123                                     Object agvIdObject = miloService.readFromOpcUa(thoroughfare + "." + device + ".AGVID").getValue();
A 124                                     //agvId 小车码是否为空
125                                     if(agvIdObject!=null){
126                                         String PACKCode = thoroughfare + "." + device + ".MPACKCode";
127                                         OmProductionOrdeInfo one = omProductionOrdeInfoService.getOne(new LambdaQueryWrapper<OmProductionOrdeInfo>().eq(OmProductionOrdeInfo::getTrolleyYard, agvIdObject.toString()));
128                                         //小车码查找工单是否为空
129                                         if(one!=null){
130                                             String packId = one.getProductNum();
131                                             miloService.writeToOpcUa(ReadWriteEntity.builder().identifier(PACKCode).value(packId).build());
132                                             miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(11).build());
133                                         }else {
134                                             miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(12).build());
135                                         }
136                                     }else {
4a5f2a 137                                         miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(12).build());
A 138                                     }
7a0867 139
288790 140                                 }else {
A 141                                     String RecordDataDoneAddress = thoroughfare + "." + device + ".RecordDataDone";
142                                     Object packCodeObject = miloService.readFromOpcUa(thoroughfare + "." + device + ".PACKCode").getValue();
143                                     if(packCodeObject!=null){
144                                         //pack 如果区分型号的话就要处理
145                                         //记录数据完成
146                                         miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(11).build());
147                                     }else {
148                                         miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(12).build());
149                                     }
150
151                                 }
e70fb4 152                             }
A 153                         }else {
154                             //手动工位处理逻辑
288790 155                             WebSocketUsers.sendMessageToUserByText(map.get(device), "IN");
e70fb4 156                             String RecordDataDoneAddress = thoroughfare + "." + device + ".RecordDataDone";
A 157
158                             miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(11).build());
32483a 159                         }
e70fb4 160
7bff29 161
a5b351 162                     }else if("2".equals(tabVlaue)){
32483a 163                         if(automaticList.stream().anyMatch(s -> s.equals(device))){
164                             //自动工位
165                             //出站保存数据
166                             recordDataDoneValue = outSaveDate(thoroughfare,device);
167                             //记录数据完成
168                             String RecordDataDoneAddress = thoroughfare + "." + device + ".RecordDataDone";
70cc26 169                             miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(Integer.valueOf(recordDataDoneValue)).build());
32483a 170                         }else {
171                             //手动工位
1c84ae 172                             if(device.contains("OP330")){
A 173                                 String str = device.substring(0,5);
174                                 WebSocketUsers.sendMessageToUserByText(map.get(str), "END");
175                             }else {
176                                 WebSocketUsers.sendMessageToUserByText(map.get(device), "END");
288790 177                             }
A 178                             if(testList.stream().anyMatch(s -> s.equals(device))){
179                                 String RecordDataDoneAddress = thoroughfare + "." + device + ".RecordDataDone";
180                                 miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(21).build());
1c84ae 181                             }
32483a 182                         }
3c2299 183                     }else {
184                         System.out.println("^");
185                     }
186                 }
187                 //保存拧紧数据
188                 else if (("AngleResult").equals(tab)) {
b5fcd8 189                     if("1".equals(tabVlaue)||"2".equals(tabVlaue)){
32483a 190                         List<String> list = new ArrayList<>();
0caf87 191                         String[] suffixes = {"Torque", "Angle", "TorqueResult", "AngleResult"};
32483a 192
193                         for (String suffix : suffixes) {
194                             String string = thoroughfare + "." + device + "." + suffix;
195                             list.add(string);
196                         }
49c784 197                         List<ReadWriteEntity> list1 = miloService.readFromOpcUa(list);
198                         List<Object> collect = list1.stream().map(ReadWriteEntity::getValue).collect(Collectors.toList());
199                         String joinedString = String.join(",", collect.toString());
4a5f2a 200                         WebSocketUsers.sendMessageToUserByText(map.get(device), TightenTheConversionOkNg(joinedString));
a5b351 201                     }
C 202                 }
203             }
204         } catch (Exception e) {
205
206         }
207     }
208
209
210
211     /**
212      * 获取SNCode
213      */
214     public String getSNCode(){
215         String SNCode = "";
216         return SNCode;
217     }
218
219     /**
220      * 出站保存数据
221      */
222     public String outSaveDate(String thoroughfare,String device) {
8f0f8d 223         String snCode = "";
a5b351 224         String result = "";
8f0f8d 225         String read = thoroughfare + "." + device + ".";
226
a5b351 227         try {
8f0f8d 228             if(nullList.stream().noneMatch(s -> s.equals(device))){
229                 if(moduleCodeList.stream().anyMatch(s -> s.equals(device))){
230                     read = read + "ModuleCode";
231                 }else {
232                     read = read + "PACKCode";
233                 }
234             }
235             snCode = miloService.readFromOpcUa(read).getValue().toString();
236
237             if(null == snCode || "".equals(snCode)){
238                 result = "22";
a5b351 239             }else{
8f0f8d 240
e70fb4 241                 String workOrderNo = "";
A 242                 String productCode = "";
243                 //2、保存过站采集数据
244                 Object orderNumberObject = miloService.readFromOpcUa(thoroughfare + "." + device + "." + "WorkOrderNumber").getValue();
245                 if(orderNumberObject!=null){
246                     workOrderNo = orderNumberObject.toString();
247                 }
248
249                 Object productCodeObject = miloService.readFromOpcUa(thoroughfare + "." + device + "." + "ProductType").getValue();
250                 if(productCodeObject!=null){
251                     productCode = productCodeObject.toString();
252                 }
89f416 253
8f0f8d 254                 saveStationInfo(snCode,thoroughfare,device,workOrderNo,productCode);
a5b351 255                 //3、保存参数采集数据
8f0f8d 256                 SaveParamData(snCode,thoroughfare,device,workOrderNo,productCode);
e70fb4 257
A 258                 //如果是末尾工站要报工
259                 if(device.equals("OP500")){
260                     RestfulService.getWorkReportResultFeedback(snCode,device,format.format(new Date()));
261                     //1、更新工单信息
262                     updateOrderInfo(snCode);
263                 }
a5b351 264
C 265                 result = "21";
266
267             }
268
269         }catch (Exception e) {
8f0f8d 270             System.out.println(e.getMessage());
a5b351 271         }
C 272         return result;
273     }
274
8f0f8d 275
276 //    /**
277 //     * 出站保存数据
278 //     */
279 //    public String outSaveDate(String thoroughfare,String device) {
280 //        String result = "";
281 //        try {
282 //            //读取SNCode
283 //            String PACKCode = thoroughfare + "." + device + ".PACKCode";
284 //            Object PACKCodeObject = miloService.readFromOpcUa(PACKCode).getValue();
285 //            if(null == PACKCodeObject || "".equals(PACKCodeObject)){
286 //               result = "22";
287 //            }else{
288 //                String PACKCodeParam = PACKCodeObject.toString();
289 //                //1、更新工单信息
290 //                //updateOrderInfo();
291 //                //2、保存过站采集数据
292 //                saveStationInfo(PACKCodeParam,thoroughfare,device);
293 //                //3、保存参数采集数据
294 //                SaveParamData(PACKCodeParam,thoroughfare,device,"","");
295 //
296 //                result = "21";
297 //
298 //            }
299 //
300 //        }catch (Exception e) {
301 //
302 //        }
303 //        return result;
304 //    }
305
a5b351 306     /**
C 307      * 保存过站采集
308      */
8f0f8d 309     public void saveStationInfo(String packCode,String thoroughfare,String device,String workOrderNo,String productCode) throws Exception {
c5e9c5 310         SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
311         sdf.setTimeZone(TimeZone.getTimeZone("GMT+8")); // CST通常表示中国标准时间,即东八区
32483a 312         String prefix = thoroughfare+"."+device+".";
c5e9c5 313         String startTime = miloService.readFromOpcUa(prefix + "StartTime").getValue().toString();
314         String stopTime = miloService.readFromOpcUa(prefix + "StopTime").getValue().toString();
32483a 315         String stationStatus = miloService.readFromOpcUa(prefix + "StationStatus").getValue().toString();
316
a5b351 317         DaPassingStationCollection daPassingStationCollection = new DaPassingStationCollection();
32483a 318         daPassingStationCollection.setSfcCode(packCode);
319         daPassingStationCollection.setWorkOrderNo(workOrderNo);
8f0f8d 320         daPassingStationCollection.setProductCode(productCode);
321         daPassingStationCollection.setLocationCode(device);
c5e9c5 322         String strt = TimeUtil.stringProcessing(startTime);
323         String end = TimeUtil.stringProcessing(stopTime);
324         daPassingStationCollection.setInboundTime(sdf.parse(strt));//入站时间
325         daPassingStationCollection.setOutboundTime(sdf.parse(end));//出站时间
32483a 326         daPassingStationCollection.setOutRsSign(stationStatus);//出站是否合格
a5b351 327         daPassingStationCollectionService.insertDaPassingStationCollection(daPassingStationCollection);
C 328     }
329
054a69 330     public static void SaveParamData(String packCode,String thoroughfare,String device,String workOrderNo,String productType) throws Exception {
a5b351 331         List<DaCollectionParamConf> list;
C 332         DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf();
333         daCollectionParamConf.setGatherAddress(thoroughfare+ "." + device);
334         list = collectionParamConfService.selectDaCollectionParamConfList(daCollectionParamConf);
335
336         List<String> nodeIdList = list.stream().map(info -> {
337             String nodeid = info.getGatherAddress();
338             return nodeid;
339         }).collect(Collectors.toList());
340
341         if(!nodeIdList.isEmpty()){
342             List<ReadWriteEntity> readWriteEntityList = miloService.readFromOpcUa(nodeIdList);
e70fb4 343             for (int i = 0; i < readWriteEntityList.size(); i++) {
A 344                 if(readWriteEntityList.get(i).getValue() == null){
345                     readWriteEntityList.get(i).setValue(" ");
346                 }
347             }
a5b351 348             List<DaParamCollection> daParamCollectionlist = new ArrayList<>();
C 349             for(int i=0;i<nodeIdList.size();i++){
350                 if(!readWriteEntityList.get(i).getValue().toString().equals("0.0")){
c5e9c5 351                     String tt = readWriteEntityList.get(i).getValue().toString();
32483a 352                     DaParamCollection ParamCollection = new DaParamCollection();
353                     ParamCollection.setParamCode(list.get(i).getCollectParameterId());
a5b351 354                     ParamCollection.setLocationCode(device);
c5e9c5 355                     if(tt.contains("Time")){
288790 356 //                        String str = TimeUtil.getTimestamp(TimeUtil.stringProcessing(tt));
A 357 //                        ParamCollection.setParamValue(str);
358                         ParamCollection.setParamValue(TimeUtil.getYyyyDdMm(TimeUtil.stringProcessing(tt)));
c5e9c5 359                     }else {
360                         ParamCollection.setParamValue(readWriteEntityList.get(i).getValue().toString());
361                     }
32483a 362                     ParamCollection.setSfcCode(packCode);
288790 363                     if(device.contains("OP360")){
A 364                         String node = nodeIdList.get(i);
365                         Object value = miloService.readFromOpcUa("PACK.OP360.ModuleSNCode"+node.charAt(node.length()-1)).getValue();
366                         if(value!=null){
367                             ParamCollection.setModuleCode(value.toString());
368                         }
369                     }
a5b351 370                     ParamCollection.setParamName(list.get(i).getCollectParameterName());
C 371                     ParamCollection.setParamUpper(list.get(i).getParamUpper());
372                     ParamCollection.setParamLower(list.get(i).getParamLower());
373                     ParamCollection.setUnit(list.get(i).getCollectParameterUnit());
374                     //ParamCollection.setState("合格");
375                     ParamCollection.setType(list.get(i).getCollectParameterType());
376                     ParamCollection.setCollectionTime(new Date());
054a69 377                     ParamCollection.setWorkOrderNo(workOrderNo);
378                     ParamCollection.setProductCode(productType);
379                     daParamCollectionlist.add(ParamCollection);
380 //                    daParamCollectionService.insertDaParamCollection(ParamCollection);
a5b351 381                 }
C 382             }
054a69 383             daParamCollectionService.saveBeachDaParamCollection(daParamCollectionlist);
a5b351 384         }
8f0f8d 385         addBaseData(workOrderNo,productType,device,packCode);
a5b351 386
C 387     }
8f0f8d 388
389     public static void addBaseData(String workOrderNo,String productCode,String locationCode,String packCode){
390         Map<String, String> map = new HashMap<>();
391         map.put("GC", "南浔工厂");
392         map.put("CXBH", "Pack线");
393         map.put("SBBH", "设备001");
394         map.put("YGBH", "员工001");
395         List<DaParamCollection> confList = new ArrayList<>();
396         map.forEach((key, value) -> {
397             List<DaCollectionParamConf> daCollectionParamConfs = collectionParamConfService.list(new LambdaQueryWrapper<DaCollectionParamConf>()
398                     .eq(DaCollectionParamConf::getProcessesCode,locationCode)
399                     .like(DaCollectionParamConf::getCollectParameterId,key));
400             DaParamCollection saveData = new DaParamCollection();
401             saveData.setWorkOrderNo(workOrderNo);
402             saveData.setProductCode(productCode);
403             saveData.setLocationCode(locationCode);
404             saveData.setSfcCode(packCode);
405             saveData.setParamCode(daCollectionParamConfs.get(0).getCollectParameterId());
406             saveData.setParamName(daCollectionParamConfs.get(0).getCollectParameterName());
407             saveData.setCollectionTime(new Date());
408             saveData.setParamValue(value);
409             confList.add(saveData);
410         });
411         daParamCollectionService.insertBatch(confList);
412     }
e70fb4 413
A 414     public static void getFactoryOrder(String locationCode){
415         String productionWorkOrder = RestfulService.getProductionWorkOrderRequest("", locationCode);
416         JSONObject jsonObject = new JSONObject(productionWorkOrder);
417         JSONObject dataObject = jsonObject.getJSONObject("data");
418
419         String productNum = dataObject.getStr("productNum");
420         String stationCode = dataObject.getStr("stationCode");
421         String materialCode = dataObject.getStr("materialCode");
422         String productionOrderNum = dataObject.getStr("productionOrderNum");
423
424         OmProductionOrdeInfo omProductionOrdeInfo = new OmProductionOrdeInfo();
425         omProductionOrdeInfo.setProductNum(productNum);
426         omProductionOrdeInfo.setWorkOrderNo(productionOrderNum);
427         omProductionOrdeInfo.setStationCode(stationCode);
428         omProductionOrdeInfo.setProductCode(materialCode);
429         omProductionOrdeInfoService.save(omProductionOrdeInfo);
430     }
431
432     public static void updateOrderInfo(String packCode){
433         OmProductionOrdeInfo one = omProductionOrdeInfoService.getOne(new LambdaQueryWrapper<OmProductionOrdeInfo>().eq(OmProductionOrdeInfo::getProductNum, packCode));
434         one.setOrderStatus("5");
435         omProductionOrdeInfoService.saveOrUpdate(one);
436     }
437
4a5f2a 438     public static void avgFunction(){
A 439
440     }
441
442     public static String TightenTheConversionOkNg(String param){
443         // 去除首尾的方括号,然后按照逗号分割字符串
444         String[] parts = param.substring(1, param.length() - 1).split(",");
445
446         // 创建一个新的StringBuilder来构建替换后的字符串
447         StringBuilder sb = new StringBuilder();
448         sb.append('['); // 添加左方括号
449
450         for (int i = 0; i < parts.length; i++) {
451             String part = parts[i].trim(); // 去除可能的空格
452             float value;
453             try {
454                 value = Float.parseFloat(part); // 尝试将字符串转换为浮点数
455                 String replacement;
456                 if (value == 1f) {
457                     replacement = "OK";
458                 } else if (value == 2f) {
459                     replacement = "NG";
460                 } else {
461                     replacement = part; // 如果不是1或2,则保持不变
462                 }
463                 sb.append(replacement);
464                 if (i < parts.length - 1) {
465                     sb.append(','); // 添加逗号(除了最后一个元素)
466                 }
467             } catch (NumberFormatException e) {
468                 // 如果转换失败,则保持原样(或进行其他错误处理)
469                 sb.append(part);
470                 if (i < parts.length - 1) {
471                     sb.append(',');
472                 }
473             }
474         }
475         sb.append(']'); // 添加右方括号
476         return sb.toString();
477     }
478
a5b351 479 }