2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.networkupstools.internal;
16 import java.util.function.Function;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
20 import javax.measure.Unit;
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;
33 * Supported NUT variables. Any NUT enum members have a complimentary channel definition in the XML thing definition.
35 * @author Hilbrand Bouwkamp - Initial contribution
36 * @see https://github.com/networkupstools/nut/blob/master/docs/nut-names.txt
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),
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),
59 OUTPUT_CURRENT("outputCurrent", "output.current", Units.AMPERE),
60 OUTPUT_VOLTAGE("outputVoltage", "output.voltage", Units.VOLT),
63 BATTERY_CHARGE("batteryCharge", "battery.charge", Units.PERCENT),
64 BATTERY_RUNTIME("batteryRuntime", "battery.runtime", Units.SECOND),
65 BATTERY_VOLTAGE("batteryVoltage", "battery.voltage", Units.VOLT);
67 private static final Map<String, NutName> NUT_NAME_MAP = Stream.of(NutName.values())
68 .collect(Collectors.toMap(NutName::getChannelId, Function.identity()));
70 private final String channelId;
71 private final String name;
72 private final Class<? extends State> stateClass;
73 private final Unit<?> unit;
75 NutName(final String channelId, final String name, final Class<? extends State> stateClass) {
76 this(channelId, name, stateClass, null);
79 NutName(final String channelId, final String name, final Unit<?> unit) {
80 this(channelId, name, QuantityType.class, unit);
83 NutName(final String channelId, final String name, final Class<? extends State> stateClass, final Unit<?> unit) {
84 this.channelId = channelId;
86 this.stateClass = stateClass;
91 * Returns the NUT enum for the given channel id or null if there is no NUT enum available for the given channel.
93 * @param channelId Channel to find the NUT enum for
94 * @return The NUT enum or null if there is none.
96 public static @Nullable NutName channelIdToNutName(final String channelId) {
97 return NUT_NAME_MAP.get(channelId);
101 * Returns the {@link State} value of the variable for this NUT as is found in the given map of variables.
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.
107 public static State toState(final String channelId, final Map<String, String> variables) {
108 final NutName nutName = channelIdToNutName(channelId);
110 if (nutName instanceof NutName) {
111 return nutName.toState(variables);
113 throw new IllegalArgumentException("Channel name '" + channelId + "'is not a known data name");
118 * Returns the {@link State} value of the variable for this NUT as is found in the given map of variables.
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.
123 public State toState(final @Nullable Map<String, String> variables) {
125 final String value = variables == null ? null : variables.get(name);
128 state = UnDefType.UNDEF;
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);
139 state = UnDefType.UNDEF;
146 * @return The name of the Channel for this NUT variable as specified in the Thing
148 String getChannelId() {
153 * @return The variable name as used by the NUT server
160 * @return The {@link State} class type of this NUT variable
162 Class<? extends State> getStateType() {
167 * @return The {@link Unit} for this NUT variable if the type is a {@link QuantityType}