]> git.basschouten.com Git - openhab-addons.git/blob
19d258dd5d077f35213cbdd263f7f66e18713131
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.*;
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, "pa"),
43     HPA(HECTO(SIUnits.PASCAL)),
44     SECOND(Units.SECOND, "seconds"),
45     MINUTE(Units.MINUTE, "minutes"),
46     HOUR(Units.HOUR, "hours"),
47     DAY(Units.DAY, "days"),
48     AMPERE(Units.AMPERE),
49     MILLI_AMPERE(MILLI(Units.AMPERE), "mA"),
50     VOLT(Units.VOLT),
51     MILLI_VOLT(MILLI(Units.VOLT), "mV"),
52     WATT(Units.WATT, "W", "w"),
53     LITRE(Units.LITRE, "liter"),
54     LUX(Units.LUX),
55     RADIANS(Units.RADIAN, "radians"),
56     DEGREE(Units.DEGREE_ANGLE, "degree"),
57     KILOWATT_HOUR(Units.KILOWATT_HOUR, "kwh", "kWH"),
58     SQUARE_METRE(SIUnits.SQUARE_METRE, "square_meter", "squaremeter"),
59     PERCENT(Units.PERCENT, "percentage"),
60     KGM3(Units.KILOGRAM_PER_CUBICMETRE, "kilogram_per_cubicmeter"),
61     UGM3(Units.MICROGRAM_PER_CUBICMETRE, "microgram_per_cubicmeter", "μg/m3"),
62     M3(SIUnits.CUBIC_METRE, "cubic_meter", "cubic_metre"),
63     LITER(Units.LITRE, "L", "litre"),
64     PPM(Units.PARTS_PER_MILLION, "parts_per_million");
65
66     private final Unit<?> unit;
67     private final String[] aliasses;
68
69     private static Map<String, Unit<?>> stringMap = Arrays.stream(values())
70             .collect(Collectors.toMap(Enum::toString, MiIoQuantiyTypes::getUnit));
71
72     private static Map<String, Unit<?>> aliasMap() {
73         Map<String, Unit<?>> aliassesMap = new HashMap<>();
74         for (MiIoQuantiyTypes miIoQuantiyType : values()) {
75             for (String alias : miIoQuantiyType.getAliasses()) {
76                 aliassesMap.put(alias.toLowerCase(), miIoQuantiyType.getUnit());
77             }
78         }
79         return aliassesMap;
80     }
81
82     private MiIoQuantiyTypes(Unit<?> unit, String... aliasses) {
83         this.unit = unit;
84         this.aliasses = aliasses;
85     }
86
87     public Unit<?> getUnit() {
88         return unit;
89     }
90
91     public String[] getAliasses() {
92         return aliasses;
93     }
94
95     public static @Nullable Unit<?> get(String unitName) {
96         Unit<?> unit = stringMap.get(unitName.toUpperCase());
97         if (unit == null) {
98             unit = aliasMap().get(unitName.toLowerCase());
99         }
100         return unit;
101     }
102 }