2 * Copyright (c) 2010-2022 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.mqtt.generic.values;
15 import java.math.BigDecimal;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
19 import javax.measure.Unit;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.library.CoreItemFactory;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.library.types.IncreaseDecreaseType;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.library.types.UpDownType;
28 import org.openhab.core.library.unit.Units;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.StateDescriptionFragmentBuilder;
31 import org.openhab.core.types.UnDefType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Implements a number value.
38 * If min / max limits are set, values below / above are (almost) silently ignored.
41 * Accepts user updates and MQTT state updates from a DecimalType, IncreaseDecreaseType and UpDownType.
44 * @author David Graeff - Initial contribution
47 public class NumberValue extends Value {
48 private final Logger logger = LoggerFactory.getLogger(NumberValue.class);
49 private final @Nullable BigDecimal min;
50 private final @Nullable BigDecimal max;
51 private final BigDecimal step;
52 private final Unit<?> unit;
54 public NumberValue(@Nullable BigDecimal min, @Nullable BigDecimal max, @Nullable BigDecimal step,
55 @Nullable Unit<?> unit) {
56 super(CoreItemFactory.NUMBER, Stream.of(QuantityType.class, IncreaseDecreaseType.class, UpDownType.class)
57 .collect(Collectors.toList()));
60 this.step = step == null ? BigDecimal.ONE : step;
61 this.unit = unit != null ? unit : Units.ONE;
64 protected boolean checkConditions(BigDecimal newValue) {
65 BigDecimal min = this.min;
66 if (min != null && newValue.compareTo(min) == -1) {
67 logger.trace("Number not accepted as it is below the configured minimum");
70 BigDecimal max = this.max;
71 if (max != null && newValue.compareTo(max) == 1) {
72 logger.trace("Number not accepted as it is above the configured maximum");
80 public String getMQTTpublishValue(@Nullable String pattern) {
81 if (state == UnDefType.UNDEF) {
85 String formatPattern = pattern;
86 if (formatPattern == null) {
90 return state.format(formatPattern);
94 public void update(Command command) throws IllegalArgumentException {
95 BigDecimal newValue = null;
96 if (command instanceof DecimalType) {
97 newValue = ((DecimalType) command).toBigDecimal();
98 } else if (command instanceof IncreaseDecreaseType || command instanceof UpDownType) {
99 BigDecimal oldValue = getOldValue();
100 if (command == IncreaseDecreaseType.INCREASE || command == UpDownType.UP) {
101 newValue = oldValue.add(step);
103 newValue = oldValue.subtract(step);
105 } else if (command instanceof QuantityType<?>) {
106 newValue = getQuantityTypeAsDecimal((QuantityType<?>) command);
108 newValue = new BigDecimal(command.toString());
110 if (!checkConditions(newValue)) {
113 // items with units specified in the label in the UI but no unit on mqtt are stored as
114 // DecimalType to avoid conversions (e.g. % expects 0-1 rather than 0-100)
115 if (!Units.ONE.equals(unit)) {
116 state = new QuantityType<>(newValue, unit);
118 state = new DecimalType(newValue);
122 private BigDecimal getOldValue() {
123 BigDecimal val = BigDecimal.ZERO;
124 if (state instanceof DecimalType) {
125 val = ((DecimalType) state).toBigDecimal();
126 } else if (state instanceof QuantityType<?>) {
127 val = ((QuantityType<?>) state).toBigDecimal();
132 private BigDecimal getQuantityTypeAsDecimal(QuantityType<?> qType) {
133 BigDecimal val = qType.toBigDecimal();
134 if (!qType.getUnit().isCompatible(Units.ONE)) {
135 QuantityType<?> convertedType = qType.toUnit(unit);
136 if (convertedType != null) {
137 val = convertedType.toBigDecimal();
144 public StateDescriptionFragmentBuilder createStateDescription(boolean readOnly) {
145 StateDescriptionFragmentBuilder builder = super.createStateDescription(readOnly);
146 BigDecimal max = this.max;
148 builder = builder.withMaximum(max);
150 BigDecimal min = this.min;
152 builder = builder.withMinimum(min);
154 return builder.withStep(step).withPattern("%s %unit%");