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