1

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?

1 Answers1

1

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.

Kevin Herron
  • 6,500
  • 3
  • 26
  • 35
  • Rewrote the example. – Killpop3770 Sep 01 '22 at 14:35
  • 1
    This looks like a problem with the server you’re connected to replaying the events every time. If you want to try to handle it in the client you can, but it’s not “normal”. You’ll have to figure out what makes an event a duplicate or not. – Kevin Herron Sep 01 '22 at 14:43
  • 1
    Oh, but you _should_ expect an initial value/event for each item you subscribe to. If that’s the case, and you want to ignore that for some reason, then you’ll just have to track some state in your listener to determine if you’ve received the initial change or not. – Kevin Herron Sep 01 '22 at 14:48
  • How will it be easiest to implement? – Killpop3770 Sep 01 '22 at 14:52
  • 1
    Maybe with AtomicBoolean and `compareAndSet`? The problem with what you want to do is that you have no idea if the initial value is a duplicate or not. It could have changed in between the times your client has connected and made subscriptions. You might end up ignoring a legitimate value change. – Kevin Herron Sep 01 '22 at 14:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/247738/discussion-between-killpop3770-and-kevin-herron). – Killpop3770 Sep 01 '22 at 15:45