Milo-OPC UA Subscription and treated Triggering

Subscription There are two modes, one is Reporting, the other is Sampling.

If the Sampling is defined, then this is a Subscription Triggered Item, i.e. excitation subscription, a definition is required for Reporting the Subscription (referred Triggering Item) connected to it. So that when the Triggering Item updates will inspire Triggered Item updates. code show as below:

public class TriggeringExample implements UaClient {

// public static void main(String[] args) throws Exception {
// TriggeringExample example = new TriggeringExample();
//
// new ClientExampleRunner(example).run();
// }

private final Logger logger = LoggerFactory.getLogger(getClass());

private final AtomicLong clientHandles = new AtomicLong(1L);

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
// synchronous connect
client.connect().get();

// create a subscription @ 1000ms subscription may comprise a plurality of monitoring Item
UaSubscription Subscription = client.getSubscriptionManager ()
.createSubscription (1000.0)
.get ();

// subscribe to a static value that reports
ReadValueId readValueId1 = new ReadValueId(
new NodeId(2, "ch1.d1.tag1"),
AttributeId.Value.uid(),
null,
QualifiedName.NULL_VALUE
);

// create a monitoring item, a first for the Reporting mode

UaMonitoredItem reportingItem = createMonitoredItem(subscription, readValueId1, MonitoringMode.Reporting);

// create a second monitoring item, it is Sampling mode, the first item needed to stimulate
ReadValueId readValueId2 = new new ReadValueId (
new new NodeId (2, "ch1.d1.tag2"),
AttributeId.Value.uid (),
null,
QualifiedName.NULL_VALUE
);


UaMonitoredItem samplingItem = createMonitoredItem(subscription, readValueId2, MonitoringMode.Sampling);

 

// connected to the Triggering Item Triggered item (there may be a plurality) up (Note the use of the asynchronous mode)

subscription.addTriggeringLinks(reportingItem, newArrayList(samplingItem)).get();

// trigger reporting of both by writing to the static item and changing its value
//VariableNode node = client.getAddressSpace().createVariableNode(
// new NodeId(2, "ch1.d1.tag1"));

// to stimulate Triggered item by changing Triggering Item Send me a message, pay attention here writeValue using asynchronous mode, if not the get (), can only get one or two news update
for (int i = 0; i <10000; i ++ ) {
the Variant = new new newVal the Variant (Unsigned.ushort (I));
the DataValue new new VA = the DataValue (newVal, null, null);
client.writeValue (the NodeId new new (2, "ch1.d1.tag1"), VA). GET ();

}
//node.writeValue(va);

// client.writeValue (
// new new the NodeId (2, "ch1.d1.tag1"),
// VA
//) .get ();

// let the example run for 5 seconds then terminate
Thread.sleep(500000);
future.complete(client);
}

private UaMonitoredItem createMonitoredItem(
UaSubscription subscription,
ReadValueId readValueId,
MonitoringMode monitoringMode
) throws ExecutionException, InterruptedException {

// important: client handle must be unique per item
UInteger clientHandle = uint(clientHandles.getAndIncrement());

MonitoringParameters parameters = new MonitoringParameters(
clientHandle,
1000.0,
null,
uint(10),
true
);

MonitoredItemCreateRequest request = new MonitoredItemCreateRequest(
readValueId,
monitoringMode,
parameters
);

BiConsumer<UaMonitoredItem, Integer> onItemCreated =
(item, id) -> item.setValueConsumer(this::onSubscriptionValue);

List<UaMonitoredItem> items = subscription.createMonitoredItems(
TimestampsToReturn.Both,
newArrayList(request),
onItemCreated
).get();

return items.get(0);
}

private void onSubscriptionValue(UaMonitoredItem item, DataValue value) {
logger.info(
"回调:subscription value received: item={}, value={}",
item.getReadValueId().getNodeId(), value.getValue());
}

}

Guess you like

Origin www.cnblogs.com/myboat/p/11891148.html