]> git.basschouten.com Git - openhab-addons.git/blob
10444493841baa6153b32f6dffb8cc92a3780762
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.binding.modbus.stiebeleltron.internal.parser;
14
15 import java.util.Optional;
16
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;
22
23 /**
24  * Base class for parsers with some helper methods
25  *
26  * @author Nagy Attila Gabor - Initial contribution
27  * @author Paul Frank - Added more methods
28  */
29 @NonNullByDefault
30 public class AbstractBaseParser {
31
32     /**
33      * Extract an optional double value
34      *
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
38      */
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);
42     }
43
44     /**
45      * Extract a mandatory double value
46      *
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
51      */
52     protected Double extractDouble(ModbusRegisterArray raw, int index, double def) {
53         return extractOptionalDouble(raw, index).orElse(def);
54     }
55
56     /**
57      * Extract an optional int16 value
58      *
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
62      */
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);
66     }
67
68     /**
69      * Extract a mandatory int16 value
70      *
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
75      */
76     protected Short extractInt16(ModbusRegisterArray raw, int index, short def) {
77         return extractOptionalInt16(raw, index).orElse(def);
78     }
79
80     /**
81      * Extract an optional uint16 value
82      *
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
86      */
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);
90     }
91
92     /**
93      * Extract a mandatory uint16 value
94      *
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
99      */
100     protected Integer extractUInt16(ModbusRegisterArray raw, int index, int def) {
101         return extractOptionalUInt16(raw, index).orElse(def);
102     }
103
104     /**
105      * Extract an optional acc32 value
106      *
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
110      */
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);
114     }
115
116     /**
117      * Extract a mandatory acc32 value
118      *
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
123      */
124     protected Long extractUnit32(ModbusRegisterArray raw, int index, long def) {
125         return extractOptionalUInt32(raw, index).orElse(def);
126     }
127 }