]> git.basschouten.com Git - openhab-addons.git/blob
f0453f8f9c494689fdb1cafab4638b4ecfa2b477
[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
69     static final Map<String, NutName> NUT_NAME_MAP = Stream.of(NutName.values())
70             .collect(Collectors.toMap(NutName::getChannelId, Function.identity()));
71
72     private final String channelId;
73     private final String name;
74     private final Class<? extends State> stateClass;
75     // unit only as a value if using a QuantityType.
76     private final @NonNullByDefault({}) Unit<?> unit;
77
78     NutName(final String channelId, final String name, final Class<? extends State> stateClass) {
79         this(channelId, name, stateClass, null);
80     }
81
82     NutName(final String channelId, final String name, final Unit<?> unit) {
83         this(channelId, name, QuantityType.class, unit);
84     }
85
86     NutName(final String channelId, final String name, final Class<? extends State> stateClass,
87             final @Nullable Unit<?> unit) {
88         this.channelId = channelId;
89         this.name = name;
90         this.stateClass = stateClass;
91         this.unit = unit;
92     }
93
94     /**
95      * Returns the NUT enum for the given channel id or null if there is no NUT enum available for the given channel.
96      *
97      * @param channelId Channel to find the NUT enum for
98      * @return The NUT enum or null if there is none.
99      */
100     public static @Nullable NutName channelIdToNutName(final String channelId) {
101         return NUT_NAME_MAP.get(channelId);
102     }
103
104     /**
105      * Returns the {@link State} value of the variable for this NUT as is found in the given map of variables.
106      *
107      * @param channelId
108      * @param variables Map of variables that contain a value for this NUT (or doesn't contain it if not available)
109      * @return The {@link State} value or UNDEF if not available in the variables map or if it can't be determined.
110      */
111     public static State toState(final String channelId, final Map<String, String> variables) {
112         final NutName nutName = channelIdToNutName(channelId);
113
114         if (nutName instanceof NutName) {
115             return nutName.toState(variables);
116         } else {
117             throw new IllegalArgumentException("Channel name '" + channelId + "'is not a known data name");
118         }
119     }
120
121     /**
122      * Returns the {@link State} value of the variable for this NUT as is found in the given map of variables.
123      *
124      * @param variables Map of variables that contain a value for this NUT (or doesn't contain it if not available)
125      * @return The {@link State} value or UNDEF if not available in the variables map or if it can't be determined.
126      */
127     public State toState(final @Nullable Map<String, String> variables) {
128         final State state;
129         final String value = variables == null ? null : variables.get(name);
130
131         if (value == null) {
132             state = UnDefType.UNDEF;
133         } else {
134             if (stateClass == StringType.class) {
135                 state = StringType.valueOf(value);
136             } else if (stateClass == DecimalType.class) {
137                 state = DecimalType.valueOf(value);
138             } else if (stateClass == PercentType.class) {
139                 state = PercentType.valueOf(value);
140             } else if (stateClass == QuantityType.class) {
141                 state = QuantityType.valueOf(Double.valueOf(value), unit);
142             } else {
143                 state = UnDefType.UNDEF;
144             }
145         }
146         return state;
147     }
148
149     /**
150      * @return The name of the Channel for this NUT variable as specified in the Thing
151      */
152     String getChannelId() {
153         return channelId;
154     }
155
156     /**
157      * @return The variable name as used by the NUT server
158      */
159     String getName() {
160         return name;
161     }
162
163     /**
164      * @return The {@link State} class type of this NUT variable
165      */
166     Class<? extends State> getStateType() {
167         return stateClass;
168     }
169
170     /**
171      * @return The {@link Unit} for this NUT variable if the type is a {@link QuantityType}
172      */
173     Unit<?> getUnit() {
174         return unit;
175     }
176 }