Search in sources :

Example 21 with PlcSubscriptionResponse

use of org.apache.plc4x.java.api.messages.PlcSubscriptionResponse in project plc4x by apache.

the class OpcuaSubscriptionHandleTest method subscribeDoesNotExists.

@Test
public void subscribeDoesNotExists() throws Exception {
    String field = "DoesNotExists";
    String identifier = DOES_NOT_EXIST_IDENTIFIER_READ_WRITE;
    LOGGER.info("Starting subscription {} test", field);
    // Create Subscription
    PlcSubscriptionRequest.Builder builder = opcuaConnection.subscriptionRequestBuilder();
    builder.addChangeOfStateField(field, identifier);
    PlcSubscriptionRequest request = builder.build();
    // Get result of creating subscription
    PlcSubscriptionResponse response = request.execute().get();
    final OpcuaSubscriptionHandle subscriptionHandle = (OpcuaSubscriptionHandle) response.getSubscriptionHandle(field);
    // Create handler for returned value
    subscriptionHandle.register(plcSubscriptionEvent -> {
        // This should never be called,
        assert false;
        LOGGER.info("Received a response from {} test {}", field, plcSubscriptionEvent.getPlcValue(field).toString());
    });
    // Wait for value to be returned from server
    Thread.sleep(1200);
    subscriptionHandle.stopSubscriber();
}
Also used : PlcSubscriptionRequest(org.apache.plc4x.java.api.messages.PlcSubscriptionRequest) PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) OpcuaPlcDriverTest(org.apache.plc4x.java.opcua.OpcuaPlcDriverTest)

Example 22 with PlcSubscriptionResponse

use of org.apache.plc4x.java.api.messages.PlcSubscriptionResponse in project plc4x by apache.

the class OpcuaSubscriptionHandleTest method subscribeInt64.

@Test
public void subscribeInt64() throws Exception {
    String field = "Int64";
    String identifier = INT64_IDENTIFIER_READ_WRITE;
    LOGGER.info("Starting subscription {} test", field);
    // Create Subscription
    PlcSubscriptionRequest.Builder builder = opcuaConnection.subscriptionRequestBuilder();
    builder.addChangeOfStateField(field, identifier);
    PlcSubscriptionRequest request = builder.build();
    // Get result of creating subscription
    PlcSubscriptionResponse response = request.execute().get();
    final OpcuaSubscriptionHandle subscriptionHandle = (OpcuaSubscriptionHandle) response.getSubscriptionHandle(field);
    // Create handler for returned value
    subscriptionHandle.register(plcSubscriptionEvent -> {
        assert plcSubscriptionEvent.getResponseCode(field).equals(PlcResponseCode.OK);
        LOGGER.info("Received a response from {} test {}", field, plcSubscriptionEvent.getPlcValue(field).toString());
    });
    // Wait for value to be returned from server
    Thread.sleep(1200);
    subscriptionHandle.stopSubscriber();
}
Also used : PlcSubscriptionRequest(org.apache.plc4x.java.api.messages.PlcSubscriptionRequest) PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) OpcuaPlcDriverTest(org.apache.plc4x.java.opcua.OpcuaPlcDriverTest)

Example 23 with PlcSubscriptionResponse

use of org.apache.plc4x.java.api.messages.PlcSubscriptionResponse in project plc4x by apache.

the class PassiveBacNetIpDriver2Manual method main.

public static void main(String[] args) throws Exception {
    String tempDirectory = FileUtils.getTempDirectoryPath();
    File pcapFile = FileSystems.getDefault().getPath(tempDirectory, "bacnet-stack-services.cap").toFile();
    if (!pcapFile.exists())
        FileUtils.copyURLToFile(new URL("https://wiki.wireshark.org/Protocols/bacnet?action=AttachFile&do=get&target=bacnet-stack-services.cap"), pcapFile);
    final BacNetIpDriver driver = new BacNetIpDriver();
    final PlcConnection connection = driver.getConnection("bacnet-ip:pcap://" + pcapFile.getAbsolutePath());
    connection.connect();
    final PlcSubscriptionResponse subscriptionResponse = connection.subscriptionRequestBuilder().addEventField("Hurz", "*/*/*").build().execute().get();
    subscriptionResponse.getSubscriptionHandle("Hurz").register(plcSubscriptionEvent -> {
        PlcStruct plcStruct = (PlcStruct) ((DefaultPlcSubscriptionEvent) plcSubscriptionEvent).getValues().get("event").getValue();
        System.out.println(plcStruct);
    });
}
Also used : PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) PlcStruct(org.apache.plc4x.java.spi.values.PlcStruct) File(java.io.File) URL(java.net.URL) PlcConnection(org.apache.plc4x.java.api.PlcConnection)

Example 24 with PlcSubscriptionResponse

