]> git.basschouten.com Git - openhab-addons.git/blob
93edfc1c3d16a4cffd58e30f69e4d89ea07cf478
[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         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         if (max != null && newValue.compareTo(max) == 1) {
69             logger.trace("Number not accepted as it is above the configured maximum");
70             return false;
71         }
72
73         return true;
74     }
75
76     @Override
77     public String getMQTTpublishValue(@Nullable String pattern) {
78         if (state == UnDefType.UNDEF) {
79             return "";
80         }
81
82         String formatPattern = pattern;
83         if (formatPattern == null) {
84             formatPattern = "%s";
85         }
86
87         return state.format(formatPattern);
88     }
89
90     @Override
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)) {
96                 return;
97             }
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);
102             } else {
103                 newValue = oldvalue.toBigDecimal().subtract(step);
104             }
105             if (!checkConditions(newValue, oldvalue)) {
106                 return;
107             }
108             state = new DecimalType(newValue);
109         } else if (command instanceof QuantityType<?>) {
110             QuantityType<?> qType = (QuantityType<?>) command;
111
112             if (qType.getUnit().isCompatible(AbstractUnit.ONE)) {
113                 newValue = qType.toBigDecimal();
114             } else {
115                 qType = qType.toUnit(unit);
116                 if (qType != null) {
117                     newValue = qType.toBigDecimal();
118                 }
119             }
120             if (newValue != null) {
121                 if (!checkConditions(newValue, oldvalue)) {
122                     return;
123                 }
124                 state = new DecimalType(newValue);
125             }
126         } else {
127             newValue = new BigDecimal(command.toString());
128             if (!checkConditions(newValue, oldvalue)) {
129                 return;
130             }
131             state = new DecimalType(newValue);
132         }
133     }
134
135     @Override
136     public StateDescriptionFragmentBuilder createStateDescription(boolean readOnly) {
137         StateDescriptionFragmentBuilder builder = super.createStateDescription(readOnly);
138         BigDecimal max = this.max;
139         if (max != null) {
140             builder = builder.withMaximum(max);
141         }
142         BigDecimal min = this.min;
143         if (min != null) {
144             builder = builder.withMinimum(min);
145         }
146         builder = builder.withStep(step);
147         if (this.unit.length() > 0) {
148             builder = builder.withPattern("%s " + this.unit.replace("%", "%%"));
149         }
150         return builder;
151     }
152 }