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 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 SQUARE_METRE(SIUnits.SQUARE_METRE, "square_meter", "squaremeter"),
52 PERCENT(Units.PERCENT),
53 KGM3(Units.KILOGRAM_PER_CUBICMETRE, "kilogram_per_cubicmeter"),
54 UGM3(Units.MICROGRAM_PER_CUBICMETRE, "microgram_per_cubicmeter"),
55 PPM(Units.PARTS_PER_MILLION, "parts_per_million");
57 private final Unit<?> unit;
58 private final String[] aliasses;
60 private static Map<String, Unit<?>> stringMap = Arrays.stream(values())
61 .collect(Collectors.toMap(Enum::toString, MiIoQuantiyTypes::getUnit));
63 private static Map<String, Unit<?>> aliasMap() {
64 Map<String, Unit<?>> aliassesMap = new HashMap<>();
65 for (MiIoQuantiyTypes miIoQuantiyType : values()) {
66 for (String alias : miIoQuantiyType.getAliasses()) {
67 aliassesMap.put(alias.toLowerCase(), miIoQuantiyType.getUnit());
73 private MiIoQuantiyTypes(Unit<?> unit, String... aliasses) {
75 this.aliasses = aliasses;
78 public Unit<?> getUnit() {
82 public String[] getAliasses() {
86 public static @Nullable Unit<?> get(String unitName) {
87 Unit<?> unit = stringMap.get(unitName.toUpperCase());
89 unit = aliasMap().get(unitName.toLowerCase());