You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2022/02/04 18:50:14 UTC
[plc4x] branch develop updated: chore(plc4j/plc4x): Finished implementing the read- and write request logic.
This is an automated email from the ASF dual-hosted git repository.
cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/develop by this push:
new 3dfdc12 chore(plc4j/plc4x): Finished implementing the read- and write request logic.
3dfdc12 is described below
commit 3dfdc12b4c3eebc3c26981838363128c69d6e32e
Author: cdutz <ch...@c-ware.de>
AuthorDate: Fri Feb 4 19:49:10 2022 +0100
chore(plc4j/plc4x): Finished implementing the read- and write request logic.
---
.../java/plc4x/protocol/Plc4xProtocolLogic.java | 67 +++++++++++++++-------
1 file changed, 47 insertions(+), 20 deletions(-)
diff --git a/plc4j/drivers/plc4x/src/main/java/org/apache/plc4x/java/plc4x/protocol/Plc4xProtocolLogic.java b/plc4j/drivers/plc4x/src/main/java/org/apache/plc4x/java/plc4x/protocol/Plc4xProtocolLogic.java
index bbe56d0..521fbe6 100644
--- a/plc4j/drivers/plc4x/src/main/java/org/apache/plc4x/java/plc4x/protocol/Plc4xProtocolLogic.java
+++ b/plc4j/drivers/plc4x/src/main/java/org/apache/plc4x/java/plc4x/protocol/Plc4xProtocolLogic.java
@@ -19,6 +19,7 @@
package org.apache.plc4x.java.plc4x.protocol;
import org.apache.plc4x.java.api.messages.*;
+import org.apache.plc4x.java.api.types.PlcResponseCode;
import org.apache.plc4x.java.api.value.PlcValue;
import org.apache.plc4x.java.plc4x.config.Plc4xConfiguration;
import org.apache.plc4x.java.plc4x.readwrite.*;
@@ -27,13 +28,16 @@ import org.apache.plc4x.java.spi.Plc4xProtocolBase;
import org.apache.plc4x.java.spi.configuration.HasConfiguration;
import org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse;
import org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse;
+import org.apache.plc4x.java.spi.messages.utils.ResponseItem;
import org.apache.plc4x.java.spi.transaction.RequestTransactionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Duration;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
@@ -57,9 +61,9 @@ public class Plc4xProtocolLogic extends Plc4xProtocolBase<Plc4xMessage> implemen
@Override
public void onConnect(ConversationContext<Plc4xMessage> context) {
- final int txId = txIdGenerator.getAndIncrement();
+ final int requestId = txIdGenerator.getAndIncrement();
- Plc4xConnectRequest connectRequest = new Plc4xConnectRequest(txId, remoteConnectionString);
+ Plc4xConnectRequest connectRequest = new Plc4xConnectRequest(requestId, remoteConnectionString);
context.sendRequest(connectRequest)
.onTimeout(e -> {
@@ -67,7 +71,7 @@ public class Plc4xProtocolLogic extends Plc4xProtocolBase<Plc4xMessage> implemen
context.getChannel().close();
})
.expectResponse(Plc4xMessage.class, requestTimeout)
- .check(p -> p.getRequestId() == txId)
+ .check(p -> p.getRequestId() == requestId)
.unwrap(plc4xMessage -> (Plc4xConnectResponse) plc4xMessage)
.handle(connectResponse -> {
// Save the connection id.
@@ -82,28 +86,40 @@ public class Plc4xProtocolLogic extends Plc4xProtocolBase<Plc4xMessage> implemen
}
@Override
- public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
+ public CompletableFuture<PlcReadResponse> read(PlcReadRequest apiReadRequest) {
CompletableFuture<PlcReadResponse> future = new CompletableFuture<>();
- List<Plc4xFieldRequest> fields = new ArrayList<>(readRequest.getNumberOfFields());
- for (String fieldName : readRequest.getFieldNames()) {
- final org.apache.plc4x.java.plc4x.field.Plc4xField field = (org.apache.plc4x.java.plc4x.field.Plc4xField) readRequest.getField(fieldName);
- Plc4xFieldRequest fieldRequest = new Plc4xFieldRequest(new Plc4xField(fieldName, field.getAddress()));
- fields.add(fieldRequest);
+ // Prepare the request.
+ List<Plc4xFieldRequest> plc4xFields = new ArrayList<>(apiReadRequest.getNumberOfFields());
+ for (String fieldName : apiReadRequest.getFieldNames()) {
+ final org.apache.plc4x.java.plc4x.field.Plc4xField plc4xField = (org.apache.plc4x.java.plc4x.field.Plc4xField) apiReadRequest.getField(fieldName);
+ Plc4xFieldRequest plc4xFieldRequest = new Plc4xFieldRequest(new Plc4xField(fieldName, plc4xField.getAddress()));
+ plc4xFields.add(plc4xFieldRequest);
}
- final int txId = txIdGenerator.getAndIncrement();
- Plc4xReadRequest read = new Plc4xReadRequest(txId, connectionId, fields);
+ final int requestId = txIdGenerator.getAndIncrement();
+ Plc4xReadRequest plc4xReadRequest = new Plc4xReadRequest(requestId, connectionId, plc4xFields);
+ // Send the request and await a response.
RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
- context.sendRequest(read)
+ context.sendRequest(plc4xReadRequest)
.expectResponse(Plc4xMessage.class, requestTimeout)
.onTimeout(future::completeExceptionally)
- .check(plc4xMessage -> plc4xMessage.getRequestId() == txId)
+ .check(plc4xMessage -> plc4xMessage.getRequestId() == requestId)
.unwrap(plc4xMessage -> (Plc4xReadResponse) plc4xMessage)
.check(plc4xReadResponse -> plc4xReadResponse.getConnectionId() == connectionId)
.handle(plc4xReadResponse -> {
- // TODO: Do something with this response.
- future.complete(new DefaultPlcReadResponse(readRequest, null));//Map.of("test", null)
+ Map<String, ResponseItem<PlcValue>> apiResponses = new HashMap<>();
+ // Create the API response from the incoming message.
+ for (Plc4xFieldValueResponse plc4xField : plc4xReadResponse.getFields()) {
+ final Plc4xResponseCode plc4xResponseCode = plc4xField.getResponseCode();
+ final PlcResponseCode apiResponseCode = PlcResponseCode.valueOf(plc4xResponseCode.name());
+ apiResponses.put(plc4xField.getField().getName(),
+ new ResponseItem<>(apiResponseCode, plc4xField.getValue()));
+ }
+
+ // Send it back to the calling process.
+ future.complete(new DefaultPlcReadResponse(apiReadRequest, apiResponses));
+
// Finish the request-transaction.
transaction.endRequest();
});
@@ -114,6 +130,7 @@ public class Plc4xProtocolLogic extends Plc4xProtocolBase<Plc4xMessage> implemen
public CompletableFuture<PlcWriteResponse> write(PlcWriteRequest writeRequest) {
CompletableFuture<PlcWriteResponse> future = new CompletableFuture<>();
+ // Prepare the request.
List<Plc4xFieldValueRequest> fields = new ArrayList<>(writeRequest.getNumberOfFields());
for (String fieldName : writeRequest.getFieldNames()) {
final org.apache.plc4x.java.plc4x.field.Plc4xField field = (org.apache.plc4x.java.plc4x.field.Plc4xField) writeRequest.getField(fieldName);
@@ -123,19 +140,29 @@ public class Plc4xProtocolLogic extends Plc4xProtocolBase<Plc4xMessage> implemen
new Plc4xField(fieldName, field.getAddress()), plc4xValueType, plcValue);
fields.add(fieldRequest);
}
- final int txId = txIdGenerator.getAndIncrement();
- Plc4xWriteRequest write = new Plc4xWriteRequest(txId, connectionId, fields);
+ final int requestId = txIdGenerator.getAndIncrement();
+ Plc4xWriteRequest write = new Plc4xWriteRequest(requestId, connectionId, fields);
+ // Send the request and await a response.
RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
context.sendRequest(write)
.expectResponse(Plc4xMessage.class, requestTimeout)
.onTimeout(future::completeExceptionally)
- .check(p -> p.getRequestId() == txId)
+ .check(p -> p.getRequestId() == requestId)
.unwrap(plc4xMessage -> (Plc4xWriteResponse) plc4xMessage)
.check(plc4xReadResponse -> plc4xReadResponse.getConnectionId() == connectionId)
.handle(plc4xWriteResponse -> {
- // TODO: Do something with this response.
- future.complete(new DefaultPlcWriteResponse(writeRequest, null)); // Map.of("test", PlcResponseCode.OK)
+ Map<String, PlcResponseCode> apiResponses = new HashMap<>();
+ // Create the API response from the incoming message.
+ for (Plc4xFieldResponse plc4xField : plc4xWriteResponse.getFields()) {
+ final Plc4xResponseCode plc4xResponseCode = plc4xField.getResponseCode();
+ final PlcResponseCode apiResponseCode = PlcResponseCode.valueOf(plc4xResponseCode.name());
+ apiResponses.put(plc4xField.getField().getName(), apiResponseCode);
+ }
+
+ // Send it back to the calling process.
+ future.complete(new DefaultPlcWriteResponse(writeRequest, apiResponses));
+
// Finish the request-transaction.
transaction.endRequest();
});