]> git.basschouten.com Git - openhab-addons.git/blob
693b8e3240abecfcedf05382f368a098e855f5d5
[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     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"),
47     AMPERE(Units.AMPERE),
48     MILLI_AMPERE(MILLI(Units.AMPERE), "mA"),
49     VOLT(Units.VOLT),
50     WATT(Units.WATT),
51     LITRE(Units.LITRE, "liter"),
52     LUX(Units.LUX),
53     RADIANS(Units.RADIAN, "radians"),
54     KILOWATT_HOUR(Units.KILOWATT_HOUR, "kwh", "kWH"),
55     SQUARE_METRE(SIUnits.SQUARE_METRE, "square_meter", "squaremeter"),
56     PERCENT(Units.PERCENT),
57     KGM3(Units.KILOGRAM_PER_CUBICMETRE, "kilogram_per_cubicmeter"),
58     UGM3(Units.MICROGRAM_PER_CUBICMETRE, "microgram_per_cubicmeter"),
59     PPM(Units.PARTS_PER_MILLION, "parts_per_million");
60
61     private final Unit<?> unit;
62     private final String[] aliasses;
63
64     private static Map<String, Unit<?>> stringMap = Arrays.stream(values())
65             .collect(Collectors.toMap(Enum::toString, MiIoQuantiyTypes::getUnit));
66
67     private static Map<String, Unit<?>> aliasMap() {
68         Map<String, Unit<?>> aliassesMap = new HashMap<>();
69         for (MiIoQuantiyTypes miIoQuantiyType : values()) {
70             for (String alias : miIoQuantiyType.getAliasses()) {
71                 aliassesMap.put(alias.toLowerCase(), miIoQuantiyType.getUnit());
72             }
73         }
74         return aliassesMap;
75     }
76
77     private MiIoQuantiyTypes(Unit<?> unit, String... aliasses) {
78         this.unit = unit;
79         this.aliasses = aliasses;
80     }
81
82     public Unit<?> getUnit() {
83         return unit;
84     }
85
86     public String[] getAliasses() {
87         return aliasses;
88     }
89
90     public static @Nullable Unit<?> get(String unitName) {
91         Unit<?> unit = stringMap.get(unitName.toUpperCase());
92         if (unit == null) {
93             unit = aliasMap().get(unitName.toLowerCase());
94         }
95         return unit;
96     }
97 }