提交 | 用户 | 时间
|
e4c3b0
|
1 |
package com.jcdm.main.da.opcuaconfig.client; |
Y |
2 |
|
|
3 |
import com.google.common.collect.ImmutableList; |
|
4 |
import com.jcdm.main.da.opcuaconfig.controller.DaOpcuaConfigController; |
|
5 |
import com.jcdm.main.da.opcuaconfig.domain.DaOpcuaConfig; |
|
6 |
import com.jcdm.main.da.opcuaconfig.domain.NodeEntity; |
|
7 |
import com.jcdm.main.da.opcuaconfig.service.IDaOpcuaConfigService; |
|
8 |
import lombok.extern.slf4j.Slf4j; |
|
9 |
import org.eclipse.milo.opcua.sdk.client.OpcUaClient; |
|
10 |
import org.eclipse.milo.opcua.sdk.client.api.nodes.VariableNode; |
|
11 |
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription; |
|
12 |
import org.eclipse.milo.opcua.stack.core.AttributeId; |
|
13 |
import org.eclipse.milo.opcua.stack.core.BuiltinDataType; |
|
14 |
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue; |
|
15 |
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId; |
|
16 |
import org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode; |
|
17 |
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant; |
|
18 |
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned; |
|
19 |
import org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode; |
|
20 |
import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn; |
|
21 |
import org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest; |
|
22 |
import org.eclipse.milo.opcua.stack.core.types.structured.MonitoringParameters; |
|
23 |
import org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId; |
|
24 |
import org.springframework.beans.factory.annotation.Autowired; |
|
25 |
import org.springframework.stereotype.Service; |
|
26 |
import org.springframework.util.CollectionUtils; |
|
27 |
|
|
28 |
import java.lang.reflect.InvocationTargetException; |
|
29 |
import java.lang.reflect.Method; |
|
30 |
import java.util.ArrayList; |
|
31 |
import java.util.List; |
|
32 |
import java.util.Set; |
|
33 |
import java.util.concurrent.ExecutionException; |
|
34 |
|
|
35 |
/** |
|
36 |
* @ClassName: ClientHandler |
|
37 |
* @Description: 客户端处理 |
|
38 |
* @author Jellyleo |
|
39 |
* @date 2019年12月12日 |
|
40 |
*/ |
|
41 |
@Slf4j |
|
42 |
@Service |
|
43 |
public class ClientHandler { |
|
44 |
|
|
45 |
// 客户端实例 |
|
46 |
public static OpcUaClient client = null; |
|
47 |
|
|
48 |
public List<DaOpcuaConfig> b = null; |
|
49 |
|
|
50 |
@Autowired |
|
51 |
private ClientRunner clientRunner; |
|
52 |
|
|
53 |
@Autowired |
|
54 |
private IDaOpcuaConfigService daOpcuaConfigService; |
|
55 |
|
|
56 |
|
|
57 |
/** |
|
58 |
* |
|
59 |
* @MethodName: connect |
|
60 |
* @Description: connect |
|
61 |
* @throws Exception |
|
62 |
* @CreateTime 2019年12月18日 上午10:41:09 |
|
63 |
*/ |
|
64 |
public String connect() throws Exception { |
|
65 |
|
|
66 |
if (client != null) { |
|
67 |
return "客户端已创建"; |
|
68 |
} |
|
69 |
|
|
70 |
client = clientRunner.run(); |
|
71 |
|
|
72 |
if (client == null) { |
|
73 |
return "客户端配置实例化失败"; |
|
74 |
} |
|
75 |
|
|
76 |
// 创建连接 |
|
77 |
client.connect().get(); |
|
78 |
return "创建连接成功"; |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* @MethodName: disconnect |
|
83 |
* @Description: 断开连接 |
|
84 |
* @return |
|
85 |
* @throws Exception |
|
86 |
* @CreateTime 2019年12月18日 上午10:45:21 |
|
87 |
*/ |
|
88 |
public String disconnect() throws Exception { |
|
89 |
|
|
90 |
if (client == null) { |
|
91 |
return "连接已断开"; |
|
92 |
} |
|
93 |
|
|
94 |
// 断开连接 |
|
95 |
clientRunner.getFuture().complete(client); |
|
96 |
client = null; |
|
97 |
return "断开连接成功"; |
|
98 |
} |
|
99 |
|
|
100 |
/** |
|
101 |
* @MethodName: subscribe |
|
102 |
* @Description: 订阅节点变量 |
|
103 |
* @throws Exception |
|
104 |
* @CreateTime 2019年12月18日 上午10:38:11 |
|
105 |
*/ |
|
106 |
public String subscribe(List<NodeEntity> nodes) throws Exception { |
|
107 |
|
|
108 |
if (client == null) { |
|
109 |
return "找不到客户端,操作失败"; |
|
110 |
} |
|
111 |
|
|
112 |
// 查询订阅对象,没有则创建 |
|
113 |
UaSubscription subscription = null; |
|
114 |
ImmutableList<UaSubscription> subscriptionList = client.getSubscriptionManager().getSubscriptions(); |
|
115 |
if (CollectionUtils.isEmpty(subscriptionList)) { |
|
116 |
subscription = client.getSubscriptionManager().createSubscription(1000.0).get(); |
|
117 |
} else { |
|
118 |
subscription = subscriptionList.get(0); |
|
119 |
} |
|
120 |
|
|
121 |
// 监控项请求列表 |
|
122 |
List<MonitoredItemCreateRequest> requests = new ArrayList<>(); |
|
123 |
|
|
124 |
if (!CollectionUtils.isEmpty(nodes)) { |
|
125 |
for (NodeEntity node : nodes) { |
|
126 |
// 创建监控的参数 |
|
127 |
MonitoringParameters parameters = new MonitoringParameters(subscription.nextClientHandle(), 1000.0, // sampling |
|
128 |
// interval |
|
129 |
null, // filter, null means use default |
|
130 |
Unsigned.uint(10), // queue size |
|
131 |
true // discard oldest |
|
132 |
); |
|
133 |
// 创建订阅的变量, 创建监控项请 求 |
|
134 |
MonitoredItemCreateRequest request = new MonitoredItemCreateRequest( |
|
135 |
new ReadValueId(new NodeId(node.getIndex(), node.getIdentifier()), AttributeId.Value.uid(), |
|
136 |
null, null), |
|
137 |
MonitoringMode.Reporting, parameters); |
|
138 |
requests.add(request); |
|
139 |
} |
|
140 |
} |
|
141 |
|
|
142 |
// 创建监控项,并且注册变量值改变时候的回调函数 |
|
143 |
subscription.createMonitoredItems(TimestampsToReturn.Both, requests, (item, id) -> { |
|
144 |
item.setValueConsumer((i, v) -> { |
|
145 |
handle(i.getReadValueId().getNodeId(), v.getValue()); |
|
146 |
}); |
|
147 |
}).get(); |
|
148 |
|
|
149 |
return "订阅成功"; |
|
150 |
} |
|
151 |
|
|
152 |
/** |
|
153 |
* * @MethodName: write |
|
154 |
* @Description: 回调函数 |
|
155 |
* @CreateTime 2023年10月13日 |
|
156 |
*/ |
|
157 |
public void handle(NodeId id, Variant value){ |
|
158 |
if (b == null || 0 > b.size()) { |
|
159 |
DaOpcuaConfig opcuaConfParam=new DaOpcuaConfig(); |
28cd73
|
160 |
opcuaConfParam.setSubscribe("Y"); |
e4c3b0
|
161 |
b=daOpcuaConfigService.selectDaOpcuaConfigList(opcuaConfParam); |
Y |
162 |
} |
|
163 |
//使用Stream API在List<T>中查找元素 |
|
164 |
DaOpcuaConfig daOpcuaConfig = b.stream() |
|
165 |
.filter(customer ->id.getIdentifier().toString().equals(customer.getNode())) |
|
166 |
.findAny() |
|
167 |
.orElse(null); |
|
168 |
try { |
054abe
|
169 |
Class<?> clazz = Class.forName("com.jcdm.main.da.opcuaconfig.cert.MethodName"); |
e4c3b0
|
170 |
Method method = clazz.getMethod(daOpcuaConfig.getrFunction(), new Class[] { String.class, String.class }); |
a69d63
|
171 |
if (value.isNull()){ |
Y |
172 |
method.invoke(clazz.newInstance(),new Object[] { |
|
173 |
new String(id.getIdentifier().toString()), new String("") }); |
|
174 |
}else{ |
e4c3b0
|
175 |
method.invoke(clazz.newInstance(),new Object[] { |
Y |
176 |
new String(id.getIdentifier().toString()), new String(value.getValue().toString()) }); |
a69d63
|
177 |
} |
e4c3b0
|
178 |
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | |
Y |
179 |
InvocationTargetException e) { |
|
180 |
e.printStackTrace(); |
|
181 |
} |
|
182 |
} |
|
183 |
|
|
184 |
|
|
185 |
/** |
|
186 |
* @MethodName: write |
|
187 |
* @Description: 变节点量写入 |
|
188 |
* @param node |
|
189 |
* @throws Exception |
|
190 |
* @CreateTime 2019年12月18日 上午9:51:40 |
|
191 |
*/ |
|
192 |
public static Boolean write(NodeEntity node) throws Exception { |
|
193 |
|
|
194 |
if (client == null) { |
|
195 |
log.info("找不到客户端,操作失败"); |
|
196 |
return false; |
|
197 |
} |
|
198 |
|
|
199 |
NodeId nodeId = new NodeId(node.getIndex(), node.getIdentifier()); |
|
200 |
Variant value = null; |
|
201 |
switch (node.getType()) { |
|
202 |
case "int": |
|
203 |
value = new Variant(Integer.parseInt(node.getValue().toString())); |
|
204 |
break; |
|
205 |
case "boolean": |
|
206 |
value = new Variant(Boolean.parseBoolean(node.getValue().toString())); |
|
207 |
break; |
|
208 |
case "short": |
|
209 |
value = new Variant(Short.parseShort(node.getValue().toString())); |
|
210 |
break; |
|
211 |
case "long": |
|
212 |
value = new Variant(Long.parseLong(node.getValue().toString())); |
|
213 |
break; |
|
214 |
case "string": |
|
215 |
value = new Variant(node.getValue().toString()); |
|
216 |
break; |
|
217 |
case "char": |
|
218 |
value = new Variant(node.getValue().toString().charAt(0)); |
|
219 |
break; |
|
220 |
} |
|
221 |
DataValue dataValue = new DataValue(value, null, null); |
|
222 |
|
|
223 |
StatusCode statusCode = client.writeValue(nodeId, dataValue).get(); |
|
224 |
|
|
225 |
return statusCode.isGood(); |
|
226 |
} |
4c41b4
|
227 |
|
Y |
228 |
/** |
|
229 |
* 方法描述: 读取多个点位的值 |
|
230 |
* |
|
231 |
* @param nodeIdList 点位集合 |
|
232 |
* @return {@link List<DataValue>} |
|
233 |
* @throws |
|
234 |
*/ |
|
235 |
public static List<DataValue> readValues(List<NodeId> nodeIdList){ |
e4c3b0
|
236 |
try { |
Y |
237 |
List<DataValue> dataValues=client.readValues(0.0, TimestampsToReturn.Both,nodeIdList).get(); |
|
238 |
return dataValues; |
|
239 |
} catch (InterruptedException | ExecutionException e) { |
|
240 |
e.printStackTrace(); |
|
241 |
} |
|
242 |
return null; |
|
243 |
} |
|
244 |
|
|
245 |
/** |
|
246 |
* @MethodName: read |
|
247 |
* @Description: 读取 |
|
248 |
* @param node |
|
249 |
* @return |
|
250 |
* @throws Exception |
|
251 |
* @CreateTime 2019年12月19日 下午2:40:34 |
|
252 |
*/ |
|
253 |
public String read(NodeEntity node) throws Exception { |
|
254 |
|
|
255 |
if (client == null) { |
|
256 |
return "找不到客户端,操作失败"; |
|
257 |
} |
|
258 |
|
|
259 |
NodeId nodeId = new NodeId(node.getIndex(), node.getIdentifier()); |
|
260 |
VariableNode vnode = client.getAddressSpace().createVariableNode(nodeId); |
|
261 |
DataValue value = vnode.readValue().get(); |
|
262 |
log.info("Value={}", value); |
|
263 |
|
|
264 |
Variant variant = value.getValue(); |
|
265 |
|
a69d63
|
266 |
if(null == variant.getValue()) |
Y |
267 |
{ |
|
268 |
return null; |
|
269 |
}else{ |
|
270 |
return variant.getValue().toString(); |
|
271 |
} |
e4c3b0
|
272 |
|
Y |
273 |
} |
|
274 |
|
|
275 |
|
|
276 |
/** |
|
277 |
* 方法描述: 写入多个节点的值 |
|
278 |
* |
|
279 |
* @param keys 节点集合 |
|
280 |
* @param values 值集合 |
|
281 |
* @param client 客户端 |
|
282 |
* @return {@link Object} |
|
283 |
* @throws |
|
284 |
*/ |
|
285 |
public static Object writeValues(Set<String> keys, List<Object> values, OpcUaClient client){ |
|
286 |
List<NodeId> nodeIs=new ArrayList<>(keys.size()); |
|
287 |
keys.forEach(e->{ |
|
288 |
NodeId nodeId = new NodeId(2, e); |
|
289 |
nodeIs.add(nodeId); |
|
290 |
}); |
|
291 |
List<DataValue> dataValues=new ArrayList<>(values.size()); |
|
292 |
values.forEach(e->{ |
|
293 |
Variant value=new Variant(Double.parseDouble(e.toString())); |
|
294 |
DataValue dataValue=new DataValue(value); |
|
295 |
dataValues.add(dataValue); |
|
296 |
}); |
|
297 |
try { |
|
298 |
client.writeValues(nodeIs,dataValues).get(); |
|
299 |
} catch (InterruptedException | ExecutionException e) { |
|
300 |
e.printStackTrace(); |
|
301 |
} |
|
302 |
return null; |
|
303 |
} |
|
304 |
} |