2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.transport.modbus;
15 import org.apache.commons.lang.builder.EqualsBuilder;
16 import org.apache.commons.lang.builder.HashCodeBuilder;
17 import org.apache.commons.lang.builder.StandardToStringStyle;
18 import org.apache.commons.lang.builder.ToStringBuilder;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
22 import net.wimpi.modbus.Modbus;
25 * Implementation of immutable representation of modbus read request
27 * Equals and hashCode implemented keeping {@link PollTask} in mind: two instances of this class are considered the same
29 * the equal parameters (same slave id, start, length, function code and maxTries).
31 * @author Sami Salonen - Initial contribution
35 public class ModbusReadRequestBlueprint {
36 private static StandardToStringStyle toStringStyle = new StandardToStringStyle();
38 toStringStyle.setUseShortClassName(true);
41 private final int slaveId;
42 private final ModbusReadFunctionCode functionCode;
43 private final int start;
44 private final int length;
45 private final int maxTries;
47 public ModbusReadRequestBlueprint(int slaveId, ModbusReadFunctionCode functionCode, int start, int length,
49 this.slaveId = slaveId;
50 this.functionCode = functionCode;
53 this.maxTries = maxTries;
57 * Returns the unit identifier of this
58 * <tt>ModbusMessage</tt> as <tt>int</tt>.<br>
59 * The identifier is a 1-byte non negative
60 * integer value valid in the range of 0-255.
63 * @return the unit identifier as <tt>int</tt>.
65 public int getUnitID() {
69 public int getReference() {
73 public ModbusReadFunctionCode getFunctionCode() {
77 public int getDataLength() {
82 * Maximum number of tries to execute the request, when request fails
84 * For example, number 1 means on try only with no re-tries.
86 * @return number of maximum tries
88 public int getMaxTries() {
93 * Returns the protocol identifier of this
94 * <tt>ModbusMessage</tt> as <tt>int</tt>.<br>
95 * The identifier is a 2-byte (short) non negative
96 * integer value valid in the range of 0-65535.
99 * @return the protocol identifier as <tt>int</tt>.
101 public int getProtocolID() {
102 return Modbus.DEFAULT_PROTOCOL_ID;
106 public int hashCode() {
107 return new HashCodeBuilder(81, 3).append(slaveId).append(functionCode).append(start).append(length)
108 .append(maxTries).toHashCode();
112 public String toString() {
113 return new ToStringBuilder(this, toStringStyle).append("slaveId", slaveId).append("functionCode", functionCode)
114 .append("start", start).append("length", length).append("maxTries", maxTries).toString();
118 public boolean equals(@Nullable Object obj) {
125 if (obj.getClass() != getClass()) {
128 ModbusReadRequestBlueprint rhs = (ModbusReadRequestBlueprint) obj;
129 return new EqualsBuilder().append(slaveId, rhs.slaveId).append(functionCode, rhs.functionCode)
130 .append(start, rhs.start).append(length, rhs.length).isEquals();