]> git.basschouten.com Git - openhab-addons.git/blob
d26a313e2001b45f6047d7c24275cdc6d7a1361d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.stream.Collectors;
17 import java.util.stream.Stream;
18
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;
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 String unit;
51
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()));
56         this.min = min;
57         this.max = max;
58         this.step = step == null ? BigDecimal.ONE : step;
59         this.unit = unit == null ? "" : unit;
60     }
61
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");
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         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)) {
97                 return;
98             }
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);
103             } else {
104                 newValue = oldvalue.toBigDecimal().subtract(step);
105             }
106             if (!checkConditions(newValue, oldvalue)) {
107                 return;
108             }
109             state = new DecimalType(newValue);
110         } else if (command instanceof QuantityType<?>) {
111             QuantityType<?> qType = (QuantityType<?>) command;
112
113             if (qType.getUnit().isCompatible(Units.ONE)) {
114                 newValue = qType.toBigDecimal();
115             } else {
116                 qType = qType.toUnit(unit);
117                 if (qType != null) {
118                     newValue = qType.toBigDecimal();
119                 }
120             }
121             if (newValue != null) {
122                 if (!checkConditions(newValue, oldvalue)) {
123                     return;
124                 }
125                 state = new DecimalType(newValue);
126             }
127         } else {
128             newValue = new BigDecimal(command.toString());
129             if (!checkConditions(newValue, oldvalue)) {
130                 return;
131             }
132             state = new DecimalType(newValue);
133         }
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         builder = builder.withStep(step);
148         if (this.unit.length() > 0) {
149             builder = builder.withPattern("%s " + this.unit.replace("%", "%%"));
150         }
151         return builder;
152     }
153 }