use of org.apache.plc4x.java.api.messages.PlcSubscriptionResponse in project plc4x by apache.

the class PassiveBacNetIpDriver3Manual method main.

public static void main(String[] args) throws Exception {
    String tempDirectory = FileUtils.getTempDirectoryPath();
    File pcapFile = FileSystems.getDefault().getPath(tempDirectory, "bo_command_failure_original.pcap").toFile();
    if (!pcapFile.exists())
        FileUtils.copyURLToFile(new URL("http://kargs.net/captures/bo_command_failure_original.pcap"), pcapFile);
    final BacNetIpDriver driver = new BacNetIpDriver();
    try (final PlcConnection connection = driver.getConnection("bacnet-ip:pcap://" + pcapFile.getAbsolutePath() + "?filter=udp%20port%2047808")) {
        connection.connect();
        final PlcSubscriptionResponse subscriptionResponse = connection.subscriptionRequestBuilder().addEventField("Hurz", "*/*/*").build().execute().get();
        subscriptionResponse.getSubscriptionHandle("Hurz").register(plcSubscriptionEvent -> {
            PlcStruct plcStruct = (PlcStruct) ((DefaultPlcSubscriptionEvent) plcSubscriptionEvent).getValues().get("event").getValue();
            System.out.println(plcStruct);
        });
        TimeUnit.SECONDS.sleep(3);
    }
}
Also used : PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) PlcStruct(org.apache.plc4x.java.spi.values.PlcStruct) File(java.io.File) URL(java.net.URL) PlcConnection(org.apache.plc4x.java.api.PlcConnection)

Example 25 with PlcSubscriptionResponse

use of org.apache.plc4x.java.api.messages.PlcSubscriptionResponse in project plc4x by apache.

the class S7ProtocolLogic method subscribe.

