-
懒羊羊
2024-04-08 b5fcd8d51443fda9e7506f31d157e6faa7e1d56c
提交 | 用户 | 时间
a5b351 1 package com.jcdm.main.plcserver.sub;
C 2
3
8f0f8d 4 import cn.hutool.core.date.DateUtil;
5 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
6 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
49c784 7 import com.jcdm.framework.websocket.WebSocketUsers;
a5b351 8 import com.jcdm.main.da.collectionParamConf.domain.DaCollectionParamConf;
C 9 import com.jcdm.main.da.collectionParamConf.service.IDaCollectionParamConfService;
10 import com.jcdm.main.da.paramCollection.domain.DaParamCollection;
11 import com.jcdm.main.da.paramCollection.service.IDaParamCollectionService;
12 import com.jcdm.main.da.passingStationCollection.domain.DaPassingStationCollection;
13 import com.jcdm.main.da.passingStationCollection.service.IDaPassingStationCollectionService;
14 import com.jcdm.main.plcserver.conf.OPCElement;
15 import com.kangaroohy.milo.model.ReadWriteEntity;
16 import com.kangaroohy.milo.runner.subscription.SubscriptionCallback;
17 import com.kangaroohy.milo.service.MiloService;
18 import org.springframework.beans.factory.annotation.Autowired;
19 import org.springframework.stereotype.Component;
20
49c784 21 import javax.websocket.Session;
32483a 22 import java.util.*;
a5b351 23 import java.util.stream.Collectors;
C 24
25
26 @Component
27 public class OPCUaSubscription implements SubscriptionCallback {
32483a 28
29     //自动工位
054a69 30     public List<String> automaticList = Arrays.asList("OP300","OP280", "OP320", "OP340", "OP350", "OP360","OP370", "OP390", "OP470", "OP530", "OP540");
a5b351 31
8f0f8d 32     public List<String> moduleCodeList = Arrays.asList("OP300","OP310", "OP320", "OP340", "OP350", "OP33");
33
34     public List<String> nullList = Arrays.asList("OP250","OP260");
35
36
a5b351 37     public static MiloService miloService;
49c784 38
39     Map<String, Session> map = WebSocketUsers.getUsers();
a5b351 40
C 41     public IDaPassingStationCollectionService daPassingStationCollectionService;
42
054a69 43     public static IDaCollectionParamConfService collectionParamConfService;
a5b351 44
054a69 45     public static IDaParamCollectionService daParamCollectionService;
a5b351 46
C 47     public OPCUaSubscription(MiloService miloService,
48                              IDaPassingStationCollectionService daPassingStationCollectionService,
49                              IDaCollectionParamConfService collectionParamConfService,
50                              IDaParamCollectionService daParamCollectionService) {
51         OPCUaSubscription.miloService = miloService;
52         this.daPassingStationCollectionService = daPassingStationCollectionService;
8f0f8d 53         OPCUaSubscription.collectionParamConfService = collectionParamConfService;
54         OPCUaSubscription.daParamCollectionService = daParamCollectionService;
a5b351 55
C 56     }
57
58
59     @Override
60     public void onSubscribe(String identifier, Object value) {
61
62         try {
63             if(null != value) {
64                 String[] nodes = identifier.split("[.]");
65                 String thoroughfare = nodes[0];//通道
66                 String device = nodes[1];//设备
67                 String tab = nodes[2];//标记
68                 String tabVlaue = value.toString();//地址值
69
70                 //请求下发SN号
71                 if (("RecordSN").equals(tab) && "1".equals(tabVlaue)) {
72                     //获取SN号方法
73                     String SNCode = getSNCode();
74
75                     //下发SN
76                     String SNCodeAddress = thoroughfare + "." + device + ".SNCode";
77                     miloService.writeToOpcChar(ReadWriteEntity.builder().identifier(SNCodeAddress).value(SNCode).build());
78                     //下发SN完成
79                     String recordSNDoneAddress = thoroughfare + "." + device + ".RecordSNDone";
80                     miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(recordSNDoneAddress).value(1).build());
81                 }
82                 //请求记录数据
83                 else if (("RecordData").equals(tab)) {
84                     String recordDataDoneValue = "";
85
86                     if("1".equals(tabVlaue)){
87                         recordDataDoneValue = "11";
32483a 88                         //校验入站是否可以工作
a5b351 89
C 90                         //进站保存数据
7bff29 91 //                        inSaveDate(thoroughfare,device)
a5b351 92                         //记录数据完成
32483a 93                         String RecordDataDoneAddress = thoroughfare + "." + device + ".RecordDataDone";
94                         miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(11).build());
7bff29 95
32483a 96                         if(automaticList.stream().noneMatch(s -> s.equals(device))){
97                             //给前端发工件到位信号
98                             WebSocketUsers.sendMessageToUserByText(map.get(device), "IN");
99                         }
7bff29 100                         //请求工单
101
a5b351 102                     }else if("2".equals(tabVlaue)){
32483a 103                         if(automaticList.stream().anyMatch(s -> s.equals(device))){
104                             //自动工位
105                             //出站保存数据
106                             recordDataDoneValue = outSaveDate(thoroughfare,device);
107                             //记录数据完成
108                             String RecordDataDoneAddress = thoroughfare + "." + device + ".RecordDataDone";
109                             miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(Integer.valueOf(RecordDataDoneAddress)).build());
110                         }else {
111                             //手动工位
112                             WebSocketUsers.sendMessageToUserByText(map.get(device), "END");
113                         }
3c2299 114                     }else {
115                         System.out.println("^");
116                     }
117                 }
118                 //保存拧紧数据
119                 else if (("AngleResult").equals(tab)) {
b5fcd8 120                     if("1".equals(tabVlaue)||"2".equals(tabVlaue)){
32483a 121                         List<String> list = new ArrayList<>();
0caf87 122                         String[] suffixes = {"Torque", "Angle", "TorqueResult", "AngleResult"};
32483a 123
124                         for (String suffix : suffixes) {
125                             String string = thoroughfare + "." + device + "." + suffix;
126                             list.add(string);
127                         }
49c784 128                         List<ReadWriteEntity> list1 = miloService.readFromOpcUa(list);
129                         List<Object> collect = list1.stream().map(ReadWriteEntity::getValue).collect(Collectors.toList());
130                         String joinedString = String.join(",", collect.toString());
131                         WebSocketUsers.sendMessageToUserByText(map.get(device), joinedString);
a5b351 132                     }
C 133                 }
134             }
135         } catch (Exception e) {
136
137         }
138     }
139
140
141
142     /**
143      * 获取SNCode
144      */
145     public String getSNCode(){
146         String SNCode = "";
147         return SNCode;
148     }
149
150     /**
151      * 出站保存数据
152      */
153     public String outSaveDate(String thoroughfare,String device) {
8f0f8d 154         String snCode = "";
a5b351 155         String result = "";
8f0f8d 156         String read = thoroughfare + "." + device + ".";
157
a5b351 158         try {
8f0f8d 159             if(nullList.stream().noneMatch(s -> s.equals(device))){
160                 if(moduleCodeList.stream().anyMatch(s -> s.equals(device))){
161                     read = read + "ModuleCode";
162                 }else {
163                     read = read + "PACKCode";
164                 }
165             }
166             snCode = miloService.readFromOpcUa(read).getValue().toString();
167
168             if(null == snCode || "".equals(snCode)){
169                 result = "22";
a5b351 170             }else{
C 171                 //1、更新工单信息
172                 //updateOrderInfo();
173                 //2、保存过站采集数据
8f0f8d 174                 String workOrderNo = miloService.readFromOpcUa(thoroughfare + "." + device + "." + "WorkOrderNumber").getValue().toString();
175                 String productCode = miloService.readFromOpcUa(thoroughfare + "." + device + "." + "ProductType").getValue().toString();
176
177                 saveStationInfo(snCode,thoroughfare,device,workOrderNo,productCode);
a5b351 178                 //3、保存参数采集数据
8f0f8d 179                 SaveParamData(snCode,thoroughfare,device,workOrderNo,productCode);
a5b351 180
C 181                 result = "21";
182
183             }
184
185         }catch (Exception e) {
8f0f8d 186             System.out.println(e.getMessage());
a5b351 187         }
C 188         return result;
189     }
190
8f0f8d 191
192 //    /**
193 //     * 出站保存数据
194 //     */
195 //    public String outSaveDate(String thoroughfare,String device) {
196 //        String result = "";
197 //        try {
198 //            //读取SNCode
199 //            String PACKCode = thoroughfare + "." + device + ".PACKCode";
200 //            Object PACKCodeObject = miloService.readFromOpcUa(PACKCode).getValue();
201 //            if(null == PACKCodeObject || "".equals(PACKCodeObject)){
202 //               result = "22";
203 //            }else{
204 //                String PACKCodeParam = PACKCodeObject.toString();
205 //                //1、更新工单信息
206 //                //updateOrderInfo();
207 //                //2、保存过站采集数据
208 //                saveStationInfo(PACKCodeParam,thoroughfare,device);
209 //                //3、保存参数采集数据
210 //                SaveParamData(PACKCodeParam,thoroughfare,device,"","");
211 //
212 //                result = "21";
213 //
214 //            }
215 //
216 //        }catch (Exception e) {
217 //
218 //        }
219 //        return result;
220 //    }
221
a5b351 222     /**
C 223      * 保存过站采集
224      */
8f0f8d 225     public void saveStationInfo(String packCode,String thoroughfare,String device,String workOrderNo,String productCode) throws Exception {
32483a 226         String prefix = thoroughfare+"."+device+".";
227         String startTime = miloService.readFromOpcUa(prefix + "StartTime").getValue().toString();
228         String stopTime = miloService.readFromOpcUa(prefix + "StopTime").getValue().toString();
229         String stationStatus = miloService.readFromOpcUa(prefix + "StationStatus").getValue().toString();
230
a5b351 231         DaPassingStationCollection daPassingStationCollection = new DaPassingStationCollection();
32483a 232         daPassingStationCollection.setSfcCode(packCode);
233         daPassingStationCollection.setWorkOrderNo(workOrderNo);
8f0f8d 234         daPassingStationCollection.setProductCode(productCode);
235         daPassingStationCollection.setLocationCode(device);
32483a 236 //        daPassingStationCollection.setInboundTime(startTime);//入站时间
237 //        daPassingStationCollection.setOutboundTime(stopTime);//出站时间
238         daPassingStationCollection.setOutRsSign(stationStatus);//出站是否合格
a5b351 239         daPassingStationCollectionService.insertDaPassingStationCollection(daPassingStationCollection);
C 240     }
241
054a69 242     public static void SaveParamData(String packCode,String thoroughfare,String device,String workOrderNo,String productType) throws Exception {
a5b351 243         List<DaCollectionParamConf> list;
C 244         DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf();
245         daCollectionParamConf.setGatherAddress(thoroughfare+ "." + device);
246         list = collectionParamConfService.selectDaCollectionParamConfList(daCollectionParamConf);
247
248         List<String> nodeIdList = list.stream().map(info -> {
249             String nodeid = info.getGatherAddress();
250             return nodeid;
251         }).collect(Collectors.toList());
252
253         if(!nodeIdList.isEmpty()){
254             List<ReadWriteEntity> readWriteEntityList = miloService.readFromOpcUa(nodeIdList);
255             List<DaParamCollection> daParamCollectionlist = new ArrayList<>();
256             for(int i=0;i<nodeIdList.size();i++){
257                 if(!readWriteEntityList.get(i).getValue().toString().equals("0.0")){
32483a 258                     DaParamCollection ParamCollection = new DaParamCollection();
259                     ParamCollection.setParamCode(list.get(i).getCollectParameterId());
a5b351 260                     ParamCollection.setLocationCode(device);
C 261                     ParamCollection.setParamValue(readWriteEntityList.get(i).getValue().toString());
32483a 262                     ParamCollection.setSfcCode(packCode);
a5b351 263                     ParamCollection.setParamName(list.get(i).getCollectParameterName());
C 264                     ParamCollection.setParamUpper(list.get(i).getParamUpper());
265                     ParamCollection.setParamLower(list.get(i).getParamLower());
266                     ParamCollection.setUnit(list.get(i).getCollectParameterUnit());
267                     //ParamCollection.setState("合格");
268                     ParamCollection.setType(list.get(i).getCollectParameterType());
269                     ParamCollection.setCollectionTime(new Date());
054a69 270                     ParamCollection.setWorkOrderNo(workOrderNo);
271                     ParamCollection.setProductCode(productType);
272                     daParamCollectionlist.add(ParamCollection);
273 //                    daParamCollectionService.insertDaParamCollection(ParamCollection);
a5b351 274                 }
C 275             }
054a69 276             daParamCollectionService.saveBeachDaParamCollection(daParamCollectionlist);
a5b351 277         }
8f0f8d 278         addBaseData(workOrderNo,productType,device,packCode);
a5b351 279
C 280     }
8f0f8d 281
282     public static void addBaseData(String workOrderNo,String productCode,String locationCode,String packCode){
283         Map<String, String> map = new HashMap<>();
284         map.put("GC", "南浔工厂");
285         map.put("CXBH", "Pack线");
286         map.put("SBBH", "设备001");
287         map.put("YGBH", "员工001");
288         List<DaParamCollection> confList = new ArrayList<>();
289         map.forEach((key, value) -> {
290             List<DaCollectionParamConf> daCollectionParamConfs = collectionParamConfService.list(new LambdaQueryWrapper<DaCollectionParamConf>()
291                     .eq(DaCollectionParamConf::getProcessesCode,locationCode)
292                     .like(DaCollectionParamConf::getCollectParameterId,key));
293             DaParamCollection saveData = new DaParamCollection();
294             saveData.setWorkOrderNo(workOrderNo);
295             saveData.setProductCode(productCode);
296             saveData.setLocationCode(locationCode);
297             saveData.setSfcCode(packCode);
298             saveData.setParamCode(daCollectionParamConfs.get(0).getCollectParameterId());
299             saveData.setParamName(daCollectionParamConfs.get(0).getCollectParameterName());
300             saveData.setCollectionTime(new Date());
301             saveData.setParamValue(value);
302             confList.add(saveData);
303         });
304         daParamCollectionService.insertBatch(confList);
305     }
a5b351 306 }