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