Skip to content

Commit

Permalink
fix(plc4j/ads): Refactored the ADS driver to support reading of compl…
Browse files Browse the repository at this point in the history
…ex types.
  • Loading branch information
chrisdutz committed Aug 31, 2022
1 parent 386189f commit 9f3cc6d
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
*/
package org.apache.plc4x.java.ads.field;

import org.apache.plc4x.java.ads.readwrite.AdsDataType;
import org.apache.plc4x.java.api.model.PlcField;
import org.apache.plc4x.java.spi.utils.Serializable;

public interface AdsField extends PlcField, Serializable {

AdsDataType getAdsDataType();

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public PlcField createField(String fieldQuery) throws PlcInvalidFieldException {
return DirectAdsStringField.of(fieldQuery);
} else if (DirectAdsField.matches(fieldQuery)) {
return DirectAdsField.of(fieldQuery);
} else if (SymbolicAdsStringField.matches(fieldQuery)) {
return SymbolicAdsStringField.of(fieldQuery);
} else if (SymbolicAdsField.matches(fieldQuery)) {
return SymbolicAdsField.of(fieldQuery);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
*/
package org.apache.plc4x.java.ads.field;

import org.apache.plc4x.java.ads.readwrite.AdsDataType;
import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException;
import org.apache.plc4x.java.spi.generation.ParseException;
import org.apache.plc4x.java.spi.generation.SerializationException;
import org.apache.plc4x.java.spi.generation.WriteBuffer;

Expand All @@ -41,24 +39,24 @@ public class DirectAdsField implements AdsField {

private final long indexOffset;

private final AdsDataType adsDataType;
private final String adsDataTypeName;

private final int numberOfElements;

public DirectAdsField(long indexGroup, long indexOffset, AdsDataType adsDataType, Integer numberOfElements) {
public DirectAdsField(long indexGroup, long indexOffset, String adsDataTypeName, Integer numberOfElements) {
//ByteValue.checkUnsignedBounds(indexGroup, 4);
this.indexGroup = indexGroup;
//ByteValue.checkUnsignedBounds(indexOffset, 4);
this.indexOffset = indexOffset;
this.adsDataType = Objects.requireNonNull(adsDataType);
this.adsDataTypeName = Objects.requireNonNull(adsDataTypeName);
this.numberOfElements = numberOfElements != null ? numberOfElements : 1;
if (this.numberOfElements <= 0) {
throw new IllegalArgumentException("numberOfElements must be greater then zero. Was " + this.numberOfElements);
}
}

public static DirectAdsField of(long indexGroup, long indexOffset, AdsDataType adsDataType, Integer numberOfElements) {
return new DirectAdsField(indexGroup, indexOffset, adsDataType, numberOfElements);
public static DirectAdsField of(long indexGroup, long indexOffset, String adsDataTypeName, Integer numberOfElements) {
return new DirectAdsField(indexGroup, indexOffset, adsDataTypeName, numberOfElements);
}

public static DirectAdsField of(String address) {
Expand Down Expand Up @@ -88,12 +86,11 @@ public static DirectAdsField of(String address) {
}

String adsDataTypeString = matcher.group("adsDataType");
AdsDataType adsDataType = AdsDataType.valueOf(adsDataTypeString);

String numberOfElementsString = matcher.group("numberOfElements");
Integer numberOfElements = numberOfElementsString != null ? Integer.valueOf(numberOfElementsString) : null;

return new DirectAdsField(indexGroup, indexOffset, adsDataType, numberOfElements);
return new DirectAdsField(indexGroup, indexOffset, adsDataTypeString, numberOfElements);
}

public static boolean matches(String address) {
Expand All @@ -108,14 +105,8 @@ public long getIndexOffset() {
return indexOffset;
}

@Override
public AdsDataType getAdsDataType() {
return adsDataType;
}

@Override
public String getPlcDataType() {
return adsDataType.toString();
public String getAdsDataTypeName() {
return adsDataTypeName;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.apache.plc4x.java.ads.field;

import org.apache.plc4x.java.ads.readwrite.AdsDataType;
import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException;
import org.apache.plc4x.java.spi.generation.SerializationException;
import org.apache.plc4x.java.spi.generation.WriteBuffer;
Expand All @@ -37,13 +36,13 @@ public class DirectAdsStringField extends DirectAdsField implements AdsStringFie

private final int stringLength;

public DirectAdsStringField(long indexGroup, long indexOffset, AdsDataType adsDataType, int stringLength, Integer numberOfElements) {
super(indexGroup, indexOffset, adsDataType, numberOfElements);
public DirectAdsStringField(long indexGroup, long indexOffset, String adsDataTypeName, int stringLength, Integer numberOfElements) {
super(indexGroup, indexOffset, adsDataTypeName, numberOfElements);
this.stringLength = stringLength;
}

public static DirectAdsStringField of(long indexGroup, long indexOffset, AdsDataType adsDataType, int stringLength, Integer numberOfElements) {
return new DirectAdsStringField(indexGroup, indexOffset, adsDataType, stringLength, numberOfElements);
public static DirectAdsStringField of(long indexGroup, long indexOffset, String adsDataTypeName, int stringLength, Integer numberOfElements) {
return new DirectAdsStringField(indexGroup, indexOffset, adsDataTypeName, stringLength, numberOfElements);
}

public static DirectAdsStringField of(String address) {
Expand Down Expand Up @@ -72,16 +71,15 @@ public static DirectAdsStringField of(String address) {
indexOffset = Long.parseLong(indexOffsetString);
}

String adsDataTypeString = matcher.group("adsDataType");
AdsDataType adsDataType = AdsDataType.valueOf(adsDataTypeString);
String adsDataTypeName = matcher.group("adsDataType");

String stringLengthString = matcher.group("stringLength");
Integer stringLength = stringLengthString != null ? Integer.valueOf(stringLengthString) : null;
int stringLength = stringLengthString != null ? Integer.parseInt(stringLengthString) : 0;

String numberOfElementsString = matcher.group("numberOfElements");
Integer numberOfElements = numberOfElementsString != null ? Integer.valueOf(numberOfElementsString) : null;

return new DirectAdsStringField(indexGroup, indexOffset, adsDataType, stringLength, numberOfElements);
return new DirectAdsStringField(indexGroup, indexOffset, adsDataTypeName, stringLength, numberOfElements);
}

public static boolean matches(String address) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.apache.plc4x.java.ads.field;

import org.apache.plc4x.java.ads.readwrite.AdsDataType;
import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException;
import org.apache.plc4x.java.spi.generation.SerializationException;
import org.apache.plc4x.java.spi.generation.WriteBuffer;
Expand All @@ -33,22 +32,12 @@
*/
public class SymbolicAdsField implements AdsField {

private static final Pattern SYMBOLIC_ADDRESS_PATTERN = Pattern.compile("^(?<symbolicAddress>.+):(?<adsDataType>\\w+)(\\[(?<numberOfElements>\\d+)])?");
private static final Pattern SYMBOLIC_ADDRESS_PATTERN = Pattern.compile("^(?<symbolicAddress>.+)");

private final String symbolicAddress;

private final AdsDataType adsDataType;

private final int numberOfElements;

public SymbolicAdsField(String symbolicAddress, AdsDataType adsDataType, Integer numberOfElements) {
public SymbolicAdsField(String symbolicAddress) {
this.symbolicAddress = Objects.requireNonNull(symbolicAddress);
this.adsDataType = Objects.requireNonNull(adsDataType);
this.numberOfElements = numberOfElements != null ? numberOfElements : 1;
if (this.numberOfElements <= 0) {
throw new IllegalArgumentException("numberOfElements must be greater then zero. Was " + this.numberOfElements);
}

}

public static SymbolicAdsField of(String address) {
Expand All @@ -58,13 +47,7 @@ public static SymbolicAdsField of(String address) {
}
String symbolicAddress = matcher.group("symbolicAddress");

String adsDataTypeString = matcher.group("adsDataType");
AdsDataType adsDataType = AdsDataType.valueOf(adsDataTypeString);

String numberOfElementsString = matcher.group("numberOfElements");
Integer numberOfElements = numberOfElementsString != null ? Integer.valueOf(numberOfElementsString) : null;

return new SymbolicAdsField(symbolicAddress, adsDataType, numberOfElements);
return new SymbolicAdsField(symbolicAddress);
}

public static boolean matches(String address) {
Expand All @@ -75,21 +58,6 @@ public String getSymbolicAddress() {
return symbolicAddress;
}

@Override
public AdsDataType getAdsDataType() {
return adsDataType;
}

@Override
public String getPlcDataType() {
return adsDataType.toString();
}

@Override
public int getNumberOfElements() {
return numberOfElements;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.plc4x.java.ads.model;

import org.apache.plc4x.java.ads.readwrite.AdsDataType;
import org.apache.plc4x.java.ads.readwrite.AdsDataTypeTableEntry;
import org.apache.plc4x.java.spi.messages.PlcSubscriber;
import org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle;

Expand All @@ -28,11 +28,11 @@ public class AdsSubscriptionHandle extends DefaultPlcSubscriptionHandle {

private final String plcFieldName;

private final AdsDataType adsDataType;
private final AdsDataTypeTableEntry adsDataType;

private final Long notificationHandle;

public AdsSubscriptionHandle(PlcSubscriber plcSubscriber, String plcFieldName, AdsDataType adsDataType, Long notificationHandle) {
public AdsSubscriptionHandle(PlcSubscriber plcSubscriber, String plcFieldName, AdsDataTypeTableEntry adsDataType, Long notificationHandle) {
super(plcSubscriber);
this.plcFieldName = plcFieldName;
this.adsDataType = adsDataType;
Expand All @@ -43,7 +43,7 @@ public String getPlcFieldName() {
return plcFieldName;
}

public AdsDataType getAdsDataType() {
public AdsDataTypeTableEntry getAdsDataType() {
return adsDataType;
}

Expand Down
Loading

0 comments on commit 9f3cc6d

Please sign in to comment.