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