Skip to content

Commit

Permalink
refactor(plc4j): use only when it applies instead of check+unwrap
Browse files Browse the repository at this point in the history
only combines those 2 and makes the code a bit more readable
  • Loading branch information
sruehl committed Feb 5, 2024
1 parent b0aadd8 commit 8a349ae
Show file tree
Hide file tree
Showing 13 changed files with 3,804 additions and 332 deletions.
3,404 changes: 3,404 additions & 0 deletions plc4go/assets/testing/protocols/modbus/tcp/Modbus-all-datatypes.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@
package org.apache.plc4x.java.abeth.protocol;

import org.apache.plc4x.java.abeth.configuration.AbEthConfiguration;
import org.apache.plc4x.java.abeth.tag.AbEthTag;
import org.apache.plc4x.java.abeth.readwrite.*;
import org.apache.plc4x.java.abeth.tag.AbEthTag;
import org.apache.plc4x.java.api.messages.PlcReadRequest;
import org.apache.plc4x.java.api.messages.PlcReadResponse;
import org.apache.plc4x.java.api.messages.PlcResponse;
import org.apache.plc4x.java.api.model.PlcTag;
import org.apache.plc4x.java.api.types.PlcResponseCode;
import org.apache.plc4x.java.api.value.*;
import org.apache.plc4x.java.api.value.PlcValue;
import org.apache.plc4x.java.spi.ConversationContext;
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.utils.ResponseItem;
import org.apache.plc4x.java.spi.transaction.RequestTransactionManager;
import org.apache.plc4x.java.spi.values.PlcValueHandler;
import org.apache.plc4x.java.spi.values.PlcINT;
import org.apache.plc4x.java.spi.values.PlcValueHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -51,8 +51,8 @@ public class AbEthProtocolLogic extends Plc4xProtocolBase<CIPEncapsulationPacket
private static final Logger logger = LoggerFactory.getLogger(AbEthProtocolLogic.class);
public static final Duration REQUEST_TIMEOUT = Duration.ofMillis(10000);

private static final List<Short> emptySenderContext = Arrays.asList((short) 0x00 ,(short) 0x00 ,(short) 0x00,
(short) 0x00,(short) 0x00,(short) 0x00, (short) 0x00,(short) 0x00);
private static final List<Short> emptySenderContext = Arrays.asList((short) 0x00, (short) 0x00, (short) 0x00,
(short) 0x00, (short) 0x00, (short) 0x00, (short) 0x00, (short) 0x00);

private AbEthConfiguration configuration;

