]> git.basschouten.com Git - openhab-addons.git/blob
9aade0851b0ed5c6da9b44adda061a499df7e018
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.enocean.internal.eep;
14
15 import javax.measure.quantity.Energy;
16
17 import org.openhab.binding.enocean.internal.config.EnOceanChannelTotalusageConfig;
18 import org.openhab.core.config.core.Configuration;
19 import org.openhab.core.library.types.QuantityType;
20 import org.openhab.core.library.unit.Units;
21 import org.openhab.core.types.State;
22 import org.openhab.core.types.UnDefType;
23
24 /**
25  *
26  * @author Dominik Vorreiter - initial contribution
27  *
28  */
29 public abstract class EEPHelper {
30     public static State validateTotalUsage(State value, State currentState, Configuration config) {
31         EnOceanChannelTotalusageConfig c = config.as(EnOceanChannelTotalusageConfig.class);
32
33         if (c.validateValue && (value instanceof QuantityType) && (currentState instanceof QuantityType)) {
34             @SuppressWarnings("unchecked")
35             QuantityType<Energy> newValue = value.as(QuantityType.class);
36
37             if (newValue != null) {
38                 newValue = newValue.toUnit(Units.KILOWATT_HOUR);
39             }
40
41             @SuppressWarnings("unchecked")
42             QuantityType<Energy> oldValue = currentState.as(QuantityType.class);
43
44             if (oldValue != null) {
45                 oldValue = oldValue.toUnit(Units.KILOWATT_HOUR);
46             }
47
48             if ((newValue != null) && (oldValue != null)) {
49                 if (newValue.compareTo(oldValue) < 0) {
50                     if ((oldValue.subtract(newValue).doubleValue() < 1.0)) {
51                         return UnDefType.UNDEF;
52                     }
53                 } else {
54                     if (newValue.subtract(oldValue).doubleValue() > 10.0) {
55                         return UnDefType.UNDEF;
56                     }
57                 }
58             }
59         }
60
61         return value;
62     }
63 }