-
admin
2024-05-09 06713afdb80bc876ffc6a09442049a08eb8a2364
提交 | 用户 | 时间
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     //新自动工站
3581b1 38     public List<String> automaticList = Arrays.asList("OP280", "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                             //手动工位
179                             WebSocketUsers.sendMessageToUserByText(map.get(device), "END");
180                         }
3c2299 181                     }else {
182                         System.out.println("^");
183                     }
184                 }
185                 //保存拧紧数据
186                 else if (("AngleResult").equals(tab)) {
b5fcd8 187                     if("1".equals(tabVlaue)||"2".equals(tabVlaue)){
32483a 188                         List<String> list = new ArrayList<>();
0caf87 189                         String[] suffixes = {"Torque", "Angle", "TorqueResult", "AngleResult"};
32483a 190
191                         for (String suffix : suffixes) {
192                             String string = thoroughfare + "." + device + "." + suffix;
193                             list.add(string);
194                         }
49c784 195                         List<ReadWriteEntity> list1 = miloService.readFromOpcUa(list);
196                         List<Object> collect = list1.stream().map(ReadWriteEntity::getValue).collect(Collectors.toList());
197                         String joinedString = String.join(",", collect.toString());
4a5f2a 198                         WebSocketUsers.sendMessageToUserByText(map.get(device), TightenTheConversionOkNg(joinedString));
a5b351 199                     }
C 200                 }
201             }
202         } catch (Exception e) {
203
204         }
205     }
206
207
208
209     /**
210      * 获取SNCode
211      */
212     public String getSNCode(){
213         String SNCode = "";
214         return SNCode;
215     }
216
217     /**
218      * 出站保存数据
219      */
220     public String outSaveDate(String thoroughfare,String device) {
8f0f8d 221         String snCode = "";
a5b351 222         String result = "";
8f0f8d 223         String read = thoroughfare + "." + device + ".";
224
a5b351 225         try {
8f0f8d 226             if(nullList.stream().noneMatch(s -> s.equals(device))){
227                 if(moduleCodeList.stream().anyMatch(s -> s.equals(device))){
228                     read = read + "ModuleCode";
229                 }else {
230                     read = read + "PACKCode";
231                 }
232             }
233             snCode = miloService.readFromOpcUa(read).getValue().toString();
234
235             if(null == snCode || "".equals(snCode)){
236                 result = "22";
a5b351 237             }else{
8f0f8d 238
e70fb4 239                 String workOrderNo = "";
A 240                 String productCode = "";
241                 //2、保存过站采集数据
242                 Object orderNumberObject = miloService.readFromOpcUa(thoroughfare + "." + device + "." + "WorkOrderNumber").getValue();
243                 if(orderNumberObject!=null){
244                     workOrderNo = orderNumberObject.toString();
245                 }
246
247                 Object productCodeObject = miloService.readFromOpcUa(thoroughfare + "." + device + "." + "ProductType").getValue();
248                 if(productCodeObject!=null){
249                     productCode = productCodeObject.toString();
250                 }
89f416 251
8f0f8d 252                 saveStationInfo(snCode,thoroughfare,device,workOrderNo,productCode);
a5b351 253                 //3、保存参数采集数据
8f0f8d 254                 SaveParamData(snCode,thoroughfare,device,workOrderNo,productCode);
e70fb4 255
A 256                 //如果是末尾工站要报工
257                 if(device.equals("OP500")){
258                     RestfulService.getWorkReportResultFeedback(snCode,device,format.format(new Date()));
259                     //1、更新工单信息
260                     updateOrderInfo(snCode);
261                 }
a5b351 262
C 263                 result = "21";
264
265             }
266
267         }catch (Exception e) {
8f0f8d 268             System.out.println(e.getMessage());
a5b351 269         }
C 270         return result;
271     }
272
8f0f8d 273
274 //    /**
275 //     * 出站保存数据
276 //     */
277 //    public String outSaveDate(String thoroughfare,String device) {
278 //        String result = "";
279 //        try {
280 //            //读取SNCode
281 //            String PACKCode = thoroughfare + "." + device + ".PACKCode";
282 //            Object PACKCodeObject = miloService.readFromOpcUa(PACKCode).getValue();
283 //            if(null == PACKCodeObject || "".equals(PACKCodeObject)){
284 //               result = "22";
285 //            }else{
286 //                String PACKCodeParam = PACKCodeObject.toString();
287 //                //1、更新工单信息
288 //                //updateOrderInfo();
289 //                //2、保存过站采集数据
290 //                saveStationInfo(PACKCodeParam,thoroughfare,device);
291 //                //3、保存参数采集数据
292 //                SaveParamData(PACKCodeParam,thoroughfare,device,"","");
293 //
294 //                result = "21";
295 //
296 //            }
297 //
298 //        }catch (Exception e) {
299 //
300 //        }
301 //        return result;
302 //    }
303
a5b351 304     /**
C 305      * 保存过站采集
306      */
8f0f8d 307     public void saveStationInfo(String packCode,String thoroughfare,String device,String workOrderNo,String productCode) throws Exception {
c5e9c5 308         SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
309         sdf.setTimeZone(TimeZone.getTimeZone("GMT+8")); // CST通常表示中国标准时间,即东八区
32483a 310         String prefix = thoroughfare+"."+device+".";
c5e9c5 311         String startTime = miloService.readFromOpcUa(prefix + "StartTime").getValue().toString();
312         String stopTime = miloService.readFromOpcUa(prefix + "StopTime").getValue().toString();
32483a 313         String stationStatus = miloService.readFromOpcUa(prefix + "StationStatus").getValue().toString();
314
a5b351 315         DaPassingStationCollection daPassingStationCollection = new DaPassingStationCollection();
32483a 316         daPassingStationCollection.setSfcCode(packCode);
317         daPassingStationCollection.setWorkOrderNo(workOrderNo);
8f0f8d 318         daPassingStationCollection.setProductCode(productCode);
319         daPassingStationCollection.setLocationCode(device);
c5e9c5 320         String strt = TimeUtil.stringProcessing(startTime);
321         String end = TimeUtil.stringProcessing(stopTime);
322         daPassingStationCollection.setInboundTime(sdf.parse(strt));//入站时间
323         daPassingStationCollection.setOutboundTime(sdf.parse(end));//出站时间
32483a 324         daPassingStationCollection.setOutRsSign(stationStatus);//出站是否合格
a5b351 325         daPassingStationCollectionService.insertDaPassingStationCollection(daPassingStationCollection);
C 326     }
327
054a69 328     public static void SaveParamData(String packCode,String thoroughfare,String device,String workOrderNo,String productType) throws Exception {
a5b351 329         List<DaCollectionParamConf> list;
C 330         DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf();
331         daCollectionParamConf.setGatherAddress(thoroughfare+ "." + device);
332         list = collectionParamConfService.selectDaCollectionParamConfList(daCollectionParamConf);
333
334         List<String> nodeIdList = list.stream().map(info -> {
335             String nodeid = info.getGatherAddress();
336             return nodeid;
337         }).collect(Collectors.toList());
338
339         if(!nodeIdList.isEmpty()){
340             List<ReadWriteEntity> readWriteEntityList = miloService.readFromOpcUa(nodeIdList);
e70fb4 341             for (int i = 0; i < readWriteEntityList.size(); i++) {
A 342                 if(readWriteEntityList.get(i).getValue() == null){
343                     readWriteEntityList.get(i).setValue(" ");
344                 }
345             }
a5b351 346             List<DaParamCollection> daParamCollectionlist = new ArrayList<>();
C 347             for(int i=0;i<nodeIdList.size();i++){
348                 if(!readWriteEntityList.get(i).getValue().toString().equals("0.0")){
c5e9c5 349                     String tt = readWriteEntityList.get(i).getValue().toString();
32483a 350                     DaParamCollection ParamCollection = new DaParamCollection();
351                     ParamCollection.setParamCode(list.get(i).getCollectParameterId());
a5b351 352                     ParamCollection.setLocationCode(device);
c5e9c5 353                     if(tt.contains("Time")){
354                         String str = TimeUtil.getTimestamp(TimeUtil.stringProcessing(tt));
355                         ParamCollection.setParamValue(str);
356                     }else {
357                         ParamCollection.setParamValue(readWriteEntityList.get(i).getValue().toString());
358                     }
32483a 359                     ParamCollection.setSfcCode(packCode);
a5b351 360                     ParamCollection.setParamName(list.get(i).getCollectParameterName());
C 361                     ParamCollection.setParamUpper(list.get(i).getParamUpper());
362                     ParamCollection.setParamLower(list.get(i).getParamLower());
363                     ParamCollection.setUnit(list.get(i).getCollectParameterUnit());
364                     //ParamCollection.setState("合格");
365                     ParamCollection.setType(list.get(i).getCollectParameterType());
366                     ParamCollection.setCollectionTime(new Date());
054a69 367                     ParamCollection.setWorkOrderNo(workOrderNo);
368                     ParamCollection.setProductCode(productType);
369                     daParamCollectionlist.add(ParamCollection);
370 //                    daParamCollectionService.insertDaParamCollection(ParamCollection);
a5b351 371                 }
C 372             }
054a69 373             daParamCollectionService.saveBeachDaParamCollection(daParamCollectionlist);
a5b351 374         }
8f0f8d 375         addBaseData(workOrderNo,productType,device,packCode);
a5b351 376
C 377     }
8f0f8d 378
379     public static void addBaseData(String workOrderNo,String productCode,String locationCode,String packCode){
380         Map<String, String> map = new HashMap<>();
381         map.put("GC", "南浔工厂");
382         map.put("CXBH", "Pack线");
383         map.put("SBBH", "设备001");
384         map.put("YGBH", "员工001");
385         List<DaParamCollection> confList = new ArrayList<>();
386         map.forEach((key, value) -> {
387             List<DaCollectionParamConf> daCollectionParamConfs = collectionParamConfService.list(new LambdaQueryWrapper<DaCollectionParamConf>()
388                     .eq(DaCollectionParamConf::getProcessesCode,locationCode)
389                     .like(DaCollectionParamConf::getCollectParameterId,key));
390             DaParamCollection saveData = new DaParamCollection();
391             saveData.setWorkOrderNo(workOrderNo);
392             saveData.setProductCode(productCode);
393             saveData.setLocationCode(locationCode);
394             saveData.setSfcCode(packCode);
395             saveData.setParamCode(daCollectionParamConfs.get(0).getCollectParameterId());
396             saveData.setParamName(daCollectionParamConfs.get(0).getCollectParameterName());
397             saveData.setCollectionTime(new Date());
398             saveData.setParamValue(value);
399             confList.add(saveData);
400         });
401         daParamCollectionService.insertBatch(confList);
402     }
e70fb4 403
A 404     public static void getFactoryOrder(String locationCode){
405         String productionWorkOrder = RestfulService.getProductionWorkOrderRequest("", locationCode);
406         JSONObject jsonObject = new JSONObject(productionWorkOrder);
407         JSONObject dataObject = jsonObject.getJSONObject("data");
408
409         String productNum = dataObject.getStr("productNum");
410         String stationCode = dataObject.getStr("stationCode");
411         String materialCode = dataObject.getStr("materialCode");
412         String productionOrderNum = dataObject.getStr("productionOrderNum");
413
414         OmProductionOrdeInfo omProductionOrdeInfo = new OmProductionOrdeInfo();
415         omProductionOrdeInfo.setProductNum(productNum);
416         omProductionOrdeInfo.setWorkOrderNo(productionOrderNum);
417         omProductionOrdeInfo.setStationCode(stationCode);
418         omProductionOrdeInfo.setProductCode(materialCode);
419         omProductionOrdeInfoService.save(omProductionOrdeInfo);
420     }
421
422     public static void updateOrderInfo(String packCode){
423         OmProductionOrdeInfo one = omProductionOrdeInfoService.getOne(new LambdaQueryWrapper<OmProductionOrdeInfo>().eq(OmProductionOrdeInfo::getProductNum, packCode));
424         one.setOrderStatus("5");
425         omProductionOrdeInfoService.saveOrUpdate(one);
426     }
427
4a5f2a 428     public static void avgFunction(){
A 429
430     }
431
432     public static String TightenTheConversionOkNg(String param){
433         // 去除首尾的方括号,然后按照逗号分割字符串
434         String[] parts = param.substring(1, param.length() - 1).split(",");
435
436         // 创建一个新的StringBuilder来构建替换后的字符串
437         StringBuilder sb = new StringBuilder();
438         sb.append('['); // 添加左方括号
439
440         for (int i = 0; i < parts.length; i++) {
441             String part = parts[i].trim(); // 去除可能的空格
442             float value;
443             try {
444                 value = Float.parseFloat(part); // 尝试将字符串转换为浮点数
445                 String replacement;
446                 if (value == 1f) {
447                     replacement = "OK";
448                 } else if (value == 2f) {
449                     replacement = "NG";
450                 } else {
451                     replacement = part; // 如果不是1或2,则保持不变
452                 }
453                 sb.append(replacement);
454                 if (i < parts.length - 1) {
455                     sb.append(','); // 添加逗号(除了最后一个元素)
456                 }
457             } catch (NumberFormatException e) {
458                 // 如果转换失败,则保持原样(或进行其他错误处理)
459                 sb.append(part);
460                 if (i < parts.length - 1) {
461                     sb.append(',');
462                 }
463             }
464         }
465         sb.append(']'); // 添加右方括号
466         return sb.toString();
467     }
468
a5b351 469 }