]> git.basschouten.com Git - openhab-addons.git/blob
78736db72a1d30253829ba4a3d053c29fe9b6fb7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.networkupstools.internal;
14
15 import java.util.Map;
16 import java.util.function.Function;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
19
20 import javax.measure.Unit;
21
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.library.types.PercentType;
25 import org.openhab.core.library.types.QuantityType;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.library.unit.SIUnits;
28 import org.openhab.core.library.unit.Units;
29 import org.openhab.core.types.State;
30 import org.openhab.core.types.UnDefType;
31
32 /**
33  * Supported NUT variables. Any NUT enum members have a complimentary channel definition in the XML thing definition.
34  *
35  * @author Hilbrand Bouwkamp - Initial contribution
36  * @see https://github.com/networkupstools/nut/blob/master/docs/nut-names.txt
37  */
38 enum NutName {
39     // UPS
40     UPS_ALARM("upsAlarm", "ups.alarm", StringType.class),
41     UPS_LOAD("upsLoad", "ups.load", Units.PERCENT),
42     UPS_POWER("upsPower", "ups.power", NUTBindingConstants.VOLT_AMPERE),
43     UPS_REALPOWER("upsRealpower", "ups.realpower", Units.WATT),
44     UPS_STATUS("upsStatus", "ups.status", StringType.class),
45     UPS_TEMPERATURE("upsTemperature", "ups.temperature", SIUnits.CELSIUS),
46     UPS_TEST_RESULT("upsTestResult", "ups.test.result", StringType.class),
47
48     // Input
49     INPUT_CURRENT("inputCurrent", "input.current", Units.AMPERE),
50     INPUT_CURRENT_STATUS("inputCurrentStatus", "input.current.status", StringType.class),
51     INPUT_LOAD("inputLoad", "input.load", Units.PERCENT),
52     INPUT_REALPOWER("inputRealpower", "input.realpower", Units.WATT),
53     INPUT_QUALITY("inputQuality", "input.quality", StringType.class),
54     INPUT_TRANSFER_REASON("inputTransferReason", "input.transfer.reason", StringType.class),
55     INPUT_VOLTAGE("inputVoltage", "input.voltage", Units.VOLT),
56     INPUT_VOLTAGE_STATUS("inputVoltageStatus", "input.voltage.status", StringType.class),
57
58     // Output
59     OUTPUT_CURRENT("outputCurrent", "output.current", Units.AMPERE),
60     OUTPUT_VOLTAGE("outputVoltage", "output.voltage", Units.VOLT),
61
62     // Battery
63     BATTERY_CHARGE("batteryCharge", "battery.charge", Units.PERCENT),
64     BATTERY_RUNTIME("batteryRuntime", "battery.runtime", Units.SECOND),
65     BATTERY_VOLTAGE("batteryVoltage", "battery.voltage", Units.VOLT);
66
67     private static final Map<String, NutName> NUT_NAME_MAP = Stream.of(NutName.values())
68             .collect(Collectors.toMap(NutName::getChannelId, Function.identity()));
69
70     private final String channelId;
71     private final String name;
72     private final Class<? extends State> stateClass;
73     private final Unit<?> unit;
74
75     NutName(final String channelId, final String name, final Class<? extends State> stateClass) {
76         this(channelId, name, stateClass, null);
77     }
78
79     NutName(final String channelId, final String name, final Unit<?> unit) {
80         this(channelId, name, QuantityType.class, unit);
81     }
82
83     NutName(final String channelId, final String name, final Class<? extends State> stateClass, final Unit<?> unit) {
84         this.channelId = channelId;
85         this.name = name;
86         this.stateClass = stateClass;
87         this.unit = unit;
88     }
89
90     /**
91      * Returns the NUT enum for the given channel id or null if there is no NUT enum available for the given channel.
92      *
93      * @param channelId Channel to find the NUT enum for
94      * @return The NUT enum or null if there is none.
95      */
96     public static @Nullable NutName channelIdToNutName(final String channelId) {
97         return NUT_NAME_MAP.get(channelId);
98     }
99
100     /**
101      * Returns the {@link State} value of the variable for this NUT as is found in the given map of variables.
102      *
103      * @param channelId
104      * @param variables Map of variables that contain a value for this NUT (or doesn't contain it if not available)
105      * @return The {@link State} value or UNDEF if not available in the variables map or if it can't be determined.
106      */
107     public static State toState(final String channelId, final Map<String, String> variables) {
108         final NutName nutName = channelIdToNutName(channelId);
109
110         if (nutName instanceof NutName) {
111             return nutName.toState(variables);
112         } else {
113             throw new IllegalArgumentException("Channel name '" + channelId + "'is not a known data name");
114         }
115     }
116
117     /**
118      * Returns the {@link State} value of the variable for this NUT as is found in the given map of variables.
119      *
120      * @param variables Map of variables that contain a value for this NUT (or doesn't contain it if not available)
121      * @return The {@link State} value or UNDEF if not available in the variables map or if it can't be determined.
122      */
123     public State toState(final @Nullable Map<String, String> variables) {
124         final State state;
125         final String value = variables == null ? null : variables.get(name);
126
127         if (value == null) {
128             state = UnDefType.UNDEF;
129         } else {
130             if (stateClass == StringType.class) {
131                 state = StringType.valueOf(value);
132             } else if (stateClass == DecimalType.class) {
133                 state = DecimalType.valueOf(value);
134             } else if (stateClass == PercentType.class) {
135                 state = PercentType.valueOf(value);
136             } else if (stateClass == QuantityType.class) {
137                 state = QuantityType.valueOf(Double.valueOf(value), unit);
138             } else {
139                 state = UnDefType.UNDEF;
140             }
141         }
142         return state;
143     }
144
145     /**
146      * @return The name of the Channel for this NUT variable as specified in the Thing
147      */
148     String getChannelId() {
149         return channelId;
150     }
151
152     /**
153      * @return The variable name as used by the NUT server
154      */
155     String getName() {
156         return name;
157     }
158
159     /**
160      * @return The {@link State} class type of this NUT variable
161      */
162     Class<? extends State> getStateType() {
163         return stateClass;
164     }
165
166     /**
167      * @return The {@link Unit} for this NUT variable if the type is a {@link QuantityType}
168      */
169     Unit<?> getUnit() {
170         return unit;
171     }
172 }