milo icon indicating copy to clipboard operation
milo copied to clipboard

Subscription returns only one item

Open LibertyWalk opened this issue 8 years ago • 1 comments

This is the code I'm using to set up the subscription:

public void createSubscription(List<NodeId> nodeIds) throws Exception {
        UaSubscription sub = null;
        logger.info("Requested NodeIds: " + nodeIds);
        try {
            sub = client.getSubscriptionManager().createSubscription(100.0).get();
        } catch (Exception e) {
            logger.error(ExceptionUtils.getStackTrace(e));
            logger.error("SUBSCRIPTION FAILED");
        }

        UInteger clientHandle = uint(clientHandles.getAndIncrement());
        MonitoringParameters parameters = new MonitoringParameters(clientHandle, 100.0, null, uint(10), true);

        List<MonitoredItemCreateRequest> MICRs = createMonitoredItemCreateRequests(nodeIds, parameters);

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

        List<UaMonitoredItem> items = sub.createMonitoredItems(
                        TimestampsToReturn.Both,
                        MICRs,
                        onItemCreated).get();

        for (UaMonitoredItem item : items) {
            if (item.getStatusCode().isGood()) {
                logger.info("Item created for NodeId: " + item.getReadValueId().getNodeId());
            } else {
                logger.error("Failed to create item for NodeId: " + item.getReadValueId().getNodeId()
                        + item.getStatusCode());
            }
        }
    }

The list of MonitoredItemCreateRequests looks fine to me, it contain these NodeIds: Sensor01, Sensor02, ..., Sensor10. The problem is that the value consumer only seems to be getting the Sensor10 item and no other item. When I debug the onSubscriptionValue method, the item always is an item with the NodeId of Sensor10.

It feels like I'm missing some crucial thing, can you help with this?

LibertyWalk avatar Aug 14 '18 15:08 LibertyWalk

You have to use a unique clientHandle for every item you create. You're currently reusing it in a shared MonitoringParameters you use to create all your MonitoredItemCreateRequests.

kevinherron avatar Aug 14 '18 16:08 kevinherron