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