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