Skip to content

Commit 23f5155

Browse files
committed
type safety
1 parent e84df14 commit 23f5155

File tree

3 files changed

+85
-56
lines changed

3 files changed

+85
-56
lines changed

modules/hivemq-edge-module-s7/src/main/java/com/hivemq/edge/adapters/s7/S7ProtocolAdapter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ public void poll(
126126
@NotNull final PollingInput<S7ToMqttConfig> pollingInput,
127127
@NotNull final PollingOutput pollingOutput) {
128128
final S7ToMqttConfig s7ToMqtt = pollingInput.getPollingContext();
129-
//Every S7 address starts with a % but the iot-communications lib doesn't like it so we are stripping it.
129+
130+
//Every S7 address starts with a % but the iot-communications lib doesn't like it, so we are stripping it.
130131
final String tagAddress = s7ToMqtt.getTagAddress().replace("%","");
131132
final DataPoint dataPoint;
132133

@@ -140,7 +141,9 @@ public void poll(
140141
if(dataPoints.containsKey(tagAddress)) {
141142
final DataPoint existingDataPoint = dataPoints.get(tagAddress);
142143
if(existingDataPoint != null && existingDataPoint.equals(dataPoint)) {
143-
log.info("Skipping sending for {} because publishChangedDataOnly=true", tagAddress);
144+
if (log.isTraceEnabled()){
145+
log.trace("Skipping sending for {} because publishChangedDataOnly=true", tagAddress);
146+
}
144147
} else {
145148
dataPoints.put(tagAddress, dataPoint);
146149
pollingOutput.addDataPoint(dataPoint);

modules/hivemq-edge-module-s7/src/main/java/com/hivemq/edge/adapters/s7/config/S7AdapterConfig.java

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,38 @@
1818
import com.fasterxml.jackson.annotation.JsonCreator;
1919
import com.fasterxml.jackson.annotation.JsonProperty;
2020
import com.hivemq.adapter.sdk.api.annotations.ModuleConfigField;
21-
import com.hivemq.adapter.sdk.api.config.MessageHandlingOptions;
22-
import com.hivemq.adapter.sdk.api.config.MqttUserProperty;
23-
import com.hivemq.adapter.sdk.api.config.PollingContext;
2421
import com.hivemq.adapter.sdk.api.config.ProtocolAdapterConfig;
2522
import org.jetbrains.annotations.NotNull;
2623
import org.jetbrains.annotations.Nullable;
2724

2825
import java.util.List;
2926
import java.util.Objects;
3027

31-
import static com.hivemq.adapter.sdk.api.config.MessageHandlingOptions.MQTTMessagePerTag;
32-
import static java.util.Objects.requireNonNullElse;
33-
3428
public class S7AdapterConfig implements ProtocolAdapterConfig {
3529

3630
private static final @NotNull String ID_REGEX = "^([a-zA-Z_0-9-_])*$";
3731

3832
private static final int PORT_MIN = 1;
3933
private static final int PORT_MAX = 65535;
4034

35+
public static final String DEFAULT_POLLING_INTERVAL_MS = "1000";
36+
public static final String DEFAULT_MAX_POLLING_ERRORS = "10";
37+
public static final String DEFAULT_PUBLISH_CHANGED_DATA_ONLY = "true";
38+
public static final String DEFAULT_S7_PORT = "102";
39+
public static final String DEFAULT_CONTROLER_TYPE = "S7_300";
40+
41+
public static final String PROPERTY_ID = "id";
42+
public static final String PROPERTY_PORT = "port";
43+
public static final String PROPERTY_HOST = "host";
44+
public static final String PROPERTY_CONTROLLER_TYPE = "controllerType";
45+
public static final String PROPERTY_REMOTE_RACK = "remoteRack";
46+
public static final String PROPERTY_REMOTE_SLOT = "remoteSlot";
47+
public static final String PROPERTY_PDU_LENGTH = "pduLength";
48+
public static final String PROPERTY_POLLING_INTERVAL_MILLIS = "pollingIntervalMillis";
49+
public static final String PROPERTY_MAX_POLLING_ERRORS_BEFORE_REMOVAL = "maxPollingErrorsBeforeRemoval";
50+
public static final String PROPERTY_PUBLISH_CHANGED_DATA_ONLY = "publishChangedDataOnly";
51+
public static final String PROPERTY_S_7_TO_MQTT_MAPPINGS = "s7ToMqttMappings";
52+
4153
public enum ControllerType {
4254
S7_200,
4355
S7_200_SMART,
@@ -48,27 +60,27 @@ public enum ControllerType {
4860
SINUMERIK_828D
4961
}
5062

51-
@JsonProperty("pollingIntervalMillis")
63+
@JsonProperty(PROPERTY_POLLING_INTERVAL_MILLIS)
5264
@ModuleConfigField(title = "Polling Interval [ms]",
5365
description = "Time in millisecond that this endpoint will be polled",
5466
numberMin = 1,
55-
defaultValue = "1000")
67+
defaultValue = DEFAULT_POLLING_INTERVAL_MS)
5668
private final int pollingIntervalMillis;
5769

58-
@JsonProperty("maxPollingErrorsBeforeRemoval")
70+
@JsonProperty(PROPERTY_MAX_POLLING_ERRORS_BEFORE_REMOVAL)
5971
@ModuleConfigField(title = "Max. Polling Errors",
6072
description = "Max. errors polling the endpoint before the polling daemon is stopped (-1 for unlimited retries)",
6173
numberMin = -1,
62-
defaultValue = "10")
74+
defaultValue = DEFAULT_MAX_POLLING_ERRORS)
6375
private final int maxPollingErrorsBeforeRemoval;
6476

65-
@JsonProperty("publishChangedDataOnly")
77+
@JsonProperty(PROPERTY_PUBLISH_CHANGED_DATA_ONLY)
6678
@ModuleConfigField(title = "Only publish data items that have changed since last poll",
67-
defaultValue = "true",
79+
defaultValue = DEFAULT_PUBLISH_CHANGED_DATA_ONLY,
6880
format = ModuleConfigField.FieldType.BOOLEAN)
6981
private final boolean publishChangedDataOnly;
7082

71-
@JsonProperty(value = "id", required = true)
83+
@JsonProperty(value = PROPERTY_ID, required = true)
7284
@ModuleConfigField(title = "Identifier",
7385
description = "Unique identifier for this protocol adapter",
7486
format = ModuleConfigField.FieldType.IDENTIFIER,
@@ -78,69 +90,73 @@ public enum ControllerType {
7890
stringMaxLength = 1024)
7991
private final @NotNull String id;
8092

81-
@JsonProperty(value = "port", required = true)
93+
@JsonProperty(value = PROPERTY_PORT, required = true)
8294
@ModuleConfigField(title = "Port",
8395
description = "The port number on the device to connect to",
8496
required = true,
97+
defaultValue = DEFAULT_S7_PORT,
8598
numberMin = PORT_MIN,
8699
numberMax = PORT_MAX)
87100
private final int port;
88101

89-
@JsonProperty(value = "host", required = true)
102+
@JsonProperty(value = PROPERTY_HOST, required = true)
90103
@ModuleConfigField(title = "Host",
91104
description = "IP Address or hostname of the device you wish to connect to",
92105
required = true,
93106
format = ModuleConfigField.FieldType.HOSTNAME)
94107
private final @NotNull String host;
95108

96-
@JsonProperty(value = "controllerType", required = true)
109+
@JsonProperty(value = PROPERTY_CONTROLLER_TYPE, required = true)
97110
@ModuleConfigField(title = "S7 Controller Type",
98111
description = "The type of the S7 Controller",
99112
required = true,
100-
defaultValue = "S7_300")
113+
defaultValue = DEFAULT_CONTROLER_TYPE)
101114
private final @NotNull S7AdapterConfig.ControllerType controllerType;
102115

103-
@JsonProperty("remoteRack")
116+
@JsonProperty(PROPERTY_REMOTE_RACK)
104117
@ModuleConfigField(title = "Remote Rack",
105118
description = "Rack value for the remote main CPU (PLC).")
106119
private final Integer remoteRack;
107120

108-
@JsonProperty("remoteSlot")
121+
@JsonProperty(PROPERTY_REMOTE_SLOT)
109122
@ModuleConfigField(title = "Remote Slot",
110123
description = "Slot value for the remote main CPU (PLC).")
111124
private final Integer remoteSlot;
112125

113-
@JsonProperty("pduLength")
126+
@JsonProperty(PROPERTY_PDU_LENGTH)
114127
@ModuleConfigField(title = "PDU length",
115128
description = "")
116129
private final Integer pduLength;
117130

118-
@JsonProperty(value = "s7ToMqttMappings", required = true)
131+
@JsonProperty(value = PROPERTY_S_7_TO_MQTT_MAPPINGS, required = true)
119132
@ModuleConfigField(title = "S7 To MQTT Config",
120133
description = "The configuration for a data stream from S7 to MQTT",
121134
required = true)
122135
private final @NotNull List<S7ToMqttConfig> s7ToMqttConfig;
123136

124137
@JsonCreator
125138
public S7AdapterConfig(
126-
@JsonProperty(value = "id", required = true) final @NotNull String id,
127-
@JsonProperty(value = "port", required = true, defaultValue = "102") final int port,
128-
@JsonProperty(value = "host", required = true) final @NotNull String host,
129-
@JsonProperty(value = "controllerType", required = true) final @NotNull ControllerType controllerType,
130-
@JsonProperty(value = "remoteRack") final @Nullable Integer remoteRack,
131-
@JsonProperty(value = "remoteSlot") final @Nullable Integer remoteSlot,
132-
@JsonProperty(value = "pduLength") final @Nullable Integer pduLength,
133-
@JsonProperty(value = "pollingIntervalMillis") final @Nullable Integer pollingIntervalMillis,
134-
@JsonProperty(value = "maxPollingErrorsBeforeRemoval") final @Nullable Integer maxPollingErrorsBeforeRemoval,
135-
@JsonProperty(value = "publishChangedDataOnly") final @Nullable Boolean publishChangedDataOnly,
136-
@JsonProperty(value = "s7ToMqttMappings", required = true) final @NotNull List<S7ToMqttConfig> s7ToMqttConfig) {
139+
@JsonProperty(value = PROPERTY_ID, required = true) final @NotNull String id,
140+
@JsonProperty(value = PROPERTY_PORT, required = true) final Integer port,
141+
@JsonProperty(value = PROPERTY_HOST, required = true) final @NotNull String host,
142+
@JsonProperty(value = PROPERTY_CONTROLLER_TYPE, required = true) final @NotNull ControllerType controllerType,
143+
@JsonProperty(value = PROPERTY_REMOTE_RACK) final @Nullable Integer remoteRack,
144+
@JsonProperty(value = PROPERTY_REMOTE_SLOT) final @Nullable Integer remoteSlot,
145+
@JsonProperty(value = PROPERTY_PDU_LENGTH) final @Nullable Integer pduLength,
146+
@JsonProperty(value = PROPERTY_POLLING_INTERVAL_MILLIS) final @Nullable Integer pollingIntervalMillis,
147+
@JsonProperty(value = PROPERTY_MAX_POLLING_ERRORS_BEFORE_REMOVAL) final @Nullable Integer maxPollingErrorsBeforeRemoval,
148+
@JsonProperty(value = PROPERTY_PUBLISH_CHANGED_DATA_ONLY) final @Nullable Boolean publishChangedDataOnly,
149+
@JsonProperty(value = PROPERTY_S_7_TO_MQTT_MAPPINGS, required = true) final @NotNull List<S7ToMqttConfig> s7ToMqttConfig) {
137150
this.id = id;
138-
this.port = port;
139151
this.host = host;
140152
this.controllerType = controllerType;
141-
this.pollingIntervalMillis = Objects.requireNonNullElse(pollingIntervalMillis, 1000);
142-
this.maxPollingErrorsBeforeRemoval = Objects.requireNonNullElse(maxPollingErrorsBeforeRemoval, 10);
143-
this.publishChangedDataOnly = Objects.requireNonNullElse(publishChangedDataOnly, true);
153+
this.port = port;
154+
this.pollingIntervalMillis = Objects.requireNonNullElse(pollingIntervalMillis,
155+
Integer.valueOf(DEFAULT_POLLING_INTERVAL_MS));
156+
this.maxPollingErrorsBeforeRemoval = Objects.requireNonNullElse(maxPollingErrorsBeforeRemoval,
157+
Integer.valueOf(DEFAULT_MAX_POLLING_ERRORS));
158+
this.publishChangedDataOnly = Objects.requireNonNullElse(publishChangedDataOnly,
159+
Boolean.valueOf(DEFAULT_PUBLISH_CHANGED_DATA_ONLY));
144160
this.remoteRack = remoteRack;
145161
this.remoteSlot = remoteSlot;
146162
this.pduLength = pduLength;

modules/hivemq-edge-module-s7/src/main/java/com/hivemq/edge/adapters/s7/config/S7ToMqttConfig.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,32 @@
3333

3434
public class S7ToMqttConfig implements PollingContext {
3535

36-
@JsonProperty(value = "mqttTopic", required = true)
36+
public static final String PROPERTY_MQTT_TOPIC = "mqttTopic";
37+
public static final String PROPERTY_MQTT_QOS = "mqttQos";
38+
public static final String PROPERTY_MESSAGE_HANDLING_OPTIONS = "messageHandlingOptions";
39+
public static final String PROPERTY_INCLUDE_TIMESTAMP = "includeTimestamp";
40+
public static final String PROPERTY_INCLUDE_TAG_NAMES = "includeTagNames";
41+
public static final String PROPERTY_TAG_NAME = "tagName";
42+
public static final String PROPERTY_TAG_ADDRESS = "tagAddress";
43+
public static final String PROPERTY_DATA_TYPE = "dataType";
44+
public static final String PROPERTY_MQTT_USER_PROPERTIES = "mqttUserProperties";
45+
46+
@JsonProperty(value = PROPERTY_MQTT_TOPIC, required = true)
3747
@ModuleConfigField(title = "Destination MQTT Topic",
3848
description = "The topic to publish data on",
3949
required = true,
4050
format = ModuleConfigField.FieldType.MQTT_TOPIC)
4151
private final @NotNull String mqttTopic;
4252

43-
@JsonProperty(value = "mqttQos")
53+
@JsonProperty(value = PROPERTY_MQTT_QOS)
4454
@ModuleConfigField(title = "MQTT QoS",
4555
description = "MQTT Quality of Service level",
4656
numberMin = 0,
4757
numberMax = 2,
4858
defaultValue = "0")
4959
private final int qos;
5060

51-
@JsonProperty(value = "messageHandlingOptions")
61+
@JsonProperty(value = PROPERTY_MESSAGE_HANDLING_OPTIONS)
5262
@ModuleConfigField(title = "Message Handling Options",
5363
description = "This setting defines the format of the resulting MQTT message, either a message per changed tag or a message per subscription that may include multiple data points per sample",
5464
enumDisplayValues = {
@@ -57,38 +67,38 @@ public class S7ToMqttConfig implements PollingContext {
5767
defaultValue = "MQTTMessagePerTag")
5868
private final @NotNull MessageHandlingOptions messageHandlingOptions;
5969

60-
@JsonProperty(value = "includeTimestamp")
70+
@JsonProperty(value = PROPERTY_INCLUDE_TIMESTAMP)
6171
@ModuleConfigField(title = "Include Sample Timestamp In Publish?",
6272
description = "Include the unix timestamp of the sample time in the resulting MQTT message",
6373
defaultValue = "true")
6474
private boolean includeTimestamp;
6575

66-
@JsonProperty(value = "includeTagNames")
76+
@JsonProperty(value = PROPERTY_INCLUDE_TAG_NAMES)
6777
@ModuleConfigField(title = "Include Tag Names In Publish?",
6878
description = "Include the names of the tags in the resulting MQTT publish",
6979
defaultValue = "false")
7080
private boolean includeTagNames;
7181

72-
@JsonProperty(value = "mqttUserProperties")
82+
@JsonProperty(value = PROPERTY_MQTT_USER_PROPERTIES)
7383
@ModuleConfigField(title = "MQTT User Properties",
7484
description = "Arbitrary properties to associate with the mapping",
7585
arrayMaxItems = 10)
7686
private @NotNull List<MqttUserProperty> userProperties;
7787

78-
@JsonProperty(value = "tagName", required = true)
88+
@JsonProperty(value = PROPERTY_TAG_NAME, required = true)
7989
@ModuleConfigField(title = "Tag Name",
8090
description = "The name to assign to this address. The tag name must be unique for all subscriptions within this protocol adapter.",
8191
required = true,
8292
format = ModuleConfigField.FieldType.IDENTIFIER)
8393
private final @NotNull String tagName;
8494

85-
@JsonProperty(value = "tagAddress", required = true)
95+
@JsonProperty(value = PROPERTY_TAG_ADDRESS, required = true)
8696
@ModuleConfigField(title = "Tag Address",
8797
description = "The well formed address of the tag to read",
8898
required = true)
8999
private final @NotNull String tagAddress;
90100

91-
@JsonProperty(value = "dataType", required = true)
101+
@JsonProperty(value = PROPERTY_DATA_TYPE, required = true)
92102
@ModuleConfigField(title = "Data Type", description = "The expected data type of the tag", enumDisplayValues = {
93103
"Boolean",
94104
"Byte",
@@ -108,15 +118,15 @@ public class S7ToMqttConfig implements PollingContext {
108118

109119
@JsonCreator
110120
public S7ToMqttConfig(
111-
@JsonProperty(value = "mqttTopic", required = true) final @NotNull String mqttTopic,
112-
@JsonProperty(value = "mqttQos") final @Nullable Integer qos,
113-
@JsonProperty(value = "messageHandlingOptions") final @Nullable MessageHandlingOptions messageHandlingOptions,
114-
@JsonProperty(value = "includeTimestamp") final @Nullable Boolean includeTimestamp,
115-
@JsonProperty(value = "includeTagNames") final @Nullable Boolean includeTagNames,
116-
@JsonProperty(value = "tagName", required = true) final @NotNull String tagName,
117-
@JsonProperty(value = "tagAddress", required = true) final @NotNull String tagAddress,
118-
@JsonProperty(value = "dataType", required = true) final @NotNull S7DataType dataType,
119-
@JsonProperty(value = "mqttUserProperties") final @Nullable List<MqttUserProperty> userProperties) {
121+
@JsonProperty(value = PROPERTY_MQTT_TOPIC, required = true) final @NotNull String mqttTopic,
122+
@JsonProperty(value = PROPERTY_MQTT_QOS) final @Nullable Integer qos,
123+
@JsonProperty(value = PROPERTY_MESSAGE_HANDLING_OPTIONS) final @Nullable MessageHandlingOptions messageHandlingOptions,
124+
@JsonProperty(value = PROPERTY_INCLUDE_TIMESTAMP) final @Nullable Boolean includeTimestamp,
125+
@JsonProperty(value = PROPERTY_INCLUDE_TAG_NAMES) final @Nullable Boolean includeTagNames,
126+
@JsonProperty(value = PROPERTY_TAG_NAME) final @NotNull String tagName,
127+
@JsonProperty(value = PROPERTY_TAG_ADDRESS, required = true) final @NotNull String tagAddress,
128+
@JsonProperty(value = PROPERTY_DATA_TYPE, required = true) final @NotNull S7DataType dataType,
129+
@JsonProperty(value = PROPERTY_MQTT_USER_PROPERTIES) final @Nullable List<MqttUserProperty> userProperties) {
120130
this.mqttTopic = mqttTopic;
121131
this.qos = requireNonNullElse(qos, 0);
122132
this.messageHandlingOptions = requireNonNullElse(messageHandlingOptions, MQTTMessagePerTag);

0 commit comments

Comments
 (0)