-
懒羊羊
2024-04-11 89f41617d8c15a43029629f9f097238e04a78a63
提交 | 用户 | 时间
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                 //请求记录数据
89f416 83                 else if (("Grade").equals(tab)) {
a5b351 84                     String recordDataDoneValue = "";
C 85
86                     if("1".equals(tabVlaue)){
87                         recordDataDoneValue = "11";
32483a 88                         //校验入站是否可以工作
89f416 89
90                         //plc给我们一个pack码,拿pack码校验出型号
a5b351 91
C 92                         //进站保存数据
7bff29 93 //                        inSaveDate(thoroughfare,device)
a5b351 94                         //记录数据完成
32483a 95                         String RecordDataDoneAddress = thoroughfare + "." + device + ".RecordDataDone";
96                         miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(11).build());
7bff29 97
32483a 98                         if(automaticList.stream().noneMatch(s -> s.equals(device))){
99                             //给前端发工件到位信号
100                             WebSocketUsers.sendMessageToUserByText(map.get(device), "IN");
101                         }
7bff29 102                         //请求工单
103
a5b351 104                     }else if("2".equals(tabVlaue)){
32483a 105                         if(automaticList.stream().anyMatch(s -> s.equals(device))){
106                             //自动工位
107                             //出站保存数据
108                             recordDataDoneValue = outSaveDate(thoroughfare,device);
109                             //记录数据完成
110                             String RecordDataDoneAddress = thoroughfare + "." + device + ".RecordDataDone";
111                             miloService.writeToOpcShort(ReadWriteEntity.builder().identifier(RecordDataDoneAddress).value(Integer.valueOf(RecordDataDoneAddress)).build());
112                         }else {
113                             //手动工位
114                             WebSocketUsers.sendMessageToUserByText(map.get(device), "END");
115                         }
3c2299 116                     }else {
117                         System.out.println("^");
118                     }
119                 }
120                 //保存拧紧数据
121                 else if (("AngleResult").equals(tab)) {
b5fcd8 122                     if("1".equals(tabVlaue)||"2".equals(tabVlaue)){
32483a 123                         List<String> list = new ArrayList<>();
0caf87 124                         String[] suffixes = {"Torque", "Angle", "TorqueResult", "AngleResult"};
32483a 125
126                         for (String suffix : suffixes) {
127                             String string = thoroughfare + "." + device + "." + suffix;
128                             list.add(string);
129                         }
49c784 130                         List<ReadWriteEntity> list1 = miloService.readFromOpcUa(list);
131                         List<Object> collect = list1.stream().map(ReadWriteEntity::getValue).collect(Collectors.toList());
132                         String joinedString = String.join(",", collect.toString());
133                         WebSocketUsers.sendMessageToUserByText(map.get(device), joinedString);
a5b351 134                     }
C 135                 }
136             }
137         } catch (Exception e) {
138
139         }
140     }
141
142
143
144     /**
145      * 获取SNCode
146      */
147     public String getSNCode(){
148         String SNCode = "";
149         return SNCode;
150     }
151
152     /**
153      * 出站保存数据
154      */
155     public String outSaveDate(String thoroughfare,String device) {
8f0f8d 156         String snCode = "";
a5b351 157         String result = "";
8f0f8d 158         String read = thoroughfare + "." + device + ".";
159
a5b351 160         try {
8f0f8d 161             if(nullList.stream().noneMatch(s -> s.equals(device))){
162                 if(moduleCodeList.stream().anyMatch(s -> s.equals(device))){
163                     read = read + "ModuleCode";
164                 }else {
165                     read = read + "PACKCode";
166                 }
167             }
168             snCode = miloService.readFromOpcUa(read).getValue().toString();
169
170             if(null == snCode || "".equals(snCode)){
171                 result = "22";
a5b351 172             }else{
C 173                 //1、更新工单信息
174                 //updateOrderInfo();
175                 //2、保存过站采集数据
8f0f8d 176                 String workOrderNo = miloService.readFromOpcUa(thoroughfare + "." + device + "." + "WorkOrderNumber").getValue().toString();
177                 String productCode = miloService.readFromOpcUa(thoroughfare + "." + device + "." + "ProductType").getValue().toString();
178
89f416 179
8f0f8d 180                 saveStationInfo(snCode,thoroughfare,device,workOrderNo,productCode);
a5b351 181                 //3、保存参数采集数据
8f0f8d 182                 SaveParamData(snCode,thoroughfare,device,workOrderNo,productCode);
a5b351 183
C 184                 result = "21";
185
186             }
187
188         }catch (Exception e) {
8f0f8d 189             System.out.println(e.getMessage());
a5b351 190         }
C 191         return result;
192     }
193
8f0f8d 194
195 //    /**
196 //     * 出站保存数据
197 //     */
198 //    public String outSaveDate(String thoroughfare,String device) {
199 //        String result = "";
200 //        try {
201 //            //读取SNCode
202 //            String PACKCode = thoroughfare + "." + device + ".PACKCode";
203 //            Object PACKCodeObject = miloService.readFromOpcUa(PACKCode).getValue();
204 //            if(null == PACKCodeObject || "".equals(PACKCodeObject)){
205 //               result = "22";
206 //            }else{
207 //                String PACKCodeParam = PACKCodeObject.toString();
208 //                //1、更新工单信息
209 //                //updateOrderInfo();
210 //                //2、保存过站采集数据
211 //                saveStationInfo(PACKCodeParam,thoroughfare,device);
212 //                //3、保存参数采集数据
213 //                SaveParamData(PACKCodeParam,thoroughfare,device,"","");
214 //
215 //                result = "21";
216 //
217 //            }
218 //
219 //        }catch (Exception e) {
220 //
221 //        }
222 //        return result;
223 //    }
224
a5b351 225     /**
C 226      * 保存过站采集
227      */
8f0f8d 228     public void saveStationInfo(String packCode,String thoroughfare,String device,String workOrderNo,String productCode) throws Exception {
32483a 229         String prefix = thoroughfare+"."+device+".";
89f416 230 //        String startTime = miloService.readFromOpcUa(prefix + "StartTime").getValue().toString();
231 //        String stopTime = miloService.readFromOpcUa(prefix + "StopTime").getValue().toString();
32483a 232         String stationStatus = miloService.readFromOpcUa(prefix + "StationStatus").getValue().toString();
233
a5b351 234         DaPassingStationCollection daPassingStationCollection = new DaPassingStationCollection();
32483a 235         daPassingStationCollection.setSfcCode(packCode);
236         daPassingStationCollection.setWorkOrderNo(workOrderNo);
8f0f8d 237         daPassingStationCollection.setProductCode(productCode);
238         daPassingStationCollection.setLocationCode(device);
32483a 239 //        daPassingStationCollection.setInboundTime(startTime);//入站时间
240 //        daPassingStationCollection.setOutboundTime(stopTime);//出站时间
241         daPassingStationCollection.setOutRsSign(stationStatus);//出站是否合格
a5b351 242         daPassingStationCollectionService.insertDaPassingStationCollection(daPassingStationCollection);
C 243     }
244
054a69 245     public static void SaveParamData(String packCode,String thoroughfare,String device,String workOrderNo,String productType) throws Exception {
a5b351 246         List<DaCollectionParamConf> list;
C 247         DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf();
248         daCollectionParamConf.setGatherAddress(thoroughfare+ "." + device);
249         list = collectionParamConfService.selectDaCollectionParamConfList(daCollectionParamConf);
250
251         List<String> nodeIdList = list.stream().map(info -> {
252             String nodeid = info.getGatherAddress();
253             return nodeid;
254         }).collect(Collectors.toList());
255
256         if(!nodeIdList.isEmpty()){
257             List<ReadWriteEntity> readWriteEntityList = miloService.readFromOpcUa(nodeIdList);
258             List<DaParamCollection> daParamCollectionlist = new ArrayList<>();
259             for(int i=0;i<nodeIdList.size();i++){
260                 if(!readWriteEntityList.get(i).getValue().toString().equals("0.0")){
32483a 261                     DaParamCollection ParamCollection = new DaParamCollection();
262                     ParamCollection.setParamCode(list.get(i).getCollectParameterId());
a5b351 263                     ParamCollection.setLocationCode(device);
C 264                     ParamCollection.setParamValue(readWriteEntityList.get(i).getValue().toString());
32483a 265                     ParamCollection.setSfcCode(packCode);
a5b351 266                     ParamCollection.setParamName(list.get(i).getCollectParameterName());
C 267                     ParamCollection.setParamUpper(list.get(i).getParamUpper());
268                     ParamCollection.setParamLower(list.get(i).getParamLower());
269                     ParamCollection.setUnit(list.get(i).getCollectParameterUnit());
270                     //ParamCollection.setState("合格");
271                     ParamCollection.setType(list.get(i).getCollectParameterType());
272                     ParamCollection.setCollectionTime(new Date());
054a69 273                     ParamCollection.setWorkOrderNo(workOrderNo);
274                     ParamCollection.setProductCode(productType);
275                     daParamCollectionlist.add(ParamCollection);
276 //                    daParamCollectionService.insertDaParamCollection(ParamCollection);
a5b351 277                 }
C 278             }
054a69 279             daParamCollectionService.saveBeachDaParamCollection(daParamCollectionlist);
a5b351 280         }
8f0f8d 281         addBaseData(workOrderNo,productType,device,packCode);
a5b351 282
C 283     }
8f0f8d 284
285     public static void addBaseData(String workOrderNo,String productCode,String locationCode,String packCode){
286         Map<String, String> map = new HashMap<>();
287         map.put("GC", "南浔工厂");
288         map.put("CXBH", "Pack线");
289         map.put("SBBH", "设备001");
290         map.put("YGBH", "员工001");
291         List<DaParamCollection> confList = new ArrayList<>();
292         map.forEach((key, value) -> {
293             List<DaCollectionParamConf> daCollectionParamConfs = collectionParamConfService.list(new LambdaQueryWrapper<DaCollectionParamConf>()
294                     .eq(DaCollectionParamConf::getProcessesCode,locationCode)
295                     .like(DaCollectionParamConf::getCollectParameterId,key));
296             DaParamCollection saveData = new DaParamCollection();
297             saveData.setWorkOrderNo(workOrderNo);
298             saveData.setProductCode(productCode);
299             saveData.setLocationCode(locationCode);
300             saveData.setSfcCode(packCode);
301             saveData.setParamCode(daCollectionParamConfs.get(0).getCollectParameterId());
302             saveData.setParamName(daCollectionParamConfs.get(0).getCollectParameterName());
303             saveData.setCollectionTime(new Date());
304             saveData.setParamValue(value);
305             confList.add(saveData);
306         });
307         daParamCollectionService.insertBatch(confList);
308     }
a5b351 309 }