春风项目四线(合箱线、总装线)
yyt
2024-02-19 a69d63feef10e3d13e91e3647274a889a311db5a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.jcdm.main.da.opcuaconfig.cert;
 
import com.jcdm.main.da.collectionParamConf.domain.DaCollectionParamConf;
import com.jcdm.main.da.collectionParamConf.service.IDaCollectionParamConfService;
import com.jcdm.main.da.opcuaconfig.client.ClientHandler;
import com.jcdm.main.da.opcuaconfig.domain.NodeEntity;
import com.jcdm.main.da.opcuaconfig.init.BeanUtils;
import com.jcdm.main.da.paramCollection.domain.DaParamCollection;
import com.jcdm.main.da.paramCollection.service.IDaParamCollectionService;
import com.jcdm.main.da.passingStationCollection.domain.DaPassingStationCollection;
import com.jcdm.main.da.passingStationCollection.service.IDaPassingStationCollectionService;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
 
import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;
 
import static com.jcdm.main.da.opcuaconfig.client.ClientHandler.readValues;
 
@Slf4j
public class MethodName {
    private IDaCollectionParamConfService collectionParamConfService = BeanUtils.getBean(IDaCollectionParamConfService.class);
    private IDaParamCollectionService daParamCollectionService = BeanUtils.getBean(IDaParamCollectionService.class);
    private IDaPassingStationCollectionService daPassingStationCollectionService = BeanUtils.getBean(IDaPassingStationCollectionService.class);
    public void Transit(String Node, String value) throws Exception {
        //log.info(Node+":"+value);
        String[] parts = Node.split("[.]");
        ClientHandler clientHandler=new ClientHandler();
        switch (value) {
            case "0":   //初始
                NodeEntity node= NodeEntity.builder().index(2).identifier(parts[0]+"."+parts[1]+".MesRecordDataDone").value("0").type("short").build();
                Boolean out=clientHandler.write(node);
                log.info("节点:{},响应结果:{}", node.getIdentifier(),out);
                break;
            case "1":   //请求下发进站状态
                NodeEntity node1= NodeEntity.builder().index(2).identifier(parts[0]+"."+parts[1]+".MesRecordDataDone").value("11").type("short").build();
                Boolean out1=clientHandler.write(node1);
                log.info("节点:{},响应结果:{}", node1.getIdentifier(),out1);
                break;
            case "2":   //请求记录工位数据
                NodeEntity SN_node= NodeEntity.builder().index(2).identifier(parts[0]+"."+parts[1]+".SNCode").value("").type("").build();
                String SNCode=clientHandler.read(SN_node);
                if(SNCode==null){
                    NodeEntity node2= NodeEntity.builder().index(2).identifier(parts[0]+"."+parts[1]+".MesRecordDataDone").value("22").type("short").build();
                    Boolean out2=clientHandler.write(node2);
                    break;
                }
                List<DaCollectionParamConf> b;
                DaCollectionParamConf daCollectionParamConf=new DaCollectionParamConf();
                daCollectionParamConf.setGatherAddress(parts[0]+"."+parts[1]);
                b=collectionParamConfService.selectDaCollectionParamConfList(daCollectionParamConf);
 
                List<NodeId> nodeId = b.stream().map(info -> {
                    NodeId nodeid = new NodeId(2,info.getGatherAddress());
                    return nodeid;
                }).collect(Collectors.toList());
                List<DataValue> s=readValues(nodeId);
                //保存PLC采集数据
                for(int i=0;i<nodeId.size();i++)
                {
                    DaParamCollection Config=new DaParamCollection();
                    Config.setParamCode(nodeId.get(i).getIdentifier().toString().split("[.]")[2]);
                    Config.setLocationCode(nodeId.get(i).getIdentifier().toString().split("[.]")[1]);
                    Config.setParamValue(s.get(i).getValue().getValue().toString());
                    Config.setSfcCode(SNCode);
                    Config.setParamName(b.get(i).getGatherAddress());
                    daParamCollectionService.insertDaParamCollection(Config);
                }
                //记录过站,并计算节拍。
                DaPassingStationCollection PassingStationCollection=new DaPassingStationCollection();
                PassingStationCollection.setSfcCode(SNCode);
                PassingStationCollection.setLocationCode(parts[1]);
                PassingStationCollection.setInboundTime(new Date());
                PassingStationCollection.setOutboundTime(new Date());
                PassingStationCollection.setCollectionTime(new Date());
                PassingStationCollection.setBeatTime("56");
                daPassingStationCollectionService.insertDaPassingStationCollection(PassingStationCollection);
                //更新PLC节点状态
                NodeEntity node2= NodeEntity.builder().index(2).identifier(parts[0]+"."+parts[1]+".MesRecordDataDone").value("21").type("short").build();
                Boolean out2=clientHandler.write(node2);
                log.info("节点:{},响应结果:{}", node2.getIdentifier(),out2);
                break;
            default:
                break;
        }
    }
 
