Search in sources :

Example 1 with PlcConsumerRegistration

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

the class ManualPlc4XAdsTest method main.

public static void main(String... args) throws Exception {
    String connectionUrl;
    if (args.length > 0 && "serial".equalsIgnoreCase(args[0])) {
        System.out.println("Using serial");
        connectionUrl = "ads:serial:///dev/ttys003/10.10.64.40.1.1:851/10.10.56.23.1.1:30000";
    } else {
        System.out.println("Using tcp");
        connectionUrl = "ads:tcp://10.10.64.40/10.10.64.40.1.1:851/10.10.56.23.1.1:30000";
    }
    // TODO: temporary workaround
    Thread.currentThread().setUncaughtExceptionHandler((t, e) -> {
        System.err.println(t + " - " + e.getMessage());
        e.printStackTrace();
        System.exit(1);
    });
    try (PlcConnection plcConnection = new PlcDriverManager().getConnection(connectionUrl)) {
        System.out.println("PlcConnection " + plcConnection);
        PlcReadRequest.Builder readRequestBuilder = plcConnection.readRequestBuilder();
        PlcReadRequest readRequest = readRequestBuilder.addItem("station", "Allgemein_S2.Station:BYTE").build();
        CompletableFuture<? extends PlcReadResponse> response = readRequest.execute();
        PlcReadResponse readResponse = response.get();
        System.out.println("Response " + readResponse);
        Collection<Integer> stations = readResponse.getAllIntegers("station");
        stations.forEach(System.out::println);
        // 2. We build a subscription
        PlcSubscriptionRequest.Builder subscriptionRequestBuilder = plcConnection.subscriptionRequestBuilder();
        PlcSubscriptionRequest subscriptionRequest = subscriptionRequestBuilder.addChangeOfStateField("stationChange", "Allgemein_S2.Station:BYTE").addCyclicField("stationChange2", "Allgemein_S2.Station:BYTE", Duration.ofSeconds(3)).build();
        PlcSubscriptionResponse plcSubscriptionResponse = subscriptionRequest.execute().get();
        List<PlcConsumerRegistration> plcConsumerRegistrations = plcSubscriptionResponse.getSubscriptionHandles().stream().map(plcSubscriptionHandle -> plcSubscriptionHandle.register(plcSubscriptionEvent -> {
            int stationChange = plcSubscriptionEvent.getNumberOfValues("stationChange");
            int stationChange2 = plcSubscriptionEvent.getNumberOfValues("stationChange2");
            System.out.println(String.format("%s: [%d]- StationsNummer: {%d}", plcSubscriptionEvent.getTimestamp(), stationChange + stationChange2, plcSubscriptionEvent.getInteger("stationChange2")));
        })).collect(Collectors.toList());
        // Now we wait a bit
        TimeUnit.SECONDS.sleep(10);
        // we unregister the listener
        plcConsumerRegistrations.forEach(PlcConsumerRegistration::unregister);
        // we unsubscribe at the plc
        PlcUnsubscriptionRequest.Builder unsubscriptionRequestBuilder = plcConnection.unsubscriptionRequestBuilder();
        PlcUnsubscriptionRequest unsubscriptionRequest = unsubscriptionRequestBuilder.addHandles(plcSubscriptionResponse.getSubscriptionHandles()).build();
        CompletableFuture<PlcUnsubscriptionResponse> unsubscriptionResponse = unsubscriptionRequest.execute();
        unsubscriptionResponse.get(5, TimeUnit.SECONDS);
        System.out.println(unsubscriptionResponse);
    }
    System.exit(0);
}
Also used : PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) PlcConnection(org.apache.plc4x.java.api.PlcConnection) Duration(java.time.Duration) Collection(java.util.Collection) CompletableFuture(java.util.concurrent.CompletableFuture) PlcConsumerRegistration(org.apache.plc4x.java.api.model.PlcConsumerRegistration) Collectors(java.util.stream.Collectors) org.apache.plc4x.java.api.messages(org.apache.plc4x.java.api.messages) PlcConnection(org.apache.plc4x.java.api.PlcConnection) PlcConsumerRegistration(org.apache.plc4x.java.api.model.PlcConsumerRegistration) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager)

