]> git.basschouten.com Git - openhab-addons.git/blob
7ebf9c40c8ea7b0169cbe5b470a1559a9e463ea5
[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     DEGREE(Units.DEGREE_ANGLE, "degree"),
56     KILOWATT_HOUR(Units.KILOWATT_HOUR, "kwh", "kWH"),
57     SQUARE_METRE(SIUnits.SQUARE_METRE, "square_meter", "squaremeter"),
58     PERCENT(Units.PERCENT, "percentage"),
59     KGM3(Units.KILOGRAM_PER_CUBICMETRE, "kilogram_per_cubicmeter"),
60     UGM3(Units.MICROGRAM_PER_CUBICMETRE, "microgram_per_cubicmeter", "μg/m3"),
61     M3(SIUnits.CUBIC_METRE, "cubic_meter", "cubic_metre"),
62     LITER(Units.LITRE, "L", "litre"),
63     PPM(Units.PARTS_PER_MILLION, "parts_per_million");
64
65     private final Unit<?> unit;
66     private final String[] aliasses;
67
68     private static Map<String, Unit<?>> stringMap = Arrays.stream(values())
69             .collect(Collectors.toMap(Enum::toString, MiIoQuantiyTypes::getUnit));
70
71     private static Map<String, Unit<?>> aliasMap() {
72         Map<String, Unit<?>> aliassesMap = new HashMap<>();
73         for (MiIoQuantiyTypes miIoQuantiyType : values()) {
74             for (String alias : miIoQuantiyType.getAliasses()) {
75                 aliassesMap.put(alias.toLowerCase(), miIoQuantiyType.getUnit());
76             }
77         }
78         return aliassesMap;
79     }
80
81     private MiIoQuantiyTypes(Unit<?> unit, String... aliasses) {
82         this.unit = unit;
83         this.aliasses = aliasses;
84     }
85
86     public Unit<?> getUnit() {
87         return unit;
88     }
89
90     public String[] getAliasses() {
91         return aliasses;
92     }
93
94     public static @Nullable Unit<?> get(String unitName) {
95         Unit<?> unit = stringMap.get(unitName.toUpperCase());
96         if (unit == null) {
97             unit = aliasMap().get(unitName.toLowerCase());
98         }
99         return unit;
100     }
101 }