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