]> git.basschouten.com Git - openhab-addons.git/blob
0767d958042e89fc7bbc583c8dfd77cdb54f406e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 java.util.Arrays;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.stream.Collectors;
19
20 import javax.measure.Unit;
21
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;
27
28 /**
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
31  *
32  * @author Marcel Verpaalen - Initial contribution
33  */
34 @NonNullByDefault
35 public enum MiIoQuantiyTypes {
36
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"),
45     AMPERE(Units.AMPERE),
46     VOLT(Units.VOLT),
47     WATT(Units.WATT),
48     LITRE(Units.LITRE, "liter"),
49     LUX(Units.LUX),
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");
56
57     private final Unit<?> unit;
58     private final String[] aliasses;
59
60     private static Map<String, Unit<?>> stringMap = Arrays.stream(values())
61             .collect(Collectors.toMap(Enum::toString, MiIoQuantiyTypes::getUnit));
62
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());
68             }
69         }
70         return aliassesMap;
71     }
72
73     private MiIoQuantiyTypes(Unit<?> unit, String... aliasses) {
74         this.unit = unit;
75         this.aliasses = aliasses;
76     }
77
78     public Unit<?> getUnit() {
79         return unit;
80     }
81
82     public String[] getAliasses() {
83         return aliasses;
84     }
85
86     public static @Nullable Unit<?> get(String unitName) {
87         Unit<?> unit = stringMap.get(unitName.toUpperCase());
88         if (unit == null) {
89             unit = aliasMap().get(unitName.toLowerCase());
90         }
91         return unit;
92     }
93 }