- *
- * @param index the index of the bit to be returned.
- * @return true if the bit at the specified index is set,
- * false otherwise.
- * @throws IndexOutOfBoundsException if the index is out of bounds.
- */
- public boolean getBit(int index) {
- if (index >= size()) {
- throw new IndexOutOfBoundsException();
- }
- return this.wrapped.get(index);
- }
-
- public void setBit(int index, boolean value) {
- if (value) {
- this.wrapped.set(index);
- } else {
- this.wrapped.clear(index);
- }
- }
-
- /**
- * Get number of bits stored in this instance
- *
- * @return
- */
- public int size() {
- return length;
- }
-
- @Override
- public String toString() {
- return new StringBuilder("BitArray(bits=").append(length == 0 ? "
- *
- * @return the unit identifier as int.
- */
- public int getUnitID() {
- return slaveId;
- }
-
- public int getReference() {
- return start;
- }
-
- public ModbusReadFunctionCode getFunctionCode() {
- return functionCode;
- }
-
- public int getDataLength() {
- return length;
- }
-
- /**
- * Maximum number of tries to execute the request, when request fails
- *
- * For example, number 1 means on try only with no re-tries.
- *
- * @return number of maximum tries
- */
- public int getMaxTries() {
- return maxTries;
- }
-
- /**
- * Returns the protocol identifier of this
- * ModbusMessage as int.
- *
- * @return the protocol identifier as int.
- */
- public int getProtocolID() {
- return Modbus.DEFAULT_PROTOCOL_ID;
- }
-
- @Override
- public int hashCode() {
- return new HashCodeBuilder(81, 3).append(slaveId).append(functionCode).append(start).append(length)
- .append(maxTries).toHashCode();
- }
-
- @Override
- public String toString() {
- return new ToStringBuilder(this, toStringStyle).append("slaveId", slaveId).append("functionCode", functionCode)
- .append("start", start).append("length", length).append("maxTries", maxTries).toString();
- }
-
- @Override
- public boolean equals(@Nullable Object obj) {
- if (obj == null) {
- return false;
- }
- if (obj == this) {
- return true;
- }
- if (obj.getClass() != getClass()) {
- return false;
- }
- ModbusReadRequestBlueprint rhs = (ModbusReadRequestBlueprint) obj;
- return new EqualsBuilder().append(slaveId, rhs.slaveId).append(functionCode, rhs.functionCode)
- .append(start, rhs.start).append(length, rhs.length).isEquals();
- }
-}
diff --git a/bundles/org.openhab.io.transport.modbus/src/main/java/org/openhab/io/transport/modbus/ModbusRegisterArray.java b/bundles/org.openhab.io.transport.modbus/src/main/java/org/openhab/io/transport/modbus/ModbusRegisterArray.java
deleted file mode 100644
index 6246d34b2..000000000
--- a/bundles/org.openhab.io.transport.modbus/src/main/java/org/openhab/io/transport/modbus/ModbusRegisterArray.java
+++ /dev/null
@@ -1,113 +0,0 @@
-/**
- * Copyright (c) 2010-2020 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.io.transport.modbus;
-
-import java.util.Arrays;
-
-import org.eclipse.jdt.annotation.NonNullByDefault;
-import org.openhab.core.util.HexUtils;
-
-/**
- * Immutable {@link ModbusRegisterArray} implementation
- *
- * @author Sami Salonen - Initial contribution
- */
-@NonNullByDefault
-public class ModbusRegisterArray {
-
- private final byte[] bytes;
-
- public ModbusRegisterArray(byte... bytes) {
- if (bytes.length % 2 != 0) {
- throw new IllegalArgumentException();
- }
- this.bytes = Arrays.copyOf(bytes, bytes.length);
- }
-
- /**
- * Construct plain
- *
- * @return the protocol identifier as int.
- */
- public int getProtocolID() {
- return Modbus.DEFAULT_PROTOCOL_ID;
- }
-
- /**
- * Returns the reference of the register/coil/discrete input to to start
- * writing with this request
- *
- *
- * @return the reference of the register
- * to start reading from as int.
- */
- public abstract int getReference();
-
- /**
- * Returns the unit identifier of this
- * ModbusMessage as int.
- *
- * @return the unit identifier as int.
- */
- public abstract int getUnitID();
-
- /**
- * Returns the function code of this
- * ModbusMessage as int.
- *
- * @return the function code as int.
- *
- * @see net.wimpi.modbus.Modbus
- */
- public abstract ModbusWriteFunctionCode getFunctionCode();
-
- /**
- * Get maximum number of tries, in case errors occur. Should be at least 1.
- */
- public abstract int getMaxTries();
-
- /**
- * Accept visitor
- *
- * @param visitor
- */
- public abstract void accept(ModbusWriteRequestBlueprintVisitor visitor);
-}
diff --git a/bundles/org.openhab.io.transport.modbus/src/main/java/org/openhab/io/transport/modbus/ModbusWriteRequestBlueprintVisitor.java b/bundles/org.openhab.io.transport.modbus/src/main/java/org/openhab/io/transport/modbus/ModbusWriteRequestBlueprintVisitor.java
deleted file mode 100644
index ab24d4ccc..000000000
--- a/bundles/org.openhab.io.transport.modbus/src/main/java/org/openhab/io/transport/modbus/ModbusWriteRequestBlueprintVisitor.java
+++ /dev/null
@@ -1,40 +0,0 @@
-/**
- * Copyright (c) 2010-2020 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.io.transport.modbus;
-
-import org.eclipse.jdt.annotation.NonNullByDefault;
-
-/**
- *
- * ModbusWriteRequestBlueprintVisitor interface.
- *
- * The identifier is a 1-byte non negative
- * integer value valid in the range of 0-255.
- *
- * The identifier is a 2-byte (short) non negative
- * integer value valid in the range of 0-65535.
- * ModbusRegisterArray array from register values
- *
- * @param registerValues register values, each int corresponding to one register
- * @return
- */
- public ModbusRegisterArray(int... registerValues) {
- bytes = new byte[registerValues.length * 2];
- for (int registerIndex = 0; registerIndex < registerValues.length; registerIndex++) {
- int register = registerValues[registerIndex] & 0xffff;
- // hi-byte
- bytes[registerIndex * 2] = (byte) (register >> 8);
- // lo byte
- bytes[registerIndex * 2 + 1] = (byte) register;
- }
- }
-
- /**
- * Get register index i as unsigned integer
- *
- * @param i register index
- * @return register value interpreted as unsigned integer (big-endian byte ordering)
- */
- public int getRegister(int i) {
- int hi = bytes[i * 2] & 0xff;
- int lo = bytes[i * 2 + 1] & 0xff;
- return ((hi << 8) | lo) & 0xffff;
- }
-
- /**
- * Return bytes representing the registers
- *
- *
- * Index 0: hi-byte of 1st register
- * Index 1: low-byte of 1st register
- * Index 3: hi-byte of 2nd register
- * Index 4: low-byte of 2nd register
- * ...
- *
- * @return set of bytes
- */
- public byte[] getBytes() {
- return bytes;
- }
-
- /**
- * Get number of registers stored in this instance
- *
- * @return
- */
- public int size() {
- return bytes.length / 2;
- }
-
- @Override
- public String toString() {
- if (bytes.length == 0) {
- return "ModbusRegisterArray(data is empty, writeMultiple is
- * false but there are many bits to write.
- */
- public ModbusWriteCoilRequestBlueprint(int slaveId, int reference, BitArray data, boolean writeMultiple,
- int maxTries) {
- super();
- this.slaveId = slaveId;
- this.reference = reference;
- this.bits = data;
- this.writeMultiple = writeMultiple;
- this.maxTries = maxTries;
-
- if (!writeMultiple && bits.size() > 1) {
- throw new IllegalArgumentException("With multiple coils, writeMultiple must be true");
- }
- if (bits.size() == 0) {
- throw new IllegalArgumentException("Must have at least one bit");
- }
- if (maxTries <= 0) {
- throw new IllegalArgumentException("maxTries should be positive, was " + maxTries);
- }
- }
-
- @Override
- public int getUnitID() {
- return slaveId;
- }
-
- @Override
- public int getReference() {
- return reference;
- }
-
- @Override
- public ModbusWriteFunctionCode getFunctionCode() {
- return writeMultiple ? ModbusWriteFunctionCode.WRITE_MULTIPLE_COILS : ModbusWriteFunctionCode.WRITE_COIL;
- }
-
- public BitArray getCoils() {
- return bits;
- }
-
- @Override
- public int getMaxTries() {
- return maxTries;
- }
-
- @Override
- public String toString() {
- return new ToStringBuilder(this, toStringStyle).append("slaveId", slaveId).append("reference", reference)
- .append("functionCode", getFunctionCode()).append("bits", bits).append("maxTries", maxTries).toString();
- }
-
- @Override
- public void accept(ModbusWriteRequestBlueprintVisitor visitor) {
- visitor.visit(this);
- }
-}
diff --git a/bundles/org.openhab.io.transport.modbus/src/main/java/org/openhab/io/transport/modbus/ModbusWriteFunctionCode.java b/bundles/org.openhab.io.transport.modbus/src/main/java/org/openhab/io/transport/modbus/ModbusWriteFunctionCode.java
deleted file mode 100644
index 9bc160bc4..000000000
--- a/bundles/org.openhab.io.transport.modbus/src/main/java/org/openhab/io/transport/modbus/ModbusWriteFunctionCode.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/**
- * Copyright (c) 2010-2020 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.io.transport.modbus;
-
-import java.util.stream.Stream;
-
-import org.eclipse.jdt.annotation.NonNull;
-
-import net.wimpi.modbus.Modbus;
-
-/**
- * Modbus write function codes supported by this transport
- *
- * @author Sami Salonen - Initial contribution
- */
-public enum ModbusWriteFunctionCode {
- WRITE_COIL(Modbus.WRITE_COIL),
- WRITE_MULTIPLE_COILS(Modbus.WRITE_MULTIPLE_COILS),
- WRITE_SINGLE_REGISTER(Modbus.WRITE_SINGLE_REGISTER),
- WRITE_MULTIPLE_REGISTERS(Modbus.WRITE_MULTIPLE_REGISTERS);
-
- private final int functionCode;
-
- ModbusWriteFunctionCode(int code) {
- functionCode = code;
- }
-
- /**
- * Get numeric function code represented by this instance
- *
- * @return
- */
- public int getFunctionCode() {
- return functionCode;
- }
-
- /**
- * Construct {@link ModbusWriteFunctionCode} from the numeric function code
- *
- * @param functionCode numeric function code
- * @return {@link ModbusWriteFunctionCode} matching the numeric function code
- * @throws IllegalArgumentException with unsupported functions
- */
- @SuppressWarnings("null")
- public static @NonNull ModbusWriteFunctionCode fromFunctionCode(int functionCode) throws IllegalArgumentException {
- return Stream.of(ModbusWriteFunctionCode.values()).filter(v -> v.getFunctionCode() == functionCode).findFirst()
- .orElseThrow(() -> new IllegalArgumentException("Invalid functionCode"));
- }
-}
diff --git a/bundles/org.openhab.io.transport.modbus/src/main/java/org/openhab/io/transport/modbus/ModbusWriteRegisterRequestBlueprint.java b/bundles/org.openhab.io.transport.modbus/src/main/java/org/openhab/io/transport/modbus/ModbusWriteRegisterRequestBlueprint.java
deleted file mode 100644
index 83c210376..000000000
--- a/bundles/org.openhab.io.transport.modbus/src/main/java/org/openhab/io/transport/modbus/ModbusWriteRegisterRequestBlueprint.java
+++ /dev/null
@@ -1,108 +0,0 @@
-/**
- * Copyright (c) 2010-2020 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.io.transport.modbus;
-
-import org.apache.commons.lang.builder.StandardToStringStyle;
-import org.apache.commons.lang.builder.ToStringBuilder;
-import org.eclipse.jdt.annotation.NonNullByDefault;
-
-/**
- * Implementation for writing registers
- *
- * @author Sami Salonen - Initial contribution
- *
- */
-@NonNullByDefault
-public class ModbusWriteRegisterRequestBlueprint extends ModbusWriteRequestBlueprint {
-
- private static StandardToStringStyle toStringStyle = new StandardToStringStyle();
-
- static {
- toStringStyle.setUseShortClassName(true);
- }
-
- private final int slaveId;
- private final int reference;
- private final ModbusRegisterArray registers;
- private final boolean writeMultiple;
- private final int maxTries;
-
- /**
- * Construct coil write request with many bits of data
- *
- * @param slaveId slave id to write to
- * @param reference reference address
- * @param registers register(s) to write
- * @param writeMultiple whether to use {@link ModbusWriteFunctionCode.WRITE_MULTIPLE_COILS} over
- * {@link ModbusWriteFunctionCode.WRITE_COIL}. Useful with single register of data.
- * @param maxTries maximum number of tries in case of errors, should be at least 1
- * @throws IllegalArgumentException in case data is empty, writeMultiple is
- * false but there are many registers to write.
- */
- public ModbusWriteRegisterRequestBlueprint(int slaveId, int reference, ModbusRegisterArray registers,
- boolean writeMultiple, int maxTries) throws IllegalArgumentException {
- super();
- this.slaveId = slaveId;
- this.reference = reference;
- this.registers = registers;
- this.writeMultiple = writeMultiple;
- this.maxTries = maxTries;
-
- if (!writeMultiple && registers.size() > 1) {
- throw new IllegalArgumentException("With multiple registers, writeMultiple must be true");
- }
- if (registers.size() == 0) {
- throw new IllegalArgumentException("Must have at least one register");
- }
- if (maxTries <= 0) {
- throw new IllegalArgumentException("maxTries should be positive");
- }
- }
-
- @Override
- public int getReference() {
- return reference;
- }
-
- @Override
- public int getUnitID() {
- return slaveId;
- }
-
- @Override
- public ModbusWriteFunctionCode getFunctionCode() {
- return writeMultiple ? ModbusWriteFunctionCode.WRITE_MULTIPLE_REGISTERS
- : ModbusWriteFunctionCode.WRITE_SINGLE_REGISTER;
- }
-
- public ModbusRegisterArray getRegisters() {
- return registers;
- }
-
- @Override
- public int getMaxTries() {
- return maxTries;
- }
-
- @Override
- public String toString() {
- return new ToStringBuilder(this, toStringStyle).append("slaveId", slaveId).append("reference", reference)
- .append("functionCode", getFunctionCode()).append("registers", registers).append("maxTries", maxTries)
- .toString();
- }
-
- @Override
- public void accept(ModbusWriteRequestBlueprintVisitor visitor) {
- visitor.visit(this);
- }
-}
diff --git a/bundles/org.openhab.io.transport.modbus/src/main/java/org/openhab/io/transport/modbus/ModbusWriteRequestBlueprint.java b/bundles/org.openhab.io.transport.modbus/src/main/java/org/openhab/io/transport/modbus/ModbusWriteRequestBlueprint.java
deleted file mode 100644
index 5cab10628..000000000
--- a/bundles/org.openhab.io.transport.modbus/src/main/java/org/openhab/io/transport/modbus/ModbusWriteRequestBlueprint.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/**
- * Copyright (c) 2010-2020 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.io.transport.modbus;
-
-import org.eclipse.jdt.annotation.NonNullByDefault;
-
-import net.wimpi.modbus.Modbus;
-
-/**
- * Base interface for Modbus write requests
- *
- * @author Sami Salonen - Initial contribution
- *
- */
-@NonNullByDefault
-public abstract class ModbusWriteRequestBlueprint {
-
- /**
- * Returns the protocol identifier of this
- * ModbusMessage as int.
- * The identifier is a 2-byte (short) non negative
- * integer value valid in the range of 0-65535.
- *
- * The identifier is a 1-byte non negative
- * integer value valid in the range of 0-255.
- *
- * The function code is a 1-byte non negative
- * integer value valid in the range of 0-127.
- * Function codes are ordered in conformance
- * classes their values are specified in
- * net.wimpi.modbus.Modbus.
- * waitMillis has passed from lastOperation
- *
- * @param lastOperation last time operation was executed, or null if it has not been executed
- * @param waitMillis
- * @return milliseconds slept
- * @throws InterruptedException
- */
- public static long waitAtleast(@Nullable Long lastOperation, long waitMillis) throws InterruptedException {
- if (lastOperation == null) {
- return 0;
- }
- long millisSinceLast = System.currentTimeMillis() - lastOperation;
- long millisToWaitStill = Math.min(waitMillis, Math.max(0, waitMillis - millisSinceLast));
- try {
- Thread.sleep(millisToWaitStill);
- } catch (InterruptedException e) {
- LoggerFactory.getLogger(ModbusSlaveConnectionFactoryImpl.class).debug("wait interrupted", e);
- throw e;
- }
- return millisToWaitStill;
- }
-
- /**
- * Disconnect returning connections which have been connected before certain time
- *
- * @param disconnectBeforeConnectedMillis disconnected connections that have been connected before this time
- */
- public void disconnectOnReturn(ModbusSlaveEndpoint endpoint, long disconnectBeforeConnectedMillis) {
- disconnectIfConnectedBefore.put(endpoint, disconnectBeforeConnectedMillis);
- }
-}
diff --git a/bundles/org.openhab.io.transport.modbus/src/main/java/org/openhab/io/transport/modbus/json/WriteRequestJsonUtilities.java b/bundles/org.openhab.io.transport.modbus/src/main/java/org/openhab/io/transport/modbus/json/WriteRequestJsonUtilities.java
deleted file mode 100644
index f0c9eab56..000000000
--- a/bundles/org.openhab.io.transport.modbus/src/main/java/org/openhab/io/transport/modbus/json/WriteRequestJsonUtilities.java
+++ /dev/null
@@ -1,217 +0,0 @@
-/**
- * Copyright (c) 2010-2020 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.io.transport.modbus.json;
-
-import java.util.Collection;
-import java.util.Deque;
-import java.util.LinkedList;
-import java.util.concurrent.atomic.AtomicBoolean;
-
-import org.apache.commons.lang.NotImplementedException;
-import org.eclipse.jdt.annotation.NonNullByDefault;
-import org.eclipse.jdt.annotation.Nullable;
-import org.openhab.io.transport.modbus.BitArray;
-import org.openhab.io.transport.modbus.ModbusConstants;
-import org.openhab.io.transport.modbus.ModbusRegisterArray;
-import org.openhab.io.transport.modbus.ModbusWriteCoilRequestBlueprint;
-import org.openhab.io.transport.modbus.ModbusWriteFunctionCode;
-import org.openhab.io.transport.modbus.ModbusWriteRegisterRequestBlueprint;
-import org.openhab.io.transport.modbus.ModbusWriteRequestBlueprint;
-
-import com.google.gson.JsonArray;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParser;
-
-/**
- * Utilities for converting JSON to {@link ModbusWriteRequestBlueprint}
- *
- *
- * @author Sami Salonen - Initial contribution
- */
-@NonNullByDefault
-public final class WriteRequestJsonUtilities {
- /**
- * Constant for the function code key in the JSON
- */
- public static final String JSON_FUNCTION_CODE = "functionCode";
- /**
- * Constant for the write address key in the JSON
- */
- public static final String JSON_ADDRESS = "address";
- /**
- * Constant for the value key in the JSON
- */
- public static final String JSON_VALUE = "value";
- /**
- * Constant for the maxTries key in the JSON
- */
- public static final String JSON_MAX_TRIES = "maxTries";
-
- /**
- * Default maxTries when it has not been specified
- */
- public static final int DEFAULT_MAX_TRIES = 3;
-
- private static final JsonParser PARSER = new JsonParser();
-
- private WriteRequestJsonUtilities() {
- throw new NotImplementedException();
- }
-
- /**
- * Parse JSON string to collection of {@link ModbusWriteRequestBlueprint}
- *
- * JSON string should represent a JSON array, with JSON objects. Each JSON object represents a write request. The
- * JSON object must have the following keys
- * - functionCode: numeric function code
- * - address: reference or start address of the write
- * - value: array of data to be written. Use zero and one when writing coils. With registers, each number
- * corresponds to register's 16 bit data.
- * - maxTries: number of tries with the write in case of errors
- *
- *
- * @param unitId unit id for the constructed {@link ModbusWriteRequestBlueprint}
- * @param jsonString json to be parsed in string format
- * @return collection of {@link ModbusWriteRequestBlueprint} representing the json
- * @throws IllegalArgumentException in case of unexpected function codes, or too large payload exceeding modbus
- * protocol specification
- * @throws IllegalStateException in case of parsing errors and unexpected json structure
- *
- * @see WriteRequestJsonUtilities.JSON_FUNCTION_CODE
- * @see WriteRequestJsonUtilities.JSON_ADDRESS
- * @see WriteRequestJsonUtilities.JSON_VALUE
- * @see WriteRequestJsonUtilities.JSON_MAX_TRIES
- */
- public static Collection