1 package de.dlr.bt.stc.bridge.uas; 2 3 import org.eclipse.milo.opcua.sdk.server.nodes.UaObjectTypeNode; 4 import org.eclipse.milo.opcua.sdk.server.nodes.filters.AttributeFilters; 5 import org.eclipse.milo.opcua.stack.core.Identifiers; 6 import org.eclipse.milo.opcua.stack.core.UaException; 7 import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue; 8 import org.eclipse.milo.opcua.stack.core.types.builtin.DateTime; 9 import org.eclipse.milo.opcua.stack.core.types.builtin.Variant; 10 11 import de.dlr.bt.stc.bridge.Bridge; 12 import de.dlr.bt.stc.init.Register; 13 import de.dlr.bt.stc.opcuaserver.ANodeCreator; 14 import de.dlr.bt.stc.opcuaserver.NodeFactory; 15 import de.dlr.bt.stc.opcuaserver.STCNamespace; 16 import de.dlr.bt.stc.opcuaserver.STCNamespace.Folders; 17 import lombok.extern.slf4j.Slf4j; 18 19 @Slf4j 20 public class BridgeNode extends ANodeCreator { 21 private static final String BRIDGE_TYPE = "BridgeType"; 22 23 private static final String SOURCE_ID = "SourceID"; 24 private static final String SINK_ID = "SinkID"; 25 private static final String QUEUE_SIZE = "QueueSize"; 26 private static final String QUEUE_DURATION = "QueueDuration"; 27 private static final String VALUE_TEMPLATE = "ValueTemplate"; 28 private static final String CACHE_CLEARED = "CacheCleared"; 29 private static final String CURRENT_SIZE = "CurrentSize"; 30 31 private UaObjectTypeNode typeNode; 32 33 @Register 34 public static void register() { 35 NodeFactory.getInstance().registerCreator(Bridge.class, BridgeNode::new); 36 } 37 38 private BridgeNode(STCNamespace namespace) { 39 super(namespace, "stc", "bridge"); 40 } 41 42 @Override 43 public void createObjectType() { 44 var sourceid = namespace.createObjectTypeComponent(SOURCE_ID, namespace.newNodeId(nodePathType(SOURCE_ID)), 45 Identifiers.String); 46 var sinkid = namespace.createObjectTypeComponent(SINK_ID, namespace.newNodeId(nodePathType(SINK_ID)), 47 Identifiers.String); 48 var queuesize = namespace.createObjectTypeComponent(QUEUE_SIZE, namespace.newNodeId(nodePathType(QUEUE_SIZE)), 49 Identifiers.Integer); 50 var queueduration = namespace.createObjectTypeComponent(QUEUE_DURATION, 51 namespace.newNodeId(nodePathType(QUEUE_DURATION)), Identifiers.Integer); 52 var valuetemplate = namespace.createObjectTypeComponent(VALUE_TEMPLATE, 53 namespace.newNodeId(nodePathType(VALUE_TEMPLATE)), Identifiers.String); 54 var queuefull = namespace.createObjectTypeComponent(CACHE_CLEARED, 55 namespace.newNodeId(nodePathType(CACHE_CLEARED)), Identifiers.DateTime); 56 var currentsize = namespace.createObjectTypeComponent(CURRENT_SIZE, 57 namespace.newNodeId(nodePathType(CURRENT_SIZE)), Identifiers.Integer); 58 59 typeNode = namespace.createObjectTypeNode(BRIDGE_TYPE, namespace.newNodeId(nodePathType(BRIDGE_TYPE)), sourceid, 60 sinkid, queuesize, queueduration, valuetemplate, queuefull, currentsize); 61 } 62 63 @Override 64 public void createInstance(Object forNode, Folders folders) { 65 if (!(forNode instanceof Bridge bridge)) 66 return; 67 68 try { 69 String id = bridge.getKey(); 70 var uon = namespace.createObjectNode(typeNode, id, namespace.newNodeId(nodePathInst(id)), 71 folders.getBridgeFolder()); 72 73 namespace.setObjectNodeComponent(uon, SOURCE_ID, new Variant(bridge.getSourceKey())); 74 namespace.setObjectNodeComponent(uon, SINK_ID, new Variant(bridge.getSinkKey())); 75 namespace.setObjectNodeComponent(uon, QUEUE_SIZE, new Variant(bridge.getQueueSize())); 76 namespace.setObjectNodeComponent(uon, QUEUE_DURATION, new Variant(bridge.getQueueDuration())); 77 namespace.setObjectNodeComponent(uon, VALUE_TEMPLATE, new Variant(bridge.getValueTemplate())); 78 79 var qf = namespace.getObjectNodeComponent(uon, CACHE_CLEARED); 80 if (qf != null) { 81 qf.getFilterChain().addLast(AttributeFilters 82 .getValue(context -> new DataValue(new Variant(new DateTime(bridge.getCacheCleared()))))); 83 } 84 85 var cs = namespace.getObjectNodeComponent(uon, CURRENT_SIZE); 86 if (cs != null) { 87 cs.getFilterChain().addLast( 88 AttributeFilters.getValue(context -> new DataValue(new Variant(bridge.getCurrentCacheSize())))); 89 } 90 91 addRootNode(forNode, uon); 92 } catch (UaException e) { 93 log.info("Exception during creation of NodeInstance {}: {}", bridge.getKey(), e); 94 } 95 96 } 97 98 }