]> git.basschouten.com Git - openhab-addons.git/blob
0d02b11de67981435308a74546b5d08ecfabbc29
[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 java.util.Objects;
16 import java.util.Optional;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19
20 /**
21  * Encapsulates result of modbus read operations
22  *
23  * @author Sami Salonen - Initial contribution
24  */
25 @NonNullByDefault
26 public class AsyncModbusReadResult {
27
28     private final ModbusReadRequestBlueprint request;
29
30     private final Optional<BitArray> bits;
31
32     private final Optional<ModbusRegisterArray> registers;
33
34     public AsyncModbusReadResult(ModbusReadRequestBlueprint request, ModbusRegisterArray registers) {
35         Objects.requireNonNull(request, "Request must not be null!");
36         Objects.requireNonNull(registers, "Registers must not be null!");
37         this.request = request;
38         this.registers = Optional.of(registers);
39         this.bits = Optional.empty();
40     }
41
42     public AsyncModbusReadResult(ModbusReadRequestBlueprint request, BitArray bits) {
43         Objects.requireNonNull(request, "Request must not be null!");
44         Objects.requireNonNull(bits, "Bits must not be null!");
45         this.request = request;
46         this.registers = Optional.empty();
47         this.bits = Optional.of(bits);
48     }
49
50     /**
51      * Get request matching this response
52      *
53      * @return request object
54      */
55     public ModbusReadRequestBlueprint getRequest() {
56         return request;
57     }
58
59     /**
60      * Get "coil" or "discrete input" bit data in the case of no errors
61      *
62      * @return bit data
63      */
64     public Optional<BitArray> getBits() {
65         return bits;
66     }
67
68     /**
69      * Get "input register" or "holding register" data in the case of no errors
70      *
71      * @return register data
72      */
73     public Optional<ModbusRegisterArray> getRegisters() {
74         return registers;
75     }
76
77     @Override
78     public String toString() {
79         StringBuilder builder = new StringBuilder("AsyncModbusReadResult(");
80         builder.append("request = ");
81         builder.append(request);
82         bits.ifPresent(bits -> {
83             builder.append(", bits = ");
84             builder.append(bits);
85         });
86         registers.ifPresent(registers -> {
87             builder.append(", registers = ");
88             builder.append(registers);
89         });
90         builder.append(")");
91         return builder.toString();
92     }
93 }