2 * Copyright (c) 2010-2021 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 KELVIN(Units.KELVIN, "K"),
40 PASCAL(SIUnits.PASCAL),
41 SECOND(Units.SECOND, "seconds"),
42 MINUTE(Units.MINUTE, "minutes"),
43 HOUR(Units.HOUR, "hours"),
44 DAY(Units.DAY, "days"),
48 LITRE(Units.LITRE, "liter"),
50 RADIANS(Units.RADIAN, "radians"),
51 KILOWATT_HOUR(Units.KILOWATT_HOUR, "kwh"),
52 SQUARE_METRE(SIUnits.SQUARE_METRE, "square_meter", "squaremeter"),
53 PERCENT(Units.PERCENT),
54 KGM3(Units.KILOGRAM_PER_CUBICMETRE, "kilogram_per_cubicmeter"),
55 UGM3(Units.MICROGRAM_PER_CUBICMETRE, "microgram_per_cubicmeter"),
56 PPM(Units.PARTS_PER_MILLION, "parts_per_million");
58 private final Unit<?> unit;
59 private final String[] aliasses;
61 private static Map<String, Unit<?>> stringMap = Arrays.stream(values())
62 .collect(Collectors.toMap(Enum::toString, MiIoQuantiyTypes::getUnit));
64 private static Map<String, Unit<?>> aliasMap() {
65 Map<String, Unit<?>> aliassesMap = new HashMap<>();
66 for (MiIoQuantiyTypes miIoQuantiyType : values()) {
67 for (String alias : miIoQuantiyType.getAliasses()) {
68 aliassesMap.put(alias.toLowerCase(), miIoQuantiyType.getUnit());
74 private MiIoQuantiyTypes(Unit<?> unit, String... aliasses) {
76 this.aliasses = aliasses;
79 public Unit<?> getUnit() {
83 public String[] getAliasses() {
87 public static @Nullable Unit<?> get(String unitName) {
88 Unit<?> unit = stringMap.get(unitName.toUpperCase());
90 unit = aliasMap().get(unitName.toLowerCase());