]> git.basschouten.com Git - openhab-addons.git/blob
505ce785cc1f35241be3e1e37b2c02c689be4ddb
[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.StandardToStringStyle;
16 import org.apache.commons.lang.builder.ToStringBuilder;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18
19 /**
20  * Implementation for writing coils
21  *
22  * @author Sami Salonen - Initial contribution
23  *
24  */
25 @NonNullByDefault
26 public class ModbusWriteCoilRequestBlueprint extends ModbusWriteRequestBlueprint {
27
28     private static StandardToStringStyle toStringStyle = new StandardToStringStyle();
29
30     static {
31         toStringStyle.setUseShortClassName(true);
32     }
33
34     private final int slaveId;
35     private final int reference;
36     private final BitArray bits;
37     private final boolean writeMultiple;
38     private final int maxTries;
39
40     /**
41      * Construct coil write request with single bit of data
42      *
43      * @param slaveId slave id to write to
44      * @param reference reference address
45      * @param data bit to write
46      * @param writeMultiple whether to use {@link ModbusWriteFunctionCode.WRITE_MULTIPLE_COILS} over
47      *            {@link ModbusWriteFunctionCode.WRITE_COIL}
48      * @param maxTries maximum number of tries in case of errors, should be at least 1
49      */
50     public ModbusWriteCoilRequestBlueprint(int slaveId, int reference, boolean data, boolean writeMultiple,
51             int maxTries) {
52         this(slaveId, reference, new BitArray(data), writeMultiple, maxTries);
53     }
54
55     /**
56      * Construct coil write request with many bits of data
57      *
58      * @param slaveId slave id to write to
59      * @param reference reference address
60      * @param data bit(s) to write
61      * @param writeMultiple whether to use {@link ModbusWriteFunctionCode.WRITE_MULTIPLE_COILS} over
62      *            {@link ModbusWriteFunctionCode.WRITE_COIL}. Useful with single bit of data.
63      * @param maxTries maximum number of tries in case of errors, should be at least 1
64      * @throws IllegalArgumentException in case <code>data</code> is empty, <code>writeMultiple</code> is
65      *             <code>false</code> but there are many bits to write.
66      */
67     public ModbusWriteCoilRequestBlueprint(int slaveId, int reference, BitArray data, boolean writeMultiple,
68             int maxTries) {
69         super();
70         this.slaveId = slaveId;
71         this.reference = reference;
72         this.bits = data;
73         this.writeMultiple = writeMultiple;
74         this.maxTries = maxTries;
75
76         if (!writeMultiple && bits.size() > 1) {
77             throw new IllegalArgumentException("With multiple coils, writeMultiple must be true");
78         }
79         if (bits.size() == 0) {
80             throw new IllegalArgumentException("Must have at least one bit");
81         }
82         if (maxTries <= 0) {
83             throw new IllegalArgumentException("maxTries should be positive, was " + maxTries);
84         }
85     }
86
87     @Override
88     public int getUnitID() {
89         return slaveId;
90     }
91
92     @Override
93     public int getReference() {
94         return reference;
95     }
96
97     @Override
98     public ModbusWriteFunctionCode getFunctionCode() {
99         return writeMultiple ? ModbusWriteFunctionCode.WRITE_MULTIPLE_COILS : ModbusWriteFunctionCode.WRITE_COIL;
100     }
101
102     public BitArray getCoils() {
103         return bits;
104     }
105
106     @Override
107     public int getMaxTries() {
108         return maxTries;
109     }
110
111     @Override
112     public String toString() {
113         return new ToStringBuilder(this, toStringStyle).append("slaveId", slaveId).append("reference", reference)
114                 .append("functionCode", getFunctionCode()).append("bits", bits).append("maxTries", maxTries).toString();
115     }
116
117     @Override
118     public void accept(ModbusWriteRequestBlueprintVisitor visitor) {
119         visitor.visit(this);
120     }
121 }