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