Search in sources :

Example 1 with OpcPoint

use of com.jbm.framework.opcua.attribute.OpcPoint in project JBM by numen06.

the class OpcUaTemplate method createItemMonitored.

private List<UaMonitoredItem> createItemMonitored(OpcUaClientBean opcUaClientBean, OpcPoint opcPoint) throws ExecutionException, InterruptedException {
    int namespace = opcPoint.getNamespace();
    String tag = opcPoint.getTagName();
    NodeId nodeId = new NodeId(namespace, tag);
    // 创建发布间隔1000ms的订阅对象
    UaSubscription subscription = this.getSubscription(opcUaClientBean.getOpcUaClient());
    MonitoringParameters parameters = new MonitoringParameters(uint(subscription.getMonitoredItems().size() + 1), 1000.0, null, uint(10), true);
    List<MonitoredItemCreateRequest> requests = Lists.newArrayList();
    ReadValueId readValueId = new ReadValueId(nodeId, AttributeId.Value.uid(), null, null);
    // 创建监控item, 第一个为Reporting mode
    MonitoredItemCreateRequest request = new MonitoredItemCreateRequest(readValueId, MonitoringMode.Reporting, parameters);
    requests.add(request);
    List<UaMonitoredItem> items = subscription.createMonitoredItems(TimestampsToReturn.Both, requests, (item, id) -> item.setValueConsumer(new UaMonitoredItem.ValueConsumer() {

        @Override
        public void onValueArrived(UaMonitoredItem item, DataValue value) {
            try {
                log.debug("OPC数据变化回调:subscription value received: item={}, value={}", item.getReadValueId().getNodeId(), value.getValue());
                ValueChanageEvent valueChanageEvent = opcUaClientBean.getSubscriptionPoints().get(opcPoint.getAlias());
                valueChanageEvent.putData(item, value);
                applicationContext.publishEvent(valueChanageEvent);
            } catch (Exception e) {
            }
        }
    })).get();
    log.info("添加监听:[{}]到监听器[{}]监听数量:{}", nodeId, subscription.getSubscriptionId(), subscription.getMonitoredItems().size());
    return items;
}
Also used : UaMonitoredItem(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem) MonitoringMode(org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode) PointSubscribeEvent(com.jbm.framework.opcua.event.PointSubscribeEvent) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) Autowired(org.springframework.beans.factory.annotation.Autowired) ValueChanageEvent(com.jbm.framework.opcua.event.ValueChanageEvent) CompletableFuture(java.util.concurrent.CompletableFuture) Lists(com.google.common.collect.Lists) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) UaSubscription(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription) Service(org.springframework.stereotype.Service) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) Map(java.util.Map) UaMonitoredItem(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem) org.eclipse.milo.opcua.stack.core.types.builtin(org.eclipse.milo.opcua.stack.core.types.builtin) AnonymousProvider(org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider) GuardSubscriptionListener(com.jbm.framework.opcua.listener.GuardSubscriptionListener) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) SessionActivityListener(org.eclipse.milo.opcua.sdk.client.SessionActivityListener) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Maps(com.google.common.collect.Maps) ApplicationContext(org.springframework.context.ApplicationContext) KeyLoader(com.jbm.framework.opcua.key.KeyLoader) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) Slf4j(lombok.extern.slf4j.Slf4j) org.eclipse.milo.opcua.stack.core.types.structured(org.eclipse.milo.opcua.stack.core.types.structured) List(java.util.List) JSON(com.alibaba.fastjson.JSON) OpcPointsRead(com.jbm.framework.opcua.attribute.OpcPointsRead) Paths(java.nio.file.Paths) ReflectUtil(cn.hutool.core.util.ReflectUtil) UaException(org.eclipse.milo.opcua.stack.core.UaException) DiscoveryClient(org.eclipse.milo.opcua.stack.client.DiscoveryClient) FileUtil(cn.hutool.core.io.FileUtil) ValueType(com.jbm.framework.opcua.attribute.ValueType) DriverUtils(com.jbm.framework.opcua.util.DriverUtils) OpcPoint(com.jbm.framework.opcua.attribute.OpcPoint) EndpointUtil(org.eclipse.milo.opcua.stack.core.util.EndpointUtil) UaSubscription(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) OpcPoint(com.jbm.framework.opcua.attribute.OpcPoint) ExecutionException(java.util.concurrent.ExecutionException) UaException(org.eclipse.milo.opcua.stack.core.UaException) ValueChanageEvent(com.jbm.framework.opcua.event.ValueChanageEvent)

Example 2 with OpcPoint

use of com.jbm.framework.opcua.attribute.OpcPoint in project JBM by numen06.

the class OpcUaTemplate method subscribeItem.

