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