-
admin
2024-04-27 7a08677172276853ea984a26b76bae0e88ab00ed
提交 | 用户 | 时间
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     //新自动工站
A 38     public List<String> automaticList = Arrays.asList("OP280", "OP320A","OP320B", "OP340A","OP340B", "OP350A","OP350B", "OP360","OP365","OP370", "OP390", "OP470");
39
7a0867 40     //采集模组工位 ModuleCode
A 41     public List<String> moduleCodeList = Arrays.asList("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
5316c5 46
A 47     //区分pack型号的工位
7a0867 48     public List<String> packIdList = Arrays.asList("OP280","OP360","OP390","OP470");
A 49
8f0f8d 50
63b254 51     //空的
8f0f8d 52     public List<String> nullList = Arrays.asList("OP250","OP260");
53
54
a5b351 55     public static MiloService miloService;
49c784 56
57     Map<String, Session> map = WebSocketUsers.getUsers();
a5b351 58
C 59     public IDaPassingStationCollectionService daPassingStationCollectionService;
60
054a69 61     public static IDaCollectionParamConfService collectionParamConfService;
a5b351 62
054a69 63     public static IDaParamCollectionService daParamCollectionService;
a5b351 64
e70fb4 65     public static IOmProductionOrdeInfoService omProductionOrdeInfoService;
A 66
a5b351 67     public OPCUaSubscription(MiloService miloService,
C 68                              IDaPassingStationCollectionService daPassingStationCollectionService,
69                              IDaCollectionParamConfService collectionParamConfService,
e70fb4 70                              IDaParamCollectionService daParamCollectionService,
A 71                              IOmProductionOrdeInfoService omProductionOrdeInfoService) {
a5b351 72         OPCUaSubscription.miloService = miloService;
C 73         this.daPassingStationCollectionService = daPassingStationCollectionService;
8f0f8d 74         OPCUaSubscription.collectionParamConfService = collectionParamConfService;
75         OPCUaSubscription.daParamCollectionService = daParamCollectionService;
e70fb4 76         OPCUaSubscription.omProductionOrdeInfoService = omProductionOrdeInfoService;
a5b351 77     }
C 78
79
80     @Override
81     public void onSubscribe(String identifier, Object value) {
82
83         try {
70cc26 84             if(null != value && Integer.valueOf(value.toString())!= 0) {
a5b351 85                 String[] nodes = identifier.split("[.]");
C 86                 String thoroughfare = nodes[0];//通道
87                 String device = nodes[1];//设备
88                 String tab = nodes[2];//标记
89                 String tabVlaue = value.toString();//地址值
90
91                 //请求下发SN号
92                 if (("RecordSN").equals(tab) && "1".equals(tabVlaue)) {
93                     //获取SN号方法
94                     String SNCode = getSNCode();
95
96                     //下发SN
97                     String SNCodeAddress = thoroughfare + "." + device + ".SNCode";
98                     miloService.writeToOpcChar(ReadWriteEntity.builder().identifier(SNCodeAddress).value(SNCode).build());
99                     //下发SN完成
100                     String recordSNDoneAddress = thoroughfare + "." + device + ".RecordSNDone";
101                     miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(recordSNDoneAddress).value(1).build());
102                 }
103                 //请求记录数据
c5e9c5 104                 else if (("RecordData").equals(tab)) {
a5b351 105                     String recordDataDoneValue = "";
C 106
107                     if("1".equals(tabVlaue)){
89f416 108
e70fb4 109                         //请求工单
A 110 //                        if(device.equals("OP230")){
111 //                            getFactoryOrder("OP230");
112 //                        }
113
114                         //自动工位
5316c5 115                         if(automaticList.stream().anyMatch(s -> s.equals(device))){
A 116                             //plc给我们一个模组码,拿模组码校验出型号
7a0867 117                             if(distinguishingModules.stream().anyMatch(s -> s.equals(device))) {
5316c5 118                                 //读模组码
A 119                                 String moduleCode = miloService.readFromOpcUa(thoroughfare + "." + device + ".ModuleCode").getValue().toString();
120                                 String productTye = thoroughfare + "." + device + ".ProductType";
121                                 if (null != moduleCode && moduleCode.length() == 24) {
122                                     miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(productTye).value(Integer.valueOf(moduleCode.substring(7, 8))).build());
123                                     //进站保存数据
e70fb4 124 //                              inSaveDate(thoroughfare,device)
5316c5 125                                     //记录数据完成
A 126                                     String RecordDataDoneAddress = thoroughfare + "." + device + ".RecordDataDone";
63b254 127
5316c5 128                                     miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(11).build());
7bff29 129
e70fb4 130
5316c5 131                                     //请求工单
A 132                                 } else {
133                                     miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(thoroughfare + "." + device + ".RecordDataDone").value(12).build());
134                                 }
7a0867 135                             }else {
A 136                                 //pack 如果区分型号的话就要处理
137                                 //记录数据完成
138                                 String RecordDataDoneAddress = thoroughfare + "." + device + ".RecordDataDone";
139
140                                 miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(11).build());
e70fb4 141                             }
A 142                         }else {
143                             //手动工位处理逻辑
144 //                            if(automaticList.stream().noneMatch(s -> s.equals(device))){
145                                 //给前端发工件到位信号
146                                 WebSocketUsers.sendMessageToUserByText(map.get(device), "IN");
147 //                            }
148                             //校验合格不合格如果合格可以进站
149                             String RecordDataDoneAddress = thoroughfare + "." + device + ".RecordDataDone";
150
151                             miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(11).build());
32483a 152                         }
e70fb4 153
7bff29 154
a5b351 155                     }else if("2".equals(tabVlaue)){
32483a 156                         if(automaticList.stream().anyMatch(s -> s.equals(device))){
157                             //自动工位
158                             //出站保存数据
159                             recordDataDoneValue = outSaveDate(thoroughfare,device);
160                             //记录数据完成
161                             String RecordDataDoneAddress = thoroughfare + "." + device + ".RecordDataDone";
70cc26 162                             miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(Integer.valueOf(recordDataDoneValue)).build());
32483a 163                         }else {
164                             //手动工位
165                             WebSocketUsers.sendMessageToUserByText(map.get(device), "END");
166                         }
3c2299 167                     }else {
168                         System.out.println("^");
169                     }
170                 }
171                 //保存拧紧数据
172                 else if (("AngleResult").equals(tab)) {
b5fcd8 173                     if("1".equals(tabVlaue)||"2".equals(tabVlaue)){
32483a 174                         List<String> list = new ArrayList<>();
0caf87 175                         String[] suffixes = {"Torque", "Angle", "TorqueResult", "AngleResult"};
32483a 176
177                         for (String suffix : suffixes) {
178                             String string = thoroughfare + "." + device + "." + suffix;
179                             list.add(string);
180                         }
49c784 181                         List<ReadWriteEntity> list1 = miloService.readFromOpcUa(list);
182                         List<Object> collect = list1.stream().map(ReadWriteEntity::getValue).collect(Collectors.toList());
183                         String joinedString = String.join(",", collect.toString());
184                         WebSocketUsers.sendMessageToUserByText(map.get(device), joinedString);
a5b351 185                     }
C 186                 }
187             }
188         } catch (Exception e) {
189
190         }
191     }
192
193
194
195     /**
196      * 获取SNCode
197      */
198     public String getSNCode(){
199         String SNCode = "";
200         return SNCode;
201     }
202
203     /**
204      * 出站保存数据
205      */
206     public String outSaveDate(String thoroughfare,String device) {
8f0f8d 207         String snCode = "";
a5b351 208         String result = "";
8f0f8d 209         String read = thoroughfare + "." + device + ".";
210
a5b351 211         try {
8f0f8d 212             if(nullList.stream().noneMatch(s -> s.equals(device))){
213                 if(moduleCodeList.stream().anyMatch(s -> s.equals(device))){
214                     read = read + "ModuleCode";
215                 }else {
216                     read = read + "PACKCode";
217                 }
218             }
219             snCode = miloService.readFromOpcUa(read).getValue().toString();
220
221             if(null == snCode || "".equals(snCode)){
222                 result = "22";
a5b351 223             }else{
8f0f8d 224
e70fb4 225                 String workOrderNo = "";
A 226                 String productCode = "";
227                 //2、保存过站采集数据
228                 Object orderNumberObject = miloService.readFromOpcUa(thoroughfare + "." + device + "." + "WorkOrderNumber").getValue();
229                 if(orderNumberObject!=null){
230                     workOrderNo = orderNumberObject.toString();
231                 }
232
233                 Object productCodeObject = miloService.readFromOpcUa(thoroughfare + "." + device + "." + "ProductType").getValue();
234                 if(productCodeObject!=null){
235                     productCode = productCodeObject.toString();
236                 }
89f416 237
8f0f8d 238                 saveStationInfo(snCode,thoroughfare,device,workOrderNo,productCode);
a5b351 239                 //3、保存参数采集数据
8f0f8d 240                 SaveParamData(snCode,thoroughfare,device,workOrderNo,productCode);
e70fb4 241
A 242                 //如果是末尾工站要报工
243                 if(device.equals("OP500")){
244                     RestfulService.getWorkReportResultFeedback(snCode,device,format.format(new Date()));
245                     //1、更新工单信息
246                     updateOrderInfo(snCode);
247                 }
a5b351 248
C 249                 result = "21";
250
251             }
252
253         }catch (Exception e) {
8f0f8d 254             System.out.println(e.getMessage());
a5b351 255         }
C 256         return result;
257     }
258
8f0f8d 259
260 //    /**
261 //     * 出站保存数据
262 //     */
263 //    public String outSaveDate(String thoroughfare,String device) {
264 //        String result = "";
265 //        try {
266 //            //读取SNCode
267 //            String PACKCode = thoroughfare + "." + device + ".PACKCode";
268 //            Object PACKCodeObject = miloService.readFromOpcUa(PACKCode).getValue();
269 //            if(null == PACKCodeObject || "".equals(PACKCodeObject)){
270 //               result = "22";
271 //            }else{
272 //                String PACKCodeParam = PACKCodeObject.toString();
273 //                //1、更新工单信息
274 //                //updateOrderInfo();
275 //                //2、保存过站采集数据
276 //                saveStationInfo(PACKCodeParam,thoroughfare,device);
277 //                //3、保存参数采集数据
278 //                SaveParamData(PACKCodeParam,thoroughfare,device,"","");
279 //
280 //                result = "21";
281 //
282 //            }
283 //
284 //        }catch (Exception e) {
285 //
286 //        }
287 //        return result;
288 //    }
289
a5b351 290     /**
C 291      * 保存过站采集
292      */
8f0f8d 293     public void saveStationInfo(String packCode,String thoroughfare,String device,String workOrderNo,String productCode) throws Exception {
c5e9c5 294         SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
295         sdf.setTimeZone(TimeZone.getTimeZone("GMT+8")); // CST通常表示中国标准时间,即东八区
32483a 296         String prefix = thoroughfare+"."+device+".";
c5e9c5 297         String startTime = miloService.readFromOpcUa(prefix + "StartTime").getValue().toString();
298         String stopTime = miloService.readFromOpcUa(prefix + "StopTime").getValue().toString();
32483a 299         String stationStatus = miloService.readFromOpcUa(prefix + "StationStatus").getValue().toString();
300
a5b351 301         DaPassingStationCollection daPassingStationCollection = new DaPassingStationCollection();
32483a 302         daPassingStationCollection.setSfcCode(packCode);
303         daPassingStationCollection.setWorkOrderNo(workOrderNo);
8f0f8d 304         daPassingStationCollection.setProductCode(productCode);
305         daPassingStationCollection.setLocationCode(device);
c5e9c5 306         String strt = TimeUtil.stringProcessing(startTime);
307         String end = TimeUtil.stringProcessing(stopTime);
308         daPassingStationCollection.setInboundTime(sdf.parse(strt));//入站时间
309         daPassingStationCollection.setOutboundTime(sdf.parse(end));//出站时间
32483a 310         daPassingStationCollection.setOutRsSign(stationStatus);//出站是否合格
a5b351 311         daPassingStationCollectionService.insertDaPassingStationCollection(daPassingStationCollection);
C 312     }
313
054a69 314     public static void SaveParamData(String packCode,String thoroughfare,String device,String workOrderNo,String productType) throws Exception {
a5b351 315         List<DaCollectionParamConf> list;
C 316         DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf();
317         daCollectionParamConf.setGatherAddress(thoroughfare+ "." + device);
318         list = collectionParamConfService.selectDaCollectionParamConfList(daCollectionParamConf);
319
320         List<String> nodeIdList = list.stream().map(info -> {
321             String nodeid = info.getGatherAddress();
322             return nodeid;
323         }).collect(Collectors.toList());
324
325         if(!nodeIdList.isEmpty()){
326             List<ReadWriteEntity> readWriteEntityList = miloService.readFromOpcUa(nodeIdList);
e70fb4 327             for (int i = 0; i < readWriteEntityList.size(); i++) {
A 328                 if(readWriteEntityList.get(i).getValue() == null){
329                     readWriteEntityList.get(i).setValue(" ");
330                 }
331             }
a5b351 332             List<DaParamCollection> daParamCollectionlist = new ArrayList<>();
C 333             for(int i=0;i<nodeIdList.size();i++){
334                 if(!readWriteEntityList.get(i).getValue().toString().equals("0.0")){
c5e9c5 335                     String tt = readWriteEntityList.get(i).getValue().toString();
32483a 336                     DaParamCollection ParamCollection = new DaParamCollection();
337                     ParamCollection.setParamCode(list.get(i).getCollectParameterId());
a5b351 338                     ParamCollection.setLocationCode(device);
c5e9c5 339                     if(tt.contains("Time")){
340                         String str = TimeUtil.getTimestamp(TimeUtil.stringProcessing(tt));
341                         ParamCollection.setParamValue(str);
342                     }else {
343                         ParamCollection.setParamValue(readWriteEntityList.get(i).getValue().toString());
344                     }
32483a 345                     ParamCollection.setSfcCode(packCode);
a5b351 346                     ParamCollection.setParamName(list.get(i).getCollectParameterName());
C 347                     ParamCollection.setParamUpper(list.get(i).getParamUpper());
348                     ParamCollection.setParamLower(list.get(i).getParamLower());
349                     ParamCollection.setUnit(list.get(i).getCollectParameterUnit());
350                     //ParamCollection.setState("合格");
351                     ParamCollection.setType(list.get(i).getCollectParameterType());
352                     ParamCollection.setCollectionTime(new Date());
054a69 353                     ParamCollection.setWorkOrderNo(workOrderNo);
354                     ParamCollection.setProductCode(productType);
355                     daParamCollectionlist.add(ParamCollection);
356 //                    daParamCollectionService.insertDaParamCollection(ParamCollection);
a5b351 357                 }
C 358             }
054a69 359             daParamCollectionService.saveBeachDaParamCollection(daParamCollectionlist);
a5b351 360         }
8f0f8d 361         addBaseData(workOrderNo,productType,device,packCode);
a5b351 362
C 363     }
8f0f8d 364
365     public static void addBaseData(String workOrderNo,String productCode,String locationCode,String packCode){
366         Map<String, String> map = new HashMap<>();
367         map.put("GC", "南浔工厂");
368         map.put("CXBH", "Pack线");
369         map.put("SBBH", "设备001");
370         map.put("YGBH", "员工001");
371         List<DaParamCollection> confList = new ArrayList<>();
372         map.forEach((key, value) -> {
373             List<DaCollectionParamConf> daCollectionParamConfs = collectionParamConfService.list(new LambdaQueryWrapper<DaCollectionParamConf>()
374                     .eq(DaCollectionParamConf::getProcessesCode,locationCode)
375                     .like(DaCollectionParamConf::getCollectParameterId,key));
376             DaParamCollection saveData = new DaParamCollection();
377             saveData.setWorkOrderNo(workOrderNo);
378             saveData.setProductCode(productCode);
379             saveData.setLocationCode(locationCode);
380             saveData.setSfcCode(packCode);
381             saveData.setParamCode(daCollectionParamConfs.get(0).getCollectParameterId());
382             saveData.setParamName(daCollectionParamConfs.get(0).getCollectParameterName());
383             saveData.setCollectionTime(new Date());
384             saveData.setParamValue(value);
385             confList.add(saveData);
386         });
387         daParamCollectionService.insertBatch(confList);
388     }
e70fb4 389
A 390     public static void getFactoryOrder(String locationCode){
391         String productionWorkOrder = RestfulService.getProductionWorkOrderRequest("", locationCode);
392         JSONObject jsonObject = new JSONObject(productionWorkOrder);
393         JSONObject dataObject = jsonObject.getJSONObject("data");
394
395         String productNum = dataObject.getStr("productNum");
396         String stationCode = dataObject.getStr("stationCode");
397         String materialCode = dataObject.getStr("materialCode");
398         String productionOrderNum = dataObject.getStr("productionOrderNum");
399
400         OmProductionOrdeInfo omProductionOrdeInfo = new OmProductionOrdeInfo();
401         omProductionOrdeInfo.setProductNum(productNum);
402         omProductionOrdeInfo.setWorkOrderNo(productionOrderNum);
403         omProductionOrdeInfo.setStationCode(stationCode);
404         omProductionOrdeInfo.setProductCode(materialCode);
405         omProductionOrdeInfoService.save(omProductionOrdeInfo);
406     }
407
408     public static void updateOrderInfo(String packCode){
409         OmProductionOrdeInfo one = omProductionOrdeInfoService.getOne(new LambdaQueryWrapper<OmProductionOrdeInfo>().eq(OmProductionOrdeInfo::getProductNum, packCode));
410         one.setOrderStatus("5");
411         omProductionOrdeInfoService.saveOrUpdate(one);
412     }
413
a5b351 414 }