Example 2 with PlcConsumerRegistration

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

the class SimulatedConnection method register.

@Override
public PlcConsumerRegistration register(Consumer<PlcSubscriptionEvent> consumer, Collection<PlcSubscriptionHandle> handles) {
    LOGGER.info("Registering consumer {} with handles {}", consumer, handles);
    PlcConsumerRegistration plcConsumerRegistration = new DefaultPlcConsumerRegistration(this, consumer, handles.toArray(new PlcSubscriptionHandle[0]));
    handles.stream().map(PlcSubscriptionHandle.class::cast).forEach(handle -> registrations.put(handle, plcConsumerRegistration));
    consumerIdMap.put(plcConsumerRegistration.getConsumerId(), consumer);
    return plcConsumerRegistration;
}
Also used : DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) DefaultPlcSubscriptionHandle(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle) DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) PlcConsumerRegistration(org.apache.plc4x.java.api.model.PlcConsumerRegistration)

Example 3 with PlcConsumerRegistration

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

the class OpcuaProtocolLogic method register.

@Override
public PlcConsumerRegistration register(Consumer<PlcSubscriptionEvent> consumer, Collection<PlcSubscriptionHandle> handles) {
    List<PlcConsumerRegistration> registrations = new LinkedList<>();
    // Register the current consumer for each of the given subscription handles
    for (PlcSubscriptionHandle subscriptionHandle : handles) {
        LOGGER.debug("Registering Consumer");
        final PlcConsumerRegistration consumerRegistration = subscriptionHandle.register(consumer);
        registrations.add(consumerRegistration);
    }
    return new DefaultPlcConsumerRegistration(this, consumer, handles.toArray(new PlcSubscriptionHandle[0]));
}
Also used : DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) PlcConsumerRegistration(org.apache.plc4x.java.api.model.PlcConsumerRegistration)

Example 4 with PlcConsumerRegistration

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

the class SimulatedConnection method unregister.

@Override
public void unregister(PlcConsumerRegistration registration) {
    LOGGER.info("Unregistering {}", registration);
    Iterator<Map.Entry<PlcSubscriptionHandle, PlcConsumerRegistration>> entryIterator = registrations.entrySet().iterator();
    while (entryIterator.hasNext()) {
        Map.Entry<PlcSubscriptionHandle, PlcConsumerRegistration> entry = entryIterator.next();
        if (!entry.getValue().equals(registration)) {
            LOGGER.debug("not the value we looking for {}. We are looking for {}", entry.getValue(), registration);
            continue;
        }
        PlcConsumerRegistration consumerRegistration = entry.getValue();
        int consumerId = consumerRegistration.getConsumerId();
        LOGGER.info("Removing consumer {}", consumerId);
        consumerIdMap.remove(consumerId);
        LOGGER.info("Removing handles {}", consumerRegistration.getSubscriptionHandles());
        device.removeHandles(consumerRegistration.getSubscriptionHandles());
        entryIterator.remove();
    }
}
Also used : PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) DefaultPlcSubscriptionHandle(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle) DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) PlcConsumerRegistration(org.apache.plc4x.java.api.model.PlcConsumerRegistration) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 5 with PlcConsumerRegistration

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

PlcConsumerRegistration (org.apache.plc4x.java.api.model.PlcConsumerRegistration)6 DefaultPlcConsumerRegistration (org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration)5 PlcSubscriptionHandle (org.apache.plc4x.java.api.model.PlcSubscriptionHandle)4 HashMap (java.util.HashMap)2 Consumer (java.util.function.Consumer)2 S7PlcSubscriptionHandle (org.apache.plc4x.java.s7.readwrite.utils.S7PlcSubscriptionHandle)2 DefaultPlcSubscriptionHandle (org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle)2 Duration (java.time.Duration)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 PlcDriverManager (org.apache.plc4x.java.PlcDriverManager)1 PlcConnection (org.apache.plc4x.java.api.PlcConnection)1 org.apache.plc4x.java.api.messages (org.apache.plc4x.java.api.messages)1