2 * Copyright (c) 2010-2023 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.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;
34 * Supported NUT variables. Any NUT enum members have a complimentary channel definition in the XML thing definition.
36 * @author Hilbrand Bouwkamp - Initial contribution
37 * @see https://github.com/networkupstools/nut/blob/master/docs/nut-names.txt
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),
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),
61 OUTPUT_CURRENT("outputCurrent", "output.current", Units.AMPERE),
62 OUTPUT_VOLTAGE("outputVoltage", "output.voltage", Units.VOLT),
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);
70 static final Map<String, NutName> NUT_NAME_MAP = Stream.of(NutName.values())
71 .collect(Collectors.toMap(NutName::getChannelId, Function.identity()));
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;
79 NutName(final String channelId, final String name, final Class<? extends State> stateClass) {
80 this(channelId, name, stateClass, null);
83 NutName(final String channelId, final String name, final Unit<?> unit) {
84 this(channelId, name, QuantityType.class, unit);
87 NutName(final String channelId, final String name, final Class<? extends State> stateClass,
88 final @Nullable Unit<?> unit) {
89 this.channelId = channelId;
91 this.stateClass = stateClass;
96 * Returns the NUT enum for the given channel id or null if there is no NUT enum available for the given channel.
98 * @param channelId Channel to find the NUT enum for
99 * @return The NUT enum or null if there is none.
101 public static @Nullable NutName channelIdToNutName(final String channelId) {
102 return NUT_NAME_MAP.get(channelId);
106 * Returns the {@link State} value of the variable for this NUT as is found in the given map of variables.
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.
112 public static State toState(final String channelId, final Map<String, String> variables) {
113 final NutName nutName = channelIdToNutName(channelId);
115 if (nutName instanceof NutName) {
116 return nutName.toState(variables);
118 throw new IllegalArgumentException("Channel name '" + channelId + "'is not a known data name");
123 * Returns the {@link State} value of the variable for this NUT as is found in the given map of variables.
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.
128 public State toState(final @Nullable Map<String, String> variables) {
130 final String value = variables == null ? null : variables.get(name);
133 state = UnDefType.UNDEF;
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);
144 state = UnDefType.UNDEF;
151 * @return The name of the Channel for this NUT variable as specified in the Thing
153 String getChannelId() {
158 * @return The variable name as used by the NUT server
165 * @return The {@link State} class type of this NUT variable
167 Class<? extends State> getStateType() {
172 * @return The {@link Unit} for this NUT variable if the type is a {@link QuantityType}