]> git.basschouten.com Git - openhab-addons.git/blob
b6862b9ec7c94acc8de13de7ae9341ba4aa56b45
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.io.homekit.internal.accessories;
14
15 import java.math.BigDecimal;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.items.GroupItem;
20 import org.openhab.core.items.Item;
21 import org.openhab.core.library.items.ContactItem;
22 import org.openhab.core.library.items.NumberItem;
23 import org.openhab.core.library.items.StringItem;
24 import org.openhab.core.library.items.SwitchItem;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.OpenClosedType;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.types.StringType;
30 import org.openhab.core.types.State;
31 import org.openhab.io.homekit.internal.HomekitOHItemProxy;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Wraps either a SwitchItem or a ContactItem, interpreting the open / closed states accordingly.
37  *
38  * @author Tim Harper - Initial contribution
39  *
40  */
41 @NonNullByDefault
42 public class BooleanItemReader {
43     private final Item item;
44     private final OnOffType trueOnOffValue;
45     private final OpenClosedType trueOpenClosedValue;
46     private final Logger logger = LoggerFactory.getLogger(BooleanItemReader.class);
47     private final @Nullable BigDecimal trueThreshold;
48     private final boolean invertThreshold;
49
50     /**
51      *
52      * @param item The item to read
53      * @param trueOnOffValue If OnOffType, then consider true if this value
54      * @param trueOpenClosedValue if OpenClosedType, then consider true if this value
55      */
56     BooleanItemReader(Item item, OnOffType trueOnOffValue, OpenClosedType trueOpenClosedValue) {
57         this(item, trueOnOffValue, trueOpenClosedValue, null, false);
58     }
59
60     /**
61      *
62      * @param item The item to read
63      * @param trueOnOffValue If OnOffType, then consider true if this value
64      * @param trueOpenClosedValue if OpenClosedType, then consider true if this value
65      * @param trueThreshold If the state is numeric, and this param is given, return true if the value is above this
66      *            threshold
67      * @param invertThreshold Invert threshold to be true if below, not above
68      */
69     BooleanItemReader(Item item, OnOffType trueOnOffValue, OpenClosedType trueOpenClosedValue,
70             @Nullable BigDecimal trueThreshold, boolean invertThreshold) {
71         this.item = item;
72         this.trueOnOffValue = trueOnOffValue;
73         this.trueOpenClosedValue = trueOpenClosedValue;
74         final Item baseItem = HomekitOHItemProxy.getBaseItem(item);
75         this.trueThreshold = trueThreshold;
76         this.invertThreshold = invertThreshold;
77         if (!(baseItem instanceof SwitchItem || baseItem instanceof ContactItem || baseItem instanceof StringItem
78                 || (trueThreshold != null && baseItem instanceof NumberItem))) {
79             if (trueThreshold != null) {
80                 logger.warn("Item {} is a {} instead of the expected SwitchItem, ContactItem, NumberItem or StringItem",
81                         item.getName(), item.getClass().getName());
82             } else {
83                 logger.warn("Item {} is a {} instead of the expected SwitchItem, ContactItem or StringItem",
84                         item.getName(), item.getClass().getName());
85             }
86         }
87     }
88
89     boolean getValue() {
90         final State state = item.getState();
91         final BigDecimal localTrueThresheold = trueThreshold;
92         if (state instanceof OnOffType) {
93             return state.equals(trueOnOffValue);
94         } else if (state instanceof OpenClosedType) {
95             return state.equals(trueOpenClosedValue);
96         } else if (state instanceof StringType) {
97             return state.toString().equalsIgnoreCase("Open") || state.toString().equalsIgnoreCase("Opened");
98         } else if (localTrueThresheold != null) {
99             if (state instanceof DecimalType) {
100                 final boolean result = ((DecimalType) state).toBigDecimal().compareTo(localTrueThresheold) > 0;
101                 return result ^ invertThreshold;
102             } else if (state instanceof QuantityType) {
103                 final boolean result = ((QuantityType<?>) state).toBigDecimal().compareTo(localTrueThresheold) > 0;
104                 return result ^ invertThreshold;
105             }
106         }
107         logger.debug("Unexpected item state,  returning false. Item {}, State {}", item.getName(), state);
108         return false;
109     }
110
111     private OnOffType getOffValue(OnOffType onValue) {
112         return onValue == OnOffType.ON ? OnOffType.OFF : OnOffType.ON;
113     }
114
115     void setValue(Boolean value) {
116         if (item instanceof SwitchItem) {
117             ((SwitchItem) item).send(value ? trueOnOffValue : getOffValue(trueOnOffValue));
118         } else if (item instanceof GroupItem) {
119             ((GroupItem) item).send(value ? trueOnOffValue : getOffValue(trueOnOffValue));
120         } else {
121             logger.debug("Cannot set value {} for item {}. Only Switch and Group items are supported.", value, item);
122         }
123     }
124 }