Expand All @@ -74,8 +74,7 @@ public void onConnect(ConversationContext<CIPEncapsulationPacket> context) {
new CIPEncapsulationConnectionRequest(0L, 0L, emptySenderContext, 0L);
context.sendRequest(connectionRequest)
.expectResponse(CIPEncapsulationPacket.class, REQUEST_TIMEOUT)
.check(p -> p instanceof CIPEncapsulationConnectionResponse)
.unwrap(p -> (CIPEncapsulationConnectionResponse) p)
.only(CIPEncapsulationConnectionResponse.class)
.handle(cipEncapsulationConnectionResponse -> {
sessionHandle = cipEncapsulationConnectionResponse.getSessionHandle();
// Send an event that connection setup is complete.
Expand All @@ -99,7 +98,7 @@ public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {

final int transactionCounter = transactionCounterGenerator.incrementAndGet();
// If we've reached the max value for a 16 bit transaction identifier, reset back to 1
if(transactionCounterGenerator.get() == 0xFFFF) {
if (transactionCounterGenerator.get() == 0xFFFF) {
transactionCounterGenerator.set(1);
}
// origin/sender: constant = 5
Expand All @@ -115,8 +114,7 @@ public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
.expectResponse(CIPEncapsulationPacket.class, REQUEST_TIMEOUT)
.onTimeout(future::completeExceptionally)
.onError((p, e) -> future.completeExceptionally(e))
.check(p -> p instanceof CIPEncapsulationReadResponse)
.unwrap(p -> (CIPEncapsulationReadResponse) p)
.only(CIPEncapsulationReadResponse.class)
.check(p -> p.getResponse().getTransactionCounter() == transactionCounter)
.handle(p -> {
PlcResponse response = decodeReadResponse(p, readRequest);
Expand Down Expand Up @@ -153,56 +151,55 @@ private PlcResponse decodeReadResponse(
try {
switch (tag.getFileType()) {
case INTEGER: // output as single bytes
if(plcReadResponse.getResponse() instanceof DF1CommandResponseMessageProtectedTypedLogicalRead) {
if (plcReadResponse.getResponse() instanceof DF1CommandResponseMessageProtectedTypedLogicalRead) {
DF1CommandResponseMessageProtectedTypedLogicalRead df1PTLR = (DF1CommandResponseMessageProtectedTypedLogicalRead) plcReadResponse.getResponse();
List<Short> data = df1PTLR.getData();
if(data.size() == 1) {
if (data.size() == 1) {
plcValue = new PlcINT(data.get(0));
} else {
plcValue = PlcValueHandler.of(data);
}
}
break;
case WORD:
if(plcReadResponse.getResponse() instanceof DF1CommandResponseMessageProtectedTypedLogicalRead) {
if (plcReadResponse.getResponse() instanceof DF1CommandResponseMessageProtectedTypedLogicalRead) {
DF1CommandResponseMessageProtectedTypedLogicalRead df1PTLR = (DF1CommandResponseMessageProtectedTypedLogicalRead) plcReadResponse.getResponse();
List<Short> data = df1PTLR.getData();
if (((data.get(1)>> 7) & 1) == 0) {
if (((data.get(1) >> 7) & 1) == 0) {
plcValue = PlcValueHandler.of((data.get(1) << 8) + data.get(0)); // positive number
} else {
plcValue = PlcValueHandler.of((((~data.get(1) & 0b01111111) << 8) + (-data.get(0) & 0b11111111)) * -1); // negative number
plcValue = PlcValueHandler.of((((~data.get(1) & 0b01111111) << 8) + (-data.get(0) & 0b11111111)) * -1); // negative number
}
}
break;
case DWORD:
if(plcReadResponse.getResponse() instanceof DF1CommandResponseMessageProtectedTypedLogicalRead) {
if (plcReadResponse.getResponse() instanceof DF1CommandResponseMessageProtectedTypedLogicalRead) {
DF1CommandResponseMessageProtectedTypedLogicalRead df1PTLR = (DF1CommandResponseMessageProtectedTypedLogicalRead) plcReadResponse.getResponse();
List<Short> data = df1PTLR.getData();
if (((data.get(3)>> 7) & 1) == 0) {
if (((data.get(3) >> 7) & 1) == 0) {
plcValue = PlcValueHandler.of((data.get(3) << 24) + (data.get(2) << 16) + (data.get(1) << 8) + data.get(0)); // positive number
} else {
plcValue = PlcValueHandler.of((((~data.get(3) & 0b01111111) << 24) + ((-data.get(2) & 0b11111111) << 16)+ ((-data.get(1) & 0b11111111) << 8) + (-data.get(0) & 0b11111111)) * -1); // negative number
plcValue = PlcValueHandler.of((((~data.get(3) & 0b01111111) << 24) + ((-data.get(2) & 0b11111111) << 16) + ((-data.get(1) & 0b11111111) << 8) + (-data.get(0) & 0b11111111)) * -1); // negative number
}
}
break;
case SINGLEBIT:
if(plcReadResponse.getResponse() instanceof DF1CommandResponseMessageProtectedTypedLogicalRead) {
if (plcReadResponse.getResponse() instanceof DF1CommandResponseMessageProtectedTypedLogicalRead) {
DF1CommandResponseMessageProtectedTypedLogicalRead df1PTLR = (DF1CommandResponseMessageProtectedTypedLogicalRead) plcReadResponse.getResponse();
List<Short> data = df1PTLR.getData();
if (tag.getBitNumber() < 8) {
plcValue = PlcValueHandler.of((data.get(0) & (1 << tag.getBitNumber())) != 0); // read from first byte
plcValue = PlcValueHandler.of((data.get(0) & (1 << tag.getBitNumber())) != 0); // read from first byte
} else {
plcValue = PlcValueHandler.of((data.get(1) & (1 << (tag.getBitNumber() - 8) )) != 0); // read from second byte
plcValue = PlcValueHandler.of((data.get(1) & (1 << (tag.getBitNumber() - 8))) != 0); // read from second byte
}
}
break;
default:
logger.warn("Problem during decoding of tag {}: Decoding of file type not implemented; " +
"TagInformation: {}", tagName, tag);
}
}
catch (Exception e) {
logger.warn("Some other error occurred casting tag {}, TagInformation: {}",tagName, tag,e);
} catch (Exception e) {
logger.warn("Some other error occurred casting tag {}, TagInformation: {}", tagName, tag, e);
}
}
ResponseItem<PlcValue> result = new ResponseItem<>(responseCode, plcValue);
Expand All @@ -214,7 +211,7 @@ private PlcResponse decodeReadResponse(
}

private PlcResponseCode decodeResponseCode(short status) {
if(status == 0) {
if (status == 0) {
return PlcResponseCode.OK;
}
return PlcResponseCode.NOT_FOUND;
Expand Down
Loading

0 comments on commit 8a349ae

Please sign in to comment.