]> git.basschouten.com Git - openhab-addons.git/blob
355edeb73286b4a739002f8ac9044a18ac0ea498
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.astro.internal.util;
14
15 import java.lang.reflect.Method;
16 import java.math.BigDecimal;
17 import java.math.RoundingMode;
18 import java.time.ZoneId;
19 import java.time.ZonedDateTime;
20 import java.util.Calendar;
21 import java.util.GregorianCalendar;
22 import java.util.Objects;
23 import java.util.TimeZone;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.astro.internal.config.AstroChannelConfig;
28 import org.openhab.core.library.types.DateTimeType;
29 import org.openhab.core.library.types.DecimalType;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.types.State;
33 import org.openhab.core.types.UnDefType;
34
35 /**
36  * Methods to get the value from a property of an object.
37  *
38  * @author Gerhard Riegler - Initial contribution
39  * @author Erdoan Hadzhiyusein - Adapted the class to work with the new DateTimeType
40  * @author Christoph Weitkamp - Introduced UoM
41  */
42 @NonNullByDefault
43 public class PropertyUtils {
44
45     /** Constructor */
46     private PropertyUtils() {
47         throw new IllegalAccessError("Non-instantiable");
48     }
49
50     /**
51      * Returns the state of the channel.
52      */
53     public static State getState(ChannelUID channelUID, AstroChannelConfig config, Object instance, ZoneId zoneId)
54             throws Exception {
55         Object value = getPropertyValue(channelUID, instance);
56         if (value == null) {
57             return UnDefType.UNDEF;
58         } else if (value instanceof State) {
59             return (State) value;
60         } else if (value instanceof Calendar) {
61             Calendar cal = (Calendar) value;
62             GregorianCalendar gregorianCal = (GregorianCalendar) DateTimeUtils.applyConfig(cal, config);
63             cal.setTimeZone(TimeZone.getTimeZone(zoneId));
64             ZonedDateTime zoned = gregorianCal.toZonedDateTime().withFixedOffsetZone();
65             return new DateTimeType(zoned);
66         } else if (value instanceof Number) {
67             BigDecimal decimalValue = new BigDecimal(value.toString()).setScale(2, RoundingMode.HALF_UP);
68             return new DecimalType(decimalValue);
69         } else if (value instanceof String || value instanceof Enum) {
70             return new StringType(value.toString());
71         } else {
72             throw new IllegalStateException("Unsupported value type " + value.getClass().getSimpleName());
73         }
74     }
75
76     /**
77      * Returns the property value from the object instance, nested properties are possible. If the propertyName is for
78      * example rise.start, the methods getRise().getStart() are called.
79      */
80     public static @Nullable Object getPropertyValue(ChannelUID channelUID, Object instance) throws Exception {
81         String[] properties = channelUID.getId().split("#");
82         return getPropertyValue(instance, properties, 0);
83     }
84
85     /**
86      * Iterates through the nested properties and returns the getter value.
87      */
88     @SuppressWarnings("all")
89     private static @Nullable Object getPropertyValue(Object instance, String[] properties, int nestedIndex)
90             throws Exception {
91         String propertyName = properties[nestedIndex];
92         Method m = instance.getClass().getMethod(toGetterString(propertyName), null);
93         Object result = m.invoke(instance, (Object[]) null);
94         if (nestedIndex + 1 < properties.length) {
95             Objects.requireNonNull(result);
96             return getPropertyValue(result, properties, nestedIndex + 1);
97         }
98         return result;
99     }
100
101     /**
102      * Converts the string to a getter property.
103      */
104     private static String toGetterString(String str) {
105         StringBuilder sb = new StringBuilder();
106         sb.append("get");
107         sb.append(Character.toTitleCase(str.charAt(0)));
108         sb.append(str.substring(1));
109         return sb.toString();
110     }
111 }