]> git.basschouten.com Git - openhab-addons.git/blob
65a4f8020ddff0f2b486ca11d95137c35c12e405
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.miio.internal;
14
15 import static org.openhab.core.library.unit.MetricPrefix.MILLI;
16
17 import java.util.Arrays;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.stream.Collectors;
21
22 import javax.measure.Unit;
23
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;
29
30 /**
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
33  *
34  * @author Marcel Verpaalen - Initial contribution
35  */
36 @NonNullByDefault
37 public enum MiIoQuantiyTypes {
38
39     CELSIUS(SIUnits.CELSIUS, "C", "celcius"),
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"),
47     AMPERE(Units.AMPERE),
48     MILLI_AMPERE(MILLI(Units.AMPERE), "mA"),
49     VOLT(Units.VOLT),
50     MILLI_VOLT(MILLI(Units.VOLT), "mV"),
51     WATT(Units.WATT),
52     LITRE(Units.LITRE, "liter"),
53     LUX(Units.LUX),
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");
63
64     private final Unit<?> unit;
65     private final String[] aliasses;
66
67     private static Map<String, Unit<?>> stringMap = Arrays.stream(values())
68             .collect(Collectors.toMap(Enum::toString, MiIoQuantiyTypes::getUnit));
69
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());
75             }
76         }
77         return aliassesMap;
78     }
79
80     private MiIoQuantiyTypes(Unit<?> unit, String... aliasses) {
81         this.unit = unit;
82         this.aliasses = aliasses;
83     }
84
85     public Unit<?> getUnit() {
86         return unit;
87     }
88
89     public String[] getAliasses() {
90         return aliasses;
91     }
92
93     public static @Nullable Unit<?> get(String unitName) {
94         Unit<?> unit = stringMap.get(unitName.toUpperCase());
95         if (unit == null) {
96             unit = aliasMap().get(unitName.toLowerCase());
97         }
98         return unit;
99     }
100 }