Search in sources :

Example 1 with KnxNetIpSubscriptionHandle

use of org.apache.plc4x.java.knxnetip.model.KnxNetIpSubscriptionHandle in project plc4x by apache.

the class KnxNetIpProtocolLogic method publishEvent.

protected void publishEvent(GroupAddress groupAddress, PlcValue plcValue) {
    // Create a subscription event from the input.
    // TODO: Check this ... this is sort of not really right ...
    final PlcSubscriptionEvent event = new DefaultPlcSubscriptionEvent(Instant.now(), Collections.singletonMap("knxData", new ResponseItem<>(PlcResponseCode.OK, plcValue)));
    // Try sending the subscription event to all listeners.
    for (Map.Entry<DefaultPlcConsumerRegistration, Consumer<PlcSubscriptionEvent>> entry : consumers.entrySet()) {
        final DefaultPlcConsumerRegistration registration = entry.getKey();
        final Consumer<PlcSubscriptionEvent> consumer = entry.getValue();
        // Only if the current data point matches the subscription, publish the event to it.
        for (PlcSubscriptionHandle handle : registration.getSubscriptionHandles()) {
            if (handle instanceof KnxNetIpSubscriptionHandle) {
                KnxNetIpSubscriptionHandle subscriptionHandle = (KnxNetIpSubscriptionHandle) handle;
                // Check if the subscription matches this current event.
                if (subscriptionHandle.getField().matchesGroupAddress(groupAddress)) {
                    consumer.accept(event);
                }
            }
        }
    }
}
Also used : DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) KnxNetIpSubscriptionHandle(org.apache.plc4x.java.knxnetip.model.KnxNetIpSubscriptionHandle) Consumer(java.util.function.Consumer) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Consumer (java.util.function.Consumer)1 PlcSubscriptionHandle (org.apache.plc4x.java.api.model.PlcSubscriptionHandle)1 KnxNetIpSubscriptionHandle (org.apache.plc4x.java.knxnetip.model.KnxNetIpSubscriptionHandle)1 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)1 DefaultPlcConsumerRegistration (org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration)1