Search in sources :

Example 1 with PlcSubscriptionHandle

use of org.apache.plc4x.java.api.model.PlcSubscriptionHandle 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 PlcSubscriptionHandle

use of org.apache.plc4x.java.api.model.PlcSubscriptionHandle 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 3 with PlcSubscriptionHandle

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

Example 4 with PlcSubscriptionHandle

use of org.apache.plc4x.java.api.model.PlcSubscriptionHandle in project plc4x by apache.

the class ManualKnxNetIp method main.

// Addresses:
// */*/10: Temperature
// */*/12: Heating
// */*/60: Primary Window
// */*/64: Second Window
// */*/101: Power Line 1
public static void main(String[] args) throws Exception {
    // final PlcConnection connection = new PlcDriverManager().getConnection("knxnet-ip://192.168.42.11?knxproj-file-path=/Users/christofer.dutz/Projects/Apache/PLC4X-Documents/KNX/Stettiner%20Str.%2013/StettinerStr-Soll-Ist-Temperatur.knxproj");
    final PlcConnection connection = new PlcDriverManager().getConnection("knxnet-ip:pcap:///Users/christofer.dutz/Projects/Apache/PLC4X-Documents/KNX/Recording-01.03.2020-2.pcapng?knxproj-file-path=/Users/christofer.dutz/Projects/Apache/PLC4X-Documents/KNX/Stettiner%20Str.%2013/StettinerStr-Soll-Ist-Temperatur.knxproj");
    // Make sure we hang up correctly when terminating.
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        try {
            connection.close();
        } catch (Exception e) {
            throw new PlcRuntimeException("Error closing connection", e);
        }
    }));
    // Create a new subscription request.
    // The address and the name is just bogus as we're always returning everything.
    // We will probably refactor the API in the near future.
    final PlcSubscriptionRequest subscriptionRequest = connection.subscriptionRequestBuilder().addEventField("knxData", "*/*/*").build();
    // Register the subscription
    // The timeout is also just a bogus value as the data is coming in actively
    // We will probably refactor the API in the near future.
    final PlcSubscriptionResponse subscriptionResponse = subscriptionRequest.execute().get(1000, TimeUnit.MILLISECONDS);
    // Register a callback which is called on new data being available.
    final PlcSubscriptionHandle subscriptionHandle = subscriptionResponse.getSubscriptionHandle("knxData");
    subscriptionHandle.register(knxData -> {
        System.out.println(knxData.getTimestamp().toString() + " - " + ((DefaultPlcSubscriptionEvent) knxData).getValues().get("knxData"));
    });
}
Also used : PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) PlcSubscriptionRequest(org.apache.plc4x.java.api.messages.PlcSubscriptionRequest) PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcConnection(org.apache.plc4x.java.api.PlcConnection) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException)

Example 5 with PlcSubscriptionHandle

use of org.apache.plc4x.java.api.model.PlcSubscriptionHandle in project plc4x by apache.

the class HelloWebservice method run.

public void run() throws Exception {
    // Establish a connection to the plc.
    try (PlcConnection plcConnection = new PlcDriverManager().getConnection(options.getConnectionString())) {
        // Check if this connection support subscriptions.
        if (!plcConnection.getMetadata().canSubscribe()) {
            logger.error("This connection doesn't support subscriptions.");
            return;
        }
        // Create a new read request:
        // - Give the single item requested the alias name "value"
        final PlcSubscriptionRequest.Builder builder = plcConnection.subscriptionRequestBuilder();
        for (int i = 0; i < options.getFieldAddress().length; i++) {
            builder.addChangeOfStateField("value-" + i, options.getFieldAddress()[i]);
        }
        PlcSubscriptionRequest subscriptionRequest = builder.build();
        // Execute the subscription response.
        final PlcSubscriptionResponse subscriptionResponse = subscriptionRequest.execute().get();
        // Attach handlers for the incoming data.
        for (String subscriptionName : subscriptionResponse.getFieldNames()) {
            final PlcSubscriptionHandle subscriptionHandle = subscriptionResponse.getSubscriptionHandle(subscriptionName);
            subscriptionHandle.register(new ValueChangeHandler(options.getWebserviceUrl()));
        }
        // Wait for the user to press "Enter" to abort the program.
        Scanner scanner = new Scanner(System.in);
        try {
            logger.info("Please press Enter to exit program.");
            scanner.nextLine();
            logger.info("Finishing");
        } catch (IllegalStateException e) {
            // System.in has been closed
            logger.error("System.in was closed; exiting");
        }
    }
}
Also used : Scanner(java.util.Scanner) PlcSubscriptionRequest(org.apache.plc4x.java.api.messages.PlcSubscriptionRequest) PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcConnection(org.apache.plc4x.java.api.PlcConnection)

Aggregations

PlcSubscriptionHandle (org.apache.plc4x.java.api.model.PlcSubscriptionHandle)20 DefaultPlcConsumerRegistration (org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration)15 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 Consumer (java.util.function.Consumer)7 DefaultPlcSubscriptionHandle (org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle)7 PlcSubscriptionResponse (org.apache.plc4x.java.api.messages.PlcSubscriptionResponse)5 PlcConsumerRegistration (org.apache.plc4x.java.api.model.PlcConsumerRegistration)5 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)5 HashMap (java.util.HashMap)4 PlcDriverManager (org.apache.plc4x.java.PlcDriverManager)4 PlcSubscriptionRequest (org.apache.plc4x.java.api.messages.PlcSubscriptionRequest)4 PlcConnection (org.apache.plc4x.java.api.PlcConnection)3 PlcValue (org.apache.plc4x.java.api.value.PlcValue)3 Map (java.util.Map)2 Scanner (java.util.Scanner)2 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)2 GenericCANField (org.apache.plc4x.java.can.generic.field.GenericCANField)2 FirmataSubscriptionHandle (org.apache.plc4x.java.firmata.readwrite.model.FirmataSubscriptionHandle)2 Instant (java.time.Instant)1 java.util (java.util)1