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