    public void Transit2(String Node, String value) throws Exception {
        log.info(Node+":"+value);
        String[] parts = Node.split("[.]");
        ClientHandler clientHandler=new ClientHandler();
        NodeEntity SN_node= NodeEntity.builder().index(2).identifier(parts[0]+"."+parts[1]+".SNCode").value("").type("").build();
        String SNCode=clientHandler.read(SN_node);
        log.info("读取节点:{},结果:{}", SN_node.getIdentifier(),SNCode);
        switch (value) {
            case "0":   //初始
                NodeEntity node= NodeEntity.builder().index(2).identifier(parts[0]+"."+parts[1]+".MesRecordDataDone").value("0").type("short").build();
                Boolean out=clientHandler.write(node);
                log.info("节点:{},响应结果:{}", node.getIdentifier(),out);
                break;
            case "1":   //请求下发进站状态
                NodeEntity node1= NodeEntity.builder().index(2).identifier(parts[0]+"."+parts[1]+".MesRecordDataDone").value("11").type("short").build();
                Boolean out1=clientHandler.write(node1);
                DaPassingStationCollection PassingStationCollection=new DaPassingStationCollection();
                PassingStationCollection.setSfcCode(SNCode);
                PassingStationCollection.setLocationCode(parts[1]);
                PassingStationCollection.setInboundTime(new Date());
                daPassingStationCollectionService.insertDaPassingStationCollection(PassingStationCollection);
                log.info("节点:{},响应结果:{}", node1.getIdentifier(),out1);
                break;
            case "2":   //请求记录工位数据
                List<DaCollectionParamConf> b;
                DaCollectionParamConf daCollectionParamConf=new DaCollectionParamConf();
                daCollectionParamConf.setGatherAddress(parts[0]+"."+parts[1]);
                b=collectionParamConfService.selectDaCollectionParamConfList(daCollectionParamConf);
 
                List<NodeId> nodeId = b.stream().map(info -> {
                    NodeId nodeid = new NodeId(2,info.getGatherAddress());
                    return nodeid;
                }).collect(Collectors.toList());
                List<DataValue> s=readValues(nodeId);
                //保存PLC采集数据
                for(int i=0;i<nodeId.size();i++)
                {
                    DaParamCollection Config=new DaParamCollection();
                    Config.setParamCode(nodeId.get(i).getIdentifier().toString().split("[.]")[2]);
                    Config.setLocationCode(nodeId.get(i).getIdentifier().toString().split("[.]")[1]);
                    Config.setParamValue(s.get(i).getValue().getValue().toString());
                    Config.setSfcCode(SNCode);
                    Config.setParamName(b.get(i).getGatherAddress());
                    daParamCollectionService.insertDaParamCollection(Config);
                }
                //更新出站时间,计算节拍。
                DaPassingStationCollection PSC=new DaPassingStationCollection();
                PSC.setSfcCode(SNCode);
                List<DaPassingStationCollection> LPSC=daPassingStationCollectionService.selectDaPassingStationCollectionList(PSC);
                if(LPSC != null && LPSC.size() > 0){
                    LPSC.get(0).setOutboundTime(new Date());
                    LPSC.get(0).setCollectionTime(new Date());
                    LPSC.get(0).setBeatTime(Long.toString(Math.abs(new Date().getTime() - LPSC.get(0).getInboundTime().getTime())));
                    daPassingStationCollectionService.updateDaPassingStationCollection(LPSC.get(0));
                }
                //更新PLC节点状态
                NodeEntity node2= NodeEntity.builder().index(2).identifier(parts[0]+"."+parts[1]+".MesRecordDataDone").value("21").type("short").build();
                Boolean out2=clientHandler.write(node2);
                log.info("节点:{},响应结果:{}", node2.getIdentifier(),out2);
                break;
            default:
                break;
        }
    }
 
