Search in sources :

Example 1 with FirmataSubscriptionHandle

use of org.apache.plc4x.java.firmata.readwrite.model.FirmataSubscriptionHandle 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 2 with FirmataSubscriptionHandle

use of org.apache.plc4x.java.firmata.readwrite.model.FirmataSubscriptionHandle in project plc4x by apache.

the class FirmataProtocolLogic method subscribe.

@Override
public CompletableFuture<PlcSubscriptionResponse> subscribe(PlcSubscriptionRequest subscriptionRequest) {
    CompletableFuture<PlcSubscriptionResponse> future = new CompletableFuture<>();
    try {
        final List<FirmataMessage> firmataMessages = ((FirmataDriverContext) getDriverContext()).processSubscriptionRequest(subscriptionRequest);
        for (FirmataMessage firmataMessage : firmataMessages) {
            context.sendToWire(firmataMessage);
        }
        Map<String, ResponseItem<PlcSubscriptionHandle>> result = new HashMap<>();
        for (String fieldName : subscriptionRequest.getFieldNames()) {
            DefaultPlcSubscriptionField subscriptionField = (DefaultPlcSubscriptionField) subscriptionRequest.getField(fieldName);
            FirmataField field = (FirmataField) subscriptionField.getPlcField();
            result.put(fieldName, new ResponseItem<>(PlcResponseCode.OK, new FirmataSubscriptionHandle(this, fieldName, field)));
        }
        future.complete(new DefaultPlcSubscriptionResponse(subscriptionRequest, result));
    } catch (PlcRuntimeException e) {
        future.completeExceptionally(e);
    }
    return future;
}
Also used : PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) FirmataField(org.apache.plc4x.java.firmata.readwrite.field.FirmataField) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletableFuture(java.util.concurrent.CompletableFuture) DefaultPlcSubscriptionField(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField) FirmataSubscriptionHandle(org.apache.plc4x.java.firmata.readwrite.model.FirmataSubscriptionHandle) FirmataDriverContext(org.apache.plc4x.java.firmata.readwrite.context.FirmataDriverContext) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem)

Example 3 with FirmataSubscriptionHandle

use of org.apache.plc4x.java.firmata.readwrite.model.FirmataSubscriptionHandle 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)

Aggregations

ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 FirmataSubscriptionHandle (org.apache.plc4x.java.firmata.readwrite.model.FirmataSubscriptionHandle)3 Consumer (java.util.function.Consumer)2 PlcSubscriptionHandle (org.apache.plc4x.java.api.model.PlcSubscriptionHandle)2 DefaultPlcConsumerRegistration (org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration)2 CompletableFuture (java.util.concurrent.CompletableFuture)1 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)1 FirmataDriverContext (org.apache.plc4x.java.firmata.readwrite.context.FirmataDriverContext)1 FirmataField (org.apache.plc4x.java.firmata.readwrite.field.FirmataField)1 FirmataFieldAnalog (org.apache.plc4x.java.firmata.readwrite.field.FirmataFieldAnalog)1 FirmataFieldDigital (org.apache.plc4x.java.firmata.readwrite.field.FirmataFieldDigital)1 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)1 DefaultPlcSubscriptionField (org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField)1 PlcBOOL (org.apache.plc4x.java.spi.values.PlcBOOL)1 PlcDINT (org.apache.plc4x.java.spi.values.PlcDINT)1