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