春风项目四线(合箱线、总装线)
wujian
2024-03-16 059083082a6d284821b70eb7bb6805763014c402
提交 | 用户 | 时间
3e03af 1 package com.jcdm.main.plcserver.sub;
C 2
3
4 import com.jcdm.main.da.collectionParamConf.domain.DaCollectionParamConf;
5 import com.jcdm.main.da.collectionParamConf.service.IDaCollectionParamConfService;
059083 6 import com.jcdm.main.da.opcuaconfig.domain.DaOpcuaConfig;
3e03af 7 import com.jcdm.main.da.paramCollection.domain.DaParamCollection;
C 8 import com.jcdm.main.da.paramCollection.service.IDaParamCollectionService;
059083 9 import com.jcdm.main.da.passingStationCollection.domain.DaPassingStationCollection;
W 10 import com.jcdm.main.da.passingStationCollection.service.impl.DaPassingStationCollectionServiceImpl;
3e03af 11 import com.jcdm.main.plcserver.conf.OPCElement;
C 12 import com.kangaroohy.milo.model.ReadWriteEntity;
13 import com.kangaroohy.milo.runner.subscription.SubscriptionCallback;
14 import com.kangaroohy.milo.service.MiloService;
059083 15 import lombok.extern.slf4j.Slf4j;
3e03af 16 import org.springframework.stereotype.Component;
C 17
059083 18 import java.text.Format;
3e03af 19 import java.util.ArrayList;
C 20 import java.util.Date;
059083 21 import java.util.HashMap;
3e03af 22 import java.util.List;
C 23 import java.util.stream.Collectors;
24
25
059083 26 @Slf4j
3e03af 27 @Component
C 28 public class OPCUaSubscription implements SubscriptionCallback {
29
30     public static MiloService miloService;
31
32     public IDaCollectionParamConfService collectionParamConfService;
33
34     public IDaParamCollectionService daParamCollectionService;
35
059083 36     public DaPassingStationCollectionServiceImpl passingStationCollectionServiceImpl;
W 37
38     public List<DaOpcuaConfig> lists;
39
40     public static final HashMap<String,Integer> map = new HashMap<>();
41
3e03af 42     public OPCUaSubscription(MiloService miloService,
C 43                              IDaCollectionParamConfService collectionParamConfService,
059083 44                              IDaParamCollectionService daParamCollectionService,
W 45                              DaPassingStationCollectionServiceImpl passingStationCollectionServiceImpl,
46                              List<DaOpcuaConfig> lists) {
3e03af 47         OPCUaSubscription.miloService = miloService;
C 48         this.collectionParamConfService = collectionParamConfService;
49         this.daParamCollectionService = daParamCollectionService;
059083 50         this.passingStationCollectionServiceImpl = passingStationCollectionServiceImpl;
W 51         this.lists = lists;
52
3e03af 53
C 54     }
55
56
57     @Override
58     public void onSubscribe(String identifier, Object value) {
59         String ecpStr = "";//异常记录标记
60         try {
61             if(null != value) {
059083 62                 //1、检索SN号
W 63                 //2、过站参数采集
64                 //3、扫码枪数据回传
65                 List<String> collect1 = lists.stream().filter(x -> OPCElement.SN_CHECK.equals(x.getrFunction()))
66                         .map(DaOpcuaConfig::getNode).collect(Collectors.toList());
67                 List<String> collect2 = lists.stream().filter(x -> OPCElement.SAVE_DATA.equals(x.getrFunction()))
68                         .map(DaOpcuaConfig::getNode).collect(Collectors.toList());
69                 if (collect1.contains(identifier)){
70                     //sn
71                     this.SNRetrieval(identifier,value.toString());
72                     if (identifier.equals(OPCElement.OP120_ZZ_CODE_CHECK) && "1".equals(value.toString())){
73                         //总装上线扫码传输数据
74                         log.info("-------监听到,{}的扫码枪扫码的CODE_CHECK的信号",identifier);
75                         Integer i = map.getOrDefault(identifier + "的扫码枪扫码的CODE_CHECK的信号",0);
76                         if (0==i){
77                             map.put(identifier + "的扫码枪扫码的CODE_CHECK的信号",i+1);
78                         }
79                         String[] parts = OPCElement.OP120_ZZ_CODE_CHECK.split("[.]");
80                         Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code1").getValue();
81                         if (null != SNCodeObject){
82                             String SNCode = SNCodeObject.toString();
83                             passingStationCollectionServiceImpl.sendMessage(SNCode);
84                         }
85                     }
86                 }
87                 else if (collect2.contains(identifier)){
88                     //save
89                     this.SaveData(identifier);
90                     //返回plc保存成功
91                     String[] parts = identifier.split("[.]");
92                     if (parts.length==3){
93                         if ("SaveRequest".equals(parts[2])){
94                             ReadWriteEntity entity = new ReadWriteEntity(parts[0] + "." + parts[1] + ".SaveFeed", 1);
95                             log.info("-------监听到,{}的saveRequest的信号",identifier);
96                             Integer i = map.getOrDefault(identifier + "的saveRequest的信号",0);
97                             if (0==i){
98                                 map.put(identifier + "的saveRequest的信号",i+1);
99                             }
100 //                            miloService.writeToOpcByte(entity);
101                         }else if ("SaveRequestLast".equals(parts[2])){
102                             ReadWriteEntity entity = new ReadWriteEntity(parts[0] + "." + parts[1] + ".SaveFeedLast", 1);
103                             log.info("-------监听到,{}的SaveRequestLast的信号",identifier);
104                             Integer i = map.getOrDefault(identifier + "的SaveRequestLast的信号",0);
105                             if (0==i){
106                                 map.put(identifier + "的SaveRequestLast的信号",i+1);
107                             }
108 //                            miloService.writeToOpcByte(entity);
109                         }
110                     }
111
112
113                 }
3e03af 114
C 115                 //OP010保存请求
059083 116 //                if (identifier.equals(OPCElement.OP120_SaveRequestLast) && "1".equals(value.toString())) {
W 117 //                    this.SaveData(OPCElement.OP120_SaveRequestLast);
118 //                    //1、更新工单数据
119 //                    //2、保存过站采集数据
120 //                    //3、保存参数采集数据
121 //                    ReadWriteEntity entity = new ReadWriteEntity(OPCElement.OP010_MesSaveFeed, 1);
122 //                    miloService.writeToOpcByte(entity);
123 //                }
124 //                else if (identifier.equals(OPCElement.OP120_ZZ_CODE_CHECK) && "1".equals(value.toString())){
125 //                    //总装上线扫码传输数据
126 //                    String[] parts = OPCElement.OP120_ZZ_CODE_CHECK.split("[.]");
127 //                    Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code1").getValue();
128 //                    if (null != SNCodeObject){
129 //                        String SNCode = SNCodeObject.toString();
130 //                        passingStationCollectionServiceImpl.sendMessage(SNCode);
131 //                    }
132 //                }
133 //                //OP010请求检索条码
134 //                else if (identifier.equals(OPCElement.OP010_CodeCheck) && "1".equals(value.toString())) {
135 //                    ReadWriteEntity entity = new ReadWriteEntity(OPCElement.OP010_MesCodeCheckFeed, 1);
136 //                    miloService.writeToOpcByte(entity);
137 //                }
3e03af 138             }
C 139
140
141
142         } catch (Exception e) {
143
144         } finally {
145             if (!"".equals(ecpStr)) {
146                 System.out.println(ecpStr + "\r\n");
147
148             }
149         }
150     }
151
059083 152     public void SNRetrieval(String Node, String value) throws Exception {
W 153         String[] parts = Node.split("[.]");
154         if(value.equals("1")) {
155             //SN号检索
156             Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code").getValue();
157             if(null != SNCodeObject) {
158                 String SNCode=SNCodeObject.toString();
159                 String a=passingStationCollectionServiceImpl.SelectSN(SNCode,parts[1]);
160                 // 1:OK可生产 2:NG不可生产 3:NG可返工 4:PC检索失败(无记录)5:PC检索失败(软件)
161                 ReadWriteEntity entity = new ReadWriteEntity(parts[0]+"."+parts[1]+".CodeCheckFeed", a);
162                 log.info("-------监听到,{}的CodeCheck的信号",Node);
163                 Integer i = map.getOrDefault(Node + "的CodeCheck的信号",0);
164                 if (0==i){
165                     map.put(Node + "的CodeCheck的信号",i+1);
166                 }
167 //                miloService.writeToOpcByte(entity);
168                 DaPassingStationCollection PassingStationCollection=new DaPassingStationCollection();
169                 PassingStationCollection.setSfcCode(SNCode);
170                 PassingStationCollection.setLocationCode(parts[1]);
171                 PassingStationCollection.setInboundTime(new Date());
172                 passingStationCollectionServiceImpl.insertDaPassingStationCollection(PassingStationCollection);
173             }
174         }
175     }
176
177
3e03af 178     public void SaveData(String Node) throws Exception {
059083 179         String[] parts = Node.split("[.]");
3e03af 180         Object SNCodeObject = miloService.readFromOpcUa(parts[0] + "." + parts[1] + ".Code1").getValue();
C 181
182         if(null != SNCodeObject)
183         {
184             String SNCode = SNCodeObject.toString();
185             List<DaCollectionParamConf> list;
186             DaCollectionParamConf daCollectionParamConf = new DaCollectionParamConf();
187             daCollectionParamConf.setGatherAddress(parts[0] + "." + parts[1]);
188             list = collectionParamConfService.selectDaCollectionParamConfList(daCollectionParamConf);
189
190             List<String> nodeIdList = list.stream().map(info -> {
191                 String nodeid = info.getGatherAddress();
192                 return nodeid;
193             }).collect(Collectors.toList());
194
195             if(!nodeIdList.isEmpty()){
196                 List<ReadWriteEntity> readWriteEntityList = miloService.readFromOpcUa(nodeIdList);
197                 List<DaParamCollection> daParamCollectionlist = new ArrayList<>();
198                 for(int i=0;i<nodeIdList.size();i++){
199                     DaParamCollection ParamCollection = new DaParamCollection();
200                     if(!readWriteEntityList.get(i).getValue().toString().equals("0.0")){
201                         ParamCollection.setParamCode(readWriteEntityList.get(i).getIdentifier().toString().split("[.]")[2]);
202                         ParamCollection.setLocationCode(parts[1]);
203                         ParamCollection.setParamValue(readWriteEntityList.get(i).getValue().toString());
204                         ParamCollection.setSfcCode(SNCode);
205                         ParamCollection.setParamName(list.get(i).getCollectParameterName());
206                         ParamCollection.setParamUpper(list.get(i).getParamUpper());
207                         ParamCollection.setParamLower(list.get(i).getParamLower());
208                         ParamCollection.setUnit(list.get(i).getCollectParameterUnit());
209                         ParamCollection.setState("合格");
210                         ParamCollection.setType(list.get(i).getCollectParameterType());
211                         ParamCollection.setCollectionTime(new Date());
212                         daParamCollectionlist.add(ParamCollection);
213                     }
214                 }
215                 daParamCollectionService.saveBeachDaParamCollection(daParamCollectionlist);
216             }
059083 217         }
3e03af 218     }
C 219 }