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