]> git.basschouten.com Git - openhab-addons.git/blob
04c6bcacd23990b40501f1182caa5e1873066b77
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.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;
31
32 import tec.uom.se.AbstractUnit;
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 String unit;
52
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()));
57         this.min = min;
58         this.max = max;
59         this.step = step == null ? BigDecimal.ONE : step;
60         this.unit = unit == null ? "" : unit;
61     }
62
63     protected boolean checkConditions(BigDecimal newValue, DecimalType oldvalue) {
64         BigDecimal min = this.min;
65         if (min != null && newValue.compareTo(min) == -1) {
66             logger.trace("Number not accepted as it is below the configured minimum");
67             return false;
68         }
69         BigDecimal max = this.max;
70         if (max != null && newValue.compareTo(max) == 1) {
71             logger.trace("Number not accepted as it is above the configured maximum");
72             return false;
73         }
74
75         return true;
76     }
77
78     @Override
79     public String getMQTTpublishValue(@Nullable String pattern) {
80         if (state == UnDefType.UNDEF) {
81             return "";
82         }
83
84         String formatPattern = pattern;
85         if (formatPattern == null) {
86             formatPattern = "%s";
87         }
88
89         return state.format(formatPattern);
90     }
91
92     @Override
93     public void update(Command command) throws IllegalArgumentException {
94         DecimalType oldvalue = (state == UnDefType.UNDEF) ? new DecimalType() : (DecimalType) state;
95         BigDecimal newValue = null;
96         if (command instanceof DecimalType) {
97             if (!checkConditions(((DecimalType) command).toBigDecimal(), oldvalue)) {
98                 return;
99             }
100             state = (DecimalType) command;
101         } else if (command instanceof IncreaseDecreaseType || command instanceof UpDownType) {
102             if (command == IncreaseDecreaseType.INCREASE || command == UpDownType.UP) {
103                 newValue = oldvalue.toBigDecimal().add(step);
104             } else {
105                 newValue = oldvalue.toBigDecimal().subtract(step);
106             }
107             if (!checkConditions(newValue, oldvalue)) {
108                 return;
109             }
110             state = new DecimalType(newValue);
111         } else if (command instanceof QuantityType<?>) {
112             QuantityType<?> qType = (QuantityType<?>) command;
113
114             if (qType.getUnit().isCompatible(AbstractUnit.ONE)) {
115                 newValue = qType.toBigDecimal();
116             } else {
117                 qType = qType.toUnit(unit);
118                 if (qType != null) {
119                     newValue = qType.toBigDecimal();
120                 }
121             }
122             if (newValue != null) {
123                 if (!checkConditions(newValue, oldvalue)) {
124                     return;
125                 }
126                 state = new DecimalType(newValue);
127             }
128         } else {
129             newValue = new BigDecimal(command.toString());
130             if (!checkConditions(newValue, oldvalue)) {
131                 return;
132             }
133             state = new DecimalType(newValue);
134         }
135     }
136
137     @Override
138     public StateDescriptionFragmentBuilder createStateDescription(boolean readOnly) {
139         StateDescriptionFragmentBuilder builder = super.createStateDescription(readOnly);
140         BigDecimal max = this.max;
141         if (max != null) {
142             builder = builder.withMaximum(max);
143         }
144         BigDecimal min = this.min;
145         if (min != null) {
146             builder = builder.withMinimum(min);
147         }
148         builder = builder.withStep(step);
149         if (this.unit.length() > 0) {
150             builder = builder.withPattern("%s " + this.unit.replace("%", "%%"));
151         }
152         return builder;
153     }
154 }