Skip to content

Commit e9771b3

Browse files
authored
Switche PLC4X version to 0.13.0 (#1297)
* Switch PLC4X version to 0.13.1 * minor API changes * Switch to kebap-case for ADS configuration * Adjust logging to deal with the now fixed bugs * Fix broken ADS adapter breaking all other adapters * Added LDATE_AND_TIME * Update to PLC4X 0.13.1
1 parent b7376ce commit e9771b3

File tree

6 files changed

+29
-10
lines changed

6 files changed

+29
-10
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
apache-commons-compress = "1.28.0"
33
apache-commons-io = "2.20.0"
44
apache-commons-lang = "3.19.0"
5-
apache-plc4x = "0.11.0"
5+
apache-plc4x = "0.13.1"
66
assertj = "3.27.6"
77
awaitility = "4.3.0"
88
bouncycastle = "1.80"

modules/hivemq-edge-module-plc4x/src/main/java/com/hivemq/edge/adapters/plc4x/config/Plc4xDataType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public enum DATA_TYPE {
5757
LTIME_OF_DAY((short) 0x56, LocalTime.class),
5858
DATE_AND_TIME((short) 0x57, LocalDateTime.class),
5959
LDATE_AND_TIME((short) 0x58, LocalDateTime.class),
60+
DATE_AND_LTIME((short) 0x1F, LocalDateTime.class),
6061
RAW_BYTE_ARRAY((short) 0x71, Byte.class);
6162

6263

modules/hivemq-edge-module-plc4x/src/main/java/com/hivemq/edge/adapters/plc4x/impl/AbstractPlc4xAdapter.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ public void start(
165165
tempConnection.startConnection(input.moduleServices().eventService(), adapterId, getProtocolAdapterInformation().getProtocolId());
166166
protocolAdapterState.setConnectionStatus(CONNECTED);
167167
} catch (final Plc4xException e) {
168+
try {
169+
tempConnection.disconnect();
170+
} catch (final Exception ex) {
171+
log.debug("Tried disconnecting after connection error and caught exception", ex);
172+
}
173+
this.connection = null;
168174
log.error("Plc4x connection failed to start", e);
169175
protocolAdapterState.setConnectionStatus(ERROR);
170176
}
@@ -276,6 +282,12 @@ public void stop(final @NotNull ProtocolAdapterStopInput input, final @NotNull P
276282
protocolAdapterState.setConnectionStatus(ProtocolAdapterState.ConnectionStatus.CONNECTED);
277283
}
278284
return processPlcFieldData(Plc4xDataUtils.readDataFromReadResponse(event));
285+
} else {
286+
tags.stream()
287+
.filter(tag -> event.getResponseCode(tag.getName()) != PlcResponseCode.OK)
288+
.forEach(tag -> log.error("Unable to read tag {}. Error Code: {}",
289+
tag.getName(),
290+
event.getResponseCode(tag.getName())));;
279291
}
280292
}
281293
return new Plc4xDataSample(adapterFactories.dataPointFactory());

