2 * Copyright (c) 2010-2023 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.binding.modbus.stiebeleltron.internal.parser;
15 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.io.transport.modbus.ModbusBitUtilities;
19 import org.openhab.core.io.transport.modbus.ModbusConstants.ValueType;
20 import org.openhab.core.io.transport.modbus.ModbusRegisterArray;
21 import org.openhab.core.library.types.DecimalType;
24 * Base class for parsers with some helper methods
26 * @author Nagy Attila Gabor - Initial contribution
27 * @author Paul Frank - Added more methods
30 public class AbstractBaseParser {
33 * Extract an optional double value
35 * @param raw the register array to extract from
36 * @param index the address of the field
37 * @return the parsed value or empty if the field is not implemented
39 protected Optional<Double> extractOptionalDouble(ModbusRegisterArray raw, int index) {
40 return ModbusBitUtilities.extractStateFromRegisters(raw, index, ValueType.INT16)
41 .map(value -> ((double) value.intValue()) / 10.0).filter(value -> value != (short) 0x8000);
45 * Extract a mandatory double value
47 * @param raw the register array to extract from
48 * @param index the address of the field
49 * @param def the default value
50 * @return the parsed value or the default if the field is not implemented
52 protected Double extractDouble(ModbusRegisterArray raw, int index, double def) {
53 return extractOptionalDouble(raw, index).orElse(def);
57 * Extract an optional int16 value
59 * @param raw the register array to extract from
60 * @param index the address of the field
61 * @return the parsed value or empty if the field is not implemented
63 protected Optional<Short> extractOptionalInt16(ModbusRegisterArray raw, int index) {
64 return ModbusBitUtilities.extractStateFromRegisters(raw, index, ValueType.INT16).map(DecimalType::shortValue)
65 .filter(value -> value != (short) 0x8000);
69 * Extract a mandatory int16 value
71 * @param raw the register array to extract from
72 * @param index the address of the field
73 * @param def the default value
74 * @return the parsed value or the default if the field is not implemented
76 protected Short extractInt16(ModbusRegisterArray raw, int index, short def) {
77 return extractOptionalInt16(raw, index).orElse(def);
81 * Extract an optional uint16 value
83 * @param raw the register array to extract from
84 * @param index the address of the field
85 * @return the parsed value or empty if the field is not implemented
87 protected Optional<Integer> extractOptionalUInt16(ModbusRegisterArray raw, int index) {
88 return ModbusBitUtilities.extractStateFromRegisters(raw, index, ValueType.UINT16).map(DecimalType::intValue)
89 .filter(value -> value != 0xffff);
93 * Extract a mandatory uint16 value
95 * @param raw the register array to extract from
96 * @param index the address of the field
97 * @param def the default value
98 * @return the parsed value or the default if the field is not implemented
100 protected Integer extractUInt16(ModbusRegisterArray raw, int index, int def) {
101 return extractOptionalUInt16(raw, index).orElse(def);
105 * Extract an optional acc32 value
107 * @param raw the register array to extract from
108 * @param index the address of the field
109 * @return the parsed value or empty if the field is not implemented
111 protected Optional<Long> extractOptionalUInt32(ModbusRegisterArray raw, int index) {
112 return ModbusBitUtilities.extractStateFromRegisters(raw, index, ValueType.UINT32).map(DecimalType::longValue)
113 .filter(value -> value != 0);
117 * Extract a mandatory acc32 value
119 * @param raw the register array to extract from
120 * @param index the address of the field
121 * @param def the default value
122 * @return the parsed value or default if the field is not implemented
124 protected Long extractUnit32(ModbusRegisterArray raw, int index, long def) {
125 return extractOptionalUInt32(raw, index).orElse(def);