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 org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.library.CoreItemFactory;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.IncreaseDecreaseType;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.types.UpDownType;
26 import org.openhab.core.library.unit.Units;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.StateDescriptionFragmentBuilder;
29 import org.openhab.core.types.UnDefType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Implements a number value.
36 * If min / max limits are set, values below / above are (almost) silently ignored.
39 * Accepts user updates and MQTT state updates from a DecimalType, IncreaseDecreaseType and UpDownType.
42 * @author David Graeff - Initial contribution
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 String unit;
52 public NumberValue(@Nullable BigDecimal min, @Nullable BigDecimal max, @Nullable BigDecimal step,
53 @Nullable String unit) {
54 super(CoreItemFactory.NUMBER, Stream.of(QuantityType.class, IncreaseDecreaseType.class, UpDownType.class)
55 .collect(Collectors.toList()));
58 this.step = step == null ? BigDecimal.ONE : step;
59 this.unit = unit == null ? "" : unit;
62 protected boolean checkConditions(BigDecimal newValue, DecimalType oldvalue) {
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");
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");
78 public String getMQTTpublishValue(@Nullable String pattern) {
79 if (state == UnDefType.UNDEF) {
83 String formatPattern = pattern;
84 if (formatPattern == null) {
88 return state.format(formatPattern);
92 public void update(Command command) throws IllegalArgumentException {
93 DecimalType oldvalue = (state == UnDefType.UNDEF) ? new DecimalType() : (DecimalType) state;
94 BigDecimal newValue = null;
95 if (command instanceof DecimalType) {
96 if (!checkConditions(((DecimalType) command).toBigDecimal(), oldvalue)) {
99 state = (DecimalType) command;
100 } else if (command instanceof IncreaseDecreaseType || command instanceof UpDownType) {
101 if (command == IncreaseDecreaseType.INCREASE || command == UpDownType.UP) {
102 newValue = oldvalue.toBigDecimal().add(step);
104 newValue = oldvalue.toBigDecimal().subtract(step);
106 if (!checkConditions(newValue, oldvalue)) {
109 state = new DecimalType(newValue);
110 } else if (command instanceof QuantityType<?>) {
111 QuantityType<?> qType = (QuantityType<?>) command;
113 if (qType.getUnit().isCompatible(Units.ONE)) {
114 newValue = qType.toBigDecimal();
116 qType = qType.toUnit(unit);
118 newValue = qType.toBigDecimal();
121 if (newValue != null) {
122 if (!checkConditions(newValue, oldvalue)) {
125 state = new DecimalType(newValue);
128 newValue = new BigDecimal(command.toString());
129 if (!checkConditions(newValue, oldvalue)) {
132 state = new DecimalType(newValue);
137 public StateDescriptionFragmentBuilder createStateDescription(boolean readOnly) {
138 StateDescriptionFragmentBuilder builder = super.createStateDescription(readOnly);
139 BigDecimal max = this.max;
141 builder = builder.withMaximum(max);
143 BigDecimal min = this.min;
145 builder = builder.withMinimum(min);
147 builder = builder.withStep(step);
148 if (this.unit.length() > 0) {
149 builder = builder.withPattern("%s " + this.unit.replace("%", "%%"));