    public void SNRetrieval(String Node, String value) throws Exception {
        String[] parts = Node.split("[.]");
        if(value=="1") {
            //SN号检索
            ClientHandler clientHandler=new ClientHandler();
            NodeEntity SN_node= NodeEntity.builder().index(2).identifier(parts[0]+"."+parts[1]+".SNCode").value("").type("").build();
            String SNCode=clientHandler.read(SN_node);
 
            String a=daPassingStationCollectionService.SelectSN(SNCode);
 
            // 1:OK可生产 2:NG不可生产 3:NG可返工 4:PC检索失败(无记录)5:PC检索失败(软件)
            NodeEntity node1= NodeEntity.builder().index(2).identifier(parts[0]+"."+parts[1]+".CodeCheckFeed").value(a).type("short").build();
            Boolean out1=clientHandler.write(node1);
            DaPassingStationCollection PassingStationCollection=new DaPassingStationCollection();
            PassingStationCollection.setSfcCode(SNCode);
            PassingStationCollection.setLocationCode(parts[1]);
            PassingStationCollection.setInboundTime(new Date());
            daPassingStationCollectionService.insertDaPassingStationCollection(PassingStationCollection);
        }
    }
    public void SaveData(String Node, String value) throws Exception {
        String[] parts = Node.split("[.]");
        if(value=="1") {
            //采集数据最终保存
            ClientHandler clientHandler=new ClientHandler();
            NodeEntity SN_node= NodeEntity.builder().index(2).identifier(parts[0]+"."+parts[1]+".SNCode").value("").type("").build();
            String SNCode=clientHandler.read(SN_node);
            List<DaCollectionParamConf> b;
            DaCollectionParamConf daCollectionParamConf=new DaCollectionParamConf();
            daCollectionParamConf.setGatherAddress(parts[0]+"."+parts[1]);
            b=collectionParamConfService.selectDaCollectionParamConfList(daCollectionParamConf);
 
            List<NodeId> nodeId = b.stream().map(info -> {
                NodeId nodeid = new NodeId(2,info.getGatherAddress());
                return nodeid;
            }).collect(Collectors.toList());
            List<DataValue> s=readValues(nodeId);
            //保存PLC采集数据
            for(int i=0;i<nodeId.size();i++)
            {
                DaParamCollection Config=new DaParamCollection();
                Config.setParamCode(nodeId.get(i).getIdentifier().toString().split("[.]")[2]);
                Config.setLocationCode(nodeId.get(i).getIdentifier().toString().split("[.]")[1]);
                Config.setParamValue(s.get(i).getValue().getValue().toString());
                Config.setSfcCode(SNCode);
                Config.setParamName(b.get(i).getGatherAddress());
                daParamCollectionService.insertDaParamCollection(Config);
            }
            //更新出站时间,计算节拍。
            DaPassingStationCollection PSC=new DaPassingStationCollection();
            PSC.setSfcCode(SNCode);
            List<DaPassingStationCollection> LPSC=daPassingStationCollectionService.selectDaPassingStationCollectionList(PSC);
            if(LPSC != null && LPSC.size() > 0){
                LPSC.get(0).setOutboundTime(new Date());
                LPSC.get(0).setCollectionTime(new Date());
                LPSC.get(0).setBeatTime(Long.toString(Math.abs(new Date().getTime() - LPSC.get(0).getInboundTime().getTime())));
                daPassingStationCollectionService.updateDaPassingStationCollection(LPSC.get(0));
            }
            //请求最终保存反馈:1 保存完成 2 保存失败 3 保存失败,数据位超长
            NodeEntity node1= NodeEntity.builder().index(2).identifier(parts[0]+"."+parts[1]+".SaveFeedLast").value("1").type("short").build();
            Boolean out1=clientHandler.write(node1);
        }
    }
}