Search in sources :

Example 1 with PlcSubscriptionRequest

use of org.apache.plc4x.java.api.messages.PlcSubscriptionRequest 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 2 with PlcSubscriptionRequest

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

Example 3 with PlcSubscriptionRequest

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

the class HelloPlc4xSubscription 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());
        }
        // 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)

Example 4 with PlcSubscriptionRequest

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

the class OpcuaSubscriptionHandleTest method subscribeString.

@Test
public void subscribeString() throws Exception {
    String field = "String";
    String identifier = STRING_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 5 with PlcSubscriptionRequest

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

the class OpcuaSubscriptionHandleTest method subscribeUInt32.

@Test
public void subscribeUInt32() throws Exception {
    String field = "UInt32";
    String identifier = UINT32_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)

Aggregations

PlcSubscriptionRequest (org.apache.plc4x.java.api.messages.PlcSubscriptionRequest)23 PlcSubscriptionResponse (org.apache.plc4x.java.api.messages.PlcSubscriptionResponse)23 OpcuaPlcDriverTest (org.apache.plc4x.java.opcua.OpcuaPlcDriverTest)16 PlcDriverManager (org.apache.plc4x.java.PlcDriverManager)5 PlcConnection (org.apache.plc4x.java.api.PlcConnection)5 PlcSubscriptionHandle (org.apache.plc4x.java.api.model.PlcSubscriptionHandle)5 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)4 Scanner (java.util.Scanner)2 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)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 ByteBuffer (java.nio.ByteBuffer)1 Duration (java.time.Duration)1 java.util (java.util)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 BlockingQueue (java.util.concurrent.BlockingQueue)1 CompletableFuture (java.util.concurrent.CompletableFuture)1