Search in sources :

Example 1 with DefaultPlcConsumerRegistration

use of org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration in project plc4x by apache.

the class CANOpenProtocolLogic method register.

@Override
public PlcConsumerRegistration register(Consumer<PlcSubscriptionEvent> consumer, Collection<PlcSubscriptionHandle> handles) {
    final DefaultPlcConsumerRegistration consumerRegistration = new DefaultPlcConsumerRegistration(this, consumer, handles.toArray(new DefaultPlcSubscriptionHandle[0]));
    consumers.put(consumerRegistration, consumer);
    return consumerRegistration;
}
Also used : DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) DefaultPlcSubscriptionHandle(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle)

Example 2 with DefaultPlcConsumerRegistration

use of org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration in project plc4x by apache.

the class FirmataProtocolLogic method publishAnalogEvents.

protected void publishAnalogEvents(int pin, int value) {
    // 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 FirmataSubscriptionHandle) {
                FirmataSubscriptionHandle subscriptionHandle = (FirmataSubscriptionHandle) handle;
                // (The bit subscribed to in this field actually changed).
                if (subscriptionHandle.getField() instanceof FirmataFieldAnalog) {
                    FirmataFieldAnalog analogField = (FirmataFieldAnalog) subscriptionHandle.getField();
                    // Check if this field would include the current pin.
                    if ((analogField.getAddress() <= pin) && (analogField.getAddress() + analogField.getNumberOfElements() >= pin)) {
                        // Build an update event containing the current values for all subscribed fields.
                        List<PlcValue> values = new ArrayList<>(analogField.getNumberOfElements());
                        for (int i = analogField.getAddress(); i < analogField.getAddress() + analogField.getNumberOfElements(); i++) {
                            if (analogValues.containsKey(i)) {
                                values.add(new PlcDINT(analogValues.get(i).intValue()));
                            } else // This could be the case if only some of the requested array values are available
                            {
                                values.add(new PlcDINT(-1));
                            }
                        }
                        sendUpdateEvents(consumer, subscriptionHandle.getName(), values);
                    }
                }
            }
        }
    }
}
Also used : DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) PlcDINT(org.apache.plc4x.java.spi.values.PlcDINT) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) FirmataSubscriptionHandle(org.apache.plc4x.java.firmata.readwrite.model.FirmataSubscriptionHandle) Consumer(java.util.function.Consumer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FirmataFieldAnalog(org.apache.plc4x.java.firmata.readwrite.field.FirmataFieldAnalog)

Example 3 with DefaultPlcConsumerRegistration

use of org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration 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)

Example 4 with DefaultPlcConsumerRegistration

use of org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration in project plc4x by apache.

the class KnxNetIpProtocolLogic method unregister.

@Override
public void unregister(PlcConsumerRegistration plcConsumerRegistration) {
    DefaultPlcConsumerRegistration consumerRegistration = (DefaultPlcConsumerRegistration) plcConsumerRegistration;
    consumers.remove(consumerRegistration);
}
Also used : DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration)

Example 5 with DefaultPlcConsumerRegistration

use of org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration in project plc4x by apache.

the class KnxNetIpProtocolLogic method register.

@Override
public PlcConsumerRegistration register(Consumer<PlcSubscriptionEvent> consumer, Collection<PlcSubscriptionHandle> collection) {
    final DefaultPlcConsumerRegistration consumerRegistration = new DefaultPlcConsumerRegistration(this, consumer, collection.toArray(new PlcSubscriptionHandle[0]));
    consumers.put(consumerRegistration, consumer);
    return consumerRegistration;
}
Also used : DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle)

Aggregations

DefaultPlcConsumerRegistration (org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration)19 PlcSubscriptionHandle (org.apache.plc4x.java.api.model.PlcSubscriptionHandle)13 Consumer (java.util.function.Consumer)6 DefaultPlcSubscriptionHandle (org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 PlcConsumerRegistration (org.apache.plc4x.java.api.model.PlcConsumerRegistration)3 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)3 HashMap (java.util.HashMap)2 PlcValue (org.apache.plc4x.java.api.value.PlcValue)2 FirmataSubscriptionHandle (org.apache.plc4x.java.firmata.readwrite.model.FirmataSubscriptionHandle)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 AdsSubscriptionHandle (org.apache.plc4x.java.ads.model.AdsSubscriptionHandle)1 PlcSubscriptionEvent (org.apache.plc4x.java.api.messages.PlcSubscriptionEvent)1 GenericCANField (org.apache.plc4x.java.can.generic.field.GenericCANField)1 CANOpenPDOField (org.apache.plc4x.java.canopen.field.CANOpenPDOField)1 CANOpenHeartbeatPayload (org.apache.plc4x.java.canopen.readwrite.CANOpenHeartbeatPayload)1 CANOpenNetworkPayload (org.apache.plc4x.java.canopen.readwrite.CANOpenNetworkPayload)1 CANOpenPDOPayload (org.apache.plc4x.java.canopen.readwrite.CANOpenPDOPayload)1 NMTState (org.apache.plc4x.java.canopen.readwrite.NMTState)1