modules/hivemq-edge-module-plc4x/src/main/java/com/hivemq/edge/adapters/plc4x/impl/Plc4xConnection.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ void startConnection(final @NotNull EventService eventService, final @NotNull St
9191
try {
9292
plcConnection = CompletableFuture.supplyAsync(() -> {
9393
try {
94-
//This is not working in all cases. An exception is thrown if PLC4X actually catches
95-
//the connection probllem. In many cases this call will simply get stuck. Afterwards
94+
// This is not working in all cases. An exception is thrown if PLC4X actually catches
95+
// the connection problem. In many cases this call will simply get stuck. Afterwards
9696
// a new connection CANNOT be opened.
9797
return Optional.of(plcDriverManager.getConnectionManager()
9898
.getConnection(connectionString));
@@ -141,7 +141,8 @@ protected void lazyConnectionCheck() {
141141
public void disconnect() throws Exception {
142142
synchronized (lock) {
143143
try {
144-
if (plcConnection != null && plcConnection.isConnected()) {
144+
final var plcConnection = this.plcConnection;
145+
if (plcConnection != null) {
145146
plcConnection.close();
146147
}
147148
} finally {
@@ -156,7 +157,7 @@ public boolean isConnected() {
156157

157158
public @NotNull CompletableFuture<? extends PlcReadResponse> read(final @NotNull List<Plc4xTag> tags) {
158159
lazyConnectionCheck();
159-
if (!plcConnection.getMetadata().canRead()) {
160+
if (!plcConnection.getMetadata().isReadSupported()) {
160161
return CompletableFuture.failedFuture(new Plc4xException("connection type read-blocking"));
161162
}
162163
if (log.isTraceEnabled()) {
@@ -176,7 +177,7 @@ public boolean isConnected() {
176177
final @NotNull Plc4xTag tag,
177178
final @NotNull Consumer<PlcSubscriptionEvent> consumer) {
178179
lazyConnectionCheck();
179-
if (!plcConnection.getMetadata().canSubscribe()) {
180+
if (!plcConnection.getMetadata().isSubscribeSupported()) {
180181
return CompletableFuture.failedFuture(new Plc4xException("connection type cannot subscribe"));
181182
}
182183
if (log.isTraceEnabled()) {

modules/hivemq-edge-module-plc4x/src/main/java/com/hivemq/edge/adapters/plc4x/impl/Plc4xDataUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.apache.plc4x.java.api.types.PlcValueType;
2121
import org.apache.plc4x.java.api.value.PlcValue;
2222
import org.jetbrains.annotations.NotNull;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
2325

2426
import java.io.ByteArrayOutputStream;
2527
import java.io.DataOutputStream;
@@ -45,6 +47,7 @@
4547
* Some data utilies to manage the interaction with the PLC API
4648
*/
4749
public class Plc4xDataUtils {
50+
private static final Logger log = LoggerFactory.getLogger(Plc4xDataUtils.class);
4851
private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
4952
private static final String AMP = "&";
5053
private static final String EQUALS = "=";
@@ -258,6 +261,7 @@ public static Object convertObject(final PlcValue value) {
258261

259262
case DATE_AND_TIME: //64bit signed, milliseconds since 1990-1-1 (1990-01-01-0:0:0 - 2089-12-31-23:59:59.999)
260263
case LDATE_AND_TIME: //64bit signed, nanoseconds since 1970-1-1 (1970-01-01-0:0:0.000000000 - 2262-04-11-23:47:16.854775807)
264+
case DATE_AND_LTIME: //64bit signed, nanoseconds since 1970-1-1 (1970-01-01-0:0:0.000000000 - 2262-04-11-23:47:16.854775807)
261265
return DATE_TIME_FORMATTER.format(value.getDateTime());
262266

263267
case RAW_BYTE_ARRAY:
@@ -267,6 +271,7 @@ public static Object convertObject(final PlcValue value) {
267271
case List: //ArrayList
268272
case NULL:
269273
default:
274+
log.error("Unable to convert PLC4X value of type {} and value {}", type, value.getDateTime());
270275
return null;
271276
}
272277
}

modules/hivemq-edge-module-plc4x/src/main/java/com/hivemq/edge/adapters/plc4x/types/ads/ADSProtocolAdapter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
*/
3232
public class ADSProtocolAdapter extends AbstractPlc4xAdapter<ADSSpecificAdapterConfig, Plc4xToMqttMapping> {
3333

34-
private static final @NotNull String SOURCE_AMS_NET_ID = "sourceAmsNetId";
35-
private static final @NotNull String SOURCE_AMS_PORT = "sourceAmsPort";
36-
private static final @NotNull String TARGET_AMS_PORT = "targetAmsPort";
37-
private static final @NotNull String TARGET_AMS_NET_ID = "targetAmsNetId";
34+
private static final @NotNull String SOURCE_AMS_NET_ID = "source-ams-net-id";
35+
private static final @NotNull String SOURCE_AMS_PORT = "source-ams-port";
36+
private static final @NotNull String TARGET_AMS_PORT = "target-ams-port";
37+
private static final @NotNull String TARGET_AMS_NET_ID = "target-ams-net-id";
3838

3939
public ADSProtocolAdapter(
4040
final @NotNull ProtocolAdapterInformation adapterInformation,

0 commit comments

Comments
 (0)