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