2 * Copyright (c) 2010-2020 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.types.Command;
27 import org.openhab.core.types.StateDescriptionFragmentBuilder;
28 import org.openhab.core.types.UnDefType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import tec.uom.se.AbstractUnit;
35 * Implements a number value.
37 * If min / max limits are set, values below / above are (almost) silently ignored.
40 * Accepts user updates and MQTT state updates from a DecimalType, IncreaseDecreaseType and UpDownType.
43 * @author David Graeff - Initial contribution
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 String unit;
53 public NumberValue(@Nullable BigDecimal min, @Nullable BigDecimal max, @Nullable BigDecimal step,
54 @Nullable String unit) {
55 super(CoreItemFactory.NUMBER, Stream.of(QuantityType.class, IncreaseDecreaseType.class, UpDownType.class)
56 .collect(Collectors.toList()));
59 this.step = step == null ? BigDecimal.ONE : step;
60 this.unit = unit == null ? "" : unit;
63 protected boolean checkConditions(BigDecimal newValue, DecimalType oldvalue) {
64 if (min != null && newValue.compareTo(min) == -1) {
65 logger.trace("Number not accepted as it is below the configured minimum");
68 if (max != null && newValue.compareTo(max) == 1) {
69 logger.trace("Number not accepted as it is above the configured maximum");
77 public String getMQTTpublishValue(@Nullable String pattern) {
78 if (state == UnDefType.UNDEF) {
82 String formatPattern = pattern;
83 if (formatPattern == null) {
87 return state.format(formatPattern);
91 public void update(Command command) throws IllegalArgumentException {
92 DecimalType oldvalue = (state == UnDefType.UNDEF) ? new DecimalType() : (DecimalType) state;
93 BigDecimal newValue = null;
94 if (command instanceof DecimalType) {
95 if (!checkConditions(((DecimalType) command).toBigDecimal(), oldvalue)) {
98 state = (DecimalType) command;
99 } else if (command instanceof IncreaseDecreaseType || command instanceof UpDownType) {
100 if (command == IncreaseDecreaseType.INCREASE || command == UpDownType.UP) {
101 newValue = oldvalue.toBigDecimal().add(step);
103 newValue = oldvalue.toBigDecimal().subtract(step);
105 if (!checkConditions(newValue, oldvalue)) {
108 state = new DecimalType(newValue);
109 } else if (command instanceof QuantityType<?>) {
110 QuantityType<?> qType = (QuantityType<?>) command;
112 if (qType.getUnit().isCompatible(AbstractUnit.ONE)) {
113 newValue = qType.toBigDecimal();
115 qType = qType.toUnit(unit);
117 newValue = qType.toBigDecimal();
120 if (newValue != null) {
121 if (!checkConditions(newValue, oldvalue)) {
124 state = new DecimalType(newValue);
127 newValue = new BigDecimal(command.toString());
128 if (!checkConditions(newValue, oldvalue)) {
131 state = new DecimalType(newValue);
136 public StateDescriptionFragmentBuilder createStateDescription(boolean readOnly) {
137 StateDescriptionFragmentBuilder builder = super.createStateDescription(readOnly);
138 BigDecimal max = this.max;
140 builder = builder.withMaximum(max);
142 BigDecimal min = this.min;
144 builder = builder.withMinimum(min);
146 builder = builder.withStep(step);
147 if (this.unit.length() > 0) {
148 builder = builder.withPattern("%s " + this.unit.replace("%", "%%"));