]> git.basschouten.com Git - openhab-addons.git/blob
f65dd125e4036b83bddd390ce307123f38f29dc4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.mqtt.generic.values;
14
15 import java.math.BigDecimal;
16 import java.util.List;
17
18 import javax.measure.Unit;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.library.CoreItemFactory;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.library.types.IncreaseDecreaseType;
25 import org.openhab.core.library.types.QuantityType;
26 import org.openhab.core.library.types.UpDownType;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.StateDescriptionFragmentBuilder;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Implements a number value.
35  *
36  * If min / max limits are set, values below / above are (almost) silently ignored.
37  *
38  * <p>
39  * Accepts user updates and MQTT state updates from a DecimalType, IncreaseDecreaseType and UpDownType.
40  * </p>
41  *
42  * @author David Graeff - Initial contribution
43  */
44 @NonNullByDefault
45 public class NumberValue extends Value {
46     private final Logger logger = LoggerFactory.getLogger(NumberValue.class);
47     private final @Nullable BigDecimal min;
48     private final @Nullable BigDecimal max;
49     private final BigDecimal step;
50     private final Unit<?> unit;
51
52     public NumberValue(@Nullable BigDecimal min, @Nullable BigDecimal max, @Nullable BigDecimal step,
53             @Nullable Unit<?> unit) {
54         super(CoreItemFactory.NUMBER, List.of(QuantityType.class, IncreaseDecreaseType.class, UpDownType.class));
55         this.min = min;
56         this.max = max;
57         this.step = step == null ? BigDecimal.ONE : step;
58         this.unit = unit != null ? unit : Units.ONE;
59     }
60
61     protected boolean checkConditions(BigDecimal newValue) {
62         BigDecimal min = this.min;
63         if (min != null && newValue.compareTo(min) == -1) {
64             logger.trace("Number not accepted as it is below the configured minimum");
65             return false;
66         }
67         BigDecimal max = this.max;
68         if (max != null && newValue.compareTo(max) == 1) {
69             logger.trace("Number not accepted as it is above the configured maximum");
70             return false;
71         }
72
73         return true;
74     }
75
76     @Override
77     public String getMQTTpublishValue(Command command, @Nullable String pattern) {
78         String formatPattern = pattern;
79         if (formatPattern == null) {
80             formatPattern = "%s";
81         }
82
83         return command.format(formatPattern);
84     }
85
86     @Override
87     public Command parseCommand(Command command) throws IllegalArgumentException {
88         BigDecimal newValue = null;
89         if (command instanceof DecimalType decimalCommand) {
90             newValue = decimalCommand.toBigDecimal();
91         } else if (command instanceof IncreaseDecreaseType || command instanceof UpDownType) {
92             BigDecimal oldValue = getOldValue();
93             if (command == IncreaseDecreaseType.INCREASE || command == UpDownType.UP) {
94                 newValue = oldValue.add(step);
95             } else {
96                 newValue = oldValue.subtract(step);
97             }
98         } else if (command instanceof QuantityType<?> quantityCommand) {
99             newValue = getQuantityTypeAsDecimal(quantityCommand);
100         } else {
101             newValue = new BigDecimal(command.toString());
102         }
103         if (!checkConditions(newValue)) {
104             throw new IllegalArgumentException(newValue + " is out of range");
105         }
106         // items with units specified in the label in the UI but no unit on mqtt are stored as
107         // DecimalType to avoid conversions (e.g. % expects 0-1 rather than 0-100)
108         if (!Units.ONE.equals(unit)) {
109             return new QuantityType<>(newValue, unit);
110         } else {
111             return new DecimalType(newValue);
112         }
113     }
114
115     private BigDecimal getOldValue() {
116         BigDecimal val = BigDecimal.ZERO;
117         if (state instanceof DecimalType decimalCommand) {
118             val = decimalCommand.toBigDecimal();
119         } else if (state instanceof QuantityType<?> quantityCommand) {
120             val = quantityCommand.toBigDecimal();
121         }
122         return val;
123     }
124
125     private BigDecimal getQuantityTypeAsDecimal(QuantityType<?> qType) {
126         BigDecimal val = qType.toBigDecimal();
127         if (!qType.getUnit().isCompatible(Units.ONE)) {
128             QuantityType<?> convertedType = qType.toInvertibleUnit(unit);
129             if (convertedType != null) {
130                 val = convertedType.toBigDecimal();
131             }
132         }
133         return val;
134     }
135
136     @Override
137     public StateDescriptionFragmentBuilder createStateDescription(boolean readOnly) {
138         StateDescriptionFragmentBuilder builder = super.createStateDescription(readOnly);
139         BigDecimal max = this.max;
140         if (max != null) {
141             builder = builder.withMaximum(max);
142         }
143         BigDecimal min = this.min;
144         if (min != null) {
145             builder = builder.withMinimum(min);
146         }
147         if (!unit.equals(Units.ONE)) {
148             builder.withPattern("%s " + unit);
149         } else {
150             builder.withPattern("%s %unit%");
151         }
152         return builder.withStep(step);
153     }
154 }