@@ -59,7 +59,7 @@ public class ModbusProtocolAdapter implements PollingProtocolAdapter<ModbusToMqt
5959 private final @ NotNull ProtocolAdapterState protocolAdapterState ;
6060 private final @ NotNull AdapterFactories adapterFactories ;
6161
62- private volatile @ Nullable IModbusClient modbusClient ;
62+ private volatile @ Nullable ModbusClient modbusClient ;
6363 private final @ NotNull Map <ModbusToMqttMapping , List <DataPoint >> lastSamples = new HashMap <>();
6464
6565 public ModbusProtocolAdapter (
@@ -78,7 +78,7 @@ public void start(
7878 try {
7979 initConnection ();
8080 output .startedSuccessfully ();
81- } catch (Exception e ) {
81+ } catch (final Exception e ) {
8282 output .failStart (e , "Exception during setup of Modbus client." );
8383 }
8484
@@ -90,7 +90,7 @@ public void stop(@NotNull final ProtocolAdapterStopInput input, @NotNull final P
9090 if (modbusClient != null ) {
9191 modbusClient .disconnect ();
9292 }
93- } catch (Exception e ) {
93+ } catch (final Exception e ) {
9494 output .failStop (e , "Error encountered closing connection to Modbus device." );
9595 return ;
9696 }
@@ -119,7 +119,7 @@ public void poll(
119119 } else {
120120 pollingOutput .fail (new IllegalStateException ("client not initialised" ),"The client is not initialised." );
121121 }
122- } catch (Exception e ) {
122+ } catch (final Exception e ) {
123123 pollingOutput .fail (e , null );
124124 }
125125 }
@@ -140,14 +140,14 @@ public int getMaxPollingErrorsBeforeRemoval() {
140140 }
141141
142142
143- private @ NotNull IModbusClient initConnection () {
143+ private @ NotNull ModbusClient initConnection () {
144144 if (modbusClient == null ) {
145145 synchronized (lock ) {
146146 if (modbusClient == null ) {
147147 if (log .isTraceEnabled ()) {
148148 log .trace ("Creating new instance of Modbus Client with {}." , adapterConfig );
149149 }
150- modbusClient = new ModbusClient (adapterConfig );
150+ modbusClient = new ModbusClient (adapterConfig , adapterFactories . dataPointFactory () );
151151 }
152152 }
153153 }
@@ -162,7 +162,7 @@ public int getMaxPollingErrorsBeforeRemoval() {
162162
163163 @ Override
164164 public void discoverValues (
165- @ NotNull ProtocolAdapterDiscoveryInput input , @ NotNull ProtocolAdapterDiscoveryOutput output ) {
165+ @ NotNull final ProtocolAdapterDiscoveryInput input , @ NotNull final ProtocolAdapterDiscoveryOutput output ) {
166166 //-- Do the discovery of registers and coils, only for root level
167167 final NodeTree nodeTree = output .getNodeTree ();
168168 if (input .getRootNode () == null ) {
@@ -199,13 +199,13 @@ protected void captureDataSample(final @NotNull ModBusData modBusData, @NotNull
199199 pollingOutput .finish ();
200200 }
201201
202- private void calculateDelta (@ NotNull ModBusData modBusData , @ NotNull PollingOutput pollingOutput ) {
203- ModbusToMqttMapping subscription =
202+ private void calculateDelta (@ NotNull final ModBusData modBusData , @ NotNull final PollingOutput pollingOutput ) {
203+ final ModbusToMqttMapping subscription =
204204 (ModbusToMqttMapping ) modBusData .getPollingContext ();
205205
206- List <DataPoint > previousSampleDataPoints = lastSamples .put (subscription , modBusData .getDataPoints ());
207- List <DataPoint > currentSamplePoints = modBusData .getDataPoints ();
208- List <DataPoint > delta = AdapterDataUtils .mergeChangedSamples (previousSampleDataPoints , currentSamplePoints );
206+ final List <DataPoint > previousSampleDataPoints = lastSamples .put (subscription , modBusData .getDataPoints ());
207+ final List <DataPoint > currentSamplePoints = modBusData .getDataPoints ();
208+ final List <DataPoint > delta = AdapterDataUtils .mergeChangedSamples (previousSampleDataPoints , currentSamplePoints );
209209 if (log .isTraceEnabled ()) {
210210 log .trace ("Calculating change data old {} samples, new {} sample, delta {}" ,
211211 previousSampleDataPoints != null ? previousSampleDataPoints .size () : 0 ,
@@ -218,25 +218,21 @@ private void calculateDelta(@NotNull ModBusData modBusData, @NotNull PollingOutp
218218 }
219219 }
220220
221- protected @ NotNull ModBusData readRegisters (@ NotNull final PollingContext sub ) {
221+ protected @ NotNull ModBusData readRegisters (final @ NotNull ModbusToMqttMapping modbusToMqttMapping ) {
222222 try {
223- ModbusToMqttMapping subscription = (ModbusToMqttMapping ) sub ;
224- AddressRange addressRange = subscription .getAddressRange ();
225- Short [] registers = modbusClient .readHoldingRegisters (addressRange .startIdx ,
226- addressRange .endIdx - addressRange .startIdx );
227- ModBusData data = new ModBusData (subscription , adapterFactories .dataPointFactory ());
228- //add data point per register
229- for (int i = 0 ; i < registers .length ; i ++) {
230- data .addDataPoint ("register-" + (addressRange .startIdx + i ), registers [i ]);
231- }
223+ final AddressRange addressRange = modbusToMqttMapping .getAddressRange ();
224+ final DataPoint dataPoint = modbusClient .readHoldingRegisters (addressRange .startIdx ,
225+ addressRange .nrRegistersToRead , modbusToMqttMapping .getDataType ());
226+ final ModBusData data = new ModBusData (modbusToMqttMapping );
227+ data .addDataPoint (dataPoint );
232228 return data ;
233- } catch (Exception e ) {
229+ } catch (final Exception e ) {
234230 throw new RuntimeException (e );
235231 }
236232 }
237233
238234 private static void addAddresses (
239- @ NotNull NodeTree tree , @ NotNull String parent , int startIdx , int count , int groupIdx ) {
235+ @ NotNull final NodeTree tree , @ NotNull final String parent , final int startIdx , final int count , final int groupIdx ) {
240236
241237 String parentNode = parent ;
242238 if (groupIdx < count ) {
0 commit comments