]> git.basschouten.com Git - openhab-addons.git/blob
d9ce0749a1f2470dde4891b5807993ffecd6b300
[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.persistence.influxdb.internal;
14
15 import java.math.BigDecimal;
16 import java.time.Instant;
17 import java.time.ZonedDateTime;
18 import java.util.TimeZone;
19
20 import javax.measure.Unit;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.items.GroupItem;
25 import org.openhab.core.items.Item;
26 import org.openhab.core.items.ItemNotFoundException;
27 import org.openhab.core.items.ItemRegistry;
28 import org.openhab.core.library.items.ColorItem;
29 import org.openhab.core.library.items.ContactItem;
30 import org.openhab.core.library.items.DateTimeItem;
31 import org.openhab.core.library.items.DimmerItem;
32 import org.openhab.core.library.items.LocationItem;
33 import org.openhab.core.library.items.NumberItem;
34 import org.openhab.core.library.items.RollershutterItem;
35 import org.openhab.core.library.items.SwitchItem;
36 import org.openhab.core.library.types.DateTimeType;
37 import org.openhab.core.library.types.DecimalType;
38 import org.openhab.core.library.types.HSBType;
39 import org.openhab.core.library.types.OnOffType;
40 import org.openhab.core.library.types.OpenClosedType;
41 import org.openhab.core.library.types.PercentType;
42 import org.openhab.core.library.types.PointType;
43 import org.openhab.core.library.types.QuantityType;
44 import org.openhab.core.library.types.StringType;
45 import org.openhab.core.types.State;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * Conversion logic between openHAB {@link State} types and InfluxDB store types
51  *
52  * @author Joan Pujol Espinar - Initial contribution, based on previous work from Theo Weiss and Dominik Vorreiter
53  */
54 @NonNullByDefault
55 public class InfluxDBStateConvertUtils {
56     static final Number DIGITAL_VALUE_OFF = 0; // Visible for testing
57     static final Number DIGITAL_VALUE_ON = 1; // Visible for testing
58     private static final Logger LOGGER = LoggerFactory.getLogger(InfluxDBStateConvertUtils.class);
59
60     /**
61      * Converts {@link State} to objects fitting into influxdb values.
62      *
63      * @param state to be converted
64      * @return integer or double value for DecimalType, 0 or 1 for OnOffType and OpenClosedType,
65      *         integer for DateTimeType, String for all others
66      */
67     public static Object stateToObject(State state) {
68         Object value;
69         if (state instanceof HSBType) {
70             value = state.toString();
71         } else if (state instanceof PointType) {
72             value = state.toString();
73         } else if (state instanceof DecimalType) {
74             value = ((DecimalType) state).toBigDecimal();
75         } else if (state instanceof QuantityType<?>) {
76             value = ((QuantityType<?>) state).toBigDecimal();
77         } else if (state instanceof OnOffType) {
78             value = state == OnOffType.ON ? DIGITAL_VALUE_ON : DIGITAL_VALUE_OFF;
79         } else if (state instanceof OpenClosedType) {
80             value = state == OpenClosedType.OPEN ? DIGITAL_VALUE_ON : DIGITAL_VALUE_OFF;
81         } else if (state instanceof DateTimeType) {
82             value = ((DateTimeType) state).getZonedDateTime().toInstant().toEpochMilli();
83         } else {
84             value = state.toString();
85         }
86         return value;
87     }
88
89     /**
90      * Converts a value to a {@link State} which is suitable for the given {@link Item}. This is
91      * needed for querying an {@link InfluxDBHistoricItem}.
92      *
93      * @param value to be converted to a {@link State}
94      * @param itemName name of the {@link Item} to get the {@link State} for
95      * @return the state of the item represented by the itemName parameter, else the string value of
96      *         the Object parameter
97      */
98     public static State objectToState(Object value, String itemName, ItemRegistry itemRegistry) {
99         try {
100             Item item = itemRegistry.getItem(itemName);
101             return objectToState(value, item);
102         } catch (ItemNotFoundException e) {
103             LOGGER.info("Could not find item '{}' in registry", itemName);
104         }
105
106         return new StringType(String.valueOf(value));
107     }
108
109     public static State objectToState(@Nullable Object value, Item itemToSetState) {
110         String valueStr = String.valueOf(value);
111
112         @Nullable
113         Item item = itemToSetState;
114         if (item instanceof GroupItem) {
115             item = ((GroupItem) item).getBaseItem();
116         }
117         if (item instanceof ColorItem) {
118             return new HSBType(valueStr);
119         } else if (item instanceof LocationItem) {
120             return new PointType(valueStr);
121         } else if (item instanceof NumberItem numberItem) {
122             Unit<?> unit = numberItem.getUnit();
123             if (unit == null) {
124                 return new DecimalType(valueStr);
125             } else {
126                 return new QuantityType<>(new BigDecimal(valueStr), unit);
127             }
128         } else if (item instanceof DimmerItem) {
129             return new PercentType(valueStr);
130         } else if (item instanceof SwitchItem) {
131             return OnOffType.from(toBoolean(valueStr));
132         } else if (item instanceof ContactItem) {
133             return toBoolean(valueStr) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
134         } else if (item instanceof RollershutterItem) {
135             return new PercentType(valueStr);
136         } else if (item instanceof DateTimeItem) {
137             Instant i = Instant.ofEpochMilli(new BigDecimal(valueStr).longValue());
138             ZonedDateTime z = ZonedDateTime.ofInstant(i, TimeZone.getDefault().toZoneId());
139             return new DateTimeType(z);
140         } else {
141             return new StringType(valueStr);
142         }
143     }
144
145     private static boolean toBoolean(@Nullable Object object) {
146         if (object instanceof Boolean) {
147             return (Boolean) object;
148         } else if (object != null) {
149             if ("1".equals(object) || "1.0".equals(object)) {
150                 return true;
151             } else {
152                 return Boolean.parseBoolean(String.valueOf(object));
153             }
154         } else {
155             return false;
156         }
157     }
158 }