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);
}
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;
}
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]));
}
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();
}
}
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;
}
Aggregations