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 static org.openhab.core.library.unit.MetricPrefix.MILLI;
17 import java.util.Arrays;
18 import java.util.HashMap;
20 import java.util.stream.Collectors;
22 import javax.measure.Unit;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.core.library.unit.ImperialUnits;
27 import org.openhab.core.library.unit.SIUnits;
28 import org.openhab.core.library.unit.Units;
31 * Enum of the units used in the miio protocol
32 * Used to find the right {@link javax.measure.Unit} given the string of the unit
34 * @author Marcel Verpaalen - Initial contribution
37 public enum MiIoQuantiyTypes {
39 CELCIUS(SIUnits.CELSIUS, "C"),
40 FAHRENHEIT(ImperialUnits.FAHRENHEIT),
41 KELVIN(Units.KELVIN, "K"),
42 PASCAL(SIUnits.PASCAL),
43 SECOND(Units.SECOND, "seconds"),
44 MINUTE(Units.MINUTE, "minutes"),
45 HOUR(Units.HOUR, "hours"),
46 DAY(Units.DAY, "days"),
48 MILLI_AMPERE(MILLI(Units.AMPERE), "mA"),
50 MILLI_VOLT(MILLI(Units.VOLT), "mV"),
52 LITRE(Units.LITRE, "liter"),
54 RADIANS(Units.RADIAN, "radians"),
55 KILOWATT_HOUR(Units.KILOWATT_HOUR, "kwh", "kWH"),
56 SQUARE_METRE(SIUnits.SQUARE_METRE, "square_meter", "squaremeter"),
57 PERCENT(Units.PERCENT, "percentage"),
58 KGM3(Units.KILOGRAM_PER_CUBICMETRE, "kilogram_per_cubicmeter"),
59 UGM3(Units.MICROGRAM_PER_CUBICMETRE, "microgram_per_cubicmeter", "μg/m3"),
60 M3(SIUnits.CUBIC_METRE, "cubic_meter", "cubic_metre"),
61 LITER(Units.LITRE, "L", "litre"),
62 PPM(Units.PARTS_PER_MILLION, "parts_per_million");
64 private final Unit<?> unit;
65 private final String[] aliasses;
67 private static Map<String, Unit<?>> stringMap = Arrays.stream(values())
68 .collect(Collectors.toMap(Enum::toString, MiIoQuantiyTypes::getUnit));
70 private static Map<String, Unit<?>> aliasMap() {
71 Map<String, Unit<?>> aliassesMap = new HashMap<>();
72 for (MiIoQuantiyTypes miIoQuantiyType : values()) {
73 for (String alias : miIoQuantiyType.getAliasses()) {
74 aliassesMap.put(alias.toLowerCase(), miIoQuantiyType.getUnit());
80 private MiIoQuantiyTypes(Unit<?> unit, String... aliasses) {
82 this.aliasses = aliasses;
85 public Unit<?> getUnit() {
89 public String[] getAliasses() {
93 public static @Nullable Unit<?> get(String unitName) {
94 Unit<?> unit = stringMap.get(unitName.toUpperCase());
96 unit = aliasMap().get(unitName.toLowerCase());