干燥机配套车间生产管理系统/云平台服务端
zhuguifei
2024-11-29 339515558253d776769dc2e2560bbb4a0450c989
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//package org.jeecg.modules.dry.common;
//
//import lombok.extern.slf4j.Slf4j;
//import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
//import org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider;
//import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription;
//import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscriptionManager;
//import org.eclipse.milo.opcua.sdk.client.nodes.UaNode;
//import org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedDataItem;
//import org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription;
//import org.eclipse.milo.opcua.stack.core.AttributeId;
//import org.eclipse.milo.opcua.stack.core.Identifiers;
//import org.eclipse.milo.opcua.stack.core.UaException;
//import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy;
//import org.eclipse.milo.opcua.stack.core.types.builtin.*;
//import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
//import org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode;
//import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
//import org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest;
//import org.eclipse.milo.opcua.stack.core.types.structured.MonitoringParameters;
//import org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId;
//
//import java.nio.file.Files;
//import java.nio.file.Path;
//import java.nio.file.Paths;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Objects;
//import java.util.concurrent.CountDownLatch;
//import java.util.concurrent.atomic.AtomicInteger;
//
//
//@Slf4j
//public class OPCUA {
//
//    private static AtomicInteger atomic = new AtomicInteger(1);
//    // 定义服务端地址
//    private final static String endPointUrl = "opc.tcp://127.0.0.1:49320";
//
//
//    /**
//     * 创建OPC UA客户端
//     * @return
//     * @throws Exception
//     */
//    public static OpcUaClient createClient() throws Exception {
//
//        Path securityTempDir = Paths.get(System.getProperty("java.io.tmpdir"), "security");
//        Files.createDirectories(securityTempDir);
//        if (!Files.exists(securityTempDir)) {
//            throw new Exception("unable to create security dir: " + securityTempDir);
//        }
//        return OpcUaClient.create(endPointUrl,
//            endpoints ->
//                endpoints.stream()
//                    .filter(e -> e.getSecurityPolicyUri().equals(SecurityPolicy.None.getUri()))
//                    .findFirst(),
//            configBuilder ->
//                configBuilder
//                    .setApplicationName(LocalizedText.english("eclipse milo opc-ua client"))
//                    .setApplicationUri("urn:eclipse:milo:examples:client")
//                    //访问方式
//                    .setIdentityProvider(new AnonymousProvider())
//                    .setRequestTimeout(UInteger.valueOf(5000))
//                    .build()
//        );
//    }
//
//    /**
//     * 遍历树形节点
//     *
//     * @param client OPC UA客户端
//     * @param uaNode 节点
//     * @throws Exception
//     */
//    public static void browseNode(OpcUaClient client, UaNode uaNode) throws Exception {
//        List<? extends UaNode> nodes;
//        if (uaNode == null) {
//            nodes = client.getAddressSpace().browseNodes(Identifiers.ObjectsFolder);
//        } else {
//            nodes = client.getAddressSpace().browseNodes(uaNode);
//        }
//        for (UaNode nd : nodes) {
//            //排除系统行性节点,这些系统性节点名称一般都是以"_"开头
//            if (Objects.requireNonNull(nd.getBrowseName().getName()).contains("_")) {
//                continue;
//            }
//            System.out.println("Node= " + nd.getBrowseName().getName());
//            browseNode(client, nd);
//        }
//    }
//
//
//    /**
//     * 读取节点数据
//     *
//     * @param client OPC UA客户端
//     * @throws Exception
//     */
//    public static void readNode(OpcUaClient client) throws Exception {
//        int namespaceIndex = 2;
//        String identifier = "TD-01.SB-01.AG-01";
//        //节点
//        NodeId nodeId = new NodeId(namespaceIndex, identifier);
//        //读取节点数据
//        DataValue value = client.readValue(0.0, TimestampsToReturn.Neither, nodeId).get();
//
//        System.out.println(identifier + ": " + String.valueOf(value.getValue().getValue()));
//    }
//
//
//    /**
//     * 写入节点数据
//     *
//     * @param client
//     * @throws Exception
//     */
//    public static void writeNodeValue(OpcUaClient client) throws Exception {
//        //节点
//        NodeId nodeId = new NodeId(2, "TD-01.SB-01.AG-01");
//        short i = 3;
//        //创建数据对象,此处的数据对象一定要定义类型,不然会出现类型错误,导致无法写入
//        DataValue nowValue = new DataValue(new Variant(i), null, null);
//        //写入节点数据
//        StatusCode statusCode = client.writeValue(nodeId, nowValue).join();
//        System.out.println("结果:" + statusCode.isGood());
//    }
//
//
//    /**
//     * 订阅(单个)
//     *
//     * @param client
//     * @throws Exception
//     */
//    public static void subscribe(OpcUaClient client) throws Exception {
//        //创建发布间隔1000ms的订阅对象
//        client
//            .getSubscriptionManager()
//            .createSubscription(1000.0)
//            .thenAccept(t -> {
//                //节点
//                NodeId nodeId = new NodeId(2, "TD-01.SB-01.AG-01");
//                ReadValueId readValueId = new ReadValueId(nodeId, AttributeId.Value.uid(), null, null);
//                //创建监控的参数
//                MonitoringParameters parameters = new MonitoringParameters(UInteger.valueOf(atomic.getAndIncrement()), 1000.0, null, UInteger.valueOf(10), true);
//                //创建监控项请求
//                //该请求最后用于创建订阅。
//                MonitoredItemCreateRequest request = new MonitoredItemCreateRequest(readValueId, MonitoringMode.Reporting, parameters);
//                List<MonitoredItemCreateRequest> requests = new ArrayList<>();
//                requests.add(request);
//                //创建监控项,并且注册变量值改变时候的回调函数。
//                t.createMonitoredItems(
//                    TimestampsToReturn.Both,
//                    requests,
//                    (item, id) -> item.setValueConsumer((it, val) -> {
//                        System.out.println("nodeid :" + it.getReadValueId().getNodeId());
//                        System.out.println("value :" + val.getValue().getValue());
//                    })
//                );
//            }).get();
//
//        //持续订阅
//        Thread.sleep(Long.MAX_VALUE);
//    }
//
//
//    /**
//     * 批量订阅
//     *
//     * @param client
//     * @throws Exception
//     */
////    private static void managedSubscriptionEvent(OpcUaClient client) throws Exception {
////        final CountDownLatch eventLatch = new CountDownLatch(1);
////
////        //处理订阅业务
////        handlerNode(client);
////
////        //持续监听
////        eventLatch.await();
////    }
//
//    /**
//     * 处理订阅业务
//     *
//     * @param client OPC UA客户端
//     */
//    public static void handlerNode(OpcUaClient client) {
//        try {
//            //创建订阅
//            ManagedSubscription subscription = ManagedSubscription.create(client);
//
//            //你所需要订阅的key
//            List<String> key = new ArrayList<>();
//            key.add("通道 1.设备 1.标记 1");
//            key.add("通道 1.设备 1.标记 2");
//
//            List<NodeId> nodeIdList = new ArrayList<>();
//            for (String s : key) {
//                nodeIdList.add(new NodeId(2, s));
//            }
//
//            //监听
//            List<ManagedDataItem> dataItemList = subscription.createDataItems(nodeIdList);
//            for (ManagedDataItem managedDataItem : dataItemList) {
//                managedDataItem.addDataValueListener((t) -> {
//                    System.out.println(managedDataItem.getNodeId().getIdentifier().toString() + ":" + t.getValue().getValue().toString());
//                });
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * 自定义订阅监听
//     */
//    public static class CustomSubscriptionListener implements UaSubscriptionManager.SubscriptionListener {
//
//        private OpcUaClient client;
//
//        CustomSubscriptionListener(OpcUaClient client) {
//            this.client = client;
//        }
//
//        public void onKeepAlive(UaSubscription subscription, DateTime publishTime) {
//            log.debug("onKeepAlive");
//        }
//
//        public void onStatusChanged(UaSubscription subscription, StatusCode status) {
//            log.debug("onStatusChanged");
//        }
//
//        public void onPublishFailure(UaException exception) {
//            log.debug("onPublishFailure");
//        }
//
//        public void onNotificationDataLost(UaSubscription subscription) {
//            log.debug("onNotificationDataLost");
//        }
//
//        /**
//         * 重连时 尝试恢复之前的订阅失败时 会调用此方法
//         * @param uaSubscription 订阅
//         * @param statusCode 状态
//         */
//        public void onSubscriptionTransferFailed(UaSubscription uaSubscription, StatusCode statusCode) {
//            log.debug("恢复订阅失败 需要重新订阅");
//            //在回调方法中重新订阅
//            handlerNode(client);
//        }
//    }
//
//    /**
//     * 批量订阅
//     *
//     * @param client
//     * @throws Exception
//     */
//    public static void managedSubscriptionEvent(OpcUaClient client) throws Exception {
//        final CountDownLatch eventLatch = new CountDownLatch(1);
//
//        //添加订阅监听器,用于处理断线重连后的订阅问题
//        client.getSubscriptionManager().addSubscriptionListener(new CustomSubscriptionListener(client));
//
//        //处理订阅业务
//        handlerNode(client);
//
//        //持续监听
//        eventLatch.await();
//    }
//}