wujian
2024-03-28 1e39c55183c1f54d1587bf3a86b4c2210bcd592e
提交 | 用户 | 时间
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     //自动工位
054a69 27     public List<String> automaticList = Arrays.asList("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
054a69 35     public static IDaCollectionParamConfService collectionParamConfService;
a5b351 36
054a69 37     public static IDaParamCollectionService daParamCollectionService;
a5b351 38
C 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                         List<String> list = new ArrayList<>();
114                         String[] suffixes = {"Torque", "Angle", "Result", "Result1"};
115
116                         for (String suffix : suffixes) {
117                             String string = thoroughfare + "." + device + "." + suffix;
118                             list.add(string);
119                         }
49c784 120                         List<ReadWriteEntity> list1 = miloService.readFromOpcUa(list);
121                         List<Object> collect = list1.stream().map(ReadWriteEntity::getValue).collect(Collectors.toList());
122                         String joinedString = String.join(",", collect.toString());
123                         WebSocketUsers.sendMessageToUserByText(map.get(device), joinedString);
a5b351 124                     }
C 125                 }
126             }
127         } catch (Exception e) {
128
129         }
130     }
131
132
133
134     /**
135      * 获取SNCode
136      */
137     public String getSNCode(){
138         String SNCode = "";
139         return SNCode;
140     }
141
142
143     /**
144      * 出站保存数据
145      */
146     public String outSaveDate(String thoroughfare,String device) {
147         String result = "";
148         try {
149             //读取SNCode
32483a 150             String PACKCode = thoroughfare + "." + device + ".PACKCode";
151             Object PACKCodeObject = miloService.readFromOpcUa(PACKCode).getValue();
152             if(null == PACKCodeObject || "".equals(PACKCodeObject)){
a5b351 153                result = "22";
C 154             }else{
32483a 155                 String PACKCodeParam = PACKCodeObject.toString();
a5b351 156                 //1、更新工单信息
C 157                 //updateOrderInfo();
158                 //2、保存过站采集数据
32483a 159                 saveStationInfo(PACKCodeParam,thoroughfare,device);
a5b351 160                 //3、保存参数采集数据
054a69 161                 SaveParamData(PACKCodeParam,thoroughfare,device,"","");
a5b351 162
C 163                 result = "21";
164
165             }
166
167         }catch (Exception e) {
168
169         }
170         return result;
171     }
172
173     /**
174      * 保存过站采集
175      */
32483a 176     public void saveStationInfo(String packCode,String thoroughfare,String device) throws Exception {
177         String prefix = thoroughfare+"."+device+".";
178         String workOrderNo = miloService.readFromOpcUa(prefix + "WorkOrderNumber").getValue().toString();
179         String startTime = miloService.readFromOpcUa(prefix + "StartTime").getValue().toString();
180         String stopTime = miloService.readFromOpcUa(prefix + "StopTime").getValue().toString();
181         String stationStatus = miloService.readFromOpcUa(prefix + "StationStatus").getValue().toString();
182
a5b351 183         DaPassingStationCollection daPassingStationCollection = new DaPassingStationCollection();
32483a 184         daPassingStationCollection.setSfcCode(packCode);
185         daPassingStationCollection.setWorkOrderNo(workOrderNo);
186 //        daPassingStationCollection.setInboundTime(startTime);//入站时间
187 //        daPassingStationCollection.setOutboundTime(stopTime);//出站时间
188         daPassingStationCollection.setOutRsSign(stationStatus);//出站是否合格
a5b351 189         daPassingStationCollectionService.insertDaPassingStationCollection(daPassingStationCollection);
C 190     }
191
054a69 192     public static void SaveParamData(String packCode,String thoroughfare,String device,String workOrderNo,String productType) throws Exception {
a5b351 193         List<DaCollectionParamConf> list;
C 194         DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf();
195         daCollectionParamConf.setGatherAddress(thoroughfare+ "." + device);
196         list = collectionParamConfService.selectDaCollectionParamConfList(daCollectionParamConf);
197
198         List<String> nodeIdList = list.stream().map(info -> {
199             String nodeid = info.getGatherAddress();
200             return nodeid;
201         }).collect(Collectors.toList());
202
203         if(!nodeIdList.isEmpty()){
204             List<ReadWriteEntity> readWriteEntityList = miloService.readFromOpcUa(nodeIdList);
205             List<DaParamCollection> daParamCollectionlist = new ArrayList<>();
206             for(int i=0;i<nodeIdList.size();i++){
207                 if(!readWriteEntityList.get(i).getValue().toString().equals("0.0")){
32483a 208                     DaParamCollection ParamCollection = new DaParamCollection();
209 //                    ParamCollection.setParamCode(readWriteEntityList.get(i).getIdentifier().toString().split("[.]")[2]);
210                     ParamCollection.setParamCode(list.get(i).getCollectParameterId());
a5b351 211                     ParamCollection.setLocationCode(device);
C 212                     ParamCollection.setParamValue(readWriteEntityList.get(i).getValue().toString());
32483a 213                     ParamCollection.setSfcCode(packCode);
a5b351 214                     ParamCollection.setParamName(list.get(i).getCollectParameterName());
C 215                     ParamCollection.setParamUpper(list.get(i).getParamUpper());
216                     ParamCollection.setParamLower(list.get(i).getParamLower());
217                     ParamCollection.setUnit(list.get(i).getCollectParameterUnit());
218                     //ParamCollection.setState("合格");
219                     ParamCollection.setType(list.get(i).getCollectParameterType());
220                     ParamCollection.setCollectionTime(new Date());
054a69 221                     ParamCollection.setWorkOrderNo(workOrderNo);
222                     ParamCollection.setProductCode(productType);
223                     daParamCollectionlist.add(ParamCollection);
224 //                    daParamCollectionService.insertDaParamCollection(ParamCollection);
a5b351 225                 }
C 226             }
054a69 227             daParamCollectionService.saveBeachDaParamCollection(daParamCollectionlist);
a5b351 228         }
C 229
230     }
231 }