public <T extends ValueChanageEvent> void subscribeItem(String deviceId, String pointName, Class<T> callBackEvent) {
    OpcUaClientBean opcUaClientBean = clientMap.get(deviceId);
    OpcPoint opcPoint = opcUaClientBean.findPoint(pointName);
    ValueChanageEvent valueChanageEvent = ReflectUtil.newInstance(null, null, null);
    this.subscribeItem(deviceId, opcPoint, valueChanageEvent);
}
Also used : ValueChanageEvent(com.jbm.framework.opcua.event.ValueChanageEvent) OpcPoint(com.jbm.framework.opcua.attribute.OpcPoint)

Example 3 with OpcPoint

use of com.jbm.framework.opcua.attribute.OpcPoint in project JBM by numen06.

the class OpcUaTemplate method writeItem.

/**
 * Write Opc Ua Point Value
 *
 * @param deviceId Device Id
 * @param point    OpcPoint Info
 * @throws UaException          UaException
 * @throws ExecutionException   ExecutionException
 * @throws InterruptedException InterruptedException
 */
public void writeItem(String deviceId, OpcPoint point) {
    OpcUaClient client;
    try {
        log.debug("OPCUA写入点位:{}", JSON.toJSONString(point));
        int namespace = point.getNamespace();
        String tag = point.getTagName();
        NodeId nodeId = new NodeId(namespace, tag);
        client = getOpcUaClient(deviceId);
        client.connect().get();
        StatusCode statusCode = StatusCode.GOOD;
        DataValue dataValue = this.convertData(point);
        statusCode = client.writeValue(nodeId, dataValue).get();
        if (!statusCode.isGood()) {
            throw new RuntimeException(statusCode.toString());
        }
    } catch (Exception e) {
        log.error("Opc Ua Point Write Error", e);
    }
}
Also used : OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) OpcPoint(com.jbm.framework.opcua.attribute.OpcPoint) ExecutionException(java.util.concurrent.ExecutionException) UaException(org.eclipse.milo.opcua.stack.core.UaException)

Example 4 with OpcPoint

use of com.jbm.framework.opcua.attribute.OpcPoint in project JBM by numen06.

the class OpcUaTemplate method readItem.

public String readItem(String deviceId, String pointName) throws Exception {
    OpcUaClientBean opcUaClientBean = clientMap.get(deviceId);
    OpcPoint point = opcUaClientBean.findPoint(pointName);
    return this.readItem(deviceId, point);
}
Also used : OpcPoint(com.jbm.framework.opcua.attribute.OpcPoint)

Example 5 with OpcPoint

use of com.jbm.framework.opcua.attribute.OpcPoint in project JBM by numen06.

the class OpcUaTemplate method readItem.

public String readItem(String deviceId, OpcPoint point) throws Exception {
    int namespace = point.getNamespace();
    String tag = point.getTagName();
    NodeId nodeId = new NodeId(namespace, tag);
    CompletableFuture<String> value = new CompletableFuture<>();
    OpcUaClient client = getOpcUaClient(deviceId);
    log.debug("start read point(ns={};s={})", namespace, tag);
    client.connect().get();
    client.readValue(0.0, TimestampsToReturn.Both, nodeId).thenAccept(dataValue -> {
        try {
            value.complete(dataValue.getValue().getValue().toString());
        } catch (Exception e) {
            log.error("accept point(ns={};s={}) value error", namespace, tag, e);
        }
    });
    String rawValue = value.get(3, TimeUnit.SECONDS);
    log.debug("end read point(ns={};s={}) value: {}", namespace, tag, rawValue);
    return rawValue;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) OpcPoint(com.jbm.framework.opcua.attribute.OpcPoint) ExecutionException(java.util.concurrent.ExecutionException) UaException(org.eclipse.milo.opcua.stack.core.UaException)

Aggregations

OpcPoint (com.jbm.framework.opcua.attribute.OpcPoint)8 ValueChanageEvent (com.jbm.framework.opcua.event.ValueChanageEvent)3 ExecutionException (java.util.concurrent.ExecutionException)3 OpcUaClient (org.eclipse.milo.opcua.sdk.client.OpcUaClient)3 UaException (org.eclipse.milo.opcua.stack.core.UaException)3 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 CollUtil (cn.hutool.core.collection.CollUtil)1 FileUtil (cn.hutool.core.io.FileUtil)1 ReflectUtil (cn.hutool.core.util.ReflectUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 JSON (com.alibaba.fastjson.JSON)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 OpcPointsRead (com.jbm.framework.opcua.attribute.OpcPointsRead)1 ValueType (com.jbm.framework.opcua.attribute.ValueType)1 PointSubscribeEvent (com.jbm.framework.opcua.event.PointSubscribeEvent)1 KeyLoader (com.jbm.framework.opcua.key.KeyLoader)1 GuardSubscriptionListener (com.jbm.framework.opcua.listener.GuardSubscriptionListener)1 DriverUtils (com.jbm.framework.opcua.util.DriverUtils)1