]> git.basschouten.com Git - openhab-addons.git/blob
e653d1f3cce9a450479e0cfc06c1763360b938a7
[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.sunspec.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  *
28  */
29 @NonNullByDefault
30 public class AbstractBaseParser {
31
32     /**
33      * Extract an optional int16 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<Short> extractOptionalInt16(ModbusRegisterArray raw, int index) {
40         return ModbusBitUtilities.extractStateFromRegisters(raw, index, ValueType.INT16).map(DecimalType::shortValue)
41                 .filter(value -> value != (short) 0x8000);
42     }
43
44     /**
45      * Extract a mandatory int16 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 Short extractInt16(ModbusRegisterArray raw, int index, short def) {
53         return extractOptionalInt16(raw, index).orElse(def);
54     }
55
56     /**
57      * Extract an optional uint16 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<Integer> extractOptionalUInt16(ModbusRegisterArray raw, int index) {
64         return ModbusBitUtilities.extractStateFromRegisters(raw, index, ValueType.UINT16).map(DecimalType::intValue)
65                 .filter(value -> value != 0xffff);
66     }
67
68     /**
69      * Extract a mandatory uint16 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 Integer extractUInt16(ModbusRegisterArray raw, int index, int def) {
77         return extractOptionalUInt16(raw, index).orElse(def);
78     }
79
80     /**
81      * Extract an optional acc32 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<Long> extractOptionalAcc32(ModbusRegisterArray raw, int index) {
88         return ModbusBitUtilities.extractStateFromRegisters(raw, index, ValueType.INT32).map(DecimalType::longValue)
89                 .filter(value -> value != 0);
90     }
91
92     /**
93      * Extract a mandatory acc32 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 default if the field is not implemented
99      */
100     protected Long extractAcc32(ModbusRegisterArray raw, int index, long def) {
101         return extractOptionalAcc32(raw, index).orElse(def);
102     }
103
104     /**
105      * Extract an optional scale factor
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<Short> extractOptionalSunSSF(ModbusRegisterArray raw, int index) {
112         return ModbusBitUtilities.extractStateFromRegisters(raw, index, ValueType.INT16).map(DecimalType::shortValue)
113                 .filter(value -> value != (short) 0x8000);
114     }
115
116     /**
117      * Extract an mandatory scale factor
118      *
119      * @param raw the register array to extract from
120      * @param index the address of the field
121      * @return the parsed value or 1 if the field is not implemented
122      */
123     protected Short extractSunSSF(ModbusRegisterArray raw, int index) {
124         return extractOptionalSunSSF(raw, index).orElse((short) 0);
125     }
126 }