javaeclipsesubscriptionopc-uamilo

How to filter out events coming to UaMonitoredItem in OPC UA Milo?


I am trying to write an internal application event log based on UaMonitoredItem and its setValueConsumer(ValueConsumer valueConsumer) method. Is it possible to filter out events coming to UaMonitoredItems after the "initialization"/"first use" of the setValueConsumer(ValueConsumer valueConsumer) method, and not during? Would using ManagedDataItem or ManagedEventItem be a good practice in this case?

Code for example:

public void subscribeOnComponentsEvent(List list) {
    
    //....
    
    //Created in MonitoringMode.Sampling to avoid race condition
    for (UaMonitoredItem item : monitoredItems) {
        allMonitoredItems.add(item);
        item.setValueConsumer((e, vs) -> {
            logger.info("Received from {}", e.getReadValueId().getNodeId());
        });
    }
}
    
public void setMonitoringMode(MonitoringMode mode) {

    try {
        subscription.setMonitoringMode(mode, allMonitoredItems).get();
    } catch (InterruptedException | ExecutionException e) {
        logger.error(e.getMessage());
    }
}

public static void main(String[] args) {

    //...
    
    client.subscribeOnComponentsEvent(list);
    client.setMonitoringMode(MonitoringMode.Reporting);
}

Output of code example:

[milo-shared-thread-pool-3] INFO client.Client - Received from NodeId{ns=1, id=state_open}
[milo-shared-thread-pool-3] INFO client.Client - Received from NodeId{ns=1, id=state_closed}
[milo-shared-thread-pool-3] INFO client.Client - Received from NodeId{ns=1, id=state_alarm}

When the application starts, this code starts immediately, therefore, data from monitors that may have already been in the log will be re-written to the log.

Is there a way to avoid re-writing data to the log?


Solution

  • This code doesn't start or run immediately, it's only going to run once the MonitoredItem has been created and the server sends a notification.