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.miio.internal;
15 import java.util.Arrays;
16 import java.util.HashMap;
18 import java.util.stream.Collectors;
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.unit.ImperialUnits;
25 import org.openhab.core.library.unit.SIUnits;
26 import org.openhab.core.library.unit.Units;
29 * Enum of the units used in the miio protocol
30 * Used to find the right {@link javax.measure.Unit} given the string of the unit
32 * @author Marcel Verpaalen - Initial contribution
35 public enum MiIoQuantiyTypes {
37 CELCIUS(SIUnits.CELSIUS, "C"),
38 FAHRENHEIT(ImperialUnits.FAHRENHEIT),
39 SECOND(Units.SECOND, "seconds"),
40 MINUTE(Units.MINUTE, "minutes"),
41 HOUR(Units.HOUR, "hours"),
44 SQUARE_METRE(SIUnits.SQUARE_METRE, "square_meter", "squaremeter"),
45 PERCENT(Units.PERCENT);
47 private final Unit<?> unit;
48 private final String[] aliasses;
50 private static Map<String, Unit<?>> stringMap = Arrays.stream(values())
51 .collect(Collectors.toMap(Enum::toString, MiIoQuantiyTypes::getUnit));
53 private static Map<String, Unit<?>> aliasMap() {
54 Map<String, Unit<?>> aliassesMap = new HashMap<>();
55 for (MiIoQuantiyTypes miIoQuantiyType : values()) {
56 for (String alias : miIoQuantiyType.getAliasses()) {
57 aliassesMap.put(alias.toLowerCase(), miIoQuantiyType.getUnit());
63 private MiIoQuantiyTypes(Unit<?> unit, String... aliasses) {
65 this.aliasses = aliasses;
68 public Unit<?> getUnit() {
72 public String[] getAliasses() {
76 public static @Nullable Unit<?> get(String unitName) {
77 Unit<?> unit = stringMap.get(unitName.toUpperCase());
79 unit = aliasMap().get(unitName.toLowerCase());