@Override
public CompletableFuture<PlcSubscriptionResponse> subscribe(PlcSubscriptionRequest subscriptionRequest) {
    CompletableFuture<PlcSubscriptionResponse> future = new CompletableFuture<>();
    DefaultPlcSubscriptionRequest request = (DefaultPlcSubscriptionRequest) subscriptionRequest;
    List<S7ParameterUserDataItem> parameterItems = new ArrayList<>(request.getNumberOfFields());
    List<S7PayloadUserDataItem> payloadItems = new ArrayList<>(request.getNumberOfFields());
    for (String fieldName : request.getFieldNames()) {
        final DefaultPlcSubscriptionField sf = (DefaultPlcSubscriptionField) request.getField(fieldName);
        final S7SubscriptionField field = (S7SubscriptionField) sf.getPlcField();
        switch(field.getFieldType()) {
            case EVENT_SUBSCRIPTION:
                encodeEventSubscriptionRequest(request, parameterItems, payloadItems);
                break;
            case EVENT_UNSUBSCRIPTION:
                // encodeEventUnSubscriptionRequest(msg, out);
                break;
            case ALARM_ACK:
                // encodeAlarmAckRequest(msg, out);
                break;
            case ALARM_QUERY:
                // encodeAlarmQueryRequest(msg, out);
                break;
            case CYCLIC_SUBSCRIPTION:
                // encodeCycledSubscriptionRequest(msg, out);
                break;
            case CYCLIC_UNSUBSCRIPTION:
                // encodeCycledUnSubscriptionRequest(msg, out);
                break;
            default:
        }
    // final PlcValue plcValue = request.getPlcValue(fieldName);
    // parameterItems.add(new S7VarRequestParameterItemAddress(encodeS7Address(field)));
    // payloadItems.add(serializePlcValue(field, plcValue));
    }
    final int tpduId = tpduGenerator.getAndIncrement();
    // If we've reached the max value for a 16 bit transaction identifier, reset back to 1
    if (tpduGenerator.get() == 0xFFFF) {
        tpduGenerator.set(1);
    }
    TPKTPacket tpktPacket = new TPKTPacket(new COTPPacketData(null, new S7MessageUserData(tpduId, new S7ParameterUserData(parameterItems), new S7PayloadUserData(payloadItems, null)), true, (short) tpduId, null));
    // Start a new request-transaction (Is ended in the response-handler)
    RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
    transaction.submit(() -> context.sendRequest(tpktPacket).onTimeout(new TransactionErrorCallback<>(future, transaction)).onError(new TransactionErrorCallback<>(future, transaction)).expectResponse(TPKTPacket.class, REQUEST_TIMEOUT).check(p -> p.getPayload() instanceof COTPPacketData).unwrap(p -> ((COTPPacketData) p.getPayload())).unwrap(COTPPacket::getPayload).check(p -> p.getTpduReference() == tpduId).handle(p -> {
        try {
            future.complete(decodeEventSubscriptionRequest(p, subscriptionRequest));
        } catch (PlcProtocolException e) {
            logger.warn("Error sending 'write' message: '{}'", e.getMessage(), e);
        }
        // Finish the request-transaction.
        transaction.endRequest();
    }));
    return future;
}
Also used : PlcWriteResponse(org.apache.plc4x.java.api.messages.PlcWriteResponse) PlcWriteRequest(org.apache.plc4x.java.api.messages.PlcWriteRequest) DriverContext(org.apache.plc4x.java.spi.context.DriverContext) LoggerFactory(org.slf4j.LoggerFactory) PlcValue(org.apache.plc4x.java.api.value.PlcValue) TimeoutException(java.util.concurrent.TimeoutException) ByteBuffer(java.nio.ByteBuffer) PlcSubscriptionRequest(org.apache.plc4x.java.api.messages.PlcSubscriptionRequest) Unpooled(io.netty.buffer.Unpooled) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConversationContext(org.apache.plc4x.java.spi.ConversationContext) Duration(java.time.Duration) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) Plc4xProtocolBase(org.apache.plc4x.java.spi.Plc4xProtocolBase) PlcUnsubscriptionRequest(org.apache.plc4x.java.api.messages.PlcUnsubscriptionRequest) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) BlockingQueue(java.util.concurrent.BlockingQueue) S7PlcSubscriptionHandle(org.apache.plc4x.java.s7.readwrite.utils.S7PlcSubscriptionHandle) InvocationTargetException(java.lang.reflect.InvocationTargetException) org.apache.plc4x.java.spi.generation(org.apache.plc4x.java.spi.generation) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) org.apache.plc4x.java.s7.readwrite(org.apache.plc4x.java.s7.readwrite) PlcNull(org.apache.plc4x.java.spi.values.PlcNull) DefaultPlcWriteRequest(org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) org.apache.plc4x.java.s7.readwrite.types(org.apache.plc4x.java.s7.readwrite.types) IntStream(java.util.stream.IntStream) S7StringField(org.apache.plc4x.java.s7.readwrite.field.S7StringField) java.util(java.util) PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) CompletableFuture(java.util.concurrent.CompletableFuture) PlcUnsubscriptionResponse(org.apache.plc4x.java.api.messages.PlcUnsubscriptionResponse) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) PlcResponse(org.apache.plc4x.java.api.messages.PlcResponse) S7DriverContext(org.apache.plc4x.java.s7.readwrite.context.S7DriverContext) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) ByteBuf(io.netty.buffer.ByteBuf) DefaultPlcSubscriptionResponse(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse) DefaultPlcSubscriptionField(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField) BiConsumer(java.util.function.BiConsumer) Logger(org.slf4j.Logger) DefaultPlcUnsubscriptionRequest(org.apache.plc4x.java.spi.messages.DefaultPlcUnsubscriptionRequest) PlcField(org.apache.plc4x.java.api.model.PlcField) IEC61131ValueHandler(org.apache.plc4x.java.spi.values.IEC61131ValueHandler) Consumer(java.util.function.Consumer) DefaultPlcSubscriptionRequest(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionRequest) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) S7SubscriptionField(org.apache.plc4x.java.s7.readwrite.field.S7SubscriptionField) S7Field(org.apache.plc4x.java.s7.readwrite.field.S7Field) DefaultPlcReadRequest(org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest) S7SubscriptionField(org.apache.plc4x.java.s7.readwrite.field.S7SubscriptionField) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) CompletableFuture(java.util.concurrent.CompletableFuture) DefaultPlcSubscriptionField(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField) PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) DefaultPlcSubscriptionResponse(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse) DefaultPlcSubscriptionRequest(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionRequest)

Aggregations

PlcSubscriptionResponse (org.apache.plc4x.java.api.messages.PlcSubscriptionResponse)27 PlcSubscriptionRequest (org.apache.plc4x.java.api.messages.PlcSubscriptionRequest)23 OpcuaPlcDriverTest (org.apache.plc4x.java.opcua.OpcuaPlcDriverTest)16 PlcConnection (org.apache.plc4x.java.api.PlcConnection)8 PlcSubscriptionHandle (org.apache.plc4x.java.api.model.PlcSubscriptionHandle)6 PlcDriverManager (org.apache.plc4x.java.PlcDriverManager)5 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)4 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)3 PlcStruct (org.apache.plc4x.java.spi.values.PlcStruct)3 File (java.io.File)2 URL (java.net.URL)2 Scanner (java.util.Scanner)2 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)2 DefaultPlcSubscriptionResponse (org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse)2 InfluxDBClient (com.influxdb.client.InfluxDBClient)1 WriteApi (com.influxdb.client.WriteApi)1 Point (com.influxdb.client.write.Point)1 ByteBuf (io.netty.buffer.ByteBuf)1 Unpooled (io.netty.buffer.Unpooled)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1