1818import com .fasterxml .jackson .annotation .JsonCreator ;
1919import com .fasterxml .jackson .annotation .JsonProperty ;
2020import 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 ;
2421import com .hivemq .adapter .sdk .api .config .ProtocolAdapterConfig ;
2522import org .jetbrains .annotations .NotNull ;
2623import org .jetbrains .annotations .Nullable ;
2724
2825import java .util .List ;
2926import java .util .Objects ;
3027
31- import static com .hivemq .adapter .sdk .api .config .MessageHandlingOptions .MQTTMessagePerTag ;
32- import static java .util .Objects .requireNonNullElse ;
33-
3428public 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 ;
0 commit comments