]> git.basschouten.com Git - openhab-addons.git/blob
24a86673f9bada06abeb39bad837999a43c6685e
[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.binding.wolfsmartset.internal.handler;
14
15 import java.time.ZonedDateTime;
16 import java.util.Date;
17
18 import javax.measure.Unit;
19 import javax.measure.quantity.Temperature;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.i18n.TimeZoneProvider;
24 import org.openhab.core.library.types.DateTimeType;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.PointType;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.types.StringType;
30 import org.openhab.core.library.unit.ImperialUnits;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.types.State;
34 import org.openhab.core.types.UnDefType;
35
36 /**
37  * The {@link WolfSmartsetUtils} contains utility methods used by the
38  * thing handler and the bridge handler.
39  *
40  * @author Bo Biene - Initial contribution
41  */
42 @NonNullByDefault
43 public final class WolfSmartsetUtils {
44
45     private static final int UNKNOWN_VALUE = -5002;
46
47     /*
48      * Checks to see if a bridge is online.
49      */
50     public static boolean isBridgeOnline(@Nullable Bridge bridge) {
51         boolean bridgeStatus = false;
52         if (bridge != null && bridge.getStatus() == ThingStatus.ONLINE) {
53             bridgeStatus = true;
54         }
55         return bridgeStatus;
56     }
57
58     /*
59      * Set the state to the passed value. If value is null, set the state to UNDEF
60      */
61     public static State undefOrOnOff(@Nullable Boolean value) {
62         return value == null ? UnDefType.UNDEF : (value.booleanValue() ? OnOffType.ON : OnOffType.OFF);
63     }
64
65     public static State undefOrString(@Nullable String value) {
66         return value == null ? UnDefType.UNDEF : new StringType(value);
67     }
68
69     public static State undefOrDecimal(@Nullable Number value) {
70         return (value == null || isUnknown(value)) ? UnDefType.UNDEF : new DecimalType(value.doubleValue());
71     }
72
73     public static State undefOrQuantity(@Nullable Number value, Unit<?> unit) {
74         return (value == null || isUnknown(value)) ? UnDefType.UNDEF : new QuantityType<>(value, unit);
75     }
76
77     public static State undefOrTemperature(@Nullable Number value) {
78         return (value == null || isUnknown(value)) ? UnDefType.UNDEF
79                 : new QuantityType<>(value.doubleValue() / 10.0, ImperialUnits.FAHRENHEIT);
80     }
81
82     public static State undefOrPoint(@Nullable String value) {
83         return value == null ? UnDefType.UNDEF : new PointType(value);
84     }
85
86     public static State undefOrDate(@Nullable Date date, TimeZoneProvider timeZoneProvider) {
87         return date == null ? UnDefType.UNDEF
88                 : new DateTimeType(ZonedDateTime.ofInstant(date.toInstant(), timeZoneProvider.getTimeZone()));
89     }
90
91     private static boolean isUnknown(Number value) {
92         return value.intValue() == UNKNOWN_VALUE;
93     }
94
95     /*
96      * Convert a QuantityType<Temperature> to the internal format used by the WolfSmartset API.
97      */
98     @SuppressWarnings("unchecked")
99     public static Integer convertQuantityTypeToWolfSmartsetTemp(Object value) {
100         if (value instanceof QuantityType<?>) {
101             QuantityType<Temperature> convertedTemp = ((QuantityType<Temperature>) value)
102                     .toUnit(ImperialUnits.FAHRENHEIT);
103             if (convertedTemp != null) {
104                 return Integer.valueOf(convertedTemp.intValue() * 10);
105             }
106         }
107         throw new IllegalArgumentException("temperature is not a QuantityType");
108     }
109 }