Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class ADSProtocolAdapter extends AbstractPlc4xAdapter<ADSSpecificAdapterC
private static final @NotNull String SOURCE_AMS_PORT = "source-ams-port";
private static final @NotNull String TARGET_AMS_PORT = "target-ams-port";
private static final @NotNull String TARGET_AMS_NET_ID = "target-ams-net-id";
private static final @NotNull String TCP_KEEP_ALIVE = "tcp.keep-alive";

public ADSProtocolAdapter(
final @NotNull ProtocolAdapterInformation adapterInformation,
Expand Down Expand Up @@ -65,6 +66,10 @@ public ADSProtocolAdapter(

map.put(TARGET_AMS_PORT, nullSafe(config.getTargetAmsPort()));
map.put(TARGET_AMS_NET_ID, nullSafe(config.getTargetAmsNetId()));

if (config.isKeepAlive()) {
map.put(TCP_KEEP_ALIVE, "true");
}
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ public class ADSSpecificAdapterConfig extends Plc4XSpecificAdapterConfig<ADSToMq
description = "The AMS Net ID of the device to connect to")
private final @NotNull String targetAmsNetId;

@JsonProperty("keepAlive")
@ModuleConfigField(title = "Keep-Alive",
description = "Enable TCP keep-alive to maintain the connection during periods of inactivity.",
defaultValue = "false")
private final boolean keepAlive;

@JsonProperty(value = "adsToMqtt", required = true)
@ModuleConfigField(title = "ADS To MQTT Config",
description = "The configuration for a data stream from ADS to MQTT",
Expand All @@ -85,13 +91,15 @@ public ADSSpecificAdapterConfig(
@JsonProperty(value = "sourceAmsPort", required = true) final int sourceAmsPort,
@JsonProperty(value = "targetAmsNetId", required = true) final @NotNull String targetAmsNetId,
@JsonProperty(value = "sourceAmsNetId", required = true) final @NotNull String sourceAmsNetId,
@JsonProperty(value = "keepAlive") final @Nullable Boolean keepAlive,
@JsonProperty(value = "adsToMqtt") final @Nullable ADSToMqttConfig adsToMqttConfig) {
super( port, host);
super(port, host);
this.port = port;
this.targetAmsPort = targetAmsPort;
this.sourceAmsPort = sourceAmsPort;
this.sourceAmsNetId = sourceAmsNetId;
this.targetAmsNetId = targetAmsNetId;
this.keepAlive = Objects.requireNonNullElse(keepAlive, false);
if (adsToMqttConfig == null) {
this.adsToMqttConfig = new ADSToMqttConfig(null, null, null);
} else {
Expand Down Expand Up @@ -121,6 +129,10 @@ public int getTargetAmsPort() {
return targetAmsNetId;
}

public boolean isKeepAlive() {
return keepAlive;
}

@Override
public @Nullable ADSToMqttConfig getPlc4xToMqttConfig() {
return adsToMqttConfig;
Expand All @@ -133,6 +145,7 @@ public boolean equals(final Object o) {
return getPort() == that.getPort() &&
getTargetAmsPort() == that.getTargetAmsPort() &&
getSourceAmsPort() == that.getSourceAmsPort() &&
isKeepAlive() == that.isKeepAlive() &&
Objects.equals(getSourceAmsNetId(), that.getSourceAmsNetId()) &&
Objects.equals(getTargetAmsNetId(), that.getTargetAmsNetId()) &&
Objects.equals(adsToMqttConfig, that.adsToMqttConfig);
Expand All @@ -145,6 +158,7 @@ public int hashCode() {
getSourceAmsPort(),
getSourceAmsNetId(),
getTargetAmsNetId(),
keepAlive,
adsToMqttConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ public S7ProtocolAdapter(
map.put(REMOTE_SLOT_2, nullSafe(config.getRemoteSlot2()));
map.put(REMOTE_TSAP, nullSafe(config.getRemoteTsap()));

//ping if polling interval greater than default read timeout - 1s grace
if (config.getPlc4xToMqttConfig() != null && config.getPlc4xToMqttConfig().getPollingIntervalMillis() >= 7000) {
// Enable ping if explicitly configured OR if polling interval >= 7s (existing behavior)
boolean autoEnablePing = config.getPlc4xToMqttConfig() != null &&
config.getPlc4xToMqttConfig().getPollingIntervalMillis() >= 7000;
if (config.isKeepAlive() || autoEnablePing) {
map.put(PING, "true");
map.put(PING_TIME, "4");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ public enum ControllerType {
defaultValue = "0")
private final int remoteTsap;

@JsonProperty("keepAlive")
@ModuleConfigField(title = "Keep-Alive",
description = "Enable keep-alive ping to prevent TCP connection timeouts during long polling intervals. Recommended when polling interval exceeds 7 seconds.",
defaultValue = "false")
private final boolean keepAlive;

@JsonProperty(value = "s7ToMqtt", required = true)
@ModuleConfigField(title = "S7 To MQTT Config",
description = "The configuration for a data stream from S7 to MQTT",
Expand All @@ -100,6 +106,7 @@ public S7SpecificAdapterConfig(
@JsonProperty(value = "remoteSlot") final @Nullable Integer remoteSlot,
@JsonProperty(value = "remoteSlot2") final @Nullable Integer remoteSlot2,
@JsonProperty(value = "remoteTsap") final @Nullable Integer remoteTsap,
@JsonProperty(value = "keepAlive") final @Nullable Boolean keepAlive,
@JsonProperty(value = "s7ToMqtt") final @Nullable S7ToMqttConfig s7ToMqttConfig) {
super(port, host);
this.port = port;
Expand All @@ -109,6 +116,7 @@ public S7SpecificAdapterConfig(
this.remoteSlot = Objects.requireNonNullElse(remoteSlot, 0);
this.remoteSlot2 = Objects.requireNonNullElse(remoteSlot2, 0);
this.remoteTsap = Objects.requireNonNullElse(remoteTsap, 0);
this.keepAlive = Objects.requireNonNullElse(keepAlive, false);
if (s7ToMqttConfig == null) {
this.s7ToMqttConfig = new S7ToMqttConfig(null, null, null);
} else {
Expand Down Expand Up @@ -142,6 +150,10 @@ public int getRemoteTsap() {
return remoteTsap;
}

public boolean isKeepAlive() {
return keepAlive;
}

public @NotNull ControllerType getControllerType() {
return controllerType;
}
Expand All @@ -161,6 +173,7 @@ public boolean equals(final Object o) {
getRemoteSlot() == that.getRemoteSlot() &&
getRemoteSlot2() == that.getRemoteSlot2() &&
getRemoteTsap() == that.getRemoteTsap() &&
isKeepAlive() == that.isKeepAlive() &&
getControllerType() == that.getControllerType() &&
Objects.equals(s7ToMqttConfig, that.s7ToMqttConfig);
}
Expand All @@ -174,6 +187,7 @@ public int hashCode() {
getRemoteSlot(),
getRemoteSlot2(),
getRemoteTsap(),
keepAlive,
s7ToMqttConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public void unconvertConfigObject_full_valid() {
16,
"1.2.3.4.5.6",
"1.2.3.4.5.7",
false,
new ADSToMqttConfig(
12,
13,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public void unconvertConfigObject_full_valid() {
3,
4,
5,
false,
new S7ToMqttConfig(
12,
13,
Expand Down
Loading