提交 | 用户 | 时间
|
487b2f
|
1 |
package cn.stylefeng.guns.opcua.client; |
YY |
2 |
|
f4a343
|
3 |
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory; |
YY |
4 |
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo; |
|
5 |
import cn.stylefeng.guns.opcua.controller.OpcuaConfController; |
|
6 |
import cn.stylefeng.guns.opcua.entity.OpcuaConf; |
|
7 |
import cn.stylefeng.guns.opcua.mapper.OpcuaConfMapper; |
|
8 |
import cn.stylefeng.guns.opcua.model.params.OpcuaConfParam; |
|
9 |
import cn.stylefeng.guns.opcua.model.result.OpcuaConfResult; |
|
10 |
import cn.stylefeng.guns.opcua.service.OpcuaConfService; |
|
11 |
import cn.stylefeng.roses.kernel.model.response.ResponseData; |
|
12 |
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
487b2f
|
13 |
import com.google.common.collect.ImmutableList; |
YY |
14 |
import cn.stylefeng.guns.opcua.entity.NodeEntity; |
|
15 |
import lombok.extern.slf4j.Slf4j; |
f4a343
|
16 |
import org.apache.poi.ss.formula.functions.T; |
487b2f
|
17 |
import org.eclipse.milo.opcua.sdk.client.OpcUaClient; |
YY |
18 |
import org.eclipse.milo.opcua.sdk.client.api.nodes.VariableNode; |
|
19 |
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription; |
|
20 |
import org.eclipse.milo.opcua.stack.core.AttributeId; |
|
21 |
import org.eclipse.milo.opcua.stack.core.BuiltinDataType; |
|
22 |
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue; |
|
23 |
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId; |
|
24 |
import org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode; |
|
25 |
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant; |
|
26 |
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned; |
|
27 |
import org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode; |
|
28 |
import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn; |
|
29 |
import org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest; |
|
30 |
import org.eclipse.milo.opcua.stack.core.types.structured.MonitoringParameters; |
|
31 |
import org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId; |
|
32 |
import org.springframework.beans.factory.annotation.Autowired; |
|
33 |
import org.springframework.stereotype.Service; |
|
34 |
import org.springframework.util.CollectionUtils; |
|
35 |
|
|
36 |
import java.lang.reflect.InvocationTargetException; |
|
37 |
import java.lang.reflect.Method; |
|
38 |
import java.util.ArrayList; |
|
39 |
import java.util.List; |
|
40 |
|
|
41 |
/** |
|
42 |
* @ClassName: ClientHandler |
|
43 |
* @Description: 客户端处理 |
|
44 |
* @author Jellyleo |
|
45 |
* @date 2019年12月12日 |
|
46 |
*/ |
|
47 |
@Slf4j |
|
48 |
@Service |
|
49 |
public class ClientHandler { |
|
50 |
|
|
51 |
// 客户端实例 |
|
52 |
private OpcUaClient client = null; |
|
53 |
|
a7c91f
|
54 |
public List<OpcuaConfResult> b = null; |
YY |
55 |
|
487b2f
|
56 |
@Autowired |
YY |
57 |
private ClientRunner clientRunner; |
f4a343
|
58 |
|
YY |
59 |
@Autowired |
|
60 |
private OpcuaConfController opcuaConfController; |
69fbaa
|
61 |
|
487b2f
|
62 |
|
YY |
63 |
/** |
|
64 |
* |
|
65 |
* @MethodName: connect |
|
66 |
* @Description: connect |
|
67 |
* @throws Exception |
|
68 |
* @CreateTime 2019年12月18日 上午10:41:09 |
|
69 |
*/ |
|
70 |
public String connect() throws Exception { |
|
71 |
|
|
72 |
if (client != null) { |
|
73 |
return "客户端已创建"; |
|
74 |
} |
|
75 |
|
|
76 |
client = clientRunner.run(); |
|
77 |
|
|
78 |
if (client == null) { |
|
79 |
return "客户端配置实例化失败"; |
|
80 |
} |
|
81 |
|
|
82 |
// 创建连接 |
|
83 |
client.connect().get(); |
|
84 |
return "创建连接成功"; |
|
85 |
} |
|
86 |
|
|
87 |
/** |
|
88 |
* @MethodName: disconnect |
|
89 |
* @Description: 断开连接 |
|
90 |
* @return |
|
91 |
* @throws Exception |
|
92 |
* @CreateTime 2019年12月18日 上午10:45:21 |
|
93 |
*/ |
|
94 |
public String disconnect() throws Exception { |
|
95 |
|
|
96 |
if (client == null) { |
|
97 |
return "连接已断开"; |
|
98 |
} |
|
99 |
|
|
100 |
// 断开连接 |
|
101 |
clientRunner.getFuture().complete(client); |
|
102 |
client = null; |
|
103 |
return "断开连接成功"; |
|
104 |
} |
|
105 |
|
|
106 |
/** |
|
107 |
* @MethodName: subscribe |
|
108 |
* @Description: 订阅节点变量 |
|
109 |
* @throws Exception |
|
110 |
* @CreateTime 2019年12月18日 上午10:38:11 |
|
111 |
*/ |
|
112 |
public String subscribe(List<NodeEntity> nodes) throws Exception { |
|
113 |
|
|
114 |
if (client == null) { |
|
115 |
return "找不到客户端,操作失败"; |
|
116 |
} |
|
117 |
|
|
118 |
// 查询订阅对象,没有则创建 |
|
119 |
UaSubscription subscription = null; |
|
120 |
ImmutableList<UaSubscription> subscriptionList = client.getSubscriptionManager().getSubscriptions(); |
|
121 |
if (CollectionUtils.isEmpty(subscriptionList)) { |
|
122 |
subscription = client.getSubscriptionManager().createSubscription(1000.0).get(); |
|
123 |
} else { |
|
124 |
subscription = subscriptionList.get(0); |
|
125 |
} |
|
126 |
|
|
127 |
// 监控项请求列表 |
|
128 |
List<MonitoredItemCreateRequest> requests = new ArrayList<>(); |
|
129 |
|
|
130 |
if (!CollectionUtils.isEmpty(nodes)) { |
|
131 |
for (NodeEntity node : nodes) { |
|
132 |
// 创建监控的参数 |
|
133 |
MonitoringParameters parameters = new MonitoringParameters(subscription.nextClientHandle(), 1000.0, // sampling |
|
134 |
// interval |
|
135 |
null, // filter, null means use default |
|
136 |
Unsigned.uint(10), // queue size |
|
137 |
true // discard oldest |
|
138 |
); |
|
139 |
// 创建订阅的变量, 创建监控项请 求 |
|
140 |
MonitoredItemCreateRequest request = new MonitoredItemCreateRequest( |
|
141 |
new ReadValueId(new NodeId(node.getIndex(), node.getIdentifier()), AttributeId.Value.uid(), |
|
142 |
null, null), |
|
143 |
MonitoringMode.Reporting, parameters); |
|
144 |
requests.add(request); |
|
145 |
} |
|
146 |
} |
|
147 |
|
|
148 |
// 创建监控项,并且注册变量值改变时候的回调函数 |
|
149 |
subscription.createMonitoredItems(TimestampsToReturn.Both, requests, (item, id) -> { |
|
150 |
item.setValueConsumer((i, v) -> { |
a7c91f
|
151 |
handle2(i.getReadValueId().getNodeId(), v.getValue()); |
487b2f
|
152 |
}); |
YY |
153 |
}).get(); |
|
154 |
|
|
155 |
return "订阅成功"; |
|
156 |
} |
|
157 |
|
|
158 |
/** |
|
159 |
* * @MethodName: write |
|
160 |
* @Description: 回调函数 |
|
161 |
* @CreateTime 2023年10月13日 |
|
162 |
*/ |
|
163 |
|
a7c91f
|
164 |
/*public void handle(NodeId id, Variant value){ |
69fbaa
|
165 |
long startTime = System.currentTimeMillis(); |
YY |
166 |
|
f4a343
|
167 |
OpcuaConfParam opcuaConfParam=new OpcuaConfParam(); |
YY |
168 |
opcuaConfParam.setNode(id.getIdentifier().toString()); |
69fbaa
|
169 |
List<OpcuaConfResult> a=opcuaConfController.mylist(opcuaConfParam); |
a7c91f
|
170 |
log.info("数据库访问代码执行时间:" + (System.currentTimeMillis() - startTime) + "毫秒"); |
487b2f
|
171 |
String str1 = id.getIdentifier().toString()+":"+value.getValue().toString(); |
YY |
172 |
try { |
f4a343
|
173 |
Class<?> clazz = Class.forName(a.get(0).getRModule()); |
YY |
174 |
Method method = clazz.getMethod(a.get(0).getRFunction(), String.class); |
487b2f
|
175 |
|
YY |
176 |
method.invoke(clazz.newInstance(), str1); |
|
177 |
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | |
|
178 |
InvocationTargetException e) { |
|
179 |
e.printStackTrace(); |
|
180 |
} |
69fbaa
|
181 |
log.info("代码执行时间:" + (System.currentTimeMillis() - startTime) + "毫秒"); |
a7c91f
|
182 |
}*/ |
YY |
183 |
public void handle2(NodeId id, Variant value){ |
|
184 |
long sTime = System.currentTimeMillis(); |
|
185 |
if (b == null || 0 > b.size()) { |
|
186 |
OpcuaConfParam opcuaConfParam=new OpcuaConfParam(); |
|
187 |
opcuaConfParam.setSubscribe(1); |
|
188 |
b=opcuaConfController.mylist(opcuaConfParam); |
6b317a
|
189 |
log.info("数据库访问代码执行时间:" + (System.currentTimeMillis() - sTime) + "毫秒"); |
a7c91f
|
190 |
} |
YY |
191 |
String str1 = id.getIdentifier().toString()+":"+value.getValue().toString(); |
6b317a
|
192 |
//使用Stream API在List<T>中查找元素 |
a7c91f
|
193 |
OpcuaConfResult opcuaConfResult = b.stream() |
YY |
194 |
.filter(customer ->id.getIdentifier().toString().equals(customer.getNode())) |
|
195 |
.findAny() |
|
196 |
.orElse(null); |
|
197 |
try { |
|
198 |
Class<?> clazz = Class.forName(opcuaConfResult.getRModule()); |
|
199 |
Method method = clazz.getMethod(opcuaConfResult.getRFunction(), String.class); |
|
200 |
|
|
201 |
method.invoke(clazz.newInstance(), str1); |
|
202 |
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | |
|
203 |
InvocationTargetException e) { |
|
204 |
e.printStackTrace(); |
|
205 |
} |
|
206 |
log.info("代码执行时间:" + (System.currentTimeMillis() - sTime) + "毫秒"); |
487b2f
|
207 |
} |
YY |
208 |
|
a7c91f
|
209 |
|
487b2f
|
210 |
/** |
YY |
211 |
* @MethodName: write |
|
212 |
* @Description: 变节点量写入 |
|
213 |
* @param node |
|
214 |
* @throws Exception |
|
215 |
* @CreateTime 2019年12月18日 上午9:51:40 |
|
216 |
*/ |
|
217 |
public String write(NodeEntity node) throws Exception { |
|
218 |
|
|
219 |
if (client == null) { |
|
220 |
return "找不到客户端,操作失败"; |
|
221 |
} |
|
222 |
|
|
223 |
NodeId nodeId = new NodeId(node.getIndex(), node.getIdentifier()); |
|
224 |
Variant value = null; |
|
225 |
switch (node.getType()) { |
|
226 |
case "int": |
|
227 |
value = new Variant(Integer.parseInt(node.getValue().toString())); |
|
228 |
break; |
|
229 |
case "boolean": |
|
230 |
value = new Variant(Boolean.parseBoolean(node.getValue().toString())); |
|
231 |
break; |
|
232 |
} |
|
233 |
DataValue dataValue = new DataValue(value, null, null); |
|
234 |
|
|
235 |
StatusCode statusCode = client.writeValue(nodeId, dataValue).get(); |
|
236 |
|
|
237 |
return "节点【" + node.getIdentifier() + "】写入状态:" + statusCode.isGood(); |
|
238 |
} |
|
239 |
|
|
240 |
/** |
|
241 |
* @MethodName: read |
|
242 |
* @Description: 读取 |
|
243 |
* @param node |
|
244 |
* @return |
|
245 |
* @throws Exception |
|
246 |
* @CreateTime 2019年12月19日 下午2:40:34 |
|
247 |
*/ |
|
248 |
public String read(NodeEntity node) throws Exception { |
|
249 |
|
|
250 |
if (client == null) { |
|
251 |
return "找不到客户端,操作失败"; |
|
252 |
} |
|
253 |
|
|
254 |
NodeId nodeId = new NodeId(node.getIndex(), node.getIdentifier()); |
|
255 |
VariableNode vnode = client.getAddressSpace().createVariableNode(nodeId); |
|
256 |
DataValue value = vnode.readValue().get(); |
|
257 |
log.info("Value={}", value); |
|
258 |
|
|
259 |
Variant variant = value.getValue(); |
|
260 |
log.info("Variant={}", variant.getValue()); |
|
261 |
|
|
262 |
log.info("BackingClass={}", BuiltinDataType.getBackingClass(variant.getDataType().get())); |
|
263 |
|
|
264 |
return "节点【" + node.getIdentifier() + "】:" + variant.getValue(); |
|
265 |
} |
|
266 |
} |