Search in sources :

Example 1 with ValueChanageEvent

use of com.jbm.framework.opcua.event.ValueChanageEvent 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 ValueChanageEvent

use of com.jbm.framework.opcua.event.ValueChanageEvent 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 ValueChanageEvent

use of com.jbm.framework.opcua.event.ValueChanageEvent in project JBM by numen06.

the class GuardSubscriptionListener method onSubscriptionTransferFailed.

// 重连时 尝试恢复之前的订阅失败时 会调用此方法
public void onSubscriptionTransferFailed(UaSubscription subscription, StatusCode statusCode) {
    log.info("设备[{}]订阅失效,开始重新订阅,数量为:{}", opcUaClientBean.getDeviceId(), opcUaClientBean.getSubscriptionPoints().size());
    for (String pointCode : opcUaClientBean.getSubscriptionPoints().keySet()) {
        OpcPoint opcPoint = opcUaClientBean.getPoints().get(pointCode);
        ValueChanageEvent valueChanageEvent = opcUaClientBean.getSubscriptionPoints().get(pointCode);
        opcUaTemplate.subscribeItem(opcUaClientBean.getDeviceId(), opcPoint, valueChanageEvent);
    }
    log.info("设备[{}]重新订阅完成", opcUaClientBean.getDeviceId());
}
Also used : ValueChanageEvent(com.jbm.framework.opcua.event.ValueChanageEvent) OpcPoint(com.jbm.framework.opcua.attribute.OpcPoint)

Aggregations

OpcPoint (com.jbm.framework.opcua.attribute.OpcPoint)3 ValueChanageEvent (com.jbm.framework.opcua.event.ValueChanageEvent)3 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 Paths (java.nio.file.Paths)1 List (java.util.List)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1