Search in sources :

Example 16 with DefaultPlcConsumerRegistration

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

the class FirmataProtocolLogic 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)

Example 17 with DefaultPlcConsumerRegistration

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

the class FirmataProtocolLogic 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 18 with DefaultPlcConsumerRegistration

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

the class FirmataProtocolLogic method publishDigitalEvents.

protected void publishDigitalEvents(BitSet changedBits, BitSet bitValues) {
    // If nothing changed, no need to do anything.
    if (changedBits.cardinality() == 0) {
        return;
    }
    // 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 FirmataFieldDigital) {
                    FirmataFieldDigital digitalField = (FirmataFieldDigital) subscriptionHandle.getField();
                    // send out an update event with all of its current values.
                    if (digitalField.getBitSet().intersects(changedBits)) {
                        List<PlcValue> values = new ArrayList<>(digitalField.getBitSet().cardinality());
                        for (int i = 0; i < digitalField.getBitSet().length(); i++) {
                            values.add(new PlcBOOL(bitValues.get(i)));
                        }
                        sendUpdateEvents(consumer, subscriptionHandle.getName(), values);
                    }
                }
            }
        }
    }
}
Also used : DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) PlcBOOL(org.apache.plc4x.java.spi.values.PlcBOOL) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) FirmataSubscriptionHandle(org.apache.plc4x.java.firmata.readwrite.model.FirmataSubscriptionHandle) Consumer(java.util.function.Consumer) FirmataFieldDigital(org.apache.plc4x.java.firmata.readwrite.field.FirmataFieldDigital) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 19 with DefaultPlcConsumerRegistration

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

the class S7ProtocolEventLogic method register.

@Override
public PlcConsumerRegistration register(Consumer<PlcSubscriptionEvent> consumer, Collection<PlcSubscriptionHandle> handles) {
    Map<PlcConsumerRegistration, Consumer> mapConsumers = null;
    S7PlcSubscriptionHandle handle = (S7PlcSubscriptionHandle) handles.toArray()[0];
    if (!mapIndex.containsKey(handle.getEventType())) {
        mapConsumers = new HashMap();
        mapIndex.put(handle.getEventType(), mapConsumers);
    }
    mapConsumers = mapIndex.get(handle.getEventType());
    // TODO: Check the implementation of "DefaultPlcConsumerRegistration". List<> vs Collection<>
    DefaultPlcConsumerRegistration registro = new DefaultPlcConsumerRegistration(this, consumer, handles.toArray(new PlcSubscriptionHandle[handles.size()]));
    mapConsumers.put(registro, consumer);
    return registro;
}
Also used : DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) Consumer(java.util.function.Consumer) HashMap(java.util.HashMap) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) S7PlcSubscriptionHandle(org.apache.plc4x.java.s7.readwrite.utils.S7PlcSubscriptionHandle) DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) PlcConsumerRegistration(org.apache.plc4x.java.api.model.PlcConsumerRegistration) S7PlcSubscriptionHandle(org.apache.plc4x.java.s7.readwrite.utils.S7PlcSubscriptionHandle)

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