Skip to content

Commit 4fc86b0

Browse files
authored
Enable keep-alive for ADS and S7 (#1315)
1 parent e9771b3 commit 4fc86b0

File tree

6 files changed

+40
-3
lines changed

6 files changed

+40
-3
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class ADSProtocolAdapter extends AbstractPlc4xAdapter<ADSSpecificAdapterC
3535
private static final @NotNull String SOURCE_AMS_PORT = "source-ams-port";
3636
private static final @NotNull String TARGET_AMS_PORT = "target-ams-port";
3737
private static final @NotNull String TARGET_AMS_NET_ID = "target-ams-net-id";
38+
private static final @NotNull String TCP_KEEP_ALIVE = "tcp.keep-alive";
3839

3940
public ADSProtocolAdapter(
4041
final @NotNull ProtocolAdapterInformation adapterInformation,
@@ -65,6 +66,10 @@ public ADSProtocolAdapter(
6566

6667
map.put(TARGET_AMS_PORT, nullSafe(config.getTargetAmsPort()));
6768
map.put(TARGET_AMS_NET_ID, nullSafe(config.getTargetAmsNetId()));
69+
70+
if (config.isKeepAlive()) {
71+
map.put(TCP_KEEP_ALIVE, "true");
72+
}
6873
return map;
6974
}
7075
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ public class ADSSpecificAdapterConfig extends Plc4XSpecificAdapterConfig<ADSToMq
7171
description = "The AMS Net ID of the device to connect to")
7272
private final @NotNull String targetAmsNetId;
7373

74+
@JsonProperty("keepAlive")
75+
@ModuleConfigField(title = "Keep-Alive",
76+
description = "Enable TCP keep-alive to maintain the connection during periods of inactivity.",
77+
defaultValue = "false")
78+
private final boolean keepAlive;
79+
7480
@JsonProperty(value = "adsToMqtt", required = true)
7581
@ModuleConfigField(title = "ADS To MQTT Config",
7682
description = "The configuration for a data stream from ADS to MQTT",
@@ -85,13 +91,15 @@ public ADSSpecificAdapterConfig(
8591
@JsonProperty(value = "sourceAmsPort", required = true) final int sourceAmsPort,
8692
@JsonProperty(value = "targetAmsNetId", required = true) final @NotNull String targetAmsNetId,
8793
@JsonProperty(value = "sourceAmsNetId", required = true) final @NotNull String sourceAmsNetId,
94+
@JsonProperty(value = "keepAlive") final @Nullable Boolean keepAlive,
8895
@JsonProperty(value = "adsToMqtt") final @Nullable ADSToMqttConfig adsToMqttConfig) {
89-
super( port, host);
96+
super(port, host);
9097
this.port = port;
9198
this.targetAmsPort = targetAmsPort;
9299
this.sourceAmsPort = sourceAmsPort;
93100
this.sourceAmsNetId = sourceAmsNetId;
94101
this.targetAmsNetId = targetAmsNetId;
102+
this.keepAlive = Objects.requireNonNullElse(keepAlive, false);
95103
if (adsToMqttConfig == null) {
96104
this.adsToMqttConfig = new ADSToMqttConfig(null, null, null);
97105
} else {
@@ -121,6 +129,10 @@ public int getTargetAmsPort() {
121129
return targetAmsNetId;
122130
}
123131

132+
public boolean isKeepAlive() {
133+
return keepAlive;
134+
}
135+
124136
@Override
125137
public @Nullable ADSToMqttConfig getPlc4xToMqttConfig() {
126138
return adsToMqttConfig;
@@ -133,6 +145,7 @@ public boolean equals(final Object o) {
133145
return getPort() == that.getPort() &&
134146
getTargetAmsPort() == that.getTargetAmsPort() &&
135147
getSourceAmsPort() == that.getSourceAmsPort() &&
148+
isKeepAlive() == that.isKeepAlive() &&
136149
Objects.equals(getSourceAmsNetId(), that.getSourceAmsNetId()) &&
137150
Objects.equals(getTargetAmsNetId(), that.getTargetAmsNetId()) &&
138151
Objects.equals(adsToMqttConfig, that.adsToMqttConfig);
@@ -145,6 +158,7 @@ public int hashCode() {
145158
getSourceAmsPort(),
146159
getSourceAmsNetId(),
147160
getTargetAmsNetId(),
161+
keepAlive,
148162
adsToMqttConfig);
149163
}
150164
}

modules/hivemq-edge-module-plc4x/src/main/java/com/hivemq/edge/adapters/plc4x/types/siemens/S7ProtocolAdapter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ public S7ProtocolAdapter(
115115
map.put(REMOTE_SLOT_2, nullSafe(config.getRemoteSlot2()));
116116
map.put(REMOTE_TSAP, nullSafe(config.getRemoteTsap()));
117117

118-
//ping if polling interval greater than default read timeout - 1s grace
119-
if (config.getPlc4xToMqttConfig() != null && config.getPlc4xToMqttConfig().getPollingIntervalMillis() >= 7000) {
118+
// Enable ping if explicitly configured OR if polling interval >= 7s (existing behavior)
119+
boolean autoEnablePing = config.getPlc4xToMqttConfig() != null &&
120+
config.getPlc4xToMqttConfig().getPollingIntervalMillis() >= 7000;
121+
if (config.isKeepAlive() || autoEnablePing) {
120122
map.put(PING, "true");
121123
map.put(PING_TIME, "4");
122124
}

modules/hivemq-edge-module-plc4x/src/main/java/com/hivemq/edge/adapters/plc4x/types/siemens/config/S7SpecificAdapterConfig.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ public enum ControllerType {
8484
defaultValue = "0")
8585
private final int remoteTsap;
8686

87+
@JsonProperty("keepAlive")
88+
@ModuleConfigField(title = "Keep-Alive",
89+
description = "Enable keep-alive ping to prevent TCP connection timeouts during long polling intervals. Recommended when polling interval exceeds 7 seconds.",
90+
defaultValue = "false")
91+
private final boolean keepAlive;
92+
8793
@JsonProperty(value = "s7ToMqtt", required = true)
8894
@ModuleConfigField(title = "S7 To MQTT Config",
8995
description = "The configuration for a data stream from S7 to MQTT",
@@ -100,6 +106,7 @@ public S7SpecificAdapterConfig(
100106
@JsonProperty(value = "remoteSlot") final @Nullable Integer remoteSlot,
101107
@JsonProperty(value = "remoteSlot2") final @Nullable Integer remoteSlot2,
102108
@JsonProperty(value = "remoteTsap") final @Nullable Integer remoteTsap,
109+
@JsonProperty(value = "keepAlive") final @Nullable Boolean keepAlive,
103110
@JsonProperty(value = "s7ToMqtt") final @Nullable S7ToMqttConfig s7ToMqttConfig) {
104111
super(port, host);
105112
this.port = port;
@@ -109,6 +116,7 @@ public S7SpecificAdapterConfig(
109116
this.remoteSlot = Objects.requireNonNullElse(remoteSlot, 0);
110117
this.remoteSlot2 = Objects.requireNonNullElse(remoteSlot2, 0);
111118
this.remoteTsap = Objects.requireNonNullElse(remoteTsap, 0);
119+
this.keepAlive = Objects.requireNonNullElse(keepAlive, false);
112120
if (s7ToMqttConfig == null) {
113121
this.s7ToMqttConfig = new S7ToMqttConfig(null, null, null);
114122
} else {
@@ -142,6 +150,10 @@ public int getRemoteTsap() {
142150
return remoteTsap;
143151
}
144152

153+
public boolean isKeepAlive() {
154+
return keepAlive;
155+
}
156+
145157
public @NotNull ControllerType getControllerType() {
146158
return controllerType;
147159
}
@@ -161,6 +173,7 @@ public boolean equals(final Object o) {
161173
getRemoteSlot() == that.getRemoteSlot() &&
162174
getRemoteSlot2() == that.getRemoteSlot2() &&
163175
getRemoteTsap() == that.getRemoteTsap() &&
176+
isKeepAlive() == that.isKeepAlive() &&
164177
getControllerType() == that.getControllerType() &&
165178
Objects.equals(s7ToMqttConfig, that.s7ToMqttConfig);
166179
}
@@ -174,6 +187,7 @@ public int hashCode() {
174187
getRemoteSlot(),
175188
getRemoteSlot2(),
176189
getRemoteTsap(),
190+
keepAlive,
177191
s7ToMqttConfig);
178192
}
179193
}

modules/hivemq-edge-module-plc4x/src/test/java/com/hivemq/edge/adapters/plc4x/types/ads/config/ADSProtocolAdapterConfigTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ public void unconvertConfigObject_full_valid() {
157157
16,
158158
"1.2.3.4.5.6",
159159
"1.2.3.4.5.7",
160+
false,
160161
new ADSToMqttConfig(
161162
12,
162163
13,

modules/hivemq-edge-module-plc4x/src/test/java/com/hivemq/edge/adapters/plc4x/types/siemens/config/S7ProtocolAdapterConfigTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public void unconvertConfigObject_full_valid() {
146146
3,
147147
4,
148148
5,
149+
false,
149150
new S7ToMqttConfig(
150151
12,
151152
13,

0 commit comments

Comments
 (0)