Search in sources :

Example 1 with PlcDriverManager

use of org.apache.plc4x.java.PlcDriverManager 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 PlcDriverManager

use of org.apache.plc4x.java.PlcDriverManager in project plc4x by apache.

the class Main method main.

public static void main(String[] args) throws Exception {
    PlcDriverManager driverManager = new PlcDriverManager();
    CANOpenDriverContext.CALLBACK.addCallback(new Callback() {

        @Override
        public void receive(CANOpenFrame frame) {
        // System.err.println("Received frame " + frame);
        }
    });
    PlcConnection connection = driverManager.getConnection("canopen:javacan://vcan0?nodeId=11");
    // UUID.randomUUID().toString();
    String value = "abcdef";
    CompletableFuture<? extends PlcWriteResponse> response = connection.writeRequestBuilder().addItem("foo", "SDO:13:0x2000/0x0:VISIBLE_STRING", value).build().execute();
    response.whenComplete((writeReply, writeError) -> {
        System.out.println("====================================");
        if (writeError != null) {
            System.out.println("Error ");
            writeError.printStackTrace();
        } else {
            System.out.println("Result " + writeReply.getResponseCode("foo") + " " + value);
            PlcReadRequest.Builder builder = connection.readRequestBuilder();
            builder.addItem("foo", "SDO:13:0x2000/0x0:VISIBLE_STRING");
            CompletableFuture<? extends PlcReadResponse> future = builder.build().execute();
            future.whenComplete((readReply, readError) -> {
                System.out.println("====================================");
                if (readError != null) {
                    System.out.println("Error ");
                    readError.printStackTrace();
                } else {
                    System.out.println("Result " + readReply.getString("foo"));
                }
            });
        }
    });
// while (true) {
// }
}
Also used : Callback(org.apache.plc4x.java.canopen.listener.Callback) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcConnection(org.apache.plc4x.java.api.PlcConnection) CANOpenFrame(org.apache.plc4x.java.canopen.readwrite.CANOpenFrame)

Example 3 with PlcDriverManager

use of org.apache.plc4x.java.PlcDriverManager 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 4 with PlcDriverManager

use of org.apache.plc4x.java.PlcDriverManager 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 5 with PlcDriverManager

use of org.apache.plc4x.java.PlcDriverManager in project plc4x by apache.

the class HelloPlc4xWrite 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().canWrite()) {
            LOGGER.error("This connection doesn't support writing.");
            return;
        }
        if (options.getFieldValues().length != options.getFieldAddress().length) {
            LOGGER.error("The number of values doesn't match the number of addresses.");
            return;
        }
        // Create a new read request:
        // - Give the single item requested the alias name "value"
        final PlcWriteRequest.Builder builder = plcConnection.writeRequestBuilder();
        for (int i = 0; i < options.getFieldAddress().length; i++) {
            // If an array value is passed instead of a single value then convert to a String array
            if ((options.getFieldValues()[i].charAt(0) == '[') && (options.getFieldValues()[i].charAt(options.getFieldValues()[i].length() - 1) == ']')) {
                String[] values = options.getFieldValues()[i].substring(1, options.getFieldValues()[i].length() - 1).split(",");
                builder.addItem("value-" + i, options.getFieldAddress()[i], values);
            } else {
                builder.addItem("value-" + i, options.getFieldAddress()[i], options.getFieldValues()[i]);
            }
        }
        PlcWriteRequest writeRequest = builder.build();
        // Execute the write request.
        final PlcWriteResponse writeResponse = writeRequest.execute().get();
        // Attach handlers for the incoming data.
        for (String fieldName : writeResponse.getFieldNames()) {
            LOGGER.info(String.format("Return code for %s was %s", fieldName, writeResponse.getResponseCode(fieldName)));
        }
    }
}
Also used : PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcConnection(org.apache.plc4x.java.api.PlcConnection)

Aggregations

PlcDriverManager (org.apache.plc4x.java.PlcDriverManager)54 PlcConnection (org.apache.plc4x.java.api.PlcConnection)35 PlcConnectionException (org.apache.plc4x.java.api.exceptions.PlcConnectionException)13 PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)13 PlcReadResponse (org.apache.plc4x.java.api.messages.PlcReadResponse)13 Test (org.junit.jupiter.api.Test)12 PooledPlcDriverManager (org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager)11 BigInteger (java.math.BigInteger)8 ExecutionException (java.util.concurrent.ExecutionException)8 TimeUnit (java.util.concurrent.TimeUnit)5 Condition (org.assertj.core.api.Condition)5 BigDecimal (java.math.BigDecimal)4 LocalDate (java.time.LocalDate)4 Map (java.util.Map)4 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)4 PlcSubscriptionRequest (org.apache.plc4x.java.api.messages.PlcSubscriptionRequest)4 PlcSubscriptionResponse (org.apache.plc4x.java.api.messages.PlcSubscriptionResponse)4 PlcSubscriptionHandle (org.apache.plc4x.java.api.model.PlcSubscriptionHandle)4 Disabled (org.junit.jupiter.api.Disabled)4 LocalDateTime (java.time.LocalDateTime)3