]> git.basschouten.com Git - openhab-addons.git/blob
67f81af51783573697e6265fd52b74d62fadc303
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.transport.modbus;
14
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;
21
22 import net.wimpi.modbus.Modbus;
23
24 /**
25  * Implementation of immutable representation of modbus read request
26  *
27  * Equals and hashCode implemented keeping {@link PollTask} in mind: two instances of this class are considered the same
28  * if they have
29  * the equal parameters (same slave id, start, length, function code and maxTries).
30  *
31  * @author Sami Salonen - Initial contribution
32  *
33  */
34 @NonNullByDefault
35 public class ModbusReadRequestBlueprint {
36     private static StandardToStringStyle toStringStyle = new StandardToStringStyle();
37     static {
38         toStringStyle.setUseShortClassName(true);
39     }
40
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;
46
47     public ModbusReadRequestBlueprint(int slaveId, ModbusReadFunctionCode functionCode, int start, int length,
48             int maxTries) {
49         this.slaveId = slaveId;
50         this.functionCode = functionCode;
51         this.start = start;
52         this.length = length;
53         this.maxTries = maxTries;
54     }
55
56     /**
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.
61      * <p>
62      *
63      * @return the unit identifier as <tt>int</tt>.
64      */
65     public int getUnitID() {
66         return slaveId;
67     }
68
69     public int getReference() {
70         return start;
71     }
72
73     public ModbusReadFunctionCode getFunctionCode() {
74         return functionCode;
75     }
76
77     public int getDataLength() {
78         return length;
79     }
80
81     /**
82      * Maximum number of tries to execute the request, when request fails
83      *
84      * For example, number 1 means on try only with no re-tries.
85      *
86      * @return number of maximum tries
87      */
88     public int getMaxTries() {
89         return maxTries;
90     }
91
92     /**
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.
97      * <p>
98      *
99      * @return the protocol identifier as <tt>int</tt>.
100      */
101     public int getProtocolID() {
102         return Modbus.DEFAULT_PROTOCOL_ID;
103     }
104
105     @Override
106     public int hashCode() {
107         return new HashCodeBuilder(81, 3).append(slaveId).append(functionCode).append(start).append(length)
108                 .append(maxTries).toHashCode();
109     }
110
111     @Override
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();
115     }
116
117     @Override
118     public boolean equals(@Nullable Object obj) {
119         if (obj == null) {
120             return false;
121         }
122         if (obj == this) {
123             return true;
124         }
125         if (obj.getClass() != getClass()) {
126             return false;
127         }
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();